dbTalk Databases Forums  

SQL query for this purpose

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL query for this purpose in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
MadhavC
 
Posts: n/a

Default SQL query for this purpose - 02-04-2008 , 01:05 AM







We have two tables and data similar as below
Table: MASTER
--------------------
Master_id - Dsite
--------------------
1 - NewYork
2 - Tokiyo
--------------------


Table: DETAILS
---------------------------------
Master_id - item_id - owner
---------------------------------
1 - id1 - James
1 - id1 - Eva
1 - id2 - Dave
1 - id3 - John
2 - id1 - Suzy
2 - id1 - Smith
2 - id4 - Ravi
---------------------------------

We want to list the result set as all those item_ids and owners for
which the item_id is associated with more than one masterids.

So the result for the above example would be as below -
Item_id - Owner
Id1 - James
Id1 - Eva
Id1 - suzy
Id1 - smith

How should one write SQL query for such scenario?

Thanks in advance for your help.

Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: SQL query for this purpose - 02-04-2008 , 08:30 AM






A few different methods here:

-- SQL Server 2005 only
SELECT item_id, owner
FROM (SELECT master_id,
item_id,
owner,
COUNT(*) OVER(
PARTITION BY item_id) AS cnt
FROM DETAILS) AS D
WHERE cnt > 1

-- SQL Server 2000
SELECT master_id,
item_id,
owner
FROM DETAILS
WHERE item_id IN (
SELECT item_id
FROM DETAILS
GROUP BY item_id
HAVING MAX(master_id) <> MIN(master_id))

-- SQL Server 2000
SELECT D.master_id,
D.item_id,
D.owner
FROM DETAILS AS D
JOIN (SELECT item_id
FROM DETAILS
GROUP BY item_id
HAVING COUNT(master_id) > 1) AS C
ON D.item_id = C.item_id

-- SQL Server 2000
SELECT master_id,
item_id,
owner
FROM DETAILS
WHERE item_id IN (
SELECT item_id
FROM DETAILS
GROUP BY item_id
HAVING COUNT(master_id) > 1)

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #3  
Old   
Daniel Eyer
 
Posts: n/a

Default Re: SQL query for this purpose - 02-04-2008 , 08:31 AM



Try something like this

Select item_id, owner
From Details
Where (Select Count(*)
From Details as DetailCount
Where DetailCount.item_Id = Detail.Item_Id) > 1


Daniel

"MadhavC" <choudharymv (AT) gmail (DOT) com> a écrit dans le message de news:
13f29221-f55b-492f-9f91-a9e46547ce8b...oglegroups.com...
Quote:
We have two tables and data similar as below
Table: MASTER
--------------------
Master_id - Dsite
--------------------
1 - NewYork
2 - Tokiyo
--------------------


Table: DETAILS
---------------------------------
Master_id - item_id - owner
---------------------------------
1 - id1 - James
1 - id1 - Eva
1 - id2 - Dave
1 - id3 - John
2 - id1 - Suzy
2 - id1 - Smith
2 - id4 - Ravi
---------------------------------

We want to list the result set as all those item_ids and owners for
which the item_id is associated with more than one masterids.

So the result for the above example would be as below -
Item_id - Owner
Id1 - James
Id1 - Eva
Id1 - suzy
Id1 - smith

How should one write SQL query for such scenario?

Thanks in advance for your help.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.