- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我曾尝试使用 IFNULL 替换 ROLLUP 为小计和总计返回的 NULL 字段,但它似乎不起作用。
查询:
select IFNULL(usergroups.name, 'GROUP') AS DEALER,
IFNULL(users.name, 'TOTAL') AS SERVICE_ADVISOR,
COUNT(DISTINCT vcrs.uid) AS COMPLETED,
..........
..........
and vcrs.vcrSubStatus = 4
group by DEALER, SERVICE_ADVISOR with ROLLUP;
输出:
DEALER SERVICE_ADVISOR COMPLETED IDENTIFIED AUTHORISED
Aston Martin Chris 3 664.56 0
Aston Martin Graham 6 0 0
Aston Martin (NULL) 15 664.56 0
Bentley Sukraj 1 0 0
Bentley Terry 4 0 0
Bentley (NULL) 5 0 0
Jaguar Emma 10 2448 1224
Jaguar Paul 1 0 0
Jaguar Simon 7 2754 918
Jaguar (NULL) 18 5202 2142
(NULL) (NULL) 2611 96591.62 42130.14
期望的输出:
DEALER SERVICE_ADVISOR COMPLETED IDENTIFIED AUTHORISED
Aston Martin Chris 3 664.56 0
Aston Martin Graham 6 0 0
Aston Martin TOTAL 15 664.56 0
Bentley Sukraj 1 0 0
Bentley Terry 4 0 0
Bentley TOTAL 5 0 0
Jaguar Emma 10 2448 1224
Jaguar Paul 1 0 0
Jaguar Simon 7 2754 918
Jaguar TOTAL 18 5202 2142
GROUP TOTAL 2611 96591.62 42130.14
最佳答案
要解决 COALESCE/IFNULL
仍然为 WITH ROLLUP
占位符返回 NULL
的问题,您需要 GROUP BY
表列名,而不是别名列表达式。
问题是由在别名列表达式上指定的 GROUP BY
子句引起的,因为别名是在列表达式之后 分配的已处理。
导致 WITH ROLLUP
NULL
占位符不在要由 COALESCE
评估的记录集中。
这意味着 GROUP BY
中的别名 DEALER
、SERVICE_ADVISOR
在 之前不存在IFNULL/COALESCE
已被执行。查看 MySQL Handling of GROUP BY
了解更多详情。
示例 DB-Fiddle
CREATE TABLE foo (
`amount` INTEGER,
`created` INTEGER
);
INSERT INTO foo
(`amount`, `created`)
VALUES
('1', '2019'),
('2', '2019');
查询 #1(重现问题)
SELECT
SUM(amount) AS amounts,
COALESCE(created, 'Total') AS created_coalesce
FROM foo
GROUP BY created_coalesce WITH ROLLUP;
| amounts | created_coalesce |
| ------- | ---------------- |
| 3 | 2019 |
| 3 | |
查询 #2(已更正)
SELECT
SUM(amount) AS amounts,
COALESCE(created, 'Total') AS created_coalesce
FROM foo
GROUP BY foo.created WITH ROLLUP;
| amounts | created_coalesce |
| ------- | ---------------- |
| 3 | 2019 |
| 3 | Total |
示例 DB-Fiddle
SELECT
COALESCE(usergroups.name, 'GROUP') AS DEALER,
COALESCE(users.name, 'TOTAL') AS SERVICE_ADVISOR,
COUNT(DISTINCT vcrs.uid) AS COMPLETED,
/* ... */
GROUP BY usergroups.name, users.name WITH ROLLUP;
查询 #1(原始)
SELECT
COALESCE(usergroups.name, 'GROUP') AS DEALER,
COALESCE(users.name, 'TOTAL') AS SERVICE_ADVISOR,
COUNT(DISTINCT vcrs.uid) AS COMPLETED
/* ... */
GROUP BY DEALER, SERVICE_ADVISOR WITH ROLLUP;
| DEALER | SERVICE_ADVISOR | COMPLETED |
| ------ | --------------- | --------- |
| Foo | Jane Doe | 1 |
| Foo | John Doe | 1 |
| Foo | | 2 |
| | | 2 |
查询 #2(已更正)
SELECT
COALESCE(usergroups.name, 'GROUP') AS DEALER,
COALESCE(users.name, 'TOTAL') AS SERVICE_ADVISOR,
COUNT(DISTINCT vcrs.uid) AS COMPLETED
/* ... */
GROUP BY usergroups.name, users.name WITH ROLLUP;
| DEALER | SERVICE_ADVISOR | COMPLETED |
| ------ | --------------- | --------- |
| Foo | Jane Doe | 1 |
| Foo | John Doe | 1 |
| Foo | TOTAL | 2 |
| GROUP | TOTAL | 2 |
With MySQL 5.7+ and ONLY_FULL_GROUP_BY enabled, selectednon-aggregate columns that are not specified in the
GROUP BY
clausewill fail. Meaning the following query will not work as expected: DB-FiddleSELECT COALESCE(YEAR(foo), 'foo') /* ... */ GROUP BY YEAR(foo) WITH ROLLUP
-> ER_WRONG_FIELD_WITH_GROUP: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.foo_bar.foo' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
COALESCE
,IFNULL
,IF(... IS NULL)
andCASE WHEN ... IS NULL
will all functionsimilarly. WhereIFNULL
is the proprietary to MySQL and is a lessfunctional replacement ofCOALESCE
. AsCOALESCE
can accept morethan two parameters to check againstNULL
, returning the firstnon-NULL
value.mysql> SELECT COALESCE(NULL, NULL, 1, NULL);
-> 1
mysql> SELECT IFNULL(NULL, 1);
-> 1
mysql> SELECT IF(NULL IS NULL, 1, '');
-> 1
mysql> SELECT CASE WHEN NULL IS NULL THEN 1 END;
-> 1
nullable columns in the
GROUP BY
as either aliases or column names, will result in theNULL
values beingdisplayed as theWITH ROLLUP
placeholder. This applies to usingWITH ROLLUP
in general. For example ifusers.name
can returnNULL
. DB-Fiddle| DEALER | SERVICE_ADVISOR | COMPLETED |
| ------ | --------------- | --------- |
| Foo | TOTAL | 1 |
| Foo | Jane Doe | 1 |
| Foo | John Doe | 1 |
| Foo | TOTAL | 3 |
| GROUP | TOTAL | 3 |
NULL
列值为确保不会意外包含可为空的列,您需要在条件中指定排除它们。
示例 DB-Fiddle
SELECT
COALESCE(usergroups.name, 'GROUP') AS DEALER,
COALESCE(users.name, 'TOTAL') AS SERVICE_ADVISOR,
COUNT(DISTINCT vcrs.uid) AS COMPLETED
FROM vcrs
LEFT JOIN users
ON users.id = vcrs.uid
LEFT JOIN usergroups
ON usergroups.id = users.group_id
WHERE vcrs.vcrSubStatus = 4
AND users.name IS NOT NULL
GROUP BY usergroups.name, users.name
WITH ROLLUP;
结果
| DEALER | SERVICE_ADVISOR | COMPLETED |
| ------ | --------------- | --------- |
| Foo | Jane Doe | 1 |
| Foo | John Doe | 1 |
| Foo | TOTAL | 2 |
| GROUP | TOTAL | 2 |
Since
LEFT JOIN
is used on thevcrs
table,IS NOT NULL
must beapplied to theWHERE
clause, instead of theON
clause. AsLEFT JOIN
returnsNULL
for non-matching criteria. To circumvent theissue, use anINNER JOIN
to limit the resultset to only those withmatchingON
criteria.
/* ... */
INNER JOIN users
ON users.id = vcrs.uid
AND users.name IS NOT NULL
/* ... */
WHERE vcrs.vcrSubStatus = 4
GROUP BY usergroups.name, users.name
WITH ROLLUP;
NULL
列值要显式包含可为空的列值,而不复制 WITH ROLLUP
占位符名称,您需要使用派生表子查询将 NULL
值替换为文本值.
示例 DB-Fiddle
SELECT
COALESCE(v.usergroup_name, 'GROUP') AS DEALER,
COALESCE(v.user_name, 'TOTAL') AS SERVICE_ADVISOR,
COUNT(DISTINCT v.uid) AS COMPLETED
FROM (
SELECT
usergroups.name AS usergroup_name,
COALESCE(users.name, 'NULL') AS user_name,
vcrs.uid
FROM vcrs
LEFT JOIN users
ON users.id = vcrs.uid
LEFT JOIN usergroups
ON usergroups.id = users.group_id
WHERE vcrs.vcrSubStatus = 4
) AS v
GROUP BY v.usergroup_name, v.user_name
WITH ROLLUP;
结果
| DEALER | SERVICE_ADVISOR | COMPLETED |
| ------ | --------------- | --------- |
| Foo | Jane Doe | 1 |
| Foo | John Doe | 1 |
| Foo | NULL | 1 |
| Foo | TOTAL | 3 |
| GROUP | TOTAL | 3 |
您还可以根据需要选择替换 'NULL'
文本占位符,甚至将其显示为 NULL
。
SELECT
COALESCE(v.usergroup_name, 'GROUP') AS DEALER,
CASE v.user_name WHEN 'NULL' THEN NULL ELSE COALESCE(v.user_name, 'TOTAL') END AS SERVICE_ADVISOR,
COUNT(DISTINCT v.uid) AS COMPLETED
FROM (
/* ... */
) AS v
GROUP BY v.usergroup_name, v.user_name
WITH ROLLUP;
关于mysql - 在 ROLLUP 中用 SUBTOTAL 和 TOTAL 替换 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25443822/
我需要计算一个 SUMPRODUCT 的 SUBTOTAL,没有总计列想不通这个公式。。。更准确地说: 我需要一个可以对 Sumproduct(B2:B4,C2:C4) 进行小计的公式 或者,如果我使
在 itext 5 中,可以使用单元格事件来计算表格列的小计。如何在 itext 7 中完成此操作? 可以在这个 URL 中找到 itext 5 示例:http://developers.itextp
在 itext 5 中,可以使用单元格事件来计算表格列的小计。如何在 itext 7 中完成此操作? 可以在这个 URL 中找到 itext 5 示例:http://developers.itextp
在Excel中,有什么区别 小计(9,我的范围) 和 SUM(我的范围)? 最佳答案 SUBTOTAL 和 SUM 之间的最大区别在于,SUBTOTAL 可以在同一列中重复使用以进行部分小计,然后在最
我有一个相当简单的三列 Excel 2007 工作表;名称(A 列)、Value1(B 列)和 Value2(C 列)。此工作表通常会按名称列自动过滤。 在第 2 列的底部,我想执行 B 列的条件小计
这是问题的延续 excel different SUM.IF array function , 但由于我已将其标记为已解决,因此我创建了一个新问题。 我想要的是一些值的不同总和,并且我已经实现了@Ma
我正在创建一个从 Oracle 数据库获取数据的宏,这意味着行数可能会因结果而异。在这个宏中,我使用了 worksheet.subtotal 函数,当组发生变化时,它会汇总多个列。这一切都有效,但我面
我正在尝试计算一个范围(H5 到 J13)中非空单元格的数量,其中另一个单元格(在 G 列中)也匹配来自不同单元格的文本字符串。我在 Google Docs 上使用电子表格,所以很遗憾 COUNTIF
我正在使用 Pandas 0.19。 考虑以下数据框: FID admin0 admin1 admin2 windspeed population 0 cntry1 state1
我有这种形式的数据: Category Source Amount Dues FTW $100 Donations ODP
getTotals() as $total) { if ($total->getCode() == 'subtotal') { $subtotal = $total->
我想在 Magento/checkout/onepage/中隐藏“小计”行,并仅显示“总计”行。 我怎样才能做到这一点? 非常感谢 最佳答案 我刚刚将位于 app/design/frontend/ba
SQL中有没有可以显示小计行的函数。我有一个这样的表: Date INVNUNBER CUSTOMER ITEM QTY SALES 20190630 IN334
PayPalExpress version 95.0 ASP.Net & C# CurrencyCodeType currency = (CurrencyCodeType) Enum.Parse
我正在使用 jQuery 制作总表和小计表,但我遇到了很多问题。第一个问题,当我重新加载此页面时,总计和小计显示 NaN,即使输入的值已默认输入。 第二个问题,第二行的数据等等,仍然只取第一行的价格。
我正在与 jQuery Calculation Plugin 合作在一个页面上,我进行数量 x 价格计算,然后添加到小计中。如此重复 4 次。 每组 4 个涉及具有不同 ID 和不同计算的字段(不仅仅
以下是相关公式的一些原始数据:。当过滤使用时,我试图在上面的公式前面添加小计函数,但它不起作用……你能帮我吗?。筛选使用时,应使用总和除法加小计公式。Thx!
以下是相关公式的一些原始数据:。当过滤使用时,我试图在上面的公式前面添加小计函数,但它不起作用……你能帮我吗?。筛选使用时,应使用总和除法加小计公式。Thx!
我正在尝试使用 OpenXml 完全从头开始创建 Excel 数据透视表。 我已经成功地创建了数据透视表本身(创建了数据透视表定义、缓存定义、所有缓存记录、数据透视字段、行项目等)。 但是如何显示任何
全部。我正在尝试制作一个包含一系列食物的购物 list ,并且在每个食物旁边提示用户输入价格和数量,然后保持所有元素的运行小计我有基本的但真的在“价格盒”中苦苦挣扎数量和小计。 var app = a
我是一名优秀的程序员,十分优秀!