Select ranges from tables of ranges - sql

I have 2 range tables ReceivedRanges and DispatchedRanges.
ReceivedRanges
From - To
1 - 100000
200000 - 300000
350000 - 400000
DispatchedRanges
From - To
10000 - 50000
250000 - 275000
350000 - 400000
I want to select new ranges from above 2 tables. output ranges will be:
InventoryRanges
From - To
1 - 9999
50001 - 100000
200000 - 249999
275001 - 300000
how to select these ranges from tables of ranges.
What I have Tried:
Alternately I have tried with generating all individual received sequence numbers as sequence table and marked dispatched numbers as is dispatched true.
based on this table i am grouping and able to retrieve InventoryRanges. But for these I need to store all the sequence number and update huge ranges during dispatch which will slowdown the dispatch process.

I think this will cover all your cases of finding differences between ranges in SQL. Hope this helps you out:
create table received_ranges(item_id int, [from] int, [to] int);
create table dispatched_ranges(item_id int, [from] int, [to] int);
insert into received_ranges (item_id,[from],[to]) values
(1, 1,5000),
(1, 7000,8000),
(2, 6000,9000),
(3, 10000,15000),
(4, 20000,25000);
insert into dispatched_ranges (item_id,[from],[to]) values
(1, 1,250),
(2, 6000,7250),
(2, 7500,8000),
(2, 8200, 9000),
(3, 12000,14000),
(4, 20000,25000);
with dispatched_batch(dispatched_batch_num, received_item, received_from, received_to, dispatched_item, dispatched_from, dispatched_to) as
(select row_number() over (partition by rr.item_id, rr.[from] order by rr.[from]) dispatched_batch_num,
rr.item_id as received_item,
rr.[from] as received_from,
rr.[to] as received_to,
dr.item_id as dispatched_item,
dr.[from] as dispatched_from,
dr.[to] as dispatched_to
from received_ranges rr
left join
dispatched_ranges dr
ON
rr.item_id = dr.item_id
AND
dr.[from] >= rr.[from]
AND
dr.[to] <= rr.[to])
select * from
(select
[current].[received_item],
case when [next].dispatched_batch_num is null then
case when [current].[received_to] <> [current].[dispatched_to] then
[current].dispatched_to + 1
else
0
end
else
case when [next].[dispatched_from] <> [current].[dispatched_to]+1 then
[current].[dispatched_to] + 1
else
0
end
end
as 'inventory from',
case when [next].dispatched_batch_num is null then
case when [current].[received_to] <> [current].[dispatched_to] then
[current].received_to
else
0
end
else
case when [next].[dispatched_from] <> [current].[dispatched_to]+1 then
[next].[dispatched_from] - 1
else
0
end
end
as 'inventory to'
from dispatched_batch [current]
left join
dispatched_batch [next]
on
[current].received_item = [next].received_item
and
[current].dispatched_batch_num + 1 = [next].dispatched_batch_num
UNION
select
[current].received_item,
case when [current].[dispatched_from] is null then
[current].[received_from]
else
case when [previous].dispatched_batch_num is null then
case when [current].[received_from] <> [current].[dispatched_from] then
[current].received_from
else
0
end
else
case when [previous].[dispatched_to] <> [current].[dispatched_from]+1 then
[previous].[dispatched_to] + 1
else
0
end
end
end
as 'inventory from',
case when [current].[dispatched_to] is null then
[current].[received_to]
else
case when [previous].dispatched_batch_num is null then
case when [current].[received_from] <> [current].[dispatched_from] then
[current].dispatched_from -1
else
0
end
else
case when [previous].[dispatched_to] <> [current].[dispatched_from]+1 then
[current].[dispatched_from] - 1
else
0
end
end
end
as 'inventory to'
from dispatched_batch [current]
left join
dispatched_batch previous
on
[current].received_item = previous.received_item
and
[current].dispatched_batch_num = previous.dispatched_batch_num + 1) result
where [inventory from] <> 0;

Assuming your table structure to be something like this:
create table received_ranges(received_date date, [from] int, [to] int);
create table dispatched_ranges(dispatched_date date, [from] int, [to] int);
and also assuming that there wont be multiple entries for each date in received_ranges or dispatched_ranges table (assumption made from the data provided) this query should work for you:
select date, inventory_from, inventory_to from
(select
rr.received_date as 'date',
case when rr.[from] = dr.[from] then
0
else
rr.[from]
end AS 'inventory_from',
case when rr.[from] = dr.[from] then
0
else
dr.[from] - 1
end AS 'inventory_to'
from
received_ranges rr left join
dispatched_ranges dr
on
rr.received_date = dr.dispatched_date
UNION
Select
rr.received_date as 'date',
case when rr.[to] = dr.[to] then
0
else
dr.[to]+1
end AS 'inventory_from',
case when rr.[to] = dr.[to] then
0
else
rr.[to]
end AS 'inventory_to'
from
received_ranges rr left join
dispatched_ranges dr
on
rr.received_date = dr.dispatched_date) result
where (inventory_from == 0 and inventory_to == 0) <> TRUE
order by inventory_from;

Related

Aggregate function in update query with case statement in sql server

I am trying to write update query by using aggregate function and case statement.
Some how i am stuck
Initially i have written following query which gave me error tha,"Aggregate function can not be used in update statement
UPDATE report
SET report.LoadDischargeQty =
CASE WHEN SUBSTRING(CRG_ArrDep,1,1) = 'D' AND cargo.CRG_Quantity is NOT NULL THEN cargo.CRG_Quantity
ELSE
CASE WHEN report.PlaId = 'LP' THEN
CASE WHEN SUBSTRING(CRG_ArrDep,1,1) = 'D' THEN ISNULL(SUM(CRG_SFgrMT),0) END -
CASE WHEN SUBSTRING(CRG_ArrDep,1,1) = 'A' THEN ISNULL(SUM(CRG_SFgrMT),0)END
WHEN report.PlaId = 'DP' THEN
CASE WHEN SUBSTRING(CRG_ArrDep,1,1) = 'A' THEN ISNULL(SUM(CRG_SFgrMT),0) END-
CASE WHEN SUBSTRING(CRG_ArrDep,1,1) = 'D' THEN ISNULL(SUM(CRG_SFgrMT),0)END
ELSE 0 END
END
from #CargoPerformanceReport report
INNER JOIN POSCARGO cargo ON cargo.POS_ID = report.PositionId AND ISNULL(cargo.CRG_Deleted,0)=0
So I refactored it as follow
UPDATE report
SET report.LoadDischargeQty =
CASE WHEN SUBSTRING(CRG_ArrDep,1,1) = 'D' AND cargo.CRG_Quantity is NOT NULL THEN cargo.CRG_Quantity
ELSE
select quantity.dischargeQuantity from (SELECT CASE WHEN report.PlaId = 'LP' THEN
CASE WHEN SUBSTRING(CRG_ArrDep,1,1) = 'D' THEN ISNULL(SUM(CRG_SFgrMT),0) END -
CASE WHEN SUBSTRING(CRG_ArrDep,1,1) = 'A' THEN ISNULL(SUM(CRG_SFgrMT),0)END
WHEN report.PlaId = 'DP' THEN
CASE WHEN SUBSTRING(CRG_ArrDep,1,1) = 'A' THEN ISNULL(SUM(CRG_SFgrMT),0) END-
CASE WHEN SUBSTRING(CRG_ArrDep,1,1) = 'D' THEN ISNULL(SUM(CRG_SFgrMT),0)END
ELSE 0 END dischargeQuantity ) quantity
END
from #CargoPerformanceReport report
INNER JOIN POSCARGO cargo ON cargo.POS_ID = report.PositionId AND ISNULL(cargo.CRG_Deleted,0)=0
table structure
CREATE TABLE #CargoPerformanceReport
(
PositionId VARCHAR(12),
PortAndActivityName VARCHAR(100),
PlaId VARCHAR(12),
LoadDischargeQty REAL,
);
Insert into #CargoPerformanceReport
Values('100',null,'LP',null)
CREATE TABLE #Poscargo
(
POS_ID VARCHAR(12),
CRG_ArrDep VARCHAR(3),
CRG_SFgrMT REAL,
CRG_Quantity REAL,
CRG_Deleted BIT
);
Insert Into #Poscargo(POS_ID,CRG_ArrDep,CRG_SFgrMT,null,0)
Values ('100','DD',100)
Insert Into #Poscargo(POS_ID,CRG_ArrDep,CRG_SFgrMT)
Values ('100','AD',100)
Insert Into #Poscargo(POS_ID,CRG_ArrDep,CRG_SFgrMT)
Values ('100','DD',200)
Insert Into #Poscargo(POS_ID,CRG_ArrDep,CRG_SFgrMT)
Values ('100','AD',50)
Insert Into #Poscargo(POS_ID,CRG_ArrDep,CRG_SFgrMT)
Values ('101','DL',200)
Insert Into #Poscargo(POS_ID,CRG_ArrDep,CRG_SFgrMT)
Values ('101','AL',200)
SELECT * FROM #Poscargo
SELECT * FROM #CargoPerformanceReport
result:-
PositionId | PlaId | LoadDischargeQty
100 | LP | 150
but it is not right way and also has error.
anyone have optimized solution for the same?
Put all that stuff into subquery:
SELECT LoadDischargeQty =
CASE
WHEN report.PlaId = 'LP'
THEN 1
ELSE -1
END * cargo.qty
FROM CargoPerformanceReport report
CROSS APPLY(
SELECT SUM(
CASE
WHEN CRG_ArrDep LIKE 'D%'
THEN 1
WHEN CRG_ArrDep LIKE 'A%'
THEN -1
ELSE 0
END * ISNULL(CRG_SFgrMT, 0)
) qty
FROM POSCARGO cargo
WHERE cargo.POS_ID = report.PositionId
AND ISNULL(cargo.CRG_Deleted,0)=0
) cargo
http://sqlfiddle.com/#!18/648d0/7
I don't understand how is CRG_Quantity column supposed to be used and you did not provide any row with that data so I removed it to show you the aggregation itself. Works fine, you'll get your 150. You may easily convert it into update statement.
Perhaps something like this?
It uses cases with summed cases.
Test on SQL Fiddle here
UPDATE t
SET LoadDischargeQty = q.CalcDischargeQty
FROM #CargoPerformanceReport t
JOIN
(
SELECT report.PositionId, report.PlaId,
CASE
WHEN SUM(CASE WHEN LEFT(cargo.CRG_ArrDep,1) = 'D' THEN cargo.CRG_Quantity END) IS NOT NULL
THEN SUM(CASE WHEN LEFT(cargo.CRG_ArrDep,1) = 'D' THEN cargo.CRG_Quantity END)
ELSE CASE
WHEN report.PlaId = 'LP'
THEN SUM(CASE WHEN LEFT(cargo.CRG_ArrDep,1) = 'D' THEN cargo.CRG_SFgrMT END) -
SUM(CASE WHEN LEFT(cargo.CRG_ArrDep,1) = 'A' THEN cargo.CRG_SFgrMT END)
WHEN report.PlaId = 'DP'
THEN SUM(CASE WHEN LEFT(cargo.CRG_ArrDep,1) = 'A' THEN cargo.CRG_SFgrMT END) -
SUM(CASE WHEN LEFT(cargo.CRG_ArrDep,1) = 'D' THEN cargo.CRG_SFgrMT END)
ELSE 0
END
END AS CalcDischargeQty
FROM #CargoPerformanceReport report
INNER JOIN #Poscargo cargo
ON cargo.POS_ID = report.PositionId AND (cargo.CRG_Deleted = 0 OR cargo.CRG_Deleted IS NULL)
GROUP BY report.PositionId, report.PlaId
) q ON t.PositionId = q.PositionId;
Sample Data
IF OBJECT_ID('tempdb..#CargoPerformanceReport') IS NOT NULL DROP TABLE #CargoPerformanceReport;
CREATE TABLE #CargoPerformanceReport
(
PositionId VARCHAR(12) PRIMARY KEY,
PortAndActivityName VARCHAR(100),
PlaId VARCHAR(12),
LoadDischargeQty REAL
);
IF OBJECT_ID('tempdb..#Poscargo') IS NOT NULL DROP TABLE #Poscargo;
CREATE TABLE #Poscargo
(
POS_ID VARCHAR(12),
CRG_ArrDep VARCHAR(3),
CRG_SFgrMT REAL,
CRG_Quantity REAL,
CRG_Deleted BIT
);
Insert into #CargoPerformanceReport Values
('100','name1','LP',null),
('101','name2','DP',null),
('102','name3','DP',null);
Insert Into #Poscargo(POS_ID, CRG_ArrDep, CRG_SFgrMT, CRG_Quantity, CRG_Deleted) Values
('100','DD',100,null,0)
,('100','AD',100,null,0)
,('100','DD',200,null,0)
,('100','AD',50,null,0)
,('101','DL',100,null,0)
,('101','AL',200,null,0)
,('102','DL',100,500,0)
,('102','AL',200,null,0);
Result
PositionId PortAndActivityName PlaId LoadDischargeQty
---------- ------------------- ----- ----------------
100 name1 LP 150
101 name2 DP 100
102 name3 DP 500

A SQL query to count how many attributes a record has in common with another record?

I am trying to write a SQL query to take the count of columns with equal value in my schema for each row by comparison to a single record.
Example:
record1: 1, 0, 1, 0, 0
record2: 0, 0, 0, 0, 1
record3: 0, 0, 1, 0, 0
record1 has 2 attributes in common with record2, go through the entire table and order by number of attributes each record has in common with record1
Is there a way to write a SQL statement that will do this? I have only found ways to compare each row and specify which attributes must be of equal value.
You can do:
select t.*,
((case when t.col1 = t1.col1 then 1 else 0 end) +
(case when t.col2 = t1.col2 then 1 else 0 end) +
(case when t.col3 = t1.col3 then 1 else 0 end) +
. . .
) as num_in_common
from t cross join
t t1
where t1.id = 1; -- or however you define "record1"
order by num_in_common desc;
Here's a nice routine you can use in SQL Server that will do this if you'd like. Replace #temp with your table name:
declare #holding table (id int, col1 int, col2 int, col3 int, col4 int, col5 int, num_in_common int)
declare #iterator int = 1
declare #row1col1 int, #row1col2 int, #row1col3 int, #row1col4 int ,#row1col5 int
while #iterator<=(select max(id) from #temp)
begin
if #iterator=1
select #row1col1=col1, #row1col2=col2, #row1col3=col3, #row1col4=col4 ,#row1col5=col5
from #temp where id=#iterator
else
insert #holding
select *, case when col1-#row1col1 = 0 then 1 else 0 end +
case when col2-#row1col2 = 0 then 1 else 0 end +
case when col3-#row1col3 = 0 then 1 else 0 end +
case when col4-#row1col4 = 0 then 1 else 0 end +
case when col5-#row1col5 = 0 then 1 else 0 end
from #temp where id=#iterator
set #iterator=#iterator+1
end
select * from #holding

T-SQL exactly 1 condition is true out of a set of conditions

What is the simplest way to express in T-SQL that only 1 and exactly 1 of a number of boolean conditions is true (needs to be usable in a CHECK constraint)?
XOR works for 2 conditons, eg A XOR B will insure that exactly 1 is set but it does not work for 3 conditions:
One solution would be to get some kind of collection out of them, filter on the conditions being true, perform and aggregate/sum and check that the result is equal to 1.
I would structure your CHECK along the lines of:
CHECK (
CASE WHEN <condition 1> THEN 1 ELSE 0 END +
CASE WHEN <condition 2> THEN 1 ELSE 0 END +
CASE WHEN <condition 3> THEN 1 ELSE 0 END
= 1
)
It's slightly verbose but it is hopefully readable to see what your intention was. It also extends to other similar requirements more easily than XOR (e.g. "exactly 2 out of 5 conditions must be matched" can follow the same structure)
For SQL Server 2012 or later, you can be slight more concise with IIF:
CHECK (
IIF(<condition 1>,1,0) +
IIF(<condition 2>,1,0) +
IIF(<condition 3>,1,0)
= 1
)
Shamelessly taken from this SO question, a general formula for an exclusive OR between three variables can be written as:
(a ^ b ^ c) && !(a && b && c)
We can express this in SQL Server as:
(A XOR B XOR C) AND NOT (A AND B AND C)
Note that this only works for three variables, and does not generalize to higher numbers. If you have more than three variables, you'll have to do more work.
Assuming that all your conditions are represented as BIT columns, you can have a constraint with the format:
alter table [table_name] add constraint [constraint_name]
check ( ( a ^ b ^ c ) = 1 AND NOT ( a & b & c ) = 1 )
Doing this, you can also then use the same conditions in a case statement, like:
select a, b, c,
case when (( a ^ b ^ c ) = 1 AND NOT ( a & b & c ) = 1) then 1
else 0 end
as true_or_false
from [table_name]
Putting this together, we can demo it with a script like:
create table #bits (a bit, b bit, c bit)
create table #bits2 (a bit, b bit, c bit)
alter table #bits2 add constraint ck_xor
check ( ( a ^ b ^ c ) = 1 AND NOT ( a & b & c ) = 1 )
insert into #bits
values
( 0, 0, 0 ), ( 0, 0, 1 ), ( 0, 1, 0 ), ( 0, 1, 1 ), ( 1, 0, 0 ), ( 1, 0, 1 ), ( 1, 1, 0 ), ( 1, 1, 1 )
select a, b, c,
case when ( a ^ b ^ c ) = 1 AND NOT ( a & b & c ) = 1 then 1
else 0 end
as true_or_false
from #bits
insert into #bits2
select * from #bits
where ( a ^ b ^ c ) = 1 AND NOT ( a & b & c ) = 1
-- the below line will fail because of the check constraint
insert into #bits2 (a,b,c) values (1,1,0)
select * from #bits2
drop table #bits
drop table #bits2
Declare three variable #a, #b , #c and it's bit type. Where 1 is true and 0 is false. Example with all possible of bit is here.
declare #a bit, #b bit, #c bit;
set #a=1; set #b=1; set #c=1; select #a as a,#b as b,#c as c, case when #a=#b then (case when #c=1 then 1 else 0 end) else (case when #c=0 then 1 else 0 end) end as xor;
set #a=1; set #b=1; set #c=0; select #a as a,#b as b,#c as c, case when #a=#b then (case when #c=1 then 1 else 0 end) else (case when #c=0 then 1 else 0 end) end as xor;
set #a=1; set #b=0; set #c=0; select #a as a,#b as b,#c as c, case when #a=#b then (case when #c=1 then 1 else 0 end) else (case when #c=0 then 1 else 0 end) end as xor;
set #a=1; set #b=0; set #c=0; select #a as a,#b as b,#c as c, case when #a=#b then (case when #c=1 then 1 else 0 end) else (case when #c=0 then 1 else 0 end) end as xor;
set #a=0; set #b=1; set #c=0; select #a as a,#b as b,#c as c, case when #a=#b then (case when #c=1 then 1 else 0 end) else (case when #c=0 then 1 else 0 end) end as xor;
set #a=0; set #b=1; set #c=0; select #a as a,#b as b,#c as c, case when #a=#b then (case when #c=1 then 1 else 0 end) else (case when #c=0 then 1 else 0 end) end as xor;
set #a=0; set #b=0; set #c=0; select #a as a,#b as b,#c as c, case when #a=#b then (case when #c=1 then 1 else 0 end) else (case when #c=0 then 1 else 0 end) end as xor;
set #a=0; set #b=0; set #c=0; select #a as a,#b as b,#c as c, case when #a=#b then (case when #c=1 then 1 else 0 end) else (case when #c=0 then 1 else 0 end) end as xor;
To reduce even more from the code duplication and make it a bit more readable something like this could be used:
(SELECT SUM(ExpressionValue)
FROM
(VALUES
(CASE WHEN 42 = 42 THEN 1 ELSE 0 END),
(CASE WHEN 42 = 42 THEN 1 ELSE 0 END),
(CASE WHEN 42 = 1 THEN 1 ELSE 0 END),
(CASE WHEN 42 = 42 THEN 1 ELSE 0 END)
) AS conditions(ExpressionValue))
=
1
Example usage:
DECLARE #say as VARCHAR(MAX) =
CASE
WHEN (
(SELECT SUM(ExpressionValue)
FROM
(VALUES
(CASE WHEN 42 = 42 THEN 1 ELSE 0 END),
(CASE WHEN 42 = 42 THEN 1 ELSE 0 END),
(CASE WHEN 42 = 1 THEN 1 ELSE 0 END),
(CASE WHEN 42 = 42 THEN 1 ELSE 0 END)
) AS conditions(ExpressionValue))
=
1
) THEN 'Only one set'
ELSE 'Non only one set'
END
PRINT #say
It could still be improved if it is somehow possible to elegantly just apply the case operation similar to a map.
declare #tt table (i int, b1 bit, b2 bit, b3 bit);
insert into #tt values (1,0,0,0), (2,1,1,1), (3,1,0,0)
select i, b1, b2, b3
from #tt
where cast(b1 as tinyint) + cast(b2 as tinyint) + cast(b3 as tinyint) = 1

Aggregating different rows in one column SQL Query

Sorry, I didn't come up with a good title for the question, so feel free to change it accordingly.
I may describe my question with a minimal example in MS SQL server 2012:
create table #tmp
(
RowID varchar(10),
SectionCode int,
SectionName varchar(10)
)
insert into #tmp values('Record1' , 1 , 'AB');
insert into #tmp values('Record1' , 2 , 'CD');
insert into #tmp values('Record1' , 3 , 'EF');
insert into #tmp values('Record2' , 1 , 'AB');
insert into #tmp values('Record2' , 4 , 'GH');
insert into #tmp values('Record2' , 5 , 'IJ');
insert into #tmp values('Record3' , 2 , 'CD');
insert into #tmp values('Record3' , 5 , 'IJ');
I am trying to create a one row per record result in which every section is a column and if there is a row associated with a section, the corresponding column value is increased. This is (not) what I want (the same record data on different rows)
select RowID,
case when SectionName = 'AB' then 1 else 0 end as [AB Section] ,
case when SectionName = 'CD' then 1 else 0 end as [CD Section] ,
case when SectionName = 'EF' then 1 else 0 end as [EF Section] ,
case when SectionName = 'GH' then 1 else 0 end as [GH Section] ,
case when SectionName = 'IJ' then 1 else 0 end as [IJ Section]
from #tmp
group by RowID , SectionName
which gives this output:
I need this:
Thanks in advance
You can use pivot for this as below and manipulate the values of sections however you want.
SELECT rowid
,CASE
WHEN ab IS NULL
THEN 0
ELSE 1
END AS ab
,CASE
WHEN cd IS NULL
THEN 0
ELSE 1
END AS cd
,CASE
WHEN ef IS NULL
THEN 0
ELSE 1
END AS ef
,CASE
WHEN gh IS NULL
THEN 0
ELSE 1
END AS gh
,CASE
WHEN ij IS NULL
THEN 0
ELSE 1
END AS ij
FROM (
SELECT *
FROM #tmp
PIVOT(MAX(Sectioncode) FOR Sectionname IN (
AB
,CD
,EF
,GH
,IJ
)) pvt
) tab
I think the result you shown is not correct for record id 2. ij of record id 2 should be 1.
I think you want this:
select RowID,
sum(case when SectionName = 'AB' then 1 else 0 end) as [AB Section] ,
sum(case when SectionName = 'CD' then 1 else 0 end) as [CD Section] ,
sum(case when SectionName = 'EF' then 1 else 0 end) as [EF Section] ,
sum(case when SectionName = 'GH' then 1 else 0 end) as [GH Section] ,
from #tmp
group by RowID;
That is, you need aggregation functions. And the group by should contain the columns that you want to define each row (i.e. only the RowId).

Stored procedure to return data based on sum of 3 columns

I have a stored procedure as follows:
SELECT DiscountId
,CompanyId
,Discount1
,Discount2
,Discount3
,(Discount1+Discount2+Discount3) as Total
FROM PriceDiscount
This can return one or more rows.
I also need to check the aggregate of Discount1, Discount2 and Discount3. If the sum for any of the rows is greater than 0, I want to set a column isDiscounted = true and return it also.
Please help me - how can I achieve this?
I do not want to check this in the code or create another stored procedure for this. Hence wanted to return both the results from this stored procedure.
You can use case to calculate/set the isDiscounted column.
with x as (
SELECT DiscountId
,CompanyId
,Discount1
,Discount2
,Discount3
,case when (Discount1+Discount2+Discount3) > 0 then 'True'
else 'False' end as isDiscounted
FROM PriceDiscount)
, y as (select case when isDiscounted = 'True' then count(*) end as true_count,
case when isDiscounted = 'False' then count(*) end as false_count
from x group by isDiscounted)
select case when true_count > 0 then 'True'
when falsecount > 0 and truecount = 0 then 'False' end
as final_status
from y
Edit:
with x as (
SELECT DiscountId
,CompanyId
,Discount1
,Discount2
,Discount3
,case when (Discount1+Discount2+Discount3) > 0 then 'True'
else 'False' end as isDiscounted
FROM PriceDiscount)
, y as (select sum(case when isDiscounted = 'True' then 1 else 0 end) as true_count,
sum(case when isDiscounted = 'False' then 1 else 0 end) as false_count
from x)
select case when true_count > 0 then 'True'
when false_count > 0 and true_count = 0 then 'False' end
as final_status
from y