Re: Simple Query problem

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 02/14/05


Date: Mon, 14 Feb 2005 14:38:16 +0100

On Mon, 14 Feb 2005 03:29:24 -0800, Wes wrote:

>Sample table as follows
>
>Order ID Stock Code Status
>--------- ------- -------
>203 STK1 3
>203 STK2 2
>203 STK4 3
>204 STK1 3
>204 STK5 3
>205 STK2 2
>205 STK1 3
>
>I want a query that will return the order id when ALL
>ORDER LINES are of Status 3.

Hi Wes,

SELECT OrderID
FROM MyTable AS a
WHERE Status = 3
GROUP BY OrderID
HAVING COUNT(*) = (SELECT COUNT(*)
                     FROM MyTable AS b
                     WHERE b.OrderID = a.OrderID)

or

SELECT DISTINCT OrderID
FROM MyTable AS a
WHERE NOT EXISTS (SELECT *
                  FROM MyTable AS b
                  WHERE b.OrderID = a.OrderID
                  AND b.Status <> 3)

or (probably the quickes, though I didn't test it)

SELECT OrderID
FROM MyTable AS a
GROUP BY OrderID
WHERE MIN(Status) = 3
AND MAX(Status) = 3

Best, Hugo

-- 
(Remove _NO_ and _SPAM_ to get my e-mail address)


Relevant Pages

  • Re: Query condition
    ... Grouping on the OrderID would not give you the detail lines. ... To create a query that groups records, depress the Total icon on the toolbar ... >> SELECT OrderDetail.* ...
    (microsoft.public.access.queries)
  • Re: Return ProdID with max ProdPrice for each OrderID
    ... I now get an error in trying to run that query. ... INNER JOIN ( ... OrderID, Maxas TheMax ...
    (microsoft.public.access.queries)
  • Re: Return ProdID with max ProdPrice for each OrderID
    ... "The Microsoft Jet database engine cannot find the input table or query ... 'SELECT OrderID, MaxAS MaxProdPrice FROM qryTest GROUP BY ... FROM qryTest AS A INNER JOIN ...
    (microsoft.public.access.queries)
  • Re: SQL Division
    ... SELECT OrderID, max ... FROM [Order Details] ... Changing views does not change the parentheses to brackets plus period, ... saving the query does. ...
    (microsoft.public.access.queries)
  • Re: Query condition
    ... I've been using the sub query you suggested and it works well. ... grouping on the orderID? ... the Total row under the ProductID.) ... I tried your suggestion and i've got a result but the query is only ...
    (microsoft.public.access.queries)