renkai — 2012-09-12T16:54:41-04:00 — #1
I have a table called Orders and another table called OrderDetails.
My Order table contains information about the order (who placed it, OrderDate, their BilltoID, etc)
The OrderDetails table contains line by line information about what products were part of the order. Example, productID, ProductName, productType, etc.
I have to begin with a query giving a list of the orders based on their BilltoID. That query is:
CASE ost.Name WHEN 'Taken' THEN 'In Progress'
ELSE ost.Name END AS OrderStatus
FROM vwOrders o INNER JOIN
vwOrderStatusTypes ost ON o.OrderStatusID=ost.ID
WHERE BillToID= 123456
How can I include in my query something that lets me know if there was a product of a certain productType in the order. I don't have to know the productID or any information about it, other than if the order contained a certain productType within the order. Something like a Boolean value called 'HasChart' which is set to 1 or 0. Any suggestions?
r937 — 2012-09-13T00:04:47-04:00 — #2
and yet the query you posted references "vwOrders" which smells a lot like a view, and "vwOrderStatusTypes" which looks nothing like an OrderDetails table
where's this productType column? which table?
renkai — 2012-09-13T06:45:22-04:00 — #3
Ah! Yes indeed these are views, not tables. The productType column is on the vwOrderDetails view.
r937 — 2012-09-13T06:57:25-04:00 — #4
what about ost.Name? do you still need that? are there any column from vwOrderStatusTypes that you need besides productType?
renkai — 2012-09-13T08:06:11-04:00 — #5
Thanks, I do still need to hang on to ost.Name. No other columns on vwOrderStatusTypes that I care about besides productType. I just need to know if the order contains a certain type of product, yes or no.
r937 — 2012-09-13T16:32:39-04:00 — #6
well, doing anything like this (if the order contains a certain type of product) can only be done at the order ("one") level, not at the product ("many") level, but you do want to return product data...
... so to get this done in the query will take what to my mind is unnecessary extra processing (a correlated subquery, which is quite inefficient)
can't you do this in your application language (php or whatever) while you're looping over the products for an order?
oddz — 2012-09-14T00:42:17-04:00 — #7
Withut seeing the table schemas something like this would be my guess:
,CASE ost.Name WHEN 'Taken' THEN 'In Progress' ELSE ost.Name END AS OrderStatus
,MAX(CASE WHEN od.productType IN ('typename') THEN 1 ELSE 0 END) hasProductType
o.ID = od.orderID #change this name of foreign key column#
Also in terms of readability you should consider qualifying each column with the table alias.
renkai — 2012-09-14T16:18:59-04:00 — #8
this works! I was thinking a case statement but had such trouble coming up with something that worked.
The only issue I came across with this was the Group By, instead of
GROUP BY o.ID
With just o.ID, I get an error about each item "not contained in either an aggregate function or the GROUP BY clause". So I had to add all the selected values:
GROUP BY o.ID, o.OrderDate, o.ShipDate,o.CALC_GrandTotal,o.ShipTrackingNum,o.ShipType,ost.Name
Does this make sense? Or would there be a better way to handle it?
renkai — 2012-09-14T18:24:37-04:00 — #9
If it was php I could. But I'm working with an asp.net datagrid and I don't know how to loop through and stick in the values into the same datagrid. Actually I'm not sure that's possible at all in .net.
oddz — 2012-09-15T16:49:02-04:00 — #10
That would be correct, thought you were using MySQL.
mittineague — 2014-09-18T22:47:33-04:00 — #11
This topic is now archived. It is frozen and cannot be changed in any way.