ColA ColB ColC ColD
data1 rata1 T Test1
data1 rata3 F Test2
data1 rata2 T Test1
data2 rata1 T Test1
data2 rata3 T Test1
data3 rata4 T Test1
有四列Varchar类型。我想确定的是: - 1.对于ColB的每个唯一值(rata1,rata2等),找到ColA和其他两列(ColC,ColD)的相应值,其中ColA的特定值具有等于'T'0或1的ColC值。
rata2 data1 T Test1
rata3 data2 T Test1
rata4 data3 T Test1
2.在上述查询中,当ColC等于'T'0或1次时,找到ColA具有ColC等于'F'的其他行,用于该特定的ColB值。
rata3 data1 F Test2
答案 0 :(得分:1)
根本不明白你的问题。如果你说别的话,我会更正答案。但据我所知,你想要这样的东西?
编辑:我制作了您在数据库中提供的相同表格。这很好用。试一试,告诉我它是否有效
SELECT COLUMNB,MAX(COLUMNA), MAX(ColumnC), MAX(COLUMND)
FROM DUMMY
WHERE ColumnC = 'T'
GROUP BY COLUMNB
HAVING SUM(CASE WHEN COLumnC = 'T' THEN 1 ELSE 0 END) <= 1
UNION ALL
SELECT *
FROM DUMMY
WHERE COLumnC = 'F'
编辑2:这是怎么回事?
WITH B
AS(
SELECT A.COLUMNB, MAX(A.COLUMNA) AS COLUMNA, MAX(A.ColumnC) AS COLUMNC, MAX(A.COLUMND) AS COLUMND
FROM DUMMY A
GROUP BY A.COLUMNB
HAVING SUM(CASE WHEN A.COLumnC = 'T' THEN 1 ELSE 0 END) <= 1)
SELECT *
FROM B
UNION ALL
SELECT D.COLUMNB, D.ColumnA, D.ColumnC, D.ColumnD
FROM B, DUMMY D
WHERE B.ColumnB = D.COLUMNB
AND D.ColumnC = 'F'