April 6, 2006 at 2:01 pm
Hi Db Gurus,
I have table with this following data
100000133 | 100000217 | 343 | 100000323 |
100000133 | 100000217 | 343 | 100000324 |
100000133 | 100000217 | 343 | 100000325 |
100000134 | 100000217 | 343 | 100000323 |
100000134 | 100000217 | 343 | 100000324 |
100000134 | 100000217 | 343 | 100000325 |
100000135 | 100000217 | 343 | 100000323 |
100000135 | 100000217 | 343 | 100000324 |
100000135 | 100000217 | 343 | 100000325 |
I wanted to select a query to to display
100000133 | 100000217 | 343 | 100000323 |
100000134 | 100000217 | 343 | 100000324 |
100000135 | 100000217 | 343 | 100000325 |
How do I do this.......
Thanks,
Ganesh
April 6, 2006 at 2:53 pm
SELECT TOP 3 * FROM myTable
ORDER BY Column4, Column1
I hope this helps
April 6, 2006 at 3:34 pm
select distinct will give you this
April 6, 2006 at 3:38 pm
Wrong my friend al rows are diferent
April 6, 2006 at 3:53 pm
Can you describe desired result in words?
What's a logic behind it?
_____________
Code for TallyGenerator
April 6, 2006 at 3:58 pm
The sledge hammer is back!
This will work, but it is somewhat involved; although the request is rather involved as well...
DECLARE @t TABLE( Column1 integer, Column2 integer,Column3 integer,Column4 integer)
DECLARE @PreFinal TABLE( IdentityField integer IDENTITY(1,1),
Column1 integer, Column2 integer,Column3 integer,Column4 integer)
DECLARE @Final TABLE( Column1 integer, Column2 integer,Column3 integer,Column4 integer)
INSERT INTO @t
SELECT 100000133, 100000217, 343, 100000323 UNION ALL
SELECT 100000133, 100000217, 343, 100000324 UNION ALL
SELECT 100000133, 100000217, 343, 100000325 UNION ALL
SELECT 100000134, 100000217, 343, 100000323 UNION ALL
SELECT 100000134, 100000217, 343, 100000324 UNION ALL
SELECT 100000134, 100000217, 343, 100000325 UNION ALL
SELECT 100000135, 100000217, 343, 100000323 UNION ALL
SELECT 100000135, 100000217, 343, 100000324 UNION ALL
SELECT 100000135, 100000217, 343, 100000325
INSERT INTO @PreFinal
SELECT Column1, Column2, Column3, Column4 FROM @t
DECLARE @Counter integer,
@CurrentNumber integer,
@MaxNumber integer,
@DesiredColumn4 integer
SELECT @Counter = 0
SELECT @CurrentNumber = (SELECT MIN( Column1) FROM @t)
SELECT @MaxNumber = (SELECT MAX( Column1) FROM @t)
SELECT @DesiredColumn4 = (SELECT MIN( IdentityField) FROM @PreFinal
WHERE Column1 = @CurrentNumber)
WHILE @CurrentNumber <= @MaxNumber
BEGIN
INSERT INTO @Final
SELECT @CurrentNumber, MIN( pf.Column2), MIN( pf.Column3), m.Column4
FROM @PreFinal pf
INNER JOIN (SELECT Column1, Column4
FROM @PreFinal
WHERE IdentityField = @DesiredColumn4) m ON( pf.Column1 = m.Column1)
WHERE pf.Column1 = @CurrentNumber
GROUP BY m.Column4
SELECT @CurrentNumber = (SELECT MIN( Column1) FROM @t WHERE Column1 > @CurrentNumber)
SELECT @Counter = @Counter + 1
SELECT @DesiredColumn4 = (SELECT MIN( IdentityField) + @Counter
FROM @PreFinal WHERE Column1 = @CurrentNumber)
END
SELECT * FROM @Final
I wasn't born stupid - I had to study.
April 6, 2006 at 5:01 pm
Thanks a lot Farrell......I appreciate it....
It worked......
Thanks,
Ganesh
April 6, 2006 at 6:08 pm
Glad I could help! This is an odd requirment - would you care to be more explicit about why you need these unusual results? As the number of Column1 entries increases, but thier records decrease, this could cause unusual returns...
Thanks
I wasn't born stupid - I had to study.
April 7, 2006 at 6:26 am
For the data presented, this'll do it... and it doesn't assume that there will only be different 3 rows in the return...
SELECT Col1,Col2,Col3,MAX(Col4) AS Col4 FROM yourtable GROUP BY Col1, Col2, Col3 ORDER BY Col1, Col2, Col3
If the requirements are a bit different than above, repost and we'll give it a whirl...
--Jeff Moden
Change is inevitable... Change for the better is not.
April 7, 2006 at 6:27 am
For the data presented, this'll do it... and it doesn't assume that there will only be different 3 rows in the return...
SELECT Col1,Col2,Col3,MAX(Col4) AS Col4 FROM yourtable GROUP BY Col1, Col2, Col3 ORDER BY Col1, Col2, Col3
If the requirements are a bit different than above, repost and we'll give it a whirl...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply