| Author |
Topic |
|
rdl
Starting Member
25 Posts |
Posted - 2011-05-18 : 09:29:52
|
| Hi All,I have the following table which includes reportvehicleID (autonumber), report reference, vehicle registration number, relation (stolen, recovered) and date of relation.ReportVehicleID ReportRef RegistrationNo Relation RelationDate1 4 AAA342 Stolen 2011-02-022 5 BBS222 Stolen 2011-02-203 4 AAA342 Recovered 2011-05-17Now I want a list of stolen vehicles i.e. if AAA342 has been recovered I don't want it in the report.Thus the report would only show the BBS222.Can someone help me through this pls cause I have been trying a lot.THANKYOU FOR YOUR HELP.Regards,rdl |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-05-18 : 09:36:11
|
[code]select *from table1 t1where t1.Relation = 'Stolen'and not exists (select * from table1 x where x.RegistratioNo = t1.RegistrationNo and x.Relation = 'Recovered')[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
rdl
Starting Member
25 Posts |
Posted - 2011-05-18 : 09:39:33
|
| Dear khtan,can you tell me what x is please cause I didn't understand that part. Please note that my table name is reportvehicle and all above data is stored in it (i.e. in one table). |
 |
|
|
rdl
Starting Member
25 Posts |
Posted - 2011-05-18 : 09:40:15
|
quote: Originally posted by khtan
select *from table1 t1where t1.Relation = 'Stolen'and not exists (select * from table1 x where x.RegistratioNo = t1.RegistrationNo and x.Relation = 'Recovered') KH[spoiler]Time is always against us[/spoiler]
Please note that these are all in one table named 'reportvehicle' |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
rdl
Starting Member
25 Posts |
Posted - 2011-05-18 : 09:46:13
|
| Ok sorry as I just got confused with the x and table 1.I did as follows:SELECT *FROM reportvehicleWHERE reportvehicle.Relation = 'Stolen'AND NOT EXISTS (SELECT * from reportvehicle WHERE reportvehicle.RegistrationNo = reportvehicle.RegistrationNo AND reportvehicle.Relation = 'Recovered')HOWEVER RETURNING 0 RESULTS :( |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2011-05-18 : 09:52:05
|
You need to do some aliasing so it can distinguish between the references to the same table...Also.. I would add the blue part to handle cases where vehicle is stolen more than once. (though probably unlikely)quote: Originally posted by rdl Ok sorry as I just got confused with the x and table 1.I did as follows:SELECT *FROM reportvehicle AWHERE reportvehicleA.Relation = 'Stolen'AND NOT EXISTS (SELECT * from reportvehicle B WHERE reportvehicleB.RegistrationNo = reportvehicleA.RegistrationNo AND reportvehicleB.Relation = 'Recovered' AND B.RelationDate > A.RelationDate)HOWEVER RETURNING 0 RESULTS :(
Corey I Has Returned!! |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
rdl
Starting Member
25 Posts |
Posted - 2011-05-18 : 09:55:34
|
| SELECT *THANKYOU VERY MUCH...IT WORKED :))))FROM reportvehicle AWHERE A.Relation = 'Stolen'AND NOT EXISTS (SELECT * from reportvehicle BWHERE B.RegistrationNo = A.RegistrationNo AND B.Relation = 'Recovered') |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-05-18 : 09:55:44
|
quote: Originally posted by Seventhnight You need to do some aliasing so it can distinguish between the references to the same table...Also.. I would add the blue part to handle cases where vehicle is stolen more than once. (though probably unlikely)quote: Originally posted by rdl Ok sorry as I just got confused with the x and table 1.I did as follows:SELECT *FROM reportvehicle AWHERE reportvehicleA.Relation = 'Stolen'AND NOT EXISTS (SELECT * from reportvehicle B WHERE reportvehicleB.RegistrationNo = reportvehicleA.RegistrationNo AND reportvehicleB.Relation = 'Recovered' AND B.RelationDate > A.RelationDate)HOWEVER RETURNING 0 RESULTS :(
Corey I Has Returned!!
Corey...o and i for outer and innerBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2011-05-18 : 10:00:00
|
quote: Originally posted by X002548
quote: Originally posted by Seventhnight You need to do some aliasing so it can distinguish between the references to the same table...Also.. I would add the blue part to handle cases where vehicle is stolen more than once. (though probably unlikely)quote: Originally posted by rdl Ok sorry as I just got confused with the x and table 1.I did as follows:SELECT *FROM reportvehicle AWHERE reportvehicleA.Relation = 'Stolen'AND NOT EXISTS (SELECT * from reportvehicle B WHERE reportvehicleB.RegistrationNo = reportvehicleA.RegistrationNo AND reportvehicleB.Relation = 'Recovered' AND B.RelationDate > A.RelationDate)HOWEVER RETURNING 0 RESULTS :(
Corey I Has Returned!!
Corey...o and i for outer and innerBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/
Yeah yeah...You should see a large query with lots of joins and nesting...From AJoin ( From Z Join ( From A Join B Join C Join D ) Y Join ( From A Join B Join C Join D ) X ) B... Corey I Has Returned!! |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
|
|
rdl
Starting Member
25 Posts |
Posted - 2011-05-18 : 10:12:27
|
| yeh you were all ok in your answers but i am new to sql.....and didn't understand but now ok :))Thanks all for fast responses :) |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2011-05-18 : 13:15:41
|
I finally got to move to newer versions of SQL, so I returned. No offense Brett, but I'm glad I don't work in the same dbs as you...I don't enjoy SCREAMING at my server  SELECT someCol FROM someTable -- DO IT NOW! I SAID NOW!! Corey I Has Returned!! |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
rdl
Starting Member
25 Posts |
Posted - 2011-05-18 : 14:08:34
|
Hi,I am finding it a problem to do joins with the below query.I want to show the details of the owner in the stolen vehicles report.I have the following:Table 1: vehicle has fields RegistrationNo, ID (identity card of owner which is foreign key)Table 2: person has fields ID, Surname, Name.Can someone help me to include also the ID, Surname, Name of the owner please.Thankyou.rdlquote: Originally posted by Seventhnight You need to do some aliasing so it can distinguish between the references to the same table...Also.. I would add the blue part to handle cases where vehicle is stolen more than once. (though probably unlikely)quote: Originally posted by rdl Ok sorry as I just got confused with the x and table 1.I did as follows:SELECT *FROM reportvehicle AWHERE reportvehicleA.Relation = 'Stolen'AND NOT EXISTS (SELECT * from reportvehicle B WHERE reportvehicleB.RegistrationNo = reportvehicleA.RegistrationNo AND reportvehicleB.Relation = 'Recovered' AND B.RelationDate > A.RelationDate)HOWEVER RETURNING 0 RESULTS :(
Corey I Has Returned!!
|
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
|
|
Next Page
|