I have 4 tables namely Products, SalesLog, Breakages, SalesReturn.
Products Table
CREATE TABLE [dbo].[Products](
[ProductId] [int] IDENTITY(1,1) NOT NULL,
[pName] [nvarchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[pSize] [int] NULL,
[pPrice] [decimal](10, 2) NULL,
[pPackQty] [int] NULL,
[pGroup] [int] NULL,
[pCode] [int] NULL,
[OpenStock] [int] NULL,
[CloseStock] [int] NULL,
[YrlyOpenStock] [int] NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[ProductId] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
SalesLog Table
CREATE TABLE [dbo].[SalesLog](
[SalesID] [int] IDENTITY(1,1) NOT NULL,
[MemoNo] [int] NULL,
[ProductCode] [int] NULL,
[Quantity] [int] NULL,
[Price] [decimal](10, 2) NULL,
[pGroup] [int] NULL,
[pName] [nvarchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[pSize] [int] NULL,
[BillDate] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_SalesLog] PRIMARY KEY CLUSTERED
(
[SalesID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
SalesReturn Table
CREATE TABLE [dbo].[SalesReturn](
[srID] [int] IDENTITY(1,1) NOT NULL,
[ProductCode] [int] NULL,
[Quantity] [int] NULL,
[pGroup] [int] NULL,
[MemoNo] [int] NULL,
[SalesmanID] [int] NULL,
[Price] [decimal](10, 2) NULL,
[JobShift] [int] NULL,
[BillDate] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[AddedOn] [datetime] NULL,
CONSTRAINT [PK_SalesReturn] PRIMARY KEY CLUSTERED
(
[srID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Breakages Table
CREATE TABLE [dbo].[Breakages](
[breakId] [int] IDENTITY(1,1) NOT NULL,
[MemoNo] [int] NULL,
[SalesmanID] [int] NULL,
[ProductCode] [int] NULL,
[pName] [nvarchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Quantity] [int] NULL,
[Price] [decimal](10, 2) NULL,
[pGroup] [int] NULL,
[JobShift] [int] NULL,
[BillDate] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[AddedOn] [datetime] NULL,
CONSTRAINT [PK_Breakages_1] PRIMARY KEY CLUSTERED
(
[breakId] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
I have to generate a single report to show SalesLOg, SalesReturn and Breakages of particular date (according to BillDate column of SalesLog, Breakages and SalesReturn) in following pattern.
Wanted Output
Code ItemName Price SalesQty BreakagesQty SalesReturnQty
1 A $10 50 2 2
1 A $12 150 1 10
15 X $5 56 0 2
20 Z $8 121 0 0
Reduced output column for the sake of reading
as you can see that Product "A" has been listed twice because of price change of that product. to achieve this i have written a query:
SELECT SalesLog.pName, SalesLog.ProductCode, MIN(ItemGroup.gName) AS GroupName,
SalesLog.Price, SUM (SalesLog.Quantity) AS SalesQty,
SUM(SalesLog.Quantity * SalesLog.Price) AS SalesValue,
ISNULL ((SELECT SUM(Quantity) AS Expr1 FROM Breakages
WHERE (ProductCode = SalesLog.ProductCode) AND (Price = SalesLog.Price) AND
(BillDate = '07/01/2010') AND (pGroup <> 15) AND (pGroup <> 16)), 0) AS BreakQty,
ISNULL ((SELECT SUM(Quantity * Price) AS Expr1
FROM Breakages
WHERE (ProductCode = SalesLog.ProductCode) AND (Price = SalesLog.Price) AND
(BillDate = '07/01/2010') AND (pGroup <> 15) AND (pGroup <> 16)), 0) AS BreakValue,
ISNULL ((SELECT SUM(Quantity) AS Expr1
FROM SalesReturn
WHERE (ProductCode = SalesLog.ProductCode) AND (Price = SalesLog.Price) AND
(BillDate = '07/01/2010') AND (pGroup <> 15) AND (pGroup <> 16)), 0) AS ReturnQty,
ISNULL ((SELECT SUM(Quantity * Price) AS Expr1
FROM SalesReturn
WHERE (ProductCode = SalesLog.ProductCode) AND (Price = SalesLog.Price) AND
(BillDate = '07/01/2010') AND (pGroup <> 15) AND (pGroup <> 16)), 0) AS ReturnValue,
ISNULL ((SELECT SUM(Quantity) AS Expr1
FROM SalesLog
WHERE (ProductCode = Products.pCode)), 0) AS CummSales
FROM SalesLog
INNER JOIN
ItemGroup ON ItemGroup.gCode = SalesLog.pGroup AND SalesLog.pGroup = ItemGroup.gCode
INNER JOIN
Products ON Products.pCode = SalesLog.ProductCode
WHERE (SalesLog.BillDate = '07/01/2010') AND (SalesLog.pGroup <> 15) AND
(SalesLog.pGroup <> 16)
GROUP BY SalesLog.pName, SalesLog.ProductCode, SalesLog.Price, Products.pCode, Products.pPrice
ORDER BY SalesLog.ProductCode, SalesLog.pName
My problem with this query is, It is unable to show entries of items where there is no sales but there is sales return. this condition is give by my client. Can you help me on this. any t-sql query or logic to implement this condition will help...
thank you for bearing me.........