Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-21
4
Medium Priority
?
308 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 2000 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Suggested Courses

963 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