使用左连接获取所有日期

时间:2016-11-30 10:40:37

标签: sql sql-server-2008

我正在努力开发库存管理系统,以便我们在发布日期跟踪产品。所以我的条件是 - 如果商店ID是1且年份是2014年,那么它应该按照特定年份的发布日期返回所有产品,如下所示:

Product  - Date - TotalIssued
Keyboard - 2014-10-10 - 1 -- This product has been issued or given 1 time on that date of the year 2014
Mouse    - 2014-10-11 - 1
Keyboard - 2014-10-12 - 0
Mouse    - 2014-10-12 - 0 
-------------------------
-------------------------
Keyboard - 2014-12-31 - 0
Mouse    - 2014-12-31 - 0 -- This will continue till the end of the year

我尝试使用以下链接使用日历表并创建了一个:

Calendar Table

我使用以下查询来使用日历表获取所需的输出,但它仅返回已发布的产品,这些产品仅表示存储在 ReturnToStore 表中的日期:

SELECT l.ItemName AS Product, m.PKDate AS Date, COUNT(m.PKDate) AS TotalIssued
FROM [days] m
LEFT JOIN ReturnToStore k ON CONVERT(DATE, k.IssuedDate) = m.PKDate
LEFT JOIN Item l ON l.ItemID = k.ItemID
WHERE m.calendar_year = 2014 AND k.StoreID = 1
GROUP BY k.StoreID, l.ItemName, m.PKDate

这是我现在得到的输出:

 Product - Date - TotalIssued
Keyboard - 2014-10-10 - 1 
Mouse    - 2014-10-11 - 1

以下是表结构:

CREATE TABLE [dbo].[Item](
        [ItemID] [int] IDENTITY(1,1) NOT NULL,
        [ItemName] [varchar](50) NULL,
        [Description] [varchar](150) NULL,
        [Tag] [varchar](50) NULL,
     CONSTRAINT [PK_Item] PRIMARY KEY CLUSTERED 
    (
        [ItemID] ASC
    )
    ) ON [PRIMARY]

    INSERT INTO [dbo].[Item]
               ([ItemName]
               ,[Description]
               ,[Tag])
         VALUES
               ('Keyboard', 'IT Equipment', ''),
               ('Mouse', 'IT Equipment', '')

    CREATE TABLE [dbo].[ReturnToStore](
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [StoreID] [int] NULL,
        [CategoryID] [int] NULL,
        [ItemID] [int] NULL,
        [Quantity] [float] NULL,
        [IssuedDate] [datetime] NULL,
        [Description] [varchar](150) NULL,
        [OrderID] [varchar](50) NULL,
     CONSTRAINT [PK_ReturnToStore] PRIMARY KEY CLUSTERED 
    (
        [ID] ASC
    )
    ) ON [PRIMARY]

    INSERT INTO [dbo].[ReturnToStore]
               ([StoreID]
               ,[CategoryID]
               ,[ItemID]
               ,[Quantity]
               ,[IssuedDate]
               ,[Description]
               ,[OrderID])
         VALUES
               (1, 1, 1, 10, '2014-10-10 00:00:00.000', '', 'PO-02'),
               (1, 1, 2, 20, '2014-10-11 00:00:00.000', '', 'PO-03')

**Note:** I guess, this could be done easily with LEFT JOIN using calendar table. But got stuck here.

已编辑:我已更新帖子,因为我已将查询转换为数据透视以下内容:

CREATE PROCEDURE [dbo].[MonthlyConsumption]
@StoreID int,
@Year int

AS

SELECT Distinct ItemName,
    [1] AS M1,
    [2] AS M2,
    [3] AS M3,
    [4] AS M4,
    [5] AS M5,
    [6] AS M6,
    [7] AS M7,
    [8] AS M8,
    [9] AS M9,
   [10] AS M10,
   [11] AS M11,
   [12] AS M12
FROM
(SELECT MONTH(m.PKDate) as YEAR, 
p.ItemName, k.IssuedDate, k.ItemID
FROM [days] m
CROSS JOIN (SELECT DISTINCT itemName,itemID FROM Item) p
LEFT JOIN ReturnToStore k
 ON CONVERT(DATE, k.IssuedDate) = m.PKDate AND
    p.itemid = k.itemid AND 
    k.StoreID = @StoreID
LEFT JOIN Item l 
 ON l.ItemID = k.ItemID
WHERE m.calendar_year = @Year
GROUP BY p.ItemName, m.PKDate, k.ItemID, k.IssuedDate)

source
PIVOT
(
    COUNT(ItemID)
    FOR YEAR
    IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
) AS PivotDay

返回以下输出:

SP_Pivot

我不确定为什么会重复,如果我在特定日期多次添加相同的产品,那么枢轴中没有显示任何变化,它与给定的输出保持一致。

2 个答案:

答案 0 :(得分:1)

我不确定这是否是解决方案,但至少它是一种改进......

LEFT JOIN时,将右侧表格条件放在ON子句中以获得真正的LEFT JOIN行为。 (在WHERE时,您将获得定期INNER JOIN结果。)即。对move k.StoreID = 1条款ON

SELECT l.ItemName AS Product, m.PKDate AS Date, COUNT(m.PKDate) AS TotalIssued
FROM [days] m
LEFT JOIN ReturnToStore k ON CONVERT(DATE, k.IssuedDate) = m.PKDate AND k.StoreID = 1
LEFT JOIN Item l ON l.ItemID = k.ItemID
WHERE m.calendar_year = 2014
GROUP BY l.ItemName, m.PKDate

答案 1 :(得分:1)

您的第一个错误是您在LEFT JOIN子句中的WHERE的右表上放置了一个条件,而不是ON子句。

第二件事,您还应该使用派生表作为项目名称,就像日期:

SELECT p.ItemName AS Product, m.PKDate AS Date, COUNT(l.ItemID) AS TotalIssued
FROM [days] m
CROSS JOIN (SELECT DISTINCT itemName,itemID FROM Item) p
LEFT JOIN ReturnToStore k
 ON CONVERT(DATE, k.IssuedDate) = m.PKDate AND
    p.itemid = k.itemid AND
    k.StoreID = 1
LEFT JOIN Item l 
 ON l.ItemID = k.ItemID
WHERE m.calendar_year = 2014 
GROUP BY p.ItemName, m.PKDate