oracle 在SQL中的查询的适当聚合级别添加标签

ecr0jaav  于 2023-04-20  发布在  Oracle
关注(0)|答案(1)|浏览(189)

问题是通过在查询的适当聚合级别添加标签来正确地格式化查询结果。
下面是Oracle SQL代码:

SELECT NVL(TO_CHAR(CITY.COUNTRY), 'Summary-total:') VISITED_COUNTRY, 
       NVL(TO_CHAR(CITY.NAME), 'Summary-country:')  CITY, 
       NVL(TO_CHAR(ACCOMMODATIONTYPE.NAME), 'Summary-city:') TYPE,
       SUM(VISIT.NUMBER_OF_VISITORS) VISITORS, 
       SUM(VISIT.PROFIT) AS PROFIT
FROM VISIT
       JOIN CITY ON VISIT.ID_CITY = CITY.ID_CITY 
       JOIN ACCOMMODATIONTYPE ON VISIT.ID_TYPE = ACCOMMODATIONTYPE.ID_TYPE
GROUP BY 
    ROLLUP(CITY.COUNTRY, CITY.NAME, ACCOMMODATIONTYPE.NAME) 
ORDER BY 
    VISITED_COUNTRY, CITY, TYPE;

它给了我以下结果:

但是我应该得到这样的结果(行中只有一个标签!)

z8dt9xmd

z8dt9xmd1#

您可以使用case表达式来决定何时添加标签,从而实现更多控制:

SELECT CASE WHEN CITY.COUNTRY IS NULL
       THEN 'Summary-total:' ELSE CITY.COUNTRY END AS VISITED_COUNTRY, 
       CASE WHEN CITY.COUNTRY IS NOT NULL AND CITY.NAME IS NULL
       THEN 'Summary-country:' ELSE CITY.NAME END AS CITY, 
       CASE WHEN CITY.COUNTRY IS NOT NULL AND CITY.NAME IS NOT NULL AND ACCOMMODATIONTYPE.NAME IS NULL
       THEN 'Summary-city:' ELSE ACCOMMODATIONTYPE.NAME END AS TYPE,
       SUM(VISIT.NUMBER_OF_VISITORS) VISITORS, 
       SUM(VISIT.PROFIT) AS PROFIT
FROM VISIT
       JOIN CITY ON VISIT.ID_CITY = CITY.ID_CITY 
       JOIN ACCOMMODATIONTYPE ON VISIT.ID_TYPE = ACCOMMODATIONTYPE.ID_TYPE
GROUP BY 
    ROLLUP(CITY.COUNTRY, CITY.NAME, ACCOMMODATIONTYPE.NAME) 
ORDER BY 
    VISITED_COUNTRY, CITY, TYPE;
访问国家城市类型参观者利润
意大利佛罗伦萨公寓360度一百
意大利佛罗伦萨房间三百三十九十二
意大利佛罗伦萨摘要-城市:六九零一百九十二
意大利罗马房间三百八十
意大利罗马摘要-城市:三百八十
意大利摘要-国家:联系我们九九零二百七十二
总计:联系我们联系我们九九零二百七十二

如果您愿意,也可以将大小写表达式与NVLCOALESCE混合使用以简化它:

SELECT COALESCE(CITY.COUNTRY, 'Summary-total:') AS VISITED_COUNTRY, 
       CASE WHEN CITY.COUNTRY IS NOT NULL
       THEN COALESCE(CITY.NAME, 'Summary-country:') END AS CITY, 
       CASE WHEN CITY.COUNTRY IS NOT NULL AND CITY.NAME IS NOT NULL
       THEN COALESCE(ACCOMMODATIONTYPE.NAME, 'Summary-city:') END AS TYPE,
       SUM(VISIT.NUMBER_OF_VISITORS) VISITORS, 
       SUM(VISIT.PROFIT) AS PROFIT
FROM VISIT
       JOIN CITY ON VISIT.ID_CITY = CITY.ID_CITY 
       JOIN ACCOMMODATIONTYPE ON VISIT.ID_TYPE = ACCOMMODATIONTYPE.ID_TYPE
GROUP BY 
    ROLLUP(CITY.COUNTRY, CITY.NAME, ACCOMMODATIONTYPE.NAME) 
ORDER BY 
    VISITED_COUNTRY, CITY, TYPE;
访问国家城市类型参观者利润
意大利佛罗伦萨公寓360度一百
意大利佛罗伦萨房间三百三十九十二
意大利佛罗伦萨摘要-城市:六九零一百九十二
意大利罗马房间三百八十
意大利罗马摘要-城市:三百八十
意大利摘要-国家:联系我们九九零二百七十二
总计:联系我们联系我们九九零二百七十二

fiddle
它是主题“SQLforOLAP”中OracleSQL学习任务
在这种情况下,可能更合适的方法是查看分组ID,并使用它们来确定何时应用标签,就像@serg建议的那样。如果你正在学习一些东西,那么任务通常希望你使用你最近被介绍过的东西。
grouping()的类似方法:

SELECT CASE WHEN GROUPING(CITY.COUNTRY) = 1
       THEN 'Summary-total:' ELSE CITY.COUNTRY END AS VISITED_COUNTRY, 
       CASE WHEN GROUPING(CITY.COUNTRY) = 0 AND GROUPING(CITY.NAME) = 1
       THEN 'Summary-country:' ELSE CITY.NAME END AS CITY, 
       CASE WHEN GROUPING(CITY.COUNTRY) = 0 AND GROUPING(CITY.NAME) = 0 AND GROUPING(ACCOMMODATIONTYPE.NAME) = 1
       THEN 'Summary-city:' ELSE ACCOMMODATIONTYPE.NAME END AS TYPE,
       SUM(VISIT.NUMBER_OF_VISITORS) VISITORS, 
       SUM(VISIT.PROFIT) AS PROFIT
FROM VISIT
       JOIN CITY ON VISIT.ID_CITY = CITY.ID_CITY 
       JOIN ACCOMMODATIONTYPE ON VISIT.ID_TYPE = ACCOMMODATIONTYPE.ID_TYPE
GROUP BY 
    ROLLUP(CITY.COUNTRY, CITY.NAME, ACCOMMODATIONTYPE.NAME) 
ORDER BY 
    VISITED_COUNTRY, CITY, TYPE;
访问国家城市类型参观者利润
意大利佛罗伦萨公寓360度一百
意大利佛罗伦萨房间三百三十九十二
意大利佛罗伦萨摘要-城市:六九零一百九十二
意大利罗马房间三百八十
意大利罗马摘要-城市:三百八十
意大利摘要-国家:联系我们九九零二百七十二
总计:联系我们联系我们九九零二百七十二

fiddle

相关问题