![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I'm having some trouble coming up with the correct select statement. Lets say I have the following two tables: ---------------- ---------------- Orders OrderItem ---------------- ---------------- PK OrderID (int) PK OrderItemID (int) OrderDate (datetime) FK OrderID (int) CustomerName (nvarchar) Priority (int) My search would basically return a list of all the orders placed within the last week. The select statement would include the entire contents of the Orders table. The tricky part is that I also need to return the number of items within each order that have a priority of 1 (so they can be displayed more prominently). The first part of the select statement is easy enough, but I'm stuck on the count part. I'd appreciate any help. Here's what I have so far: SELECT Orders.OrderID, Orders.OrderDate, Orders.CustomerName FROM Orders WHERE DATEDIFF(dd, Orders.OrderDate, GetDate()) < 7 -- Jason |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
Maybe I am missing something. I didn't understand how priority of 1 mattered. The field wasn't in your database structure. |
|
SELECT Orders.OrderID, Orders.OrderDate, Orders.CustomerName, (select count(orderid) from orderitem where orders.orderid=orderitem.orderid) as Count FROM Orders WHERE DATEDIFF(dd, Orders.OrderDate, GetDate()) < 7 |
![]() |
| Thread Tools | |
| Display Modes | |
| |