如何使用sql从四个sql表在rdlc中显示销售摘要
我在生成 rdlc 报告以以客户提供的预定义格式显示报告时遇到了困难。
正如您所注意到的,红色背景是我的问题区域,商品“A”的价格已更改三次。并相应地显示其销售、破损和退款数量。我有 4 个表 Products、SalesLog、Breakages、SalesReturn。产品表包含商品的最新(当前)价格,但销售日志、破损和销售退货表包含销售、破损和销售退货时的价格。我编写了 SalesLog、Breges 和 SalesReturn 查询,它们根据 ProductCode、Price 和 Quantity 显示行。但我不明白如何合并他们的(4 个表输出)输出以显示以下结果。
查询销售日志
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]
查询销售退货
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]
任何人都可以帮助我,或者建议任何完成的技巧这。
提前致谢.....
I am having bad times to generate a rdlc report to show report in predefined format given by my client.
As you can notice RED background is my problem area, Item "A" price has been changed thrice. And accordingly their sales, breakages and refund quantity is shown. I have 4 tables Products, SalesLog, Breakages, SalesReturn. Product table consists latest(current) price of an item but SalesLog, Breakages and SalesReturn tables consists price at the time of Sales, Breakages and Sales Return. I have written queries for SalesLog, Breakges and SalesReturn which shows rows according to ProductCode, Price and Quantity. But i don't understand how I can merge their (4 tables output) output to show below result.
Query for 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]
Query for Breakages
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]
Query for 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]
Product 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]
Can anyone help me, or suggest any trick to accomplish this.
thanks in advance.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过查看您的数据,这只是我的想法。我想我只需拿出“代码”、“商品”、“价格”的唯一列表,然后使用这个唯一列表对您的每个查询(销售、破损、退货)进行 LEFT JOIN。如果它们为空,您可以使用合并将值默认为 0,并且可以使用最终的 where 子句删除所有三个结果都为零的任何记录(除非您想显示在给定的时间内没有执行任何操作)价格)。
This is just off the top of my head from looking at your data. I think I'd just pull out a unique list of "Code", "Item", "Price" and then use this unique list to LEFT JOIN against each one of your queries (Sales, Breakage, Return). You can use a coalesce to default the values to 0 if they are null, and you can use a final where clause to drop-out any records that have zero for all three results (unless you want to show there was nothing done at a given price).
以下内容有什么用吗?
(给出或去掉一两个方括号,因为我不是 SQL Server 方面的高手)。
Would something along the following lines be of any use?
(Give or take a square bracket or two as I'm no great whizz on SQL Server).
最后,我在 ServerFault.co,m 上提出的一个问题被他们的管理员移至 StackOverFlow.com,得到了一个我认为很容易得到的答案(至少对我来说)。请不要介意那些帮助过我的人。这个查询正在做我想做的事情。
感谢大家的支持(尤其是 STACKOVERFLOW.COM)
单击此处查看答案
Finally a question which I asked on ServerFault.co,m which moved to StackOverFlow.com by their Admin has got an answer which I think is easy to get (for me at least). Please don't mind those guys who helped me. this query is doing what i wanted.
Thanks to all for their support (especially STACKOVERFLOW.COM)
Click here to view answer