Solved

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

Posted on 2011-02-21
4
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

630 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