May 14, 2009 at 10:09 am
So here is the data
Id Term
1, A
2, A
3, A
3, B
i need a query that can give me following result
1, A
2, A
3, A
Means i need only 1 record for each Id (it could be either one of the multiple records)
May 14, 2009 at 10:15 am
Look for ROW_NUMBER() function with PARTITION BY clause in Books Online.
--Ramesh
May 14, 2009 at 10:16 am
DECLARE @MyTable TABLE
(Id INT,
Team CHAR(1))
INSERT INTO @MyTable
SELECT 1, 'A' UNION ALL
SELECT 2, 'A' UNION ALL
SELECT 3, 'A' UNION ALL
SELECT 3, 'B'
SELECT
Id,MAX(Team)
FROM @MyTable
GROUP BY Id
Look at how the Group by works in BOL and the aggregation functions that go with it, e.g. MAX,MIN etc
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
May 14, 2009 at 10:19 am
In that example grouping by ID and using MIN or MAX on the other column will do it.
If there are a lot of cols and you need them all to relate to the same record, see the suggestion above. (Ramesh's) 😉
--------
[font="Tahoma"]I love deadlines. I like the whooshing sound they make as they fly by. -Douglas Adams[/font]
May 14, 2009 at 10:19 am
Ramesh (5/14/2009)
Look for ROW_NUMBER() function with PARTITION BY clause in Books Online.
Thanks for replying Ramesh, i know ROW_NUMBER() function, but can you please explain how that can be used to get the required result? Becuase ROW_NUMBER will generate a unique id for the data but will it give me the data i want?
May 14, 2009 at 10:21 am
Christopher Stobbs (5/14/2009)
DECLARE @MyTable TABLE
(Id INT,
Team CHAR(1))
INSERT INTO @MyTable
SELECT 1, 'A' UNION ALL
SELECT 2, 'A' UNION ALL
SELECT 3, 'A' UNION ALL
SELECT 3, 'B'
SELECT
Id,MAX(Team)
FROM @MyTable
GROUP BY Id
Thanks for this query, but the problem is in actual table i have tons of columns so i cannot use MIN,MAX for each of them
(for simplicity i gave a temporary table in my orignal post)
May 14, 2009 at 10:25 am
usman.tanveer (5/14/2009)
Thanks for this query, but the problem is in actual table i have tons of columns so i cannot use MIN,MAX for each of them(for simplicity i gave a temporary table in my orignal post)
Is there a unique key on the table?
--------
[font="Tahoma"]I love deadlines. I like the whooshing sound they make as they fly by. -Douglas Adams[/font]
May 14, 2009 at 10:25 am
Rob Goddard (5/14/2009)
In that example grouping by ID and using MIN or MAX on the other column will do it.If there are a lot of cols and you need them all to relate to the same record, see the suggestion above. (Ramesh's) 😉
Thanks, yes i have a lot of cols, but still i am not sure how Row_number will work for me
here is the query with Row number
DECLARE @MyTable TABLE
(Id INT,
Team CHAR(1))
INSERT INTO @MyTable
SELECT 1, 'A' UNION ALL
SELECT 2, 'A' UNION ALL
SELECT 3, 'A' UNION ALL
SELECT 3, 'B'
SELECT
Id,Team, ROW_NUMBER() OVER (ORDER BY Id) RowNum
FROM @MyTable
May 14, 2009 at 10:26 am
If you could have read Books Online, you would surely be able to solve it on your own. Anyways, here is the solution for the sample data provided by Christopher (thanks for this, Chris:-))
DECLARE @MyTable TABLE
(Id INT,
Team CHAR(1))
INSERT INTO @MyTable
SELECT 1, 'A' UNION ALL
SELECT 2, 'A' UNION ALL
SELECT 3, 'A' UNION ALL
SELECT 3, 'B'
; WITH Test
AS
(
SELECTROW_NUMBER() OVER( PARTITION BY Id ORDER BY Team ) AS RowNumber, *
FROM@MyTable
)
SELECTId, Team
FROMTest
WHERERowNumber = 1
--Ramesh
May 14, 2009 at 11:03 am
Thanks everyone, specially Ramesh.
May 14, 2009 at 11:19 am
usman.tanveer (5/14/2009)
Christopher Stobbs (5/14/2009)
DECLARE @MyTable TABLE
(Id INT,
Team CHAR(1))
INSERT INTO @MyTable
SELECT 1, 'A' UNION ALL
SELECT 2, 'A' UNION ALL
SELECT 3, 'A' UNION ALL
SELECT 3, 'B'
SELECT
Id,MAX(Team)
FROM @MyTable
GROUP BY Id
Thanks for this query, but the problem is in actual table i have tons of columns so i cannot use MIN,MAX for each of them
(for simplicity i gave a temporary table in my orignal post)
Sure you can... how many columns?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 15, 2009 at 1:07 am
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply