简体   繁体   English

SAP HANA SQL | 计算同一列中的不同投影值

[英]SAP HANA SQL | Count different projected values in the same column

I have the following query in SAP HANA.我在 SAP HANA 中有以下查询。 I projected a column that displays 02 results in millions of lines.我投影了一列,在数百万行中显示 02 结果。 I would like to count how many of this two results each DISTINCT ZCGNOTAL has.我想计算每个 DISTINCT ZCGNOTAL 有多少这两个结果。

Please help me.请帮我。

SELECT ZCGINSTAL, 
       ZCGNOTAL, 
       "Latitude",
       CASE WHEN "Latitude" > '0' THEN 'ZERADA' ELSE 'COORDENADA' END AS COORD
FROM "CLB162585"."062021MOM"

As I understand the question, the OP wants to know, for every value of ZCGNOTAL how many records have a value of "ZERADA" and how many have a value of "COORDENADA" in the computed column COORD .正如我所理解的问题,OP 想知道,对于ZCGNOTAL每个值,在计算列COORD有多少记录具有“ZERADA”的值以及有多少记录具有“COORDENADA”的值。

That can be computed by a simple multi-column GROUP BY and a COUNT aggregation "on-top" of the existing query:这可以通过一个简单的多列GROUP BY和现有查询的COUNT聚合“on-top”来计算:

WITH base_data as (
   SELECT   ZCGINSTAL
          , ZCGNOTAL 
          , "Latitude"
          , CASE 
              WHEN "Latitude" > '0' THEN 'ZERADA' 
              ELSE 'COORDENADA' 
            END AS     COORD
FROM 
     "CLB162585"."062021MOM")
SELECT
     ZCGNOTAL
   , COORD
   , COUNT(*) as COORD_CNT 
FROM 
   base_data
GROUP BY
     ZCGNOTAL
   , COORD

If desired, the query can be rewritten to not use a common-table expression (WITH CLAUSE) or a subquery, but for SAP HANA this is unlikely to yield better performance.如果需要,可以将查询重写为不使用公用表表达式 (WITH CLAUSE) 或子查询,但对于 SAP HANA,这不太可能产生更好的性能。 HANA rewrites the query before optimisation anyway and resolving subqueries is part of that rewriting process. HANA 无论如何都会在优化之前重写查询,并且解析子查询是重写过程的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM