2010-07-19 65 views
3

我正在用糟糕的時間生成一個rdlc報表,以按客戶端給出的預定義格式顯示報表。如何使用sql在四個sql表中顯示rdlc中的銷售彙總

客戶給定的格式 Output format http://i29.tinypic.com/4t9g7b.jpg

正如你可以看到紅色的背景是我的問題區域,項目「A」的價格已經三次改變。並據此顯示其銷售額,損壞和退款數量。我有4個表產品,銷售日誌,破損,銷售退貨。產品表包含項目的最新(當前)價格,但SalesLog,Breakages和SalesReturn表格包含銷售,破損和銷售退貨時的價格。我已經爲SalesLog,Breakges和SalesReturn編寫了查詢,它根據ProductCode,Price和Quantity顯示行。但我不明白如何合併他們的(4個表格輸出)輸出以顯示下面的結果。

查詢爲SalesLog

SELECT [Products].pCode AS Code, 
    [Products].pName AS Item, 
    SalesLog.[Price] AS Price , 
    COUNT(*)AS Quantity , 
    SUM(SalesLog.[Price]) AS Total 
FROM SalesLog 
    INNER JOIN [Products] ON [Products].pCode = SalesLog.ProductCode 
WHERE BillDate = '07/01/2010' 
GROUP BY [Products].pCode,[Products].pName , 
    SalesLog.[Price] 

查詢有關損毀

SELECT [Products].pCode AS Code, 
    [Products].pName AS Item, 
    Breakages.[Price] AS Price , 
    COUNT(*)AS Quantity , 
    SUM(Breakages.[Price]) AS Total 
FROM Breakages 
    INNER JOIN [Products] ON [Products].pCode = Breakages.ProductCode 
WHERE BillDate = '07/01/2010' 
GROUP BY [Products].pCode,[Products].pName , 
    Breakages.[Price] 

查詢爲SalesReturn

SELECT [Products].pCode AS Code, 
    [Products].pName AS Item, 
    Breakages.[Price] AS Price , 
    COUNT(*)AS Quantity , 
    SUM(Breakages.[Price]) AS Total 
FROM Breakages 
    INNER JOIN [Products] ON [Products].pCode = Breakages.ProductCode 
WHERE BillDate = '07/01/2010' 
GROUP BY [Products].pCode,[Products].pName , 
    Breakages.[Price] 

產品表

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] 

誰能幫助我,或提出任何把戲做到這一點。

在此先感謝.....

回答

2

這只是從我的頭頂看你的數據。我想我只是提出一個唯一的「代碼」,「項目」,「價格」列表,然後將這個唯一列表用於左對齊每個查詢(銷售,破損,退貨)。你可以使用coalesce將這些值默認爲0(如果它們爲null),並且你可以使用最終的where子句來刪除所有三個結果都爲零的記錄(除非你想顯示給定的內容沒有做任何事情價錢)。

+0

感謝您幫助 – 2010-07-20 01:47:22

2

沿着下面幾行會有什麼用處? (給予或採取一個方括號或兩個,因爲我不是SQL Server上的偉大的專家)。

SELECT [Products].pCode AS Code, 
     [Products].pName AS Item, 
     SLO.[Price]  AS Price, 
     (SELECT COUNT(*) 
      FROM SalesLog SL 
      WHERE SL.ProductCode = SLO.ProductCode AND 
       SL.Billdate = SLO.BillDate) AS SalesQty, 
     (SELECT SUM(SL.Price) 
      FROM SalesLog SL 
      WHERE SL.ProductCode = SLO.ProductCode AND 
       SL.Billdate = SLO.BillDate) AS SalesValue, 
     (SELECT COUNT(*) 
      FROM Breakages 
      WHERE Breakages.ProductCode = SLO.ProductCode AND 
        Breakages.BillDate = SLO.BillDate) AS BreakageQty, 
     ...etc... 

FROM SalesLog SLO INNER JOIN [Products] ON [Products].pCode = SLO.ProductCode 
    WHERE BillDate = '07/01/2010' 
    GROUP BY [Products].pCode, 
      [Products].pName , 
      SLO.[Price] 
1

最後它通過他們的聯繫搬到StackOverFlow.com一個問題,我問的ServerFault.co,男已經有了答案,我認爲是很容易得到(至少對我來說)。請不要介意那些幫助我的人。這個查詢正在做我想做的事情。

感謝所有的支持(尤其是STACKOVERFLOW.COM

Click here to view answer