SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL server 2008 query help


SQL server 2008 query help

Author
Message
mamzy.rahman
mamzy.rahman
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 38
Hi guys im trying to show all the duplicate data from a table how would one do this. this is what i have tried:

SELECT L01, COUNT (DISTINCT L03)
FROM (
SELECT A.L01, A.L03, A.L39, B.A04, B.A10, B.A31
FROM [LEARNER_SN05]AS A
INNER JOIN (SELECT L01, L03, A04, A10, A31
FROM [AIMS_SN05]
WHERE A04 <> 35
AND A10 IN (10, 22, 81)
AND A31 IS NOT NULL
GROUP BY L01, L03, A04, A10, A31) AS B ON B.L01 = A.L01 AND B.L03 = A.L03
WHERE A.L39 = 95
GROUP BY A.L01, A.L03, A.L39, B.A04, B.A10, B.A31 )
GROUP BY L01, L03
HAVING (COUNT (DISTINCT L03) > 1)

basically i first need to list all the l01 and l03
where a04 <>35
and a10 = 10,22,81
and a31 is not null
and where l39 = 95

please notice there is 2 tables in the query the aims table and the learner table.
BriPan
BriPan
SSC Veteran
SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)

Group: General Forum Members
Points: 241 Visits: 296
I am not getting you.. will you provide some schema of table and sample data and what you
want (result data).
which help us for proper answer
mamzy.rahman
mamzy.rahman
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 38
hi mate

basically this part of the query
SELECT A.L01, A.L03, A.L39, B.A04, B.A10, B.A31
FROM [LEARNER_SN05]AS A
INNER JOIN (SELECT L01, L03, A04, A10, A31
FROM [AIMS_SN05]
WHERE A04 <> 35
AND A10 IN (10, 22, 81)
AND A31 IS NOT NULL
GROUP BY L01, L03, A04, A10, A31) AS B ON B.L01 = A.L01 AND B.L03 = A.L03
WHERE A.L39 = 95
GROUP BY A.L01, A.L03, A.L39, B.A04, B.A10, B.A31

gives me this

L01 L03 L39 A04 A10 A31
1 123 95 10 22 01/09/1990
1 234 95 22 22 02/09/1990
2 345 95 32 10 03/09/1990
3 456 95 45 81 19/09/2012
4 567 95 10 22 10/10/2012
4 567 95 10 22 15/10/2012
5 678 95 11 81 01/09/1990

and i want to show the number of l03 that are duplicates so in this case 567 record shows up twice
Kingston Dhasian
Kingston Dhasian
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4747 Visits: 5261
Not sure, but I feel this is what you are after

SELECT   L01, LO3, COUNT (*) Counts
FROM (
SELECT A.L01, A.L03, A.L39, B.A04, B.A10, B.A31
FROM [LEARNER_SN05]AS A
INNER JOIN (
SELECT L01, L03, A04, A10, A31
FROM [AIMS_SN05]
WHERE A04 <> 35
AND A10 IN (10, 22, 81)
AND A31 IS NOT NULL
GROUP BY L01, L03, A04, A10, A31
) AS B ON B.L01 = A.L01 AND B.L03 = A.L03
WHERE A.L39 = 95
GROUP BY A.L01, A.L03, A.L39, B.A04, B.A10, B.A31
)
GROUP BY L01, L03
HAVING COUNT( * ) > 1




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
mamzy.rahman
mamzy.rahman
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 38
HI thanks for your help but it came up with an error

incorrect syntax near the keyword 'group'
highlights groub by l01, l03
ChrisM@Work
ChrisM@Work
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28390 Visits: 19842
mamzy.rahman (2/26/2013)
HI thanks for your help but it came up with an error

incorrect syntax near the keyword 'group'
highlights groub by l01, l03


The derived table - subselect - requires an alias.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
BriPan
BriPan
SSC Veteran
SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)

Group: General Forum Members
Points: 241 Visits: 296
Add alias name to query like C(or any else ) then try

SELECT   L01, LO3, COUNT (*) Counts
FROM (
SELECT A.L01, A.L03, A.L39, B.A04, B.A10, B.A31
FROM [LEARNER_SN05]AS A
INNER JOIN (
SELECT L01, L03, A04, A10, A31
FROM [AIMS_SN05]
WHERE A04 <> 35
AND A10 IN (10, 22, 81)
AND A31 IS NOT NULL
GROUP BY L01, L03, A04, A10, A31
) AS B ON B.L01 = A.L01 AND B.L03 = A.L03
WHERE A.L39 = 95
GROUP BY A.L01, A.L03, A.L39, B.A04, B.A10, B.A31
)C --Here
GROUP BY L01, L03
HAVING COUNT( * ) > 1




--------------------------------------------------------------------------------
ChrisM@Work
ChrisM@Work
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28390 Visits: 19842
BriPan (2/26/2013)
Add alias name to query like C(or any else ) then try ...



That's what I...never mind.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
mamzy.rahman
mamzy.rahman
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 38
Tis done boys thanks for all your help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search