Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

670 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