| 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 dataProperty ID NAME TYPE550 ONE CORRECT550 TWO CORRECT551 TWO CORRECT552 ONE WRONGI 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
|
| Hiyou can pass the WHERE condition both the field...Values-------------------------R.. |
 |
|
|
Velnias
Yak Posting Veteran
58 Posts |
Posted - 2009-07-28 : 07:26:22
|
quote: Originally posted by rajdaksha Hiyou can pass the WHERE condition both the field...Values-------------------------R..
Not sure I canI couldSelect 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 |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-28 : 07:29:24
|
| HiSelect propertId from tableName where type = 'Correct' and Name = 'Two'-------------------------R.. |
 |
|
|
Velnias
Yak Posting Veteran
58 Posts |
Posted - 2009-07-28 : 07:31:31
|
quote: Originally posted by rajdaksha HiSelect propertId from tableName where type = 'Correct' and Name = 'Two'-------------------------R..
Hi,Nah sorry you don't understand what I meanThe 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 |
 |
|
|
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' |
 |
|
|
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 workI triedSELECT * FROM (SELECT * FROM tblCharges WHERE (propertyType = 'Sales')) AS TWHERE (description = 'home charge')And then I got for example a bunch of Ids which was fineSELECT *FROM tblChargesWHERE (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. |
 |
|
|
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 fWHERE 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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
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 workI triedSELECT * FROM (SELECT * FROM tblCharges WHERE (propertyType = 'Sales')) AS TWHERE (description = 'home charge')And then I got for example a bunch of Ids which was fineSELECT *FROM tblChargesWHERE (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 @tblselect 550, 'ONE', 'CORRECT' union allselect 550, 'TWO', 'CORRECT' union allselect 551, 'TWO', 'CORRECT' union allselect 552, 'ONE', 'WRONG'select * from @tblSelect * from(select * from @tbl where type='CORRECT')T where ID='TWO' It works for me. |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-07-28 : 08:16:24
|
ayamas,You get id's 550551 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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-28 : 08:32:48
|
| ORSELECT [propertyID] FROM @fooGROUP BY [propertyID] HAVING MAX(CASE WHEN [Type]='CORRECT' AND [Name]='TWO' THEN 0 ELSE 1 END)=0MadhivananFailing to plan is Planning to fail |
 |
|
|
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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
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. |
 |
|
|
Velnias
Yak Posting Veteran
58 Posts |
Posted - 2009-07-28 : 09:03:51
|
quote: Originally posted by Transact Charlie ayamas,You get id's 550551 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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Thats correct dude |
 |
|
|
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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
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 1736The 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 ? |
 |
|
|
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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Yes thats right.NOT EXISTS performance will be more than 50% better than the aggregate function. |
 |
|
|
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 1736The 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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
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 fWHERE 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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
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 Type550 MyHome 25/02/2009 Correct550 MyApartment 27/02/2009 CorrectIm 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. |
 |
|
|
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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
Next Page
|