473,396 Members | 1,872 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,396 software developers and data experts.

Help joining tables properly.

I have about 7 tables I need to join, but am having a lot of
difficulty with the joins, that I need some help on.

I'll provide the details of four tables to illustrate the scenario.

I have one table called Product, which contains a complete list of
products.
I have another table called Date, which contains a complete list of
dates.
I have a table called sales, which contains Product, date and Qty.
I have a table called Purchases which contains product, date and qty.

I want to link my Purchases and sales tables to my Product and date
tables.

On each row I want product from the Product table, Date from the date
table, Sales Qty from the sales table and Purchases Qty from the
Purchases table.

I've been messing around with full outer, inner joins etc, but can't
get it right.

All help greatfull.

Regards,
Ciarán
Jul 20 '05 #1
5 1617
To answer your question, we need to know your table schema, especially the
primary and foreign keys. From your narrative, it seems an INNER JOIN
(matching rows only) will provide the desired results.

Please post your DDL (CREATE TABLE statements)

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Ciar?n" <ch********@hotmail.com> wrote in message
news:7f**************************@posting.google.c om...
I have about 7 tables I need to join, but am having a lot of
difficulty with the joins, that I need some help on.

I'll provide the details of four tables to illustrate the scenario.

I have one table called Product, which contains a complete list of
products.
I have another table called Date, which contains a complete list of
dates.
I have a table called sales, which contains Product, date and Qty.
I have a table called Purchases which contains product, date and qty.

I want to link my Purchases and sales tables to my Product and date
tables.

On each row I want product from the Product table, Date from the date
table, Sales Qty from the sales table and Purchases Qty from the
Purchases table.

I've been messing around with full outer, inner joins etc, but can't
get it right.

All help greatfull.

Regards,
Ciarán

Jul 20 '05 #2
Dan,

I don't have any create table statements...
Basically what has happened is that I received a number of text file
exports from another system.

All the files have a date and product field, so using union statements
I created master lists of all products and all dates and am now hoping
to link all the files based on these.

I have tried using inner joins, but they fail as I could have sales of
a product on one day, but no purchases and as a result inner joins
fail to capture the sales. Hope that makes sense. I need a statement
that will capture all sales and purchases data.

All help greatly appreciated.

Ciarán
"Dan Guzman" <gu******@nospam-online.sbcglobal.net> wrote in message news:<jo******************@newssvr12.news.prodigy. com>...
To answer your question, we need to know your table schema, especially the
primary and foreign keys. From your narrative, it seems an INNER JOIN
(matching rows only) will provide the desired results.

Please post your DDL (CREATE TABLE statements)

--
Hope this helps.

Dan Guzman
SQL Server MVP

Jul 20 '05 #3
> I don't have any create table statements...

One method to generate the DDL is using Query Analyzer. Right-click on the
object in the object browser and select 'script object...as CREATE'.
I have tried using inner joins, but they fail as I could have sales of
a product on one day, but no purchases and as a result inner joins
fail to capture the sales. Hope that makes sense. I need a statement
that will capture all sales and purchases data.
You can use a LEFT join to select all products regardless of whether or not
you have purchases or sales. A CROSS JOIN will return all possible
combinations of Product and Dates. Example below.
CREATE TABLE Product
(
ProductCode int NOT NULL
CONSTRAINT PK_Product PRIMARY KEY,
ProductName varchar(30) NOT NULL
)

CREATE TABLE Calendar
(
CalendarDate datetime NOT NULL
CONSTRAINT PK_Dates PRIMARY KEY
)

CREATE TABLE Sales
(
ProductCode int NOT NULL,
SaleDate datetime NOT NULL,
Qty int NOT NULL,
CONSTRAINT PK_Sales PRIMARY KEY
(ProductCode, SaleDate),
CONSTRAINT FK_Sales_Producct FOREIGN KEY
(ProductCode) REFERENCES Product(ProductCode)
)

CREATE TABLE Purchases
(
ProductCode int NOT NULL,
PurchaseDate datetime NOT NULL,
Qty int NOT NULL,
CONSTRAINT PK_Purchases PRIMARY KEY
(ProductCode, PurchaseDate),
CONSTRAINT FK_Purchases_Producct FOREIGN KEY
(ProductCode) REFERENCES Product(ProductCode)
)

SET NOCOUNT ON

INSERT INTO Product VALUES(1,'Product1')
INSERT INTO Product VALUES(2,'Product2')
INSERT INTO Product VALUES(3,'Product3')
INSERT INTO Product VALUES(4,'Product4')
INSERT INTO Product VALUES(5,'Product5')

INSERT INTO Calendar VALUES('20041110')
INSERT INTO Calendar VALUES('20041111')
INSERT INTO Calendar VALUES('20041112')
INSERT INTO Calendar VALUES('20041113')
INSERT INTO Calendar VALUES('20041114')

INSERT INTO Purchases VALUES(1, '20041111', 3)
INSERT INTO Purchases VALUES(2, '20041113', 4)
INSERT INTO Purchases VALUES(3, '20041114', 5)

INSERT INTO Sales VALUES(2, '20041111', 2)
INSERT INTO Sales VALUES(3, '20041113', 1)
INSERT INTO Sales VALUES(4, '20041113', 4)

SELECT
Calendar.CalendarDate,
Product.ProductCode,
Purchases.Qty AS PurchasesQuantity,
Sales.Qty AS SalesQuantity
FROM Product
CROSS JOIN Calendar
LEFT JOIN Purchases ON
Purchases.ProductCode = Product.ProductCode AND
Purchases.PurchaseDate = Calendar.CalendarDate
LEFT JOIN Sales ON
Sales.ProductCode = Product.ProductCode AND
Sales.SaleDate = Calendar.CalendarDate
ORDER BY
Calendar.CalendarDate,
Product.ProductCode

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Ciar?n" <ch********@hotmail.com> wrote in message
news:7f**************************@posting.google.c om... Dan,

I don't have any create table statements...
Basically what has happened is that I received a number of text file
exports from another system.

All the files have a date and product field, so using union statements
I created master lists of all products and all dates and am now hoping
to link all the files based on these.

I have tried using inner joins, but they fail as I could have sales of
a product on one day, but no purchases and as a result inner joins
fail to capture the sales. Hope that makes sense. I need a statement
that will capture all sales and purchases data.

All help greatly appreciated.

Ciarán
"Dan Guzman" <gu******@nospam-online.sbcglobal.net> wrote in message
news:<jo******************@newssvr12.news.prodigy. com>...
To answer your question, we need to know your table schema, especially
the
primary and foreign keys. From your narrative, it seems an INNER JOIN
(matching rows only) will provide the desired results.

Please post your DDL (CREATE TABLE statements)

--
Hope this helps.

Dan Guzman
SQL Server MVP

Jul 20 '05 #4
Ciar?n (ch********@hotmail.com) writes:
I don't have any create table statements...
If you don't have any CREATE TABLE statements, you don't have any tables...
Basically what has happened is that I received a number of text file
exports from another system.

All the files have a date and product field, so using union statements
I created master lists of all products and all dates and am now hoping
to link all the files based on these.

I have tried using inner joins, but they fail as I could have sales of
a product on one day, but no purchases and as a result inner joins
fail to capture the sales. Hope that makes sense. I need a statement
that will capture all sales and purchases data.


....and if you don't have any tables, you don't have any need of any
statements. So I suspect that you have tables anyway.

You can script your tables from Query Analyzer, by using the Object
Browser (press F8 to make it appear, if you don't see it), finding the
table, and then right-click to pick the script alternative for CREATE
TABLE.

Be sure that your constraints are included.

And we don't ask this only to be pedantic. We ask this, because we don't
like to guess, but want to give you an accurate answer.

It also often helps to include sample data in form of INSERT statements,
and the desired output given the sample.
--
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 20 '05 #5
> If you don't have any CREATE TABLE statements, you don't have any tables...

Sure you can. And you can generate new create statements from them,
but once the tables have been created, you don't really need the old
commands anymore.
It also often helps to include sample data in form of INSERT statements,
and the desired output given the sample.


The desired results is really the thing. What the pk/fk relationships
might have nothing to do with what they guy is trying to find out.
Jul 20 '05 #6

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

Similar topics

4
by: sdowney717 | last post by:
Select LOCGeneralHave.LSCH, LOCSubClassHave.LSCH from LOCGeneralHave , LOCSubClassHave Where (LOCGeneralHave.LCNT <> '0' and LOCSubClassHave.LCNT = '0') This query seems to be ignoring the...
2
by: James | last post by:
Can anyone please shed some light on the following... I have a framework that uses dynamically created tables, named using an incremental "attribute set ID", as follows: attrdata_1 attrdata_2...
3
by: james | last post by:
Hi, I would like to get all the records from 9 tables that have the same field value in one field (it is a unique field)that is shared by all the tables. Would this method of joining work: ...
4
by: lorirobn | last post by:
Hello, I'd be curious to hear other thoughts on my database and table design. My database is for 'space use' in a lodging facility - will hold all spaces, like rooms (lodging rooms, dining...
12
by: eyoung | last post by:
Can someone tell me what I've done wrong here...this kinda works...but no cigar! I want to loop threw each "Service Number" and get all entries before going on to the next. What looks something...
3
by: Reader | last post by:
Hello all, I am joining two tables in a query and the output should be all those records where two fields in table1 match two corresponding fields in table2. I joined the tables using both...
3
by: isaac2004 | last post by:
hello i am making a spoof online book store site for a class and I was wondering how i could fix a problem i am having. I have two tables, one the cart and the other a table with book descriptions....
1
by: mkhalid | last post by:
Hi... I am doing an assignment of MS access. I have made 3 tables (One joining table for M:M relationship). When I was trying to make a form (a form with subform) to update the fields, an error...
4
by: Rnt6872 | last post by:
Table A Table B BOL# B_BOL# Chargeback# Hi All, I have been struggling with this for...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.