I have a query which checks the status of a specific tank, however I have 50 tanks which I'd like to run this for (FV101, FV102, FV103 etc.)
I could union together 50 queries with the WHERE changed, but I suspect that there must be a better way.
The query is very simple:
DECLARE @tank varchar(5)
SET @tank = 'FV101'
SELECT
ROW_NUMBER() OVER (PARTITION BY [Item] ORDER BY [TankName]) [Row],
*
FROM
(
SELECT TOP 1
@tank [TankName],
T1.[Item],
CASE WHEN AVG(CAST(T0.[pHValue] AS dec (10,5))) NOT BETWEEN MAX(T2. [pH_lower]) AND MAX(T2.[pH_upper]) THEN 'Red' ELSE 'Black' END [Spec]
FROM
t005_pci_data T0 INNER JOIN t001_fvbatch T1 ON T1.[FVBatch] = T0. [FVBatch] LEFT JOIN t024_specifications T2 ON T2.[BeerBrand] = T1.[BeerBrand] AND [Type] = 'Finished Product'
WHERE
T0.[FVBatch] = (SELECT TOP 1 T0.[FVBatch] FROM t005_pci_data T0 INNER JOIN t001_fvbatch T1 ON T1.[FVBatch] = T0.[FVBatch] WHERE T1.[TankName] = 'FV101' ORDER BY T0.[DateTime] DESC) AND
EXISTS (SELECT [FVBatch] FROM t005_pci_data WHERE [TankName] = @tank AND [DateTime] >= DATEADD(day,-2,GETDATE()))
GROUP BY
T1.[Item],T0.[DateTime]
ORDER BY
T0.[DateTime] DESC
) a
Is there a way to pass a list of values to this query for it to use instead of repeating the query manually multiple times?
EDIT as per iamdave's suggestion
Schema (simplified) is as below:
The goal is to list everything which is currently in each tank and check if the value of the pH is within the acceptable limits.
Desired output (up to FV105) would be:
In this example there is nothing in tanks FV101 or FV104, as decided by the following code in the WHERE
EXISTS (SELECT [FVBatch] FROM t005_pci_data WHERE [TankName] = @tank AND [DateTime] >= DATEADD(day,-2,GETDATE()))
The end result is I would like to create a table in MSSRS which shows what item is in each tank and whether it is within specifications or not.
FURTHER EDIT with sample data as requested (Not very imaginative I'm afraid)
declare @t1 table(FVBatch int, TankName nvarchar(5), Item nvarchar(20));
declare @t2 table(Item nvarchar(20), ph_lower decimal(10,2), ph_upper decimal(10,2));
declare @t3 table(FVBatch int, pHValue decimal(10,2), DateValue datetime);
insert into @t1 values
(3160001,'FV101','Stout')
,(3160002,'FV102','Stout')
,(3160003,'FV103','Stout')
,(3160004,'FV104','Pale Ale')
,(3160005,'FV105','Pale Ale')
,(3160070,'FST04','IPA');
insert into @t2 values
('Pale Ale',3.5,5.5)
,('Stout',2,3.5);
insert into @t3 values
(3160001,4 ,'20161209')
,(3160001,4 ,'20161210')
,(3160001,4 ,'20161212')
,(3160002,4 ,'20161218')
,(3160002,4 ,'20161220')
,(3160002,4 ,'20161222')
,(3160003,4 ,'20161218')
,(3160003,4 ,'20161220')
,(3160003,4 ,'20161222')
,(3160004,4 ,'20161209')
,(3160004,4 ,'20161210')
,(3160004,4 ,'20161212')
,(3160005,4 ,'20161218')
,(3160005,4 ,'20161220')
,(3160005,4 ,'20161222')
,(3160070,4.26,'20161218')
,(3160070,4.26,'20161216')
,(3160070,4.24,'20161215')
,(3160070,4.24,'20161214')
,(3160070,4.26,'20161213')
,(3160070,4.2 ,'20161212')
,(3160070,4.21,'20161211')
,(3160070,4.12,'20161209')
,(3160070,4.09,'20161208')
,(3160070,4.1 ,'20161207');
How does this do?
Using your test data, the above query returns:
Edit based on conversation