473,378 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Complicated SELECT Problem

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

Jul 23 '05 #1
3 2041
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
Jason wrote:
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


Jul 23 '05 #2
Thanks. That should work just fine. I was unaware that you could
include a select statement inside another select like that. You learn
something new every day.

--
Jason

Jul 23 '05 #3
AS400 Guru (ha***@candid.com) writes:
Maybe I am missing something. I didn't understand how priority of 1
mattered. The field wasn't in your database structure.
It is in OrderItem.
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


This is likely to perform better:

SELECT o.OrderID, o.OrderDate, o.CustomerName, coalesce(oi.cnt, 0)
FROM Orders o
LEFT JOIN (SELECT OrderId, cnt = COUNT(*)
FROM OrderItem
WHERE Priority = 1
GROUP BY OrderId) oi ON o.OrderID = oi.OrderID
WHERE DATEDIFF(dd, Orders.OrderDate, GetDate()) < 7

It's always a good idea to benchmark different solutions. But my experience
is that a derived table gives better performance that sub-selects in the
SELECT list. The latter are usally computed for each row, whereas the
derived table permits the optimizer to do all at once.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Agoston Bejo | last post by:
Hi, I'm having trouble with implementing some constraints on the database level. An example: --Table A(AID, BID ref. B.BID, ATXT) --Table B(BID, CID ref. C.CID) --Table C(CID) upon insertion...
3
by: Jason | last post by:
The best way to explain this is by example. I have a source table with many columns. Source SYMBOL EXCHANGE_NAME CUSIP TYPE ISSUE_NAME
3
by: Narine | last post by:
Hi All, I need to write one complicated update statement and I'm looking at maybe finding a simpler way to do it. I have 2 tables: 1.Photo Table PhotoID FileName 1 111.jpg
10
by: jqq | last post by:
SQL2K on W2Kserver I need some help revamping a rather complicated query. I've given the table and existing query information below. (FYI, changing the database structure is right out.) The...
2
by: gimme_this_gimme_that | last post by:
Can this statement be translated into DB2 ? CREATE VIEW RELEASE_REPORTS AS (SELECT RI.GROUP_ID AS GROUP_ID, RI.RELEASE_ID AS RELEASE_ID, RI.TARGET_REL_ID, RI.REL_DROP, B.DROPCOUNT ,...
1
by: Stewart Allen | last post by:
I have a form with 2 subforms; 1 displaying the modules a trainee is currently doing and the second subform displaying the units that are associated with the selected module while the main form is...
26
by: Jeff | last post by:
Ok gang. Here is something complicated, well, at least to me anyway. Using Access DB I have a table in my DB called members. In that table, I have 2 tables I will be using "username" and...
4
by: Matthew Crouch | last post by:
i suck so much that i don't even know if this is a JOIN or a subquery or who-knows what. Here's the idea: I want to select two things at the same time (form one table) average for columnX and...
4
by: robtyketto | last post by:
I have generated combo boxes based on contents of other combos and am newbie who slowly learning vba code. Heres an example of vba code use to update one combo box from another ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.