SQL 按相似条件分组
SQL group by with like condition
假设有一个 SQL table: 包含列的 testTable:clientID、colA、colB、colC。
reference clientID colA colB colC
---------------------------------------------
001 1 test1 test2 test3
002 1 test1 ball2 test3
003 2 test1 ball2 test3
004 2 test1 ball2 test3
005 3 test1 test2 test3
006 4 test1 test2 test3
007 4 test1 test2 test3
009 5 test1 ball2 test3
---------------------------------------------
我想 select 所有不同的行,其中 colB 类似于 'test' 并按 clientID 分组。所以我最终得到:
reference clientID colA colB colC
----------------------------------------------
001 1 test1 test2 test3
005 3 test1 test2 test3
006 4 test1 test2 test3
----------------------------------------------
编辑:
参考列是唯一的
如果我使用
select distinct * .. from .. where colB like '%test%' group by clientID
那么返回的结果没有分组的clientID
仅仅一个不同的是不够的?
Select distinct * from #data where ColB like 'test%'
或者,如果您只需要考虑某些特定的列,则可以使用前 1 位并列
Select top (1) with ties * from #groupby where ColB like 'test%'
order by row_number() over(partition by clientid order by cola,colb,colc)--you can include your required columns only
where 子句位于 having 和 group by 之前。如果你想在分组前过滤掉记录条件在where子句中,如果你想过滤掉分组记录条件在having子句中:
select ...
from ...
where ...
group by ...
having ...
或者类似这样的东西,如果上面的东西不起作用的话
select ...
from (
select ...
from ...
where ...
group by ...
having ...
) x
where ...
试试这个,
Select distinct * From tableA where colB like '%test%'
当您使用 GROUP BY 时,您必须指定您在查询中使用且不使用聚合函数的所有列。
所以,例如,
SELECT CLIENTID, COLA, COLB, COLC, COUNT(*) AS RC
FROM YOURTABLE
WHERE COLB LIKE '%test%'
GROUP BY CLIENTID, COLA, COLB, COLC
按一列分组时,会将多行合并为一行,select中的其他列必须是聚合函数或子查询。使用哪个功能取决于您的需要。如果与字符串 column
一起使用,如下例所示使用 MIN() 将按字母顺序给出第一个结果
SELECT clientID
, MIN(colA) AS colA
, MIN(colB) AS colB
, MIN(colC) AS colC
FROM tableA
WHERE colB LIKE '%test%'
GROUP BY clientID
编辑:
这是另一种解决方案,不使用 GROUP BY,而是使用 Common Table Expression using ROW_NUMBER()
WITH CTE_Source AS
(
SELECT *
FROM TableA
WHERE colB LIKE '%test%'
)
, CTE_Filter AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY ClientID ORDER BY reference) RN
FROM CTE_Source
)
SELECT *
FROM CTE_Filter
WHERE RN = 1
只需使用:-
group by clientID,colA,colB,colC
而不是
group by clientID
演示:-
Create table testTable (clientID int , colA varchar(10), colB varchar(10), colC varchar(10))
go
insert into testTable values
(1,'test1','test2','test3'),
(1,'test1','ball2','test3'),
(2,'test1','ball2','test3'),
(2,'test1','ball2','test3'),
(3,'test1','test2','test3'),
(4,'test1','test2','test3'),
(4,'test1','test2','test3'),
(5,'test1','ball2','test3')
select * from testTable
where colB LIKE '%test%'
group by clientID,colA,colB,colC
输出:-
clientID colA colB colC
1 test1 test2 test3
3 test1 test2 test3
4 test1 test2 test3
假设有一个 SQL table: 包含列的 testTable:clientID、colA、colB、colC。
reference clientID colA colB colC
---------------------------------------------
001 1 test1 test2 test3
002 1 test1 ball2 test3
003 2 test1 ball2 test3
004 2 test1 ball2 test3
005 3 test1 test2 test3
006 4 test1 test2 test3
007 4 test1 test2 test3
009 5 test1 ball2 test3
---------------------------------------------
我想 select 所有不同的行,其中 colB 类似于 'test' 并按 clientID 分组。所以我最终得到:
reference clientID colA colB colC
----------------------------------------------
001 1 test1 test2 test3
005 3 test1 test2 test3
006 4 test1 test2 test3
----------------------------------------------
编辑: 参考列是唯一的 如果我使用 select distinct * .. from .. where colB like '%test%' group by clientID 那么返回的结果没有分组的clientID
仅仅一个不同的是不够的?
Select distinct * from #data where ColB like 'test%'
或者,如果您只需要考虑某些特定的列,则可以使用前 1 位并列
Select top (1) with ties * from #groupby where ColB like 'test%'
order by row_number() over(partition by clientid order by cola,colb,colc)--you can include your required columns only
where 子句位于 having 和 group by 之前。如果你想在分组前过滤掉记录条件在where子句中,如果你想过滤掉分组记录条件在having子句中:
select ...
from ...
where ...
group by ...
having ...
或者类似这样的东西,如果上面的东西不起作用的话
select ...
from (
select ...
from ...
where ...
group by ...
having ...
) x
where ...
试试这个,
Select distinct * From tableA where colB like '%test%'
当您使用 GROUP BY 时,您必须指定您在查询中使用且不使用聚合函数的所有列。 所以,例如,
SELECT CLIENTID, COLA, COLB, COLC, COUNT(*) AS RC
FROM YOURTABLE
WHERE COLB LIKE '%test%'
GROUP BY CLIENTID, COLA, COLB, COLC
按一列分组时,会将多行合并为一行,select中的其他列必须是聚合函数或子查询。使用哪个功能取决于您的需要。如果与字符串 column
一起使用,如下例所示使用 MIN() 将按字母顺序给出第一个结果SELECT clientID
, MIN(colA) AS colA
, MIN(colB) AS colB
, MIN(colC) AS colC
FROM tableA
WHERE colB LIKE '%test%'
GROUP BY clientID
编辑: 这是另一种解决方案,不使用 GROUP BY,而是使用 Common Table Expression using ROW_NUMBER()
WITH CTE_Source AS
(
SELECT *
FROM TableA
WHERE colB LIKE '%test%'
)
, CTE_Filter AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY ClientID ORDER BY reference) RN
FROM CTE_Source
)
SELECT *
FROM CTE_Filter
WHERE RN = 1
只需使用:-
group by clientID,colA,colB,colC
而不是
group by clientID
演示:-
Create table testTable (clientID int , colA varchar(10), colB varchar(10), colC varchar(10))
go
insert into testTable values
(1,'test1','test2','test3'),
(1,'test1','ball2','test3'),
(2,'test1','ball2','test3'),
(2,'test1','ball2','test3'),
(3,'test1','test2','test3'),
(4,'test1','test2','test3'),
(4,'test1','test2','test3'),
(5,'test1','ball2','test3')
select * from testTable
where colB LIKE '%test%'
group by clientID,colA,colB,colC
输出:-
clientID colA colB colC
1 test1 test2 test3
3 test1 test2 test3
4 test1 test2 test3