Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
deniseaddy
Starting Member
12 Posts |
Posted - 2009-03-13 : 17:28:22
|
| Hi,I'm having trouble using row over partition and distinct to get single Order Records. I keep getting multiple records with the same id and only need one for each order.3 Tables:OrdersOrder_ItemsOrders_Items_OwnerOrders record has various order details such as id, date, value etc.Orders_Items has various product details such as id, price, name etc and references Orders_id and Orders_Items_Owner_idOrders_Items_Owner has various product owner details such as id, name, email etc.What I need to be able to do is retrieve all the details from the main Orders Record by supplying the @Orders_Items_Owner_id to the query.So, I need the Order_date, Order_id, Order_value.. and sum(Order_Items_price), count(Order_Items_id).. and Orders_Items_Owner_name, Orders_Items_Owner_emailBy this I mean, I need not just the order record details but the sum value of all the item prices in that order and the total number of all items in that order according to the owner id of the item that I supply. Problem is I get duplicate order ids and can't seem to get the right sums and counts value.If I supply an Orders_Items_Owner_id of 0 the sum should equal the Orders_value, and the count should be the total number of items in that order. If I supply the Orders_Items_Owner_id I should only get the sum and count values of the items in that order that the item owner is associated with.Hope you can help.Denise |
|
|
deniseaddy
Starting Member
12 Posts |
Posted - 2009-03-13 : 19:58:58
|
Here is a basic version of what I'm trying to achieve but it doesn't work. @owner int = 0,@date_start smalldatetime = '1/1/2008',@date_end smalldatetime = '1/1/2050'SELECT DISTINCT o.id as 'order_id', o.value as 'order_value', o.date as 'order_date', w.id as 'owner_id', w.name as 'owner_name', (SELECT sum(price) FROM items WHERE (@owner = 0 or (@owner = owner_id))) as 'sum_item_price', (SELECT count(id) FROM items WHERE (@owner = 0 or (@owner = owner_id))) as 'count_items' FROM items i inner join orders o on o.id = i.order_id inner join owner w on w.id = i.owner_idWHERE (@owner = 0 or (@owner = i.owner_id))AND (o.date >= convert(smalldatetime, @date_start) and o.date <= dateadd(day, 1, convert(smalldatetime, @date_end)) )ORDER BY o.date DESC It also means repeating the same where conditions in the sum and count sub queries and there must be a better way of achieving that with a single conditional query for the whole statement.Thanks. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-03-14 : 10:02:02
|
quote: Originally posted by deniseaddy Here is a basic version of what I'm trying to achieve but it doesn't work. @owner int = 0,@date_start smalldatetime = '1/1/2008',@date_end smalldatetime = '1/1/2050'SELECT o.id as 'order_id', o.value as 'order_value', o.date as 'order_date', w.id as 'owner_id', w.name as 'owner_name', sum(i.price) as 'sum_item_price', count(i.id) as 'count_items' FROM items i inner join orders o on o.id = i.order_id inner join owner w on w.id = i.owner_idWHERE (@owner = 0 or (@owner = i.owner_id))AND (o.date >= @date_start and o.date <= dateadd(day, 1,@date_end) )Group by o.id,o.value , o.date, w.id, w.nameORDER BY o.date DESC It also means repeating the same where conditions in the sum and count sub queries and there must be a better way of achieving that with a single conditional query for the whole statement.Thanks.
|
 |
|
|
deniseaddy
Starting Member
12 Posts |
Posted - 2009-03-17 : 05:54:18
|
| Thanks. Group By did the trick. |
 |
|
|
|
|
|
|
|