Value Checking / Grouping SQL Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Coolboy55
    New Member
    • Jul 2007
    • 67

    Value Checking / Grouping SQL Query

    I have a query that returns three fields: OrderID, ProductID, and Satisfied. Each Order can have multiple Products associated with it. Satisfied is a yes/no field that tells me if the order quantity for the Product has been shipped. So I might have an order where the order quantity for one product was satisfied but the order quantity for another product in the same order was not satisfied. For example:

    OrderID ProductID Satisfied
    1 3 0
    1 5 1

    Now, what I want to do is create another query with two fields: OrderID and OrderSatisfied. OrderSatisfied would return 1 if each Satisfied is 1 in that Order; else it would return 0. For example, in this case since one Product is not satisfied, the order is not satisfied:

    OrderID OrderSatisfied
    1 0

    Does anyone know how I could accomplish the second query? :( Thanks!
  • yarbrough40
    Contributor
    • Jun 2009
    • 320

    #2
    one solution would be to first build a query returning these 3 columns:
    UniqueOrder#--------CountOfOrder#--------SumOfOrderSatis fied
    then,
    using that as a subquery, return UniqueOrder# as OrderID and then the OrderSatisfied column would be where CountOfOrder# = SumOfOrderSatis fied then "1" else "0"

    the end result will be:
    OrderID --------- OrderSatisfied
    1 --------------------- 1
    2 --------------------- 1
    3 --------------------- 0
    4 --------------------- 0
    5 --------------------- 1

    Rock!

    Comment

    Working...