gpt4 book ai didi

mysql - SQL : Creating new columns using Case When, 分组 ID

转载 作者:行者123 更新时间:2023-11-29 16:24:03 24 4
gpt4 key购买 nike

如何使用 CASE WHENGROUPING_ID()ROLLUP() 语法在 SQL 中创建全新列?

到目前为止我已经尝试过:

SELECT Country, ContactTitle, COUNT(ContactTitle) AS Count
,CASE(
WHEN
GROUPING_ID(Legend) = 0 THEN ' '
WHEN
GROUPING_ID(Legend) = 1 THEN 'SUBTOTAL(Country)')
GROUP BY ROLLUP(Country, ContactTitle)
FROM dbo.Customers

最佳答案

SELECT Country, ContactTitle, COUNT(ContactTitle) AS Count, 
CASE
WHEN GROUPING_ID(Country,ContactTitle) = 0 THEN ''
WHEN GROUPING_ID(Country,ContactTitle) = 1 THEN CONCAT('Subtotal for ',Country)
END AS Legend
FROM dbo.Customers
GROUP BY ROLLUP(Country, ContactTitle);

关于mysql - SQL : Creating new columns using Case When, 分组 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54361438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com