在sql中获取相同行数的concat col。

问题描述 投票:0回答:1

我正在尝试Concat( description, number, date),然后像count(concat(description,number, date)) <=1 then 'y', else''end那样计数。

但是我得到了错误。我正在尝试Concatenate三列,并希望返回一行在col中出现多少次。

sql count case concat
1个回答
0
投票

如果你按你的连贯字符串分组,你已经得到所有不同的组合。只需增加一个字段来知道每行出现的次数,可以用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))

0
投票

简单的决策,返回分组的行和这行在输入表中存在的次数。

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
© www.soinside.com 2019 - 2024. All rights reserved.