I have multiple query which is providing count based on different where condition.
Will it be possible to bring all result in single Rows.
for eg:
Query1:
SELECT COUNT(COL25) ASSURED, FROM TAB1 WHERE COL1= 'ALPHA' AND COL2='ROLE'
Query2:
SELECT COUNT(COL25) RELEASE FROM TAB1 WHERE COL3 in('BEETA','X','Y') AND COLSTATUS='ABC'
The result for 1st query is ASSURED = 100
and 2nd Query is RELEASE = 5000
i am trying to display output as
ASSURED | RELEASE
100 | 5000
Use conditional aggregation:
SELECT SUM(CASE WHEN COL1 = 'ALPHA' AND COL2 = 'ROLE' THEN 1 ELSE 0 END) as ASSURED,
SUM(CASE WHEN COL3 = 'BEETA' AND COLSTATUS = 'ABC' THEN 1 ELSE 0 END) as RELEASE
FROM TAB1;
EDIT:
If you actually need to count non-NULL values, you can be explicit (my preference):
SELECT SUM(CASE WHEN COL1 = 'ALPHA' AND COL2 = 'ROLE' AND col25 IS NOT NULL THEN 1 ELSE 0 END) as ASSURED,
SUM(CASE WHEN COL3 = 'BEETA' AND COLSTATUS = 'ABC' AND col25 IS NOT NULL THEN 1 ELSE 0 END) as RELEASE
FROM TAB1;
Or be a bit more implicit:
SELECT COUNT(CASE WHEN COL1 = 'ALPHA' AND COL2 = 'ROLE' THEN col25 END) as ASSURED,
COUNT(CASE WHEN COL3 = 'BEETA' AND COLSTATUS = 'ABC' THEN col25 END) as RELEASE
FROM TAB1;
You can also use joins, but #Gordon Linoff answer is cleaner and way shorter than this.
SELECT first.ASSURED, second.RELEASE
FROM
(SELECT
COUNT(COL25) ASSURED
FROM TAB1
WHERE COL1= 'ALPHA' AND COL2='ROLE') AS first
INNER JOIN (SELECT
COUNT(COL25) RELEASE
FROM TAB1
WHERE COL3 in('BEETA','X','Y')
AND COLSTATUS='ABC') AS second
ON 1=1
Related
I am having a SQL query to realise in Odata URL query -
SELECT col1,
SUM (CASE WHEN col2 = 'str' THEN 1 ELSE 0) display_col2,
SUM (CASE WHEN col3 = 'str' THEN 1 ELSE 0) display_col3
FROM table
GROUP BY col1;
But I am not able to do so. facing issue in execution.
Try this
Select col1,
CAST(SUM(CASE WHEN col2 = 'str' THEN 1 ELSE 0 END) AS display_col2,
CAST(SUM(CASE WHEN col3 = 'str' THEN 1 ELSE 0 END) AS display_col3
FROM table
GROUP BY col1;
I have the table below which has similar names in the column 'Nam':
I need to update the column status with the condition below:
If val2 > Val1 (either in first line or in the second line)-->both status should change to "OK"
IF val2 in both lines are smaller than Val1 (in both lines) --> Status=False'
I don't know how to update this condition like (if else)
Thanks for your help.
You can do it with EXISTS:
UPDATE t1
SET t1.Status = CASE
WHEN EXISTS (SELECT 1 FROM tablename t2 WHERE t2.Nam = t1.Nam AND t2.Val2 > t2.Val1) THEN 'OK'
ELSE 'False'
END
FROM tablename t1
See the demo.
You can use window functions and an updatable CTE:
with toupdate as (
select t.*,
sum(case when val2 >= val1 then 1 else 0 end) over (partition by nam) as num_increasing,
sum(case when val2 <= val1 then 1 else 0 end) over (partition by nam) as num_decreasing
from t
)
update toupdate
set status = (case when num_increasing = 0 then 'false' else 'ok' end)
where num_increasing = 0 or num_decreasing = 0
Note that this is doing the "inverse" of your logic -- counting the exceptions.
Please amend the below query for my requirement. Please find code below.
CASE Select NVL(Sum(Value1+value2 +value3),0) from Table1 Where Step = 4
WHEN 0 THEN (Select Sum(Value1) from Table1 Where Step < 4)
Else NVL(Sum(Value1 + value2 + value3) END AS "SumValue"
Here your go. Use subquery.
select case when t1.val = 0 then t1.val2 else t1.val3 end as "SumValue"
from
(Select NVL(Sum(case when step = 4 then Value1+value2+value3 else 0 end),0) val
, Sum(case when step < 4 then Value1 else 0 end) val2
, Sum(Value1+value2+value3) val3 from Table1) t1
I have this data (for example):
id col1 col2
--------------------------------
5614 (null) Y
5614 Y (null)
5614 Y (null)
I want to obtain a single line where it tells me if the fields has some value Y.
id col1 col2
5614 Y Y
If no value Y present, then it should be N.
My actual query do that, but I think it should be a shorter way:
SELECT op.id,
CASE WHEN (SUM(CASE WHEN col1 = 'S' THEN 1 ELSE 0 END)) > 0 THEN 'S' ELSE 'N' END AS col1,
CASE WHEN (SUM(CASE WHEN col2 = 'S' THEN 1 ELSE 0 END)) > 0 THEN 'S' ELSE 'N' END AS col2
FROM OPERATOR op LEFT JOIN TRAM_OP tr ON op.id = tr.id AND tr.id2 = 20
WHERE op.id = 5614 GROUP BY op.id
As you can see I have two tables and I need to join them to match the id2 key.
One way is to use a shorthand that eliminates the need for some of the case statements:
SELECT op.id,
(CASE WHEN SUM(col1 = 'S') > 0 THEN 'S' ELSE 'N' END) AS col1,
(CASE WHEN SUM(col2 = 'S') > 0 THEN 'S' ELSE 'N' END) AS col2
FROM OPERATOR op LEFT JOIN
TRAM_OP tr
ON op.id = tr.id AND tr.id2 = 20
WHERE op.id = 5614
GROUP BY op.id;
You could also take advantage of the fact the 'S' > 'N':
SELECT op.id, MAX(col1) AS col1, MAX(col2) AS col2
FROM OPERATOR op LEFT JOIN
TRAM_OP tr
ON op.id = tr.id AND tr.id2 = 20
WHERE op.id = 5614
GROUP BY op.id;
However, this version might be confusing to someone else reading the code.
If Y is the only possible value, then with a simple max, you get either this value or NULL:
SELECT id, max(col1) AS col1, max(col2) AS col2
FROM ...
WHERE ...
GROUP BY id
You can change the NULL to 'N' with the ifnull function:
SELECT id,
ifnull(max(col1), 'N') AS col1,
ifnull(max(col2), 'N') AS col2
FROM ...
WHERE ...
GROUP BY id
Select ID,
case when Col1 > 0 then 'Y' else 'N' end 'COL1',
case when Col2 > 0 then 'Y' else 'N' end 'COL2'
from
(
Select id,SUM(case when Col1 = 'y' then 1 else 0 end)'COL1', SUM(
case when Col2 = 'y' then 1 else 0 end
)'COL2'
from #temp
group by id
) t
I think my question really boils down do doing a sql IN on multiple values.
Here's a SQL Fiddle of what I have so far.
So long story short, I have Cases that have multiple Types.
I need to filter Cases containing certain Types
Say I have 6 rows (col1,col2)... (1,2),(1,3)(1,4),(2,3),(2,4),(2,5)
How do I select only Cases that have 2, AND 3 AND 4 in col2?
2,3,4 is just a use case. It could be just about any combination (the SQL Fiddle has a couple tables that determine this).
Try:
select [CaseId]
from CaseTypes
where [TypeID] in (2,3,4)
group by [CaseId]
having count(distinct [TypeID]) = 3
(SQLFiddle here)
Couple of things you might try:
Using Derived Tables:
SELECT a.col1
FROM (SELECT col1 FROM Cases WHERE col2 = 2) a
INNER JOIN (SELECT col1 FROM Cases WHERE col2 = 3) b ON a.col1 = b.col1
INNER JOIN (SELECT col1 FROM Cases WHERE col2 = 4) c ON a.col1 = c.col1
Using Aggregation:
SELECT col1
FROM Cases
GROUP BY col1
HAVING
SUM(CASE col2 WHEN 2 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE col2 WHEN 3 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE col2 WHEN 4 THEN 1 ELSE 0 END) > 0
is this a bit closer?
SELECT * from CaseTypes CT
INNER JOIN TypeBillable TB ON CT.TypeId = TB.TypeId
where CT.CaseId in
(
select CaseID from CaseTypes
PIVOT (count (TypeId) for TypeId in ([2],[3],[4])) as x
where [2] > 0 and [3] > 0 and [4] > 0
)