create table #temp
(
TREE varchar (500),
SEED varchar (500),
FLOWER varchar (500),
Year varchar (500),
FLRNO varchar (500),
COMMENT1 varchar (500),
COMMENT3 varchar (500),
hash AS hashbytes('MD5', [TREE] + SEED + FLOWER + YEAR + FLRNO +
COMMENT1 + COMMENT3)
)
insert #temp
SELECT 'BANANA', 'Nis', 'Tit', '2004', '57101', 'Sweet Smell
Flower', '' UNION ALL
SELECT 'BANANA', 'Nis', 'Tit', '2004', '57101', 'Bad Smell
flower' , '' UNION ALL
SELECT 'BANANA', 'Nis', 'Tit', '2004', 'B0019B', 'Sweet Smell
Flower', 'CATCH ME' UNION ALL
SELECT 'BANANA', 'Nis', 'Tit', '2004', 'B0019B', 'Bad Smell
flower', 'CATCH ME' UNION ALL
SELECT 'APPLE', 'ALE', 'PPL', '2015', 'PRS13', 'SXT; SWEET
SMELL FLOWER', 'God' UNION ALL
SELECT 'APPLE', 'ALE', 'PPL', '2015', 'PRS13', 'SE; Bad SMELL
FLOWER', 'CATCH ME' UNION ALL
SELECT 'APPLE', 'ALE', 'PPL', '2015', 'PRS13', 'SXT; SWEET
SMELL FLOWER', 'CATCH ME'
我们如何检查TREE,SEED,FLOWER和Year组是否存在FLRNO,该组存在于组中的所有可用COMMENT1 和 同一组TREE,SEED,FLOWER和Year都有评论3,其中包含所有可用的COMMENT1
树APPLE上帝不适用于" SE;坏SMELL FLOWER"所以整个组都从输出中排除。
预期产出: / *
'BANANA', 'Nis', 'Tit', '2004', '57101', 'Sweet Smell Flower', ''
'BANANA', 'Nis', 'Tit', '2004', '57101', 'Bad Smell flower' , ''
'BANANA', 'Nis', 'Tit', '2004', 'B0019B', 'Sweet Smell Flower',
'CATCH ME'
'BANANA', 'Nis', 'Tit', '2004', 'B0019B', 'Bad Smell flower', 'CATCH ME'
* /
- 已尝试查询但未提供结果
; WITH CTE AS (
SELECT [TREE], SEED, FLOWER, YEAR,
COUNT(DISTINCT hash) AS grp_count,
--COUNT(*) AS grp_count,
COUNT(DISTINCT FLRNO) AS part_grp_count,
COUNT(DISTINCT COMMENT1) AS COMMENT1_grp_count,
COUNT(DISTINCT COMMENT3)AS COMMENT3_grp_count
FROM #temp
GROUP BY [TREE], SEED, FLOWER, YEAR
)
SELECT *
FROM #temp t
WHERE EXISTS (SELECT *
FROM CTE
WHERE CTE.[TREE] = t.[TREE]
AND CTE.FLOWER = t.FLOWER
AND CTE.SEED = t.SEED
AND CTE.YEAR = t.YEAR
AND CTE.grp_count = CTE.COMMENT1_grp_count * CTE.COMMENT3_grp_count * CTE.part_grp_count
AND CTE.COMMENT1_grp_count > 1)
答案 0 :(得分:0)
目前还不清楚你想要完成什么,但至少这会得到你的结果:
select * from temp t
where
(
select top 1 count(distinct comment1)
from temp t2
where t2.tree = t.tree
group by t2.comment3
order by 1 asc
) > 1
中的示例