简体   繁体   English

SQL Select 查询 - 将行旋转为列的问题

[英]SQL Select Query - problem pivoting rows into columns

I have three tables in an SQL 2005 database, that I need to query and display on one row.我在 SQL 2005 数据库中有三个表,我需要在一行上查询和显示。 The tables are:这些表格是:

MasterStock
StockID, Description
1, Plate
2, Bowl

ShopStock
ShopID, StockID, StockLevel
1,1,6
2,1,0
3,1,0
4,1,10

Sales
StockId, ShopId, SoldQuantity, transDate
1, 1, 1, 5/1/2011
1,2,1, 5/1/2011

I need to get them to show one row:我需要让他们显示一行:

StockID, Description, 1 Sales, 1 Stock, 2 Sales, 2 Stock, 3 Sales,… StockID、描述、1 个销售、1 个库存、2 个销售、2 个库存、3 个销售……

I have managed to get what somewhere with the query below:我已经设法通过以下查询在某处得到了什么:

SELECT     MasterStock.StockID, MasterStock.Description, 
SUM(CASE WHEN sales.shopid = 1 THEN sales.Soldquantity ELSE 0 END) AS [1 Sold], 
MAX(CASE WHEN shopstock.shopid = 1 THEN shopstock.stockLevel ELSE 0 END) AS [1 Stock], 
SUM(CASE WHEN sales.shopid = 2 THEN sales.Soldquantity ELSE 0 END) AS [2 Sold], 
MAX(CASE WHEN shopstock.shopid = 2 THEN shopstock.stockLevel ELSE 0 END) AS [2 Stock], 
SUM(CASE WHEN sales.shopid = 3 THEN sales.Soldquantity ELSE 0 END) AS [3 Sold], 
MAX(CASE WHEN shopstock.shopid = 3 THEN shopstock.stockLevel ELSE 0 END) AS [3 Stock], 
SUM(CASE WHEN sales.shopid = 4 THEN sales.Soldquantity ELSE 0 END) AS [4 Sold], 
MAX(CASE WHEN shopstock.shopid = 4 THEN shopstock.stockLevel ELSE 0 END) AS [4 Stock]
FROM         ShopStock INNER JOIN
Sales ON ShopStock.StockID = Sales.StockID AND ShopStock.shopID = Sales.ShopID 
INNER JOIN MasterStock ON ShopStock.StockID = MasterStock.StockID
WHERE (sales.transdate > 1/1/2010)
GROUP BY MasterStock.StockID, MasterStock.Description

However, if there are no sales for the product it doesn't show any stock levels.但是,如果产品没有销售,则不会显示任何库存水平。 If I remove the shopID join on shopstock and sales it shows the stock levels, but reports inaccurate sales - multiplies by four (one for each shopstock record?).如果我在 shopstock 和 sales 上删除 shopID 连接,它会显示库存水平,但会报告不准确的销售额 - 乘以四(每个 shopstock 记录一个?)。

I know I'm missing something here, but I'm not getting anywhere.我知道我在这里遗漏了一些东西,但我没有得到任何地方。 Any help would be greatly received.任何帮助都会受到极大的欢迎。

Two problems:两个问题:

1) You need a LEFT OUTER JOIN between ShopStock and Sales, which will ensure that the query returns records from ShopStock even if there are no related entries in Sales. 1) ShopStock 和 Sales 之间需要 LEFT OUTER JOIN,这将确保即使 Sales 中没有相关条目,查询也会从 ShopStock 返回记录。 By definition, an INNER JOIN will not return records from either side of the join, if one of the sides is missing records.根据定义,如果其中一方缺少记录,则 INNER JOIN 不会从连接的任何一方返回记录。

2) You need to move your sales.transdate > 1/1/2010 condition to the inner join, rather than the WHERE clause. 2)您需要将您的 sales.transdate > 1/1/2010 条件移动到内部连接,而不是 WHERE 子句。 Conditions in the WHERE clause will be logically applied after any logic in the table joins. WHERE 子句中的条件将在表连接中的任何逻辑之后进行逻辑应用。 So even if you get your joins right, the where clause will filter out stock without sales because sales.transdate will appear null.因此,即使您的联接正确,where 子句也会过滤掉没有销售的库存,因为 sales.transdate 将出现 null。

Something like this:像这样的东西:

FROM ShopStock LEFT OUTER JOIN Sales 
    ON ShopStock.StockID = Sales.StockID 
    AND Sales.transdate > 1/1/2010
INNER JOIN // the rest of your joins here

I'm guessing you also want >= on your transdate filter as well, but that's just a hunch.我猜你也希望 >= 在你的 transdate 过滤器上,但这只是一种预感。

Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM