473,406 Members | 2,369 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,406 software developers and data experts.

HELP: Building SQL Query

My situation is as follows:
I have two tables: InvoiceHeader and InvoiceItemList

InvoiceHeader provides me with InvoiceNumber
InvoiceItemList provides me with ProductID, Description and Qty

I am trying to run two different queries that will answer following questions:

1. How many products were sold were ProductID=X and ProductID=Y on a given invoice for a given date range? In other words, for the date range provided, how many ProductID=X were sold were ProductID=Y also existed?

2. How many products were sold were ProductID=X existed on an invoice but ProductID=Y did not exist.

Ultimately, I need to be able to ascertain how many times ProductID=X was sold when ProductID=Y was present and how many times it was sold without ProductID=Y.

I can do a simple query that shows me how many times X or Y were sold, but not how many times X was sold when Y was present vs when Y is not present.

Hope this makes sense. Thank you for your assistance.
Sep 28 '07 #1
2 1033
example of what I have and desired results:

[InvoiceHeader]
InvoiceID,InvoiceNo, ClientID
1, 1004, 20
2, 1007, 33
3, 1009, 55
4, 1011, 22
5, 1012, 23
6, 1014, 20
7, 1015, 33
8, 1016, 33

[InvoiceItemList]
InvoiceID, ProductID, Description, Qty
1, X, Widget, 1
1, Y, Widget, 1
1, Z, Widget, 1
2, X, Widget, 1
3, Y, Widget, 1
4, X, Widget, 1
4, Z, Widget, 1
5, X, Widget, 1
6, X, Widget, 1
6, Z, Widget, 1
7, Y, Widget, 1
8, X, Widget, 1
8, Y, Widget, 1

ideally, the results should be:

X sold with Y = 2
InvoiceID, ClientID, ItemID, Description, Qty
1004, 20, X, Widget, 1
1016, 33, X, Widget, 1

X sold without Y = 4
InvoiceID, ClientID, ItemID, Description, Qty
1007, 33, X, Widget, 1
1011, 22, X, Widget, 1
1012, 23, X, Widget, 1
1014, 20, X, Widget, 1
Sep 28 '07 #2
Shashi Sadasivan
1,435 Expert 1GB
example of what I have and desired results:

[InvoiceHeader]
InvoiceID,InvoiceNo, ClientID
1, 1004, 20
2, 1007, 33
3, 1009, 55
4, 1011, 22
5, 1012, 23
6, 1014, 20
7, 1015, 33
8, 1016, 33

[InvoiceItemList]
InvoiceID, ProductID, Description, Qty
1, X, Widget, 1
1, Y, Widget, 1
1, Z, Widget, 1
2, X, Widget, 1
3, Y, Widget, 1
4, X, Widget, 1
4, Z, Widget, 1
5, X, Widget, 1
6, X, Widget, 1
6, Z, Widget, 1
7, Y, Widget, 1
8, X, Widget, 1
8, Y, Widget, 1

ideally, the results should be:

X sold with Y = 2
InvoiceID, ClientID, ItemID, Description, Qty
1004, 20, X, Widget, 1
1016, 33, X, Widget, 1

X sold without Y = 4
InvoiceID, ClientID, ItemID, Description, Qty
1007, 33, X, Widget, 1
1011, 22, X, Widget, 1
1012, 23, X, Widget, 1
1014, 20, X, Widget, 1
For the first query
Expand|Select|Wrap|Line Numbers
  1. select count(*) from InvoiveItemlist IL , InvoiceHeader IH where IL.invoiceID = IH.InvoiceID and IL.productID = 'X' and IL.product ID in (Select IL1.ProductID from InvoiceItemList IL1 where IL1.ProductID = IL.ProductID and IL1.ProductID = 'Y')
Does that help?
if yes build upon this for the next one. Let us know if you get through.

cheers
Sep 28 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Si | last post by:
Hi there. I have a page on a website I am building where is want to look for all records added in the last 60 days. The date is added in short UK format, that is 25/12/2003 in an access...
2
by: mark | last post by:
I've been working on an Access 2000 database for a couple of weeks now. I took a course in access about a year ago, a crash course, and I learned a ton, but I didn't touch Access for the year since...
5
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my...
0
by: MNFV | last post by:
Hi, I've wrote the code GRAPHIC_ENGINE.ASPX.VB but it doesn't work properly, because the chart appears only with one dataline (the last line resulting from the query). Can anyone tell me what...
3
by: Kenjamin.Lafayette | last post by:
Here is the scenario. I have a list of people, what building they work in, and on what days. Is there a way in access to pull up this database and have it make a list based only on the days,...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
17
by: Liam.M | last post by:
Hey guys, Forgive me if my question my be alittle silly, but I would very much appreciate and assistance that could be given! My situation is as follows: I have created a Button, and set...
9
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
6
by: ewpatton | last post by:
Good day, I've been trying to work with SQL and an Access database in order to handle custom user profiles. I haven't had any trouble reading from my database, but inserting new entries into...
2
by: jfrancis | last post by:
Hello, Let me simply explain what I'm trying to do and maybe it will make sense. I think it's a fairly easy thing, but I've been looking at the DB too long. Any suggestions are welcome. ...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.