I have this first case statement below which I need to merge with the case statement below this for grouping values together like this:
days range | Activity_submit
-------------------------------------
0-9 | 11
10-19 | 14
20-29 | 3
SELECT
events_raw.properties ->> 'days_before_stay' AS "Days_before_stay",
COUNT(CASE WHEN ((events_raw.properties ->> 'event_type') LIKE
'fs_mystay_activity_submit') THEN 1 ELSE NULL END) AS "Request Count"
FROM events_raw
second case statement to merge with first
select events_raw.properties ->> 'days_before_stay' AS "Days_before_stay" [days range], count(*) as [number of occurences]
from (
select case
when days between 0 and 9 then ' 0- 9'
when days between 10 and 19 then '10-19'
else '20-99' end as range
from events_raw)
group by 1
I do not see your table structure, but here is example where you can see group by expression.
SELECT
(id/10)*10 toOrder, -- addition field to sort by
(id/10)*10 || ' - ' || ((id)/10+1)*10 AS range,
count(*) myCount
FROM report
GROUP BY
(id/10)*10,
(id/10)*10 || ' - ' || ((id)/10+1)*10, -- group by expression
ORDER BY toOrder;
Instead if id you can use any integer field.
Related
I want no results for null when nothing matches
select t.range as [PriceRange], count(1) as [BooksCount]
from (
select case
when Offer > 3 and offer < 5
then 'Criteria1'
when offer >= 5 then 'Criteria2'
END as range
from BookDetailsMaster) t
group by t.range
This query return value for null range also, because of which it is taking long time to execute. I want it should not search anything in database if no case matches.
If I apply a filter where t.range is not null it is not returning the null values but the query is taking long, so I am doubting it is still checking for no match criteria but while returning result it is not returning that value.
Id Offer
1 2
2 4
3 5
4 6
5 7
6 1
PriceRange BooksCount
-------------------------
Criteria1 1
Criteria2 3
The doubt is it is also making query for a null criteria
You should do this simple condition
SELECT t.RANGE AS [PriceRange], COUNT(1) AS [BooksCount]
FROM
(
SELECT
CASE
WHEN Offer > 3 AND offer < 5 THEN 'Criteria1'
WHEN offer >= 5 THEN 'Criteria2'
END AS RANGE
FROM BookDetailsMaster
WHERE offer > 3
) t
GROUP BY t.RANGE;
if you don't want to use where then you can try below option
SELECT SUM(CASE WHEN (OFFER >3 AND OFFER<5) THEN 1 ELSE 0 END) [BooksCount], 'Criteria1' [PriceRange]
FROM BookDetailsMaster
UNION
SELECT SUM(CASE WHEN (offer >= 5) THEN 1 ELSE 0 END), 'Criteria2'
FROM BookDetailsMaster
You are already defining range in a subquery. Just use it for filtering in the outer query:
select t.range as PriceRange, count(1) as BooksCount
from (select (case when Offer > 3 and offer < 5 then 'Criteria1'
when offer >= 5 then 'Criteria2'
end) as range
from BookDetailsMaster
) t
where t.range is not null
group by t.range
I have a list of words I need to find in a specific column , "description of what happenned "
this holds anything up to 500 or more characters. I have the script below that does work
However how do I replace the Name column 1.2.3 with the actual name of the word I am looking for with the total next to it.
Just cant get it to display prob something simple.
select GROUPING_ID ( Amoxicillin ,Atorvastatin ) as Name ,count(*) as Total
from ( select case when [description_of_what_happened] like '%Amoxicillin%'
then 1 else 0 end as Amoxicillin ,
case when [description_of_what_happened] like '%Atorvastatin%'
then 1 else 0 end as Atorvastatin
FROM "NAME OF TABLE"
group by grouping sets (() ,(Amoxicillin),(Atorvastatin))
having coalesce (Amoxicillin,1) != 0 and coalesce (Atorvastatin,1) != 0
order by grouping_id (Amoxicillin,Atorvastatin)
row 3 being the total I need row 1 and row 2 to show the name of the product
result as below
Name Total
1 7
2 9
3 4112
You can use strings instead of flags:
select coalesce(Amoxicillin, Atorvastatin, 'Total') as Name,
count(*) as Total
from (select (case when [description_of_what_happened] like '%Amoxicillin%'
then 'Amoxicillin'
end) as Amoxicillin ,
(case when [description_of_what_happened] like '%Atorvastatin%'
then 'Atorvastatin'
end
) as Atorvastatin
from "NAME OF TABLE"
where Amoxicillin is not null or Atorvastatin is not null
group by grouping sets ((), (Amoxicillin), (Atorvastatin))
order by name;
Note that I also moved the logic in the having to the where.
This question already has answers here:
SQL Case Expression Syntax?
(8 answers)
Closed 7 years ago.
SQL knowledge Beginner
I have a below table: ScoreTable
Name Score Reason Subject
a1 0 NULL NULL
a2 -1 NULL NULL
a3 -3 fail Maths
a4 -3 fail History
a3 0 NULL NULL
I want to write a query which will look some thing like below
Select DISTINCT Name, Result,
(If Result = -3
then Concat(Reason,' ',Subject))As FailedIn)
From ScoreTable
Expected Output:
Name Score FailedIn
a1 0 0
a2 -1 0
a3 -3 fail Maths
a4 -3 fail History
You probably don't want to put "0" and a string in the same column. It is better to use NULL. So:
Select Name, Result,
(case when Result = -3 then Reason + ' ' + Subject
end) as FailedIn
It is unclear why the last row disappears, but perhaps you want something like:
Select Name, min(Result),
(case when min(Result) = -3 then max(Reason + ' ' + Subject)
end) as FailedIn
from table t
group by name
Try using CASE..WHEN..THEN like below and use minimum result score :
SELECT Name, MIN(Result)
CASE WHEN Result = -3
THEN Reason +' '+Subject
ELSE Result
END As FailedIn
FROM mytable
GROUP BY Name, Result
Try this
SELECT NAME, SCORE,
CASE WHEN SCORE = -3 THEN CONCAT(REASON, ' ' ,SUBJECT )
ELSE '0'
END as FAILEDIN FROM ScoreTable
I have to check certain rows from table and check if-else condition for each rows
TABLE: report
COLUMNS :
sl.no, count, total_count, calls
Sample Data:
sl.no count total_count calls
----------- ----------- ----------- -----------
1 2 6 4
2 2 7 5
3 4 9 3
Here i have to check if condition
if total_count > 6
select 'Y
else
select 'N'
I get correct answer for single row. If there is multiple row it can't check all, it check only last row of my table.
Use CASE expression
SELECT
*,
CASE WHEN total_count > 6 THEN 'Yes' ELSE 'N' END
FROM report
You must use CASE.It work like if-else in t-SQL. MSDN
For example:
SELECT [num],[count], [total_count], [calls], CASE WHEN [total_count] > 6 THEN 'Y' ELSE 'N' END FROM t
You could use CASE. You can read documentation.
SELECT *,
CASE WHEN total_count > 6 THEN 'Y' ELSE ' N' END
FROM Report
The SQL version of "inline if" is CASE:
SELECT
*,
CASE WHEN total_count > 6 THEN 'Y'
ELSE 'N'
END AS IsTotalCountGreaterThanSix
FROM YourTable;
It is really hard to find a good title for this.
Here is the question: I have a SELECT query GROUP BY a field which returns me up to three values (1,2,3). These values are representing the positions of '1' in a binary number.
In other words:
Query Output | Reult
0,1,2 | 7 (111)
1,2 | 6 (110)
3 | 1 (001)
- | 0 (000)
Ok, I know it is easy. But there are two constraints. First, I want a query not a function/store procedure. Second, the result should be a string (like '010') not the number.
I found the solution for integer value, but not the string (varchar)
SELECT COALESCE(sum(power(2, field)), 0) AS test FROM (
SELECT field FROM myTable GROUP BY field) a
I am using SQL server 2008, just in case.
I also have this solution, but this one cannot be extended to bigger number of outputs:
SELECT output =
CASE TEST
WHEN 0 THEN '000'
WHEN 1 THEN '001'
WHEN 2 THEN '010'
WHEN 3 THEN '011'
WHEN 4 THEN '100'
WHEN 5 THEN '101'
WHEN 6 THEN '110'
WHEN 7 THEN '111'
END
FROM(
select COALESCE(sum(power(2, 3 - field)), 0) as test from (
select field from myTable group by field) a) b
You can use binary and and string concatenation:
select (case when test&4 > 0 then '1' else '0' end) +
(case when test&2 > 0 then '1' else '0' end) +
(case when test&1 > 0 then '1' else '0' end)
from (select 6 as test) t;
If you are allergic to case statements, you could do this:
select CHAR(ascii(0) + (test&4)/4) +
CHAR(ascii(0) + (test&2)/2) +
CHAR(ascii(0) + (test&1)/1)
from (select 6 as test) t