Easy SQL Question

Status
Not open for further replies.

Sneedes

Smack-Fu Master, in training
95
Simple query for someone...

I have an 'standard' order_detail table that contains the order number, order_line, item_id, and item_type fields.

I want to be able to view a count of orders and order lines that contain at least 1 order line with item_type=x AND 1 order line with item_type=y.

I would also like to be able to sort the report on the count of order lines.

code:<HR>
Code:
i.e. item_id   type    orders      order_lines
     --------  ------  ---------   ----------
     itm_32A   Z2      3,233       3,452
     itm_39C   X1        598         598
 
     itm_32A   Z2      2,900       2,942
     itm_888   X4      2,832       2,901

Forgive my ramblings and hopefully it makes sends for what I'm asking for. Any help in general direction would also be appreciated.

btw, database=sybase
Thanks!
Sneedes
 

kleel

Ars Scholae Palatinae
1,022
hmmm so your table structure is order number, order_line, item_id, item_type

and you want to create 2 cols that are a sum of orders and a sum of order_lines for a partuclar item_id and type?

try this:

code:<HR>
Code:
select item_id,item_type,sum(orders) as Sum_Orders,sum(orderlines) as Sum_OrderLines from order_detail
where (item_type like "X%" ) or (item_type="whatever")
group by item_id,item_type

that will work in MSSQL server .. not 100% sure about sybase ;)

HTH

Kleel
 

Sneedes

Smack-Fu Master, in training
95
This wasn't exactly what I was looking for. Hopefully, the statement below can clear things up.

I want to be able to view the orders that have multiple order lines. For the order number to be displayed in the results, it must have at least each the following: 1 line for item type=A; 1 line for item type=B.

The result should be a listing of all orders that contain both of these identified order lines. I am really looking for item combinations (seeing what items were ordered together on a single order)

MSSQL Server is very similar to Sybase. I mentioned Sybase so that any replies wouldn't solve my problem with some kind of Oracle function.

Thanks!

Sneedes
 

kleel

Ars Scholae Palatinae
1,022
Sorry man ... just got my wires crossed .. OK so ...
Assuming the order_detail table orders which has the following fields

OrderID = Unique ID for each row (database primary key)
OrderNumber= the order reference number, all items on this order will have this OrderNumber
item_id = Unique Id of Item (forgin key to items table?)
item_type = item type
orders = data field
order_line = data field

the way I would then proceed is thus:

create a view called "v_OrderCount" which implements the following SQL
code:<HR>
Code:
SELECT ordernumber, COUNT(ordernumber) AS NumberOfOrders
FROM order_detail
GROUP BY ordernumber

then our query would be easy to implement
code:<HR>
Code:
SELECT order_detail.*
FROM order_detail INNER JOIN
    v_OrderCount ON 
    order_detail.OrderNumber = v_OrderCount.ordernumber
WHERE View1.NumberOfOrders > 1

you then get all the orders in order_detail that have more than one row which you can then limit even further by adding "and item_type in ('foo','bar')" to your where clause.

Hope that finally helps :)
 
Status
Not open for further replies.