"Simon Hayes" <sq*@hayes.ch > wrote in message news:<41******* ***@news.bluewi n.ch>...
"Terry Bell" <dr**********@h otmail.com> wrote in message
news:92******** *************** **@posting.goog le.com... I'm upsizing an Access database. Got the data converted, working on
the front end, converting queries to views, but having trouble
converting queries that use logical expressions like the following:
SELECT OrderId,
Sum((BackOrderQ tyAvailable>0)*-1) AS ReadyBackOrderI tems
FROM OrderDetails
INNER JOIN Items
ON (OrderDetails.C lientId = Items.ClientId)
AND (OrderDetails.I temId = Items.ItemId)
WHERE (NOT (SitesCustomerT ypeId = 2
AND ExpressBackorde r =True
AND OrderUrgency = 1 ))
GROUP BY OrderId;
Can someone suggest a strategy to achieve the same result, ie
OrderId,ReadBac kOrderItems that I can use in further joins?
Thanks in anticipation
Terry Bell
Are you asking how to rewrite the SUM expression? I don't know exactly what
the syntax above means, so this is a guess:
sum(case when BackOrderQtyAva ilable > 0 then BackOrderQtyAva ilable else 0
end * -1)
If this is wrong, then I suggest you post CREATE TABLE and INSERT statements
to create your tables and populate some sample data, along with the result
you expect to see from your query.
Simon
Thanks very much Simon you have given me the direction I needed.
For the record, here's my full converted code - with some side errors
fixed
SELECT Q845Undelivered OrderDetails.Or derId, SUM(CASE WHEN
BackOrderQtyAva ilable > 0 THEN 1 ELSE 0 END) AS ReadyBackOrderI tems
FROM Q845Undelivered OrderDetails INNER JOIN
Items ON (Q845Undelivere dOrderDetails.C lientId =
Items.ClientId) AND (Q845Undelivere dOrderDetails.I temId =
Items.ItemId)
WHERE (NOT (SitesCustomerT ypeId = 2 AND ExpressBackorde r = 1 AND
OrderUrgency = 1))
GROUP BY Q845Undelivered OrderDetails.Or derId;
So:
Sum((BackOrderQ tyAvailable>0)*-1) AS ReadyBackOrderI tems ... in Access
SQL
becomes
SUM(CASE WHEN BackOrderQtyAva ilable > 0 THEN 1 ELSE 0 END) AS
ReadyBackOrderI tems ... in SQL
I also note that in Access you can say something like
WHERE IsBackOrder
and it evaluates IsBackOrder as a logical expression
whereas in sql server we need to say
WHERE IsBackorder = 1
Is that right?
Then I guess I need to think about NULL too ...
Also I notice in the query analyser it comes up with a message saying
it can't understand the CASE statement, but I can ignore that, can I,
as it seems to go ahead and execute the query anyway?
Once again thanks a million this has saved me lots of time
Terry Bell