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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 comparing select statements

Author  Topic 

cjhardie
Yak Posting Veteran

58 Posts

Posted - 2007-03-02 : 15:32:19
I have 2 select statements and I want to make the results be where select statement 1 != select statement 2.

So say select statement 1 returns values 1,2,3,5,6
and select statement 2 returns values 1,2,3,4,5,6
I want the result set to be 4.

select distinct postalcode from business b
inner join franchiseregionvenue frv on b.businessid = frv.businessid and frv.deletedate is null
inner join regionpostalcoderef rp on frv.regionid = rp.regionid
where b.businessid = 19844

select a.postalcode from business bb
inner join siteregionvenue srv on bb.businessid = srv.businessid and srv.deletedate is null
left join address a on a.addressid = (select top 1 a1.addressid from Address a1 where a1.businessid = bb.businessid and a1.deletedate is null)
inner join businesspermission bp on bb.businessid = bp.businessid and bp.deletedate is null
where bp.permittedbusinessid = 19844


any help?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-03-02 : 15:42:15
This might work:


select postalcode = case when p1 is not null then p1 else p2 end
from
(
select distinct a.postalcode p1, t.postcalcode p2
from business b
inner join franchiseregionvenue frv
on b.businessid = frv.businessid and frv.deletedate is null
inner join regionpostalcoderef rp
on frv.regionid = rp.regionid
full outer join
(
select a.postalcode
from business bb
inner join siteregionvenue srv
on bb.businessid = srv.businessid and srv.deletedate is null
left join address a
on a.addressid = (select top 1 a1.addressid from Address a1 where a1.businessid = bb.businessid and a1.deletedate is null)
inner join businesspermission bp
on bb.businessid = bp.businessid and bp.deletedate is null
where bp.permittedbusinessid = 19844
) t
on b.postcalcode = t.postalcode
where b.businessid = 19844 AND (a.postalcode is null or t.postalcode is null)
)


Tara Kizer
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-03-02 : 15:44:16
Or maybe this:


select postalcode
from
(
select distinct postalcode
from business b
inner join franchiseregionvenue frv
on b.businessid = frv.businessid and frv.deletedate is null
inner join regionpostalcoderef rp
on frv.regionid = rp.regionid
where b.businessid = 19844
union all
select a.postalcode
from business bb
inner join siteregionvenue srv
on bb.businessid = srv.businessid and srv.deletedate is null
left join address a
on a.addressid = (select top 1 a1.addressid from Address a1 where a1.businessid = bb.businessid and a1.deletedate is null)
inner join businesspermission bp
on bb.businessid = bp.businessid and bp.deletedate is null
where bp.permittedbusinessid = 19844
) t
group by postalcode
having count(*) = 1


Tara Kizer
Go to Top of Page

cjhardie
Yak Posting Veteran

58 Posts

Posted - 2007-03-02 : 15:47:44
Sorry but that didn't quite work out the way I wanted any other suggestions?
Go to Top of Page

cjhardie
Yak Posting Veteran

58 Posts

Posted - 2007-03-02 : 15:50:48
It gave me and error message "Incorrect syntax near the keyword 'group'."
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-02 : 15:52:59
Is "1,2,3,5,6" one column and one record?
Or is one column and five records?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

cjhardie
Yak Posting Veteran

58 Posts

Posted - 2007-03-02 : 15:54:40
One column 5 rows
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-03-02 : 15:55:21
quote:
Originally posted by cjhardie

Sorry but that didn't quite work out the way I wanted any other suggestions?



Please provide more details on how it didn't work. We can't read your mind.

Tara Kizer
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-03-02 : 15:55:50
quote:
Originally posted by cjhardie

It gave me and error message "Incorrect syntax near the keyword 'group'."



I fixed the second query, so it shouldn't error now. I was missing the derived table alias.

Tara Kizer
Go to Top of Page

cjhardie
Yak Posting Veteran

58 Posts

Posted - 2007-03-02 : 16:02:04
OK what should the second query be not that it is fixed?
and the first query you gave me said "error incorrect syntax by )" On line 24 which was the last one.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-02 : 16:03:42
Try this
select		postalcode
from (
select distinct postalcode
from business as b
inner join franchiseregionvenue as frv on frv.businessid = b.businessid
inner join regionpostalcoderef as rp on rp.regionid = frv.regionid
where b.businessid = 19844
and frv.deletedate is null

union all

select distinct a.postalcode
from business as bb
inner join siteregionvenue as srv on srv.businessid = bb.businessid
inner join businesspermission as bp on bp.businessid = bb.businessid
inner join (
select businessid,
max(addressid) as addressid
from address
where deletedate is null
group by businessid
) as a1 on a1.businessid = bb.businessid
inner join address as a on a.addressid = a1.addressid
where bp.permittedbusinessid = 19844
and srv.deletedate is null
and bp.deletedate is null
) as x
group by postalcode
having count(*) = 1

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-03-02 : 16:04:58
quote:
Originally posted by cjhardie

OK what should the second query be not that it is fixed?
and the first query you gave me said "error incorrect syntax by )" On line 24 which was the last one.



I edited the second query in my post, so try it out. I'm not goign to fix the first post as the second one is so much better.

Tara Kizer
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-02 : 16:07:41



Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -