count—获取sql中concat col中相同行的数目

flmtquvp  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(395)

我正在尝试concat(description,number,date),然后像count(concat(description,number,date))<=1,然后是“y”,否则是“end”。
但我犯了个错误。我正在尝试连接三列,并想返回一行在列中出现的次数。您能告诉我吗

zpqajqem

zpqajqem1#

如果按连接的字符串分组,则已获得所有不同的组合。只需再添加一个字段,就可以知道每行显示sum(1)或count(1)/count(*)的次数。
编辑

SELECT concat(t1.description,t1,number, t1.date) AS UID,
CASE WHEN t2.NUM_TIMES_UID <= 1 THEN 'y' ELSE 'dummy' END AS DUMMY_VAR
FROM table t1
LEFT JOIN (
     SELECT
         concat(description,number, date) AS UID,
         SUM(1) AS NUM_TIMES_UID
     FROM table
     GROUP BY
         concat(description,number, date)
) t2 ON (concat(t1.description,t1,number, t1.date) = concat(t2.description,t2,number, t2.date))
g0czyy6m

g0czyy6m2#

返回分组行和该行在输入表中存在的次数的简单决策:

with items as (
select concat(description,number, date) as item
from table1
group by concat(description,number, date)
)

select item, count(item) as item_cnt
from items
group by item

相关问题