Avatar of troycomp
troycomp

asked on 

Sql query to certain data only (no mix and match)

I have attached my database diagram. Very simple structure. I need to write a query to display only orders who have ordered a certain product only. To be specific, Halloween products are to only show. So if Jane ordered 2 Halloween products, see should show in my query. If Jill ordered 4 Halloween products and 1 Christmas product, she should not show in my query. I Think i'm close, but I need assistance. Heres my query:

select orderid
FROM  orderheader
where isfulfilled = 0 and inprocess=0
and OrderId in (
SELECT oh.orderid FROM
OrderHeader oh
inner join OrderLines ol on oh.OrderId = ol.OrderId
WHERE oh.OrderId in (SELECT OrderId from OrderLines group by OrderId having COUNT(*) > 0)
and ol.ProductId > 225)

In my database, all Halloween products have order ids greater than 225. Thanks
Capture.JPG
Microsoft SQL Server 2008Microsoft SQL Server 2005

Avatar of undefined
Last Comment
_agx_
Avatar of lcohan
lcohan
Flag of Canada image

I think this should be just like below:

select orderid
FROM orderheader oh
      inner join OrderLines ol on oh.OrderId = ol.OrderId and ol.ProductId > 225
Avatar of lcohan
lcohan
Flag of Canada image

Sorry I missed the WHERE clause when I copy/paset:


select orderid
FROM orderheader oh
      inner join OrderLines ol on oh.OrderId = ol.OrderId and ol.ProductId > 225
where oh.isfulfilled = 0 and oh.inprocess=0
Avatar of troycomp
troycomp

ASKER

@lcohan

Thanks for responding, but keep in mind i only want to show orders that have halloween products in them, not just orders with halloween combined with other products. Your query is like mine
Avatar of vincem1099
vincem1099
Flag of United States of America image

Try this
Avatar of vincem1099
vincem1099
Flag of United States of America image

Looks like code did not attach
select orderid 
FROM  orderheader 
where isfulfilled = 0 and inprocess=0
and OrderId in (
SELECT oh.orderid FROM 
OrderHeader oh 
inner join OrderLines ol on oh.OrderId = ol.OrderId
WHERE oh.OrderId in (SELECT OrderId from OrderLines group by OrderId having COUNT(*) > 0)
and ol.ProductId > 225)
and OrderId not in (
SELECT oh.orderid FROM 
OrderHeader oh 
inner join OrderLines ol on oh.OrderId = ol.OrderId
WHERE oh.OrderId in (SELECT OrderId from OrderLines group by OrderId having COUNT(*) > 0)
and ol.ProductId < 226)

Open in new window

Avatar of lcohan
lcohan
Flag of Canada image

<<In my database, all Halloween products have order ids greater than 225. Thanks >>

OrderID's or rather ProductID's > 255??

If ProductId's then I believe the query I sent does just that - it will filter from orderheader all orderid's that are in  OrderLines ol on oh.OrderId = ol.OrderId AND the  ol.ProductId > 225
and of course
where oh.isfulfilled = 0 and oh.inprocess=0
Avatar of HainKurt
HainKurt
Flag of Canada image

try this


select orderid from (
select orderid, sum(case when ProductID>255 then 1 else 0 end) cntGT255, count(1) cnt
  from orderLines o
 group by orderid
) x where cnt=cntGT255

Open in new window

Avatar of HainKurt
HainKurt
Flag of Canada image

above query will give you all orders which contains only Halloween products in it... is this what you want?
Avatar of troycomp
troycomp

ASKER

@vincem1099

That worked. This is the most congested sql query i've ever seen. What magic did you add to my original query to make it work? I can see what you did but i cant explain it.
Avatar of vincem1099
vincem1099
Flag of United States of America image

I used your existing subquery that had all holloween products to show all orders without halloween products and used it to not include orders that matched.  I agree it can probably be optimized better
Avatar of HainKurt
HainKurt
Flag of Canada image

did you try mine @ 36932303?
SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of _agx_
_agx_
Flag of United States of America image

    >> it should do the same thing

Though you may need to add back the the extra filters on isfulfilled  and inprocess

ASKER CERTIFIED SOLUTION
Avatar of vincem1099
vincem1099
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of troycomp
troycomp

ASKER

Sorry for the delay...i went to lunch and then got caught up here at work:

@HainKurt - your query worked fine
@vincem1099 - yours did too, the simplified version is much better
@_aqx- thanks for your input as well

I have implemented vincem1099 query but i will keep HainKurt's as well. Everyone gets points. Thank you so much. I learned something today
Avatar of _agx_
_agx_
Flag of United States of America image

Thanks, but not for me :)  @HainKurt  and @vincem1099 did all the work. I just added a small fyi comment.
Microsoft SQL Server 2005
Microsoft SQL Server 2005

Microsoft SQL Server 2005 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. It includes support for managing XML data and allows a database server to be exposed over web services using Tabular Data Stream (TDS) packets encapsulated within SOAP (protocol) requests.

72K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo