Showing sum of conditions as one row - sql

Hi I have a table like this
select id, value, condition from mytable
result
and I need a query to make it like this
is it possible?

Just use conditional aggregation:
select id,
sum(case when condition = 'true' then value else 0 end) as num_true,
sum(case when condition = 'false' then value else 0 end) as num_false
from t
group by id;
You have tagged the question with both Oracle and SQL Server. Neither supports a boolean type directly in SQL, so I am guessing that the condition is a string.

yes possible
select id "id", sum(decode(condition,'TRUE',value,0)) "sum_of_condition_true",
sum(decode(condition,'FALSE',value,0)) "sum_of_condition_false"
from mytable
group by id
order by id;

Related

replace some value in sql with Null and perform count function in sql server while performing group by

there is LAT_LONG field which I want to count for each ID, the LAT_LONG contain some string value as 'null/null' which I want to replace with NULL, and than perform the group by operation
select ID, COUNT(LAT_LONG) as LAT_LONG_CAT from mytable group by ID
How to replace 'null/null' string with Null in LAT_LONG field
In SQL Server you can simply use NULLIF function like so:
SELECT lat_long, NULLIF(lat_long, 'null/null') AS lat_long_parsed FROM mytable
Example on dbfiddle
So in the end your query would be:
SELECT ID
, COUNT(NULLIF(lat_long, 'null/null')) AS LAT_LONG_CAT
FROM mytable
GROUP BY id
Use a CASE expression:
SELECT ID,
COUNT(CASE WHEN LAT_LONG <> 'null/null'
THEN LAT_LONG END) AS LAT_LONG_CNT
FROM mytable
GROUP BY ID;
The above CASE expression will treat a value of null/null as NULL, otherwise it counts the LAT_LONG field directly.

sql group by satisfying multiple conditions within the group

I have a table like below:
I want to select the group which has RELB_CD =9093 and INFO_SRC_CD with 7784. Both conditions should be present in the group. In the table below my output should be the group with id=139993690.
You can use aggregation with having:
select id
from t
group by id
having sum(case when relb_cd = 9093 then 1 else 0 end) > 0 and
sum(case when info_src_cde = 7784 then 1 else 0 end) > 0
hey use this code hope this will help you.
you have to ignore the date column because that one is not allowing to group
select id,fisc_ind, sum(sls_amt),relb_cd,info_scop,info_src_cd from yourtable group by id,fisc_ind,relb_cd,info_scop,info_src_cd
Another working answer. If your data are large, you could compare both GL's and this working answer and see which runs faster for you. I honestly don't know which is faster. This was slightly faster with a very short set of data.
select id
from table1
where relb_cd = 9093
intersect
select id
from table1
where info_src_cd = 7784

Is there a way to contruct this kind of result using group by in sql?

I have a table which consists of data where in I'm having trouble counting the corresponding rows.
Here is the sample table:
I am expecting an output like this:
You can do conditional aggregation:
select
sum(case when result = 'X' then 1 else 0 end) count_x,
sum(case when result is null then 1 else 0 end) count_blank
from mytable
I assume that by blank you mean null. If not, then you can change the condition in the second sum() from result is null to result = ''.
If you are running MySQL, this can be shortened a little:
select
sum(result = 'X') count_x,
sum(result is null) count_blank
from mytable

BigQuery(standard SQL) grouping values based on first CASE WHEN statement

Here is my query with the output below the syntax.
SELECT DISTINCT CASE WHEN id = 'RUS0261431' THEN value END AS sr_type,
COUNT(CASE WHEN id in ('RUS0290788') AND value in ('1','2','3','4') THEN respondentid END) AS sub_ces,
COUNT(CASE WHEN id IN ('RUS0290788') AND value in ('5','6','7') THEN respondentid END) AS pos_ces,
COUNT(*) as total_ces
FROM `some_table`
WHERE id in ( 'RUS0261431') AND id <> '' AND value IS NOT NULL
GROUP BY 1
As you can see with the attached table I'm unable to group the values based on Id RUS0290788 with the distinct values that map to RUS0261431. Is there anyway to pivot with altering my case when statements so I can group sub_ces and pos_ces by sr_type. Thanks in advanceenter image description here
You can simplify your WHERE condition to WHERE id = ('RUS0261431'). Only records with this value will be selected so you do not have to repeat this in the CASE statements.

How to COUNT a value in a column?

I have a column where I flag two values (0 and 1). How can I select all from the table but only counting the value 1 from the flag column?
SQL Server
You need a COUNT with CASE expression, like this:
COUNT(CASE WHEN flag = 1 then 1 END)
If you want to return all of your values and also have a total count (using sort of logic) you could use a window function aggregation using over like so:
select
t.*
, count(case when flag = 1 then 1 end) over(partition by 1) as flag_1_count
from t
Err, are you asking how to perform a simple select statement or am I missing something...
Like this?
SELECT *
FROM [YourTableName]
WHERE FlagColumnName = 1
This should do it
Select count(0)
from YourTableName
where ValueField = 1
I think, you mean something like this:
SELECT COUNT(*) FROM [YOUR_TABLE] WHERE [FLAGCOLUMN] = 1;
Not quite sure I understand the question, but wouldn't it be something like:
SELECT * FROM your_table WHERE flag = 1