我正在构建一个非常简单的库存项目数据库,这将允许我在日期之间签出项目。我需要返回一行,告诉我该项目的可用,保留和总库存。我似乎无法理解这一点。
BEGIN TRAN
DECLARE @startDate AS DATE
DECLARE @endDate AS DATE
DECLARE @partID AS INT
SET @startDate = '4/15/2015'
SET @endDate = '4/25/2015'
SET @partID = 248
SELECT COUNT(ii.[PartId] WHERE ii.[PartStatus] = 1 ) AS [Available],
COUNT(ii.[PartId] WHERE ii.[PartStatus] = 2 ) AS [Reserverd],
COUNT(ii.[PartId] WHERE ii.[PartStatus] <> 4 ) AS [TotalInventory],
FROM ShipListInventory.dbo.InventoryItems AS ii
LEFT JOIN ShipListInventory.dbo.InventoryItemCalendars AS iic
ON iic.[ItemId] = ii.[Id]
WHERE iic.[StartDate] NOT BETWEEN @startDate AND @endDate
AND iic.[InboundDate] NOT BETWEEN @startDate AND @endDate
AND ii.[PartId] = 248
COMMIT TRAN
编辑:我只返回InventoryItemCalendars
的行,我想从InventoryItems
CREATE TABLE [dbo].[InventoryItems](
[Id] [INT] IDENTITY(1,1) NOT NULL,
[PartSatuts] [INT] NOT NULL,
[PartId] [INT] NOT NULL,
[Barcode] [NVARCHAR](MAX) NULL,
[PicturePath] [NVARCHAR](255) NULL,
[Notes] [NVARCHAR](1000) NULL,
CONSTRAINT [PK_dbo.InventoryItems] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[InventoryItems] WITH CHECK ADD CONSTRAINT [FK_dbo.InventoryItems_dbo.Parts_PartId] FOREIGN KEY([PartId])
REFERENCES [dbo].[Parts] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[InventoryItems] CHECK CONSTRAINT [FK_dbo.InventoryItems_dbo.Parts_PartId]
GO
CREATE TABLE [dbo].[InventoryItemCalendars](
[Id] [INT] IDENTITY(1,1) NOT NULL,
[StartDate] [DATETIME] NOT NULL,
[InboundDate] [DATETIME] NOT NULL,
[ProjectNumber] [INT] NOT NULL,
[ItemId] [INT] NOT NULL,
[Project_Id] [INT] NULL,
CONSTRAINT [PK_dbo.InventoryItemCalendars] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[InventoryItemCalendars] WITH CHECK ADD CONSTRAINT [FK_dbo.InventoryItemCalendars_dbo.InventoryItems_ItemId] FOREIGN KEY([ItemId])
REFERENCES [dbo].[InventoryItems] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[InventoryItemCalendars] CHECK CONSTRAINT [FK_dbo.InventoryItemCalendars_dbo.InventoryItems_ItemId]
GO
ALTER TABLE [dbo].[InventoryItemCalendars] WITH CHECK ADD CONSTRAINT [FK_dbo.InventoryItemCalendars_dbo.Projects_Project_Id] FOREIGN KEY([Project_Id])
REFERENCES [dbo].[Projects] ([Id])
GO
ALTER TABLE [dbo].[InventoryItemCalendars] CHECK CONSTRAINT [FK_dbo.InventoryItemCalendars_dbo.Projects_Project_Id]
GO
INSERT INTO ShipListInventory.[dbo].[InventoryItems]([PartSatuts],[PartId]) VALUES(1,1);
INSERT INTO ShipListInventory.[dbo].[InventoryItems]([PartSatuts],[PartId]) VALUES(2,1);
INSERT INTO ShipListInventory.[dbo].[InventoryItems]([PartSatuts],[PartId]) VALUES(1,1);
INSERT INTO ShipListInventory.[dbo].[InventoryItems]([PartSatuts],[PartId]) VALUES(4,1);
INSERT INTO ShipListInventory.[dbo].[InventoryItemCalendars](StartDate,InboundDate,Project_Id, ItemId)VALUES('1/1/2015', '4/15/2015',1,1)
INSERT INTO ShipListInventory.[dbo].[InventoryItemCalendars](StartDate,InboundDate,Project_Id, ItemId)VALUES('1/1/2015', '4/15/2015',1,1)
INSERT INTO ShipListInventory.[dbo].[InventoryItemCalendars](StartDate,InboundDate,Project_Id, ItemId)VALUES('1/1/2015', '4/14/2015',1,2)
INSERT INTO ShipListInventory.[dbo].[InventoryItemCalendars](StartDate,InboundDate,Project_Id, ItemId)VALUES('1/1/2015', '4/15/2015',1,1)
这不是从正确的表返回数据,它只返回InventoryItemCalendars
的数据,这不是我想要的。我希望它只用它来检查日期。它应该从InventoryItems
表
答案 0 :(得分:2)
这是一个语法正确的版本:
SELECT SUM(CASE WHEN ii.[PartStatus] = 1 THEN 1 ELSE 0 END) AS [Available],
SUM(CASE WHEN ii.[PartStatus] = 2 THEN 1 ELSE 0 END) AS [Reserverd],
SUM(CASE WHEN ii.[PartStatus] <> 4 THEN 1 ELSE 0 END) AS [TotalInventory]
FROM ShipListInventory.dbo.InventoryItems ii LEFT JOIN
ShipListInventory.dbo.InventoryItemCalendars AS iic
ON iic.[ItemId] = ii.[Id]
WHERE iic.[StartDate] NOT BETWEEN @startDate AND @endDate AND
iic.[InboundDate] NOT BETWEEN @startDate AND @endDate AND
ii.[PartId] = 248;
执行select时没有事务,因此不需要提交。
答案 1 :(得分:1)
SELECT COUNT(CASE WHEN ii.[PartStatus] = 1 THEN ii.[PartId] END) AS [Available],
COUNT(CASE WHEN ii.[PartStatus] = 2 THEN ii.[PartId] END) AS [Reserverd],
COUNT(CASE WHEN ii.[PartStatus] <> 4 THEN ii.[PartId] END) AS [TotalInventory]
答案 2 :(得分:0)
在Juan Carlos的帮助下,我找到了正确答案。
compileSdkVersion 24
buildToolsVersion '24.0.1'