[quoted text, click to view] > 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'.
[quoted text, click to view] > 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
[quoted text, click to view] "Ciar?n" <chudson007@hotmail.com> wrote in message
news:7f9b6870.0411150111.285e2935@posting.google.com...
> 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" <guzmanda@nospam-online.sbcglobal.net> wrote in message
> news:<jo3ld.21230$bP2.8036@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