April 3, 2014 at 7:21 am
Pls help me
My Table looks like
Track | Books
-----------------
204 | A
204 | B
204 | B
204 | C
206 | A
204 | A
204 | A
203 | A
202 | C
202 | C
202 | B
I need a output like
Track | Books
-----------------
204 | A
204 | B
204 | C
202 | C
202 | B
pls someone help me
April 3, 2014 at 7:39 am
What have you tried?
SELECT DISTINCT ?
April 3, 2014 at 7:52 am
Are you trying to dedupe the data in this table or just the result set?
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
April 3, 2014 at 7:54 am
I no need the distinct , I need same tracks which have multiple books and I don't need tracks which have
unique books for(eg)
Track | Books
-----------------
204 | A
204 | B
204 | C
202 | C
202 | B
track 204 has multiple books and track 202 has multiple books ???
and i no need of the DISTINCT ...
thanks for the reply in advance..
April 3, 2014 at 7:58 am
This would do as you ask...
SELECT Track, Books
FROM dbo.YourTable
GROUP BY Track, Books
HAVING COUNT(*) > 1
ORDER BY Track, Books
;
--Jeff Moden
Change is inevitable... Change for the better is not.
April 3, 2014 at 7:58 am
Sorry just reread your reply.
April 3, 2014 at 8:01 am
djj (4/3/2014)
Try running this:
create table #temp1 (Track INT, Books VARCHAR(10))
insert into #temp1 values
(204,'A'),
(204,'B'),
(204,'B'),
(204,'C'),
(206,'A'),
(204,'A'),
(204,'A'),
(203,'A'),
(202,'C'),
(202,'C'),
(202,'B')
SELECT Track, Books FROM #temp1
SELECT DISTINCT Track, Books FROM #temp1
order by 1, 2
Like the OP said, he's not looking for DISTINCT items... he's only looking for items that have more than one occurance.
Also, you should get out of the habit of using column ordinals in the ORDER BY. It was never a good practice and the method has been deprecated by Microsoft.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 3, 2014 at 8:02 am
Yep I see that now. Again sorry for the misstep.
April 3, 2014 at 8:09 am
This should produce the results you have requested
DECLARE @table TABLE (Track INT, Books CHAR(1))
INSERT INTO @Table
VALUES (204,'A'),
(204,'B'),
(204,'B'),
(204,'C'),
(206,'A'),
(204,'A'),
(204,'A'),
(203,'A'),
(202,'C'),
(202,'C'),
(202,'B');
WITH presel AS (
SELECT Track
FROM @table
GROUP BY Track
HAVING COUNT(*) > 1
)
SELECT Track, Books
FROM @table t
WHERE Track IN (SELECT Track FROM presel)
GROUP BY Track, Books
ORDER BY Track DESC, Books ASC
;
here is the output from that
TrackBooks
204A
204B
204C
202B
202C
The one difference is the sort order you have prescribed in your desired output conflicts within itself. You have an ASC sort followed by a DESC sort within the Books portion of the sort. So I stuck with the ASC sort.
The other queries presented thus far fall a bit short in your request
Taking an example
DECLARE @table TABLE (Track INT, Books CHAR(1))
INSERT INTO @Table
VALUES (204,'A'),
(204,'B'),
(204,'B'),
(204,'C'),
(206,'A'),
(204,'A'),
(204,'A'),
(203,'A'),
(202,'C'),
(202,'C'),
(202,'B');
SELECT Track, Books
FROM @table t
GROUP BY Track, Books
HAVING COUNT(*) > 1
ORDER BY Track, Books
;
This produces
TrackBooks
202C
204A
204B
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
April 3, 2014 at 8:19 am
i will make clear,
I am looking for items that have more than one occurrence in track, in the above sample track 204 has 3 distinct Multiples and the track 202 has 2 distinct multiple ,i need a query to get this output
tks for replay in advance..
April 3, 2014 at 8:20 am
rvenkatesan (4/3/2014)
i will make clear,I am looking for items that have more than one occurrence in track, in the above sample track 204 has 3 distinct Multiples and the track 202 has 2 distinct multiple ,i need a query to get this output
tks for replay in advance..
Did you even look at the query I provided? It does exactly what you requested.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
April 3, 2014 at 8:22 am
Guess I am not the only one not reading today. 🙂
April 3, 2014 at 8:26 am
djj (4/3/2014)
Guess I am not the only one not reading today. 🙂
:hehe::-D
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
April 3, 2014 at 8:29 am
tks for the query its working but it missed the
TrackBooks
202B
204C
pls help us?
April 3, 2014 at 8:30 am
djj (4/3/2014)
Yep I see that now. Again sorry for the misstep.
Nah... not a misstep. I was just making sure. It's good that other people post and you were trying. Thanks for that.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply