Solved

Use of HAVING and WHERE conditon of something with count > 1

Posted on 2011-02-21
4
296 Views
Last Modified: 2012-05-11
Hi,
I have a tbl named Order_Discount that stores all order line items that get a discount per order.
I need to write a SQL statement that lists the OrderIDs of all orders that have one or more line items discounted.

I've started the SQL but I don't think it's correct:

SELECT OrderID, OrderLineItemID, DiscountYN
FROM Order_Discount
WHERE DiscountYN = 'Y'
HAVING COUNT(DiscountYN ) > 1

I need to see all OrderID's along with their OrderLineItemID's where one or more OrderLineItemID has a 'Y' for DiscountYN. I don't want to include only those orders that have ALL discounted order line items, having line item id(s) with no discount is ok too (DiscountYN = 'N') as long as one or more order line items per order have a discount. Can I do something like this: HAVING COUNT(DiscountYN ) > 1 WHERE DiscountYN = 'Y'. I know this is bad syntax - what would be the correct syntax to use in my case?

Best regards,
WDelaney
 
0
Comment
Question by:wdelaney05
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
Alex Matzinger earned 500 total points
ID: 34945189
Try this:
SELECT OrderID, OrderLineItemID, DiscountYN
FROM Order_Discount
WHERE OrderID IN ( SELECT Distinct OrderID
                                       FROM Order_Discount
                                       WHERE DiscountYN = 'Y')

If an OrderID has any discounted item in it, this will return the entire order id with all orderlineitmeids as well

0
 
LVL 8

Expert Comment

by:wolfgang_93
ID: 34945227
Try something like this (I did not test this so may need tweaking):

SELECT OrderID, OrderLineItemID, DiscountYN
FROM Order_Discount
WHERE OrderID in (
  SELECT OrderID FROM Order_Discount
  WHERE DiscountYN='Y'
  GROUP by DiscountYN
  HAVING COUNT(DiscountYN)>0
  )


0
 
LVL 8

Expert Comment

by:wolfgang_93
ID: 34945241
Oops. I see amatzinger got their response in before mine and it looks like it will
do the job better as it does not involve GROUP BY and HAVING COUNT
as mine does.
0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 34945243
also, inorder to use the having count() clause, you would need a group by clause.

But i don't believe you can do this with just one query, i believe that the nested query is the way to go on this one.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now