- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写的报告包括总计列和总计行。到目前为止,我只是使用子查询分配变量并将它们添加到总计列中。对于总计行,我刚刚执行了联合并重新运行查询,但约束更少。这是代码。
set @sdate = '2015-01-01';
set @edate = @sdate + interval 1 year;
select `a` as 'First Name', `aa` as 'Last Name',
`b` as 'Field Tests', `c` as 'Field Inspections',
`d` as 'Lab Tests', `e` as 'Lab Inspections',
`f` as 'Total' from
(
select a.first as 'a', a.last as 'aa',
(select @b := count(*) from field_test b where b.inspector_id = a.id
and b.date_time >= @sdate and b.date_time < @edate) as 'b',
(select @c := count(*) from field_insp c where c.inspector_id = a.id
and c.inspection_date >= @sdate and c.inspection_date < @edate) as 'c',
(select @d := count(*) from lab_test d where d.inspector_id = a.id
and d.date_time >= @sdate and d.date_time < @edate) as 'd',
(select @e := count(*) from lab_insp e where e.inspector_id = a.id
and e.inspection_date >= @sdate and e.inspection_date < @edate) as 'e',
(select @f := @b + @c + @d + @e) as 'f'
from inspector a
order by `f` desc
) as t
where `f` > 0
union
select 'TOTALS', '',
(select @n := count(*) from field_test
where date_time >= @sdate and date_time < @edate),
(select @o := count(*) from field_insp
where inspection_date >= @sdate and inspection_date < @edate),
(select @p := count(*) from lab_test
where date_time >= @sdate and date_time < @edate),
(select @q := count(*) from lab_insp
where inspection_date >= @sdate and inspection_date < @edate),
(select @n + @o + @p + @q)
有点冗长和困惑,但它生成了一份不错的报告。我最初用连接编写它,但我用子查询重写了它,因为当时它似乎是添加总计列和总计行的最简单方法。除了代码的冗长之外,在使用较大表的类似报告上运行所有这些子查询所花费的时间有点长。
最佳答案
您可以使用 group by
子句的 with rollup
修饰符。为此,您需要按检查员分组。
这是一种方法:
select case when id is null then 'Totals' else min(first) end as `First name`,
case when id is null then '' else min(last) end as `Last Name`,
sum(ft) as `Field Tests`,
sum(fi) as `Field Inspections`,
sum(lt) as `Lab Tests`,
sum(li) as `Lab Inspections`,
sum(ft + fi + lt + li) as `Total`
from inspector as insp
inner join (
select inspector_id, count(*) as ft, 0 as fi, 0 as lt, 0 as li
from field_test
where date_time >= @sdate and date_time < @edate
group by inspector_id
union
select inspector_id, 0, count(*), 0, 0
from field_insp
where inspection_date >= @sdate and inspection_date < @edate
group by inspector_id
union
select inspector_id, 0, 0, count(*), 0
from lab_test
where date_time >= @sdate and date_time < @edate
group by inspector_id
union
select inspector_id, 0, 0, 0, count(*)
from lab_insp
where inspection_date >= @sdate and inspection_date < @edate
group by inspector_id
) as cnt
on cnt.inspector_id = id
group by id with rollup;
关于MySQL - 添加总计行和列的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42585751/
我需要一些帮助。 我希望“总计”由“数量*价格=总计”计算(到目前为止没问题)。问题是我还需要通过“总/价格=数量”来计算“数量”,即如果一个字段发生更改,另一个字段也会自动更改。 我做了一个非常简单
我的项目即将结束,但在解决它时遇到了一些问题。我是一名厨师,想要自己的应用程序与他自己的业务相关 我的表单称为“菜谱”,并包含数量、单价、使用百分比。 假设您有 1 公斤苹果,价格为 5 欧元。清洁结
我的数据库表是: 产品展示 - ID - 名称 ... 订单 - ID - 状态 ... OrderItems - ID -order_id -product_id -数量 -line_total 我
我的项目即将结束,但在解决它时遇到了一些问题。我是一名厨师,想要自己的应用程序与他自己的业务相关 我的表单称为“菜谱”,并包含数量、单价、使用百分比。 假设您有 1 公斤苹果,价格为 5 欧元。清洁结
exchanges表结构: id exchange created_at updated_at deleted_at start_time
我正在开发 Telerik RadGrid,网格内有多个 radtextbox,如 A、B、C 和 D。我使用带有正则表达式验证的 radtextbox 来验证带有数字的 A、B 和 C 文本框。然而
我有一个非常简单的$lookup聚合查询,如下所示: {'$lookup': {'from': 'edge', 'localField': 'gid', 'foreignField': 't
我正在尝试做一些与我之前问过的问题非常相似的事情,但我似乎无法让它正常工作。这是我之前的问题:How to get totals per day 表格如下: Table N
[更新] 谢谢大家,最终代码: var EUR_share_cost = 0; var USD_share_cost = 0; var GBP_shar
由于某种原因,下面的这部分代码没有将每个数组项添加到一起。我单步执行调试器,正在创建并递增数组项,但 total += ScoreArray[i]; 似乎没有将已输入的数字相加。相反,我只是将第一个输
我在这里有点迷路了。我有一个 for 循环,它增加一个值,如下所示: int nu01 = 10000; int level = 0; int increase = 35000; for (int i
请引用之前的问题:Sum total for column in jQuery 我使用了 Aymen 的解决方案,但我对其进行了编辑以满足我的需要。它停止工作,我的代码如下所示,在 jsfiddle
Date Flight ID Member ID Seat Type Seat Price 2013-07-28 F71498 M692
我希望程序忽略零总数。我正试图阻止向非参与者提供零总数的汽车奖励。我在 && 运算符后面添加了 >= 表达式。这对于击杀数和净值来说正确吗? $fetch_nor_killers = mysq
我有下表: CREATE TABLE zoom (`id` int, `fb_id` int, `date` datetime); INSERT INTO zoom (`id`, `f
我想知道是否有一种方法可以将已计算的派生列的总和添加到新列中。 Employee_KT_State --------------------------------------------------
在 SQL 中,我想获得来自每个用户的网站点击率的百分比。为此,我需要获取 site hits 列的总和,但我的查询在另一列上使用了 GROUP By。 除了 GROUP BY 中的每个 user_i
我有以下查询: SELECT SUM(case when s1 = 'fel' then 1 else 0 end) as s1_count, SUM(case when s2 = '
我已经使用 HTML 创建了一个表格。该表由一个标题和许多称为 Family 的行组成。每个家庭行可以有许多行代表家庭的各个成员(请参见下面的代码)。 Family N
如何使用 javascript 给出 float 总计。请 friend 们帮助我。 function addNumbers() {
我是一名优秀的程序员,十分优秀!