|
View previous topic :: View next topic
|
| Author |
Message |
hcl_ln
New User
Joined: 14 Sep 2005 Posts: 21
|
|
|
|
Hello,
I have a table like this:
ColA ColB ColC
123456 1996-01-01 983.50
123456 1996-01-01 1290.01
123456 2007-01-01 560.05
123456 2006-01-01 893.50
I need to select only the first two rows of the table as the output, meaning I need to get the rows with same ColA and ColB but with diff ColC, as below.
ColA ColB ColC
123456 1996-01-01 983.50
123456 1996-01-01 1290.01
I have written the query like this:
SELECT ColA, ColB, ColC
FROM T1
where ColA = '123456'
GROUP BY ColA, ColB, ColC
HAVING COUNT(ColB) > 1 ;
But the result is Zero rows.
If I remove the ColC from the query, it is retrieving only one row instead of two. Can somebidy help?
Thanks in advance ! |
|
| Back to top |
|
 |
Balraj
New User
Joined: 16 Aug 2007 Posts: 34 Location: Bangalore
|
|
|
|
Hai
Try out this Query
SELECT ColA, ColB, ColC
FROM T1
where ColA = '123456' AND ColB='1996-01-01 ' |
|
| Back to top |
|
 |
dbzTHEdinosauer
Global Moderator

Joined: 20 Oct 2006 Posts: 6965 Location: porcelain throne
|
|
|
|
| Code: |
SELECT A.COLA
, A.COLB
, A.COLC
FROM T1 A
, T1 B
WHERE A.COLA = B.COLA
AND A.COLB = B.COLB
AND A.COLC <> B.COLB;
|
or
| Code: |
SELECT A.COLA
, A.COLB
, A.COLC
FROM T1 A JOIN T1 B
ON A.COLA = B.COLA
AND A.COLB = B.COLB
WHERE A.COLC <> B.COLB;
|
|
|
| Back to top |
|
 |
hcl_ln
New User
Joined: 14 Sep 2005 Posts: 21
|
|
|
|
Thank you dbzTHEdinosauer & Balraj,
Balraj, Since the values I have given are examples only and I donot know how many of these type of rows are there, I can use host variables. But thanks for the suggestion.
dbzTHEdinosauer,
Thank you for providing the solutions. I have tried both the queries and gave my desired results.
The third condition in the WHERE clause should be: A.COLC <> B.COLC.
I think this is a typo, but I do appreciate your efforts. |
|
| Back to top |
|
 |
dbzTHEdinosauer
Global Moderator

Joined: 20 Oct 2006 Posts: 6965 Location: porcelain throne
|
|
|
|
hcl_ln,
good catch. I did not do that intentionally, a typo as you said. |
|
| Back to top |
|
 |
|
|