Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

identifying records in the same table Expand / Collapse
Author
Message
Posted Tuesday, December 11, 2012 1:49 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, April 11, 2013 2:33 PM
Points: 125, Visits: 325
Hi everyone
I have a table in SQL server that contains the following : ID, Date, Description, Type, Manager and Site. There can be more than one record per ID. The Type field contains either A, B, C or D and there should always be an A and a B record for each ID (ie ID number 00001 has 2 records in the table one of which is a type A and an another a type B). However I've noticed that in some cases the IDs are missing one of their type A and/or type B records. I would like some code that would identify when an ID has a missing A or B record in the table and tell me which ones they are.
Hope that makes sense.
Thanks in advance.
BO
Post #1394935
Posted Tuesday, December 11, 2012 1:55 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, January 28, 2013 1:45 AM
Points: 386, Visits: 199
select id
from tbl
where type in ('A','B')
group by id
having count(distinct type) <> 2



Cursors never.
DTS - only when needed and never to control.
Post #1394941
Posted Tuesday, December 11, 2012 2:11 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, March 21, 2013 2:14 AM
Points: 77, Visits: 184
ByronOne (12/11/2012)
Hi everyone
I have a table in SQL server that contains the following : ID, Date, Description, Type, Manager and Site. There can be more than one record per ID. The Type field contains either A, B, C or D and there should always be an A and a B record for each ID (ie ID number 00001 has 2 records in the table one of which is a type A and an another a type B). However I've noticed that in some cases the IDs are missing one of their type A and/or type B records. I would like some code that would identify when an ID has a missing A or B record in the table and tell me which ones they are.
Hope that makes sense.
Thanks in advance.
BO


Can you please provide some sample data.
Post #1394948
Posted Tuesday, December 11, 2012 2:57 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, April 11, 2013 2:33 PM
Points: 125, Visits: 325
Thanks Nigel - just what I was after!
BO
Post #1394976
Posted Tuesday, December 11, 2012 3:03 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 10:39 AM
Points: 2,556, Visits: 4,398
Provided sample is very basic, but should give an idea how to do what you want:

declare @sample table (id int, [type] char(1))

insert @sample select 1,'A'
insert @sample select 1,'B'
insert @sample select 2,'A'
insert @sample select 3,'B'
insert @sample select 4,'D'

select s1.id, 'A' as missingType
from @sample s1
left join @sample sA on sA.id = s1.id and sA.[type] = 'A'
where sA.id is null
union
select s1.id, 'B' as missingType
from @sample s1
left join @sample sB on sB.id = s1.id and sB.[type] = 'B'
where sB.id is null



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1394981
Posted Tuesday, December 11, 2012 3:04 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 10:39 AM
Points: 2,556, Visits: 4,398
nigelrivett (12/11/2012)
select id
from tbl
where type in ('A','B')
group by id
having count(distinct type) <> 2


That is good, but only will work if one of the types is missing...



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1394982
Posted Tuesday, December 11, 2012 3:19 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, April 11, 2013 2:33 PM
Points: 125, Visits: 325
Eugene

Thanks for this, really helpful.

BO
Post #1394991
Posted Tuesday, December 11, 2012 3:45 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:42 PM
Points: 21,627, Visits: 27,478
Just some food for thought:


declare @sample table (id int, [type] char(1));

insert @sample select 1,'A' ;
insert @sample select 1,'B';
insert @sample select 2,'A';
insert @sample select 3,'B';
insert @sample select 4,'D';

with ReqIdsTypes as (
select distinct
id,
dt.[type]
from
@sample
cross join (select [type] from (values ('A'),('B'))dt([type]))dt([type]))
select id as MissingId, [type] as MissingType from ReqIdsTypes
except
select id, [type] from @sample;





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1395004
Posted Tuesday, December 11, 2012 6:45 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, April 11, 2013 2:33 PM
Points: 125, Visits: 325
Thank you too Lynn!
Post #1395091
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse