用SQL语句做,重复的数据只显示一条,其它重复相同的为空
目前我有A、B、C三张表。我主要想用SQL语句查询显示图片中的‘查询结果’,A表中重复的keyjobno,name只显示一条记录,但B表、C表中的数据需要全部显示出来,请高手指点一下,不能用临时表
[解决办法]
--#1.拼字段串的话,'号需要转义成''
--#2.access不支持CTE,且不支持ROW_NUMBER函数,建议增加自增ID,且用只能用字查询实现
--#3.加个PARTITION BY keyjobno即可
;WITH
cte1 AS
(
SELECT *, rowid=ROW_NUMBER() OVER(PARTITION BY keyjobno ORDER BY GETDATE()) FROM A表
),
cte2 AS
(
SELECT *, rowid=ROW_NUMBER() OVER(PARTITION BY keyjobno ORDER BY GETDATE()) FROM B表
),
cte3 AS
(
SELECT *, rowid=ROW_NUMBER() OVER(PARTITION BY keyjobno ORDER BY GETDATE()) FROM C表
)
SELECT
keyjobno = (CASE a.rowid WHEN 1 THEN a.keyjobno ELSE '' end),
[name] = (CASE a.rowid WHEN 1 THEN a.[name] ELSE '' end),
a.marry,a.bear,a.other,b.indate,c.activity,c.[date]
FROM cte1 a
INNER JOIN cte2 b
ON a.keyjobno = b.keyjobno
AND a.[name] = b.[name]
AND a.rowid = b.rowid
INNER JOIN cte3 c
ON a.keyjobno = c.keyjobno
AND a.[name] = c.[name]
AND a.rowid = c.rowid