开发者

Oracle aggregated query

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

Have four columns of type Varchar. What i wish to determine is:-

1. For each unique value of ColB(rata1, rata2 etc), find corresponding value of ColA and other two columns (ColC, ColD) where that specific value of ColA has ColC value equal to 'T' 0 or 1 times.

rata2 data1 T Test1
rata3 data2 T Test1 
rata4 data3 T Test1

2. In the above query when ColC was equal to 'T' 0 or 1 times, find other rows for which Co开发者_如何学运维lA has ColC equal to 'F' for that specific value of ColB.

rata3 data1 F Test2  


Didn't understand your question at all. I'll correct the answer if you say something else. but as far I understood you want something like this?

Edit: I made the same table that you gave us in a DB. And this worked fine. Try it and tell me if it works

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'

Edit 2: What about this?

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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜