Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Help, Compare Records for missing type

Author  Topic 

Velnias
Yak Posting Veteran

58 Posts

Posted - 2009-07-28 : 07:19:01
Hi, what I am trying to do is as follows.

Check a table as follows...

Lets say I have the following data

Property ID NAME TYPE
550 ONE CORRECT
550 TWO CORRECT
551 TWO CORRECT
552 ONE WRONG

I want to check the table for all type which equal 'CORRECT' and in this subset give me back all property ID which have a record for 'TWO' but not for 'ONE' because I would like them to have both.

Thanks

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-07-28 : 07:21:34
Hi

you can pass the WHERE condition both the field...Values

-------------------------
R..
Go to Top of Page

Velnias
Yak Posting Veteran

58 Posts

Posted - 2009-07-28 : 07:26:22
quote:
Originally posted by rajdaksha

Hi

you can pass the WHERE condition both the field...Values

-------------------------
R..



Not sure I can

I could

Select propertId from tableName where type = 'Correct' Yes but then how do I do the second part where I check for all instances where the propertId has a record for Name 'TWO' but doesnt have an instance of NAME 'ONE' just using where.

Thanks
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-07-28 : 07:29:24
Hi


Select propertId from tableName where type = 'Correct' and Name = 'Two'

-------------------------
R..
Go to Top of Page

Velnias
Yak Posting Veteran

58 Posts

Posted - 2009-07-28 : 07:31:31
quote:
Originally posted by rajdaksha

Hi


Select propertId from tableName where type = 'Correct' and Name = 'Two'

-------------------------
R..



Hi,

Nah sorry you don't understand what I mean

The above would return all the propertIds which have NAME = 'Two'

I want all of the above but only if they dont another record where NAME = 'One'

If there is a propertyID with 2 records 1 with NAME 'ONE' and the Other with NAME = 'TWO' i dont want it
Go to Top of Page

ayamas
Aged Yak Warrior

552 Posts

Posted - 2009-07-28 : 07:46:08
Select * from

(
select * from yourtable where type='CORRECT'

)T where ID='TWO'
Go to Top of Page

Velnias
Yak Posting Veteran

58 Posts

Posted - 2009-07-28 : 08:03:23
quote:
Originally posted by ayamas

Select * from

(
select * from yourtable where type='CORRECT'

)T where ID='TWO'



This did not work

I tried


SELECT * FROM (SELECT *
FROM tblCharges
WHERE (propertyType = 'Sales')) AS T
WHERE (description = 'home charge')

And then I got for example a bunch of Ids which was fine

SELECT *
FROM tblCharges
WHERE (propertyId = 382)

I got a lot of instances for this propertyId.

I only want the Ids where whatever I type in decription, that the only instances returned are ids which have this description and not any other descriptions.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-07-28 : 08:13:02
Maybe this?

DECLARE @searchString NVARCHAR(255)
SET @searchString = 'TWO'

DECLARE @foo TABLE (
[propertyID] INT
, [Name] NVARCHAR(255)
, [Type] NVARCHAR(255)
)

INSERT @foo
SELECT 550, 'ONE', 'CORRECT'
UNION SELECT 550, 'TWO', 'CORRECT'
UNION SELECT 551, 'TWO', 'CORRECT'
UNION SELECT 552, 'ONE', 'WRONG'

SELECT
*
FROM
@foo f
WHERE
f.[type] = 'CORRECT'
AND f.[name] = @searchString
AND NOT EXISTS (
SELECT 1
FROM @foo f2
WHERE
f2.[type] = 'CORRECT'
AND f2.[name] <> @searchString
AND f2.[propertyId] = f.[propertyId]
)



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

ayamas
Aged Yak Warrior

552 Posts

Posted - 2009-07-28 : 08:13:06
quote:
Originally posted by Velnias

quote:
Originally posted by ayamas

Select * from

(
select * from yourtable where type='CORRECT'

)T where ID='TWO'



This did not work

I tried


SELECT * FROM (SELECT *
FROM tblCharges
WHERE (propertyType = 'Sales')) AS T
WHERE (description = 'home charge')

And then I got for example a bunch of Ids which was fine

SELECT *
FROM tblCharges
WHERE (propertyId = 382)

I got a lot of instances for this propertyId.

I only want the Ids where whatever I type in decription, that the only instances returned are ids which have this description and not any other descriptions.




Why is that?

declare @tbl as table(Property int, ID varchar(5), type varchar(10))
insert into @tbl
select 550, 'ONE', 'CORRECT' union all
select 550, 'TWO', 'CORRECT' union all
select 551, 'TWO', 'CORRECT' union all
select 552, 'ONE', 'WRONG'
select * from @tbl

Select * from

(
select * from @tbl where type='CORRECT'

)T where ID='TWO'


It works for me.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-07-28 : 08:16:24
ayamas,

You get id's

550
551

from your query which is wrong.

OP *only* wants id's where there is NO OTHER description as well as the one he is looking for. I believe the only value he wants is 551 as there is no row with NAME=ONE and TYPE=CORRECT for 551 and there is for 550.

Correct Velnias?


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-07-28 : 08:32:48
OR


SELECT [propertyID] FROM @foo
GROUP BY [propertyID]
HAVING MAX(CASE WHEN [Type]='CORRECT' AND [Name]='TWO' THEN 0 ELSE 1 END)=0

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-07-28 : 08:38:29
madhivanan:

Ah the old HAVING MAX(CASE.....) construct. the sql it hurts!


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

ayamas
Aged Yak Warrior

552 Posts

Posted - 2009-07-28 : 08:57:38
Good old Madhi got it sweet & simple.
Charlie yours also was awesome.
Go to Top of Page

Velnias
Yak Posting Veteran

58 Posts

Posted - 2009-07-28 : 09:03:51
quote:
Originally posted by Transact Charlie

ayamas,

You get id's

550
551

from your query which is wrong.

OP *only* wants id's where there is NO OTHER description as well as the one he is looking for. I believe the only value he wants is 551 as there is no row with NAME=ONE and TYPE=CORRECT for 551 and there is for 550.

Correct Velnias?


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION




Thats correct dude
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-07-28 : 09:10:51
Either Madhi's or my suggestion should work then.

I think the NOT EXISTS method has better performance but either will probably be good enough.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Velnias
Yak Posting Veteran

58 Posts

Posted - 2009-07-28 : 09:18:19
quote:
Originally posted by Transact Charlie

Either Madhi's or my suggestion should work then.

I think the NOT EXISTS method has better performance but either will probably be good enough.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION




Hi Charlie

I can't seem to understand your query in the searchString do I have to search for the description I want to search for or not to ?
Go to Top of Page

ayamas
Aged Yak Warrior

552 Posts

Posted - 2009-07-28 : 09:23:20
quote:
Originally posted by Transact Charlie



I think the NOT EXISTS method has better performance but either will probably be good enough.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION




Yes thats right.NOT EXISTS performance will be more than 50% better than the aggregate function.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-07-28 : 09:27:53
quote:
Originally posted by Velnias

quote:
Originally posted by Transact Charlie

Either Madhi's or my suggestion should work then.

I think the NOT EXISTS method has better performance but either will probably be good enough.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION




Hi Charlie

I can't seem to understand your query in the searchString do I have to search for the description I want to search for or not to ?



I'm not sure I follow you. You said that you wanted to find rows that match a particular string (@searchString) and don't have any other rows with a different string?

If so then just put in the value you are looking for and my query will show you rows that have that value only if that propertyId has no other rows without that value (so it won't return 550) but will return 551.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-07-28 : 09:29:54
if you actually want to exclude only 1 other possible value then just add an @excludeString. Liek so:

DECLARE @searchString NVARCHAR(255) SET @searchString = 'TWO'
DECLARE @excludeString NVARCHAR(255) SET @excludeString = 'ONE'

DECLARE @foo TABLE (
[propertyID] INT
, [Name] NVARCHAR(255)
, [Type] NVARCHAR(255)
)

INSERT @foo
SELECT 550, 'ONE', 'CORRECT'
UNION SELECT 550, 'TWO', 'CORRECT'
UNION SELECT 551, 'TWO', 'CORRECT'
UNION SELECT 552, 'ONE', 'WRONG'

SELECT
*
FROM
@foo f
WHERE
f.[type] = 'CORRECT'
AND f.[name] = @searchString
AND NOT EXISTS (
SELECT 1
FROM @foo f2
WHERE
f2.[type] = 'CORRECT'
AND f2.[name] = @excludeString
AND f2.[propertyId] = f.[propertyId]
)



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Velnias
Yak Posting Veteran

58 Posts

Posted - 2009-07-28 : 10:14:18
Thanks Charlie I got you in the end.

Ok another query then.

Im looking for something similar again.

PropertyId Name Datepublished Type
550 MyHome 25/02/2009 Correct
550 MyApartment 27/02/2009 Correct

Im looking for any propertyId where MyApartment Datepublished is less then MyHome Apartment datepublished where type is Correct for both.

Ideally MyApartment should always have the earliest date but im looking for the propertyIds where this isnt happening.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-07-28 : 11:28:13
does the table have a unique identifier? What is the primary key?


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
    Next Page

- Advertisement -