Solved

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

Posted on 2011-02-21
4
300 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
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 Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

813 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

12 Experts available now in Live!

Get 1:1 Help Now