简体   繁体   中英

SQL Server Count items available

I am building a pretty simple inventory items database that will allow me to check out items between dates. I will need to return a single row that will tell me the available, reserved, and total inventory of this item. I cannot seem to get this correct.

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

EDIT: I am only returning rows from InventoryItemCalendars I want to return items from 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)

this isnt returning data from correct table it is only returning data form InventoryItemCalendars which isnt what i want. i want it to only use this to check the dates. it should return the items from InventoryItems table

Here is a syntactically correct version:

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;

There is no transaction when you do a select, so committing is not necessary.

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]

With the help of Juan Carlos I have found the correct answer.

SELECT COUNT(CASE WHEN  ii.[PartSatuts]=1 THEN ii.[PartId] END ) AS [Available],
       COUNT(CASE WHEN  ii.[PartSatuts]=2 THEN ii.[PartId] END ) AS [Reserverd],
       COUNT(CASE WHEN  ii.[PartSatuts]<> 4 THEN ii.[PartId] END ) 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)
  OR iic.InboundDate IS NULL)
  AND ii.PartId = 248

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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