我有一张表:
Column1 || Column2 || Column3
------------------------
A || 01 || Test1
A || 02 || Test2
A || 03 || Test3
B || 04 || Test4
C || 05 || Test5
C || 06 || Test6
注意:Column2是主键
所需的输出表如下:
Related || Column1 || Column2 || Column3
-------------------------------
** || A || 01 || Test1
** || A || 02 || Test2
** || A || 03 || Test3
|| B || 04 || Test4
** || C || 05 || Test5
** || C || 06 || Test6
这里我试图通过创建一个新列来显示column1的所有相关数据和“**”。我已尝试将查询用作:
SELECT kh.Column4,
CASE WHEN COUNT(c.Column1) > 1 THEN '*' ELSE '' END AS Related
FROM <TableName> WITH(NOLOCK)
JOIN <TableName2> kh WITH(NOLOCK) ON (kh.Column4 = c.Column1)
GROUP BY Column4
但没有获得所需的输出。还有其他办法吗?
答案 0 :(得分:0)
这应该有效:
/* Setting up the sample data */
declare @kh table
(
Letter nvarchar(1),
Number int identity(1, 1)
primary key
not null,
Test nvarchar(10)
)
insert into @kh
(Letter)
values (N'A'),
(N'A'),
(N'A'),
(N'B'),
(N'C'),
(N'C')
update @kh
set Test = N'Test' + cast(Number as nvarchar)
/* The requested solution using the above sample data */
select case when h.ColCount > 1 then N'**'
else N''
end as Dupes,
k.Letter,
k.Number,
k.Test
from @kh as k
left join (
select kh.Letter,
count(1) as ColCount
from @kh as kh
group by kh.Letter
) as h
on k.Letter = h.Letter
答案 1 :(得分:0)
在您的方案中,以下代码将起作用。首先,我在内部块中取重复值,并使用实际表左连接以获得结果。
SELECT
CASE WHEN REP.Column1 IS NOT NULL THEN '**' ELSE '' END AS Related,
TT.Column1, TT.Column2, TT.Column3
FROM TestTable TT
LEFT JOIN (
SELECT Column1
FROM TestTable
GROUP BY Column1
HAVING COUNT(Column1) > 1
) REP ON REP.Column1 = TT.Column1
答案 2 :(得分:0)
您可以在案例陈述中使用import moduleFoo from './foo';
import moduleBar from './bar';
angular.module('app', [moduleFoo, moduleBar]);
函数,而无需分组
COUNT() OVER(PARTITION BY)