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

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.

Related

how to ignore the values before and after between the rows in a table

I have a table as like below
I have to add a new column to the table as "value_", which should contains age values for rows between last TRUE i.e row number 4 (highlighted in green color) value and last record(which has 01-01-9999) and remaining all should be "zero"
like as below
If all values (except last record which have 01-01-9999) are FALSE then we need all the age values like as below
how to achieve this in sql? Could you please help me on this
If I understand correctly , here is one way:
SELECT * , CASE WHEN maxtrueflag IS NULL THEN age_
CASE WHEN maxtrueflag IS NOT NULL AND from_>= maxtruefalg THEN age_
ELSE 0 END AS value_
FROM (
SELECT *, MAX(CASE WHEN flag = TRUE THEN from_ END) OVER() maxtrueflag
FROM tableName
) t

check and compare the count from two tables without relation

I have below tables
Table1: "Demo"
Columns: SSN, sales, Create_DT,Update_Dt
Table2: "Agent"
Columns: SSN,sales, Agent_Name, Create_Dt, Update_DT
Scenario 1 and desired result set:
I want output as 0 if the count of SSN in Demo table is matched with the count of SSN in Agent table
if the count is not matched then I want result as 1
Scenario 2 and desired result set:
I want output as 0 if the sum of sales in Demo table is matched with the sum of sales in Agent table
if the sum is not matched then I want result as 1
Please help on this query part
Thanks
You can write two queries separately to take counts within the result query
SELECT (SELECT count(Demo.SSN) as SSN1 from Demo)!=(SELECT count(Agent.SSN) as SSN2 from Agent) AS Result;
Basically what the inner queries does is it checked whether the counts are equal or not and outputs 1 if it is true and 0 if it is false. Since you have asked to output 1 if it is false I used '!=' sign.
You can try the same procedure in scenario 2 also
For scenario 1
select (Case when (select count(ssn) from Demo)=(select count(ssn) from Agent) then 0 else 1 end) as desired_result
If you want to count unique ssn then:
select (Case when (select count(distinct ssn) from Demo)=(select count(distinct ssn) from Agent) then 0 else 1 end) as desired_result
For scenario 2:
select (Case when (select sum(sales) from Demo)=(select sum(sales) from Agent) then 0 else 1 end) as desired_result
I would suggest one query with both sets of information:
select (d.num_ssn <> a.num_ssn) as have_different_ssn_count,
(d.sales <> a.sales) as have_different_sales
from (select count(distinct ssn) as num_ssn,
coalesce(sum(sales), 0) as sales
from demo
) d cross join
(select count(distinct ssn) as num_ssn,
coalesce(sum(sales), 0) as sales
from agent
) a;
Note: This returns boolean values -- true/false rather than 1/0. If you really want 0/1, then use case:
select (case when d.num_ssn <> a.num_ssn then 1 else 0 end) as have_different_ssn_count,
(case when d.sales <> a.sales then 1 else 0 end) as have_different_sales
It would not surprise me if you were not only interested in the total counts but also that the agent/sales combinations are the same in both tables. If that is the case, please ask a new question with a clear explanation. Sample data and desired results help.

Retrieving specific column data from SQL sub-query

My goal is to pull out the values shown in red in the picture.
I used this query to pull the data together. The rows are associated via ActionID and filtered with the 'AND' conditions shown.
SELECT [lclTime],[Descript],[Event],[PValue],[Phase],[ActionID]
FROM TableA
WHERE uniqueid = 5742
AND ActionID <> 0
AND LEN(Phase) = 0
AND [ActionID] in
(
SELECT [ActionID] FROM TableA
WHERE uniqueid = 5742
AND ActionID <> 0
AND LEN(Phase) = 0
GROUP BY [ActionID]
HAVING COUNT(*) > 2
)
I thought I could treat the output from above, as the input to another query, to allow me to choose the values I need to extract. But was unsuccesful.
I also thought that maybe PIVOT could be used to pull out the values in the columns... but I need values from multiple columns and couldn't get that syntax to even work.
Can this be done without temp tables? I don't think the 3rd party app, that will execute this code, plays nicely with temp tables.
Thanks in advance.
UPDATE: I should have clarified what I'm trying to achieve for a result set. For each set described by the subquery, I'm trying to extract a single record per unique ActionID. And the values with the ActionID would include the value of lclTime, Descript and Pvalue when Event=Successful Signoff, and the value of PValue when Event=State Command.
I suspect that you want conditional aggregation, to bring the values of events "State Command" and "Successful Signoff" for each actionid that satisfies the filter conditions and that have at least 3 occurences.
If so, you can use a window count for filtering, and then conditional aggregation to pivot:
select actionid,
max(case when event = 'State Command' then pvalue end) command_value,
max(case when event = 'State Command' then lcltime end) command_lcltime,
max(case when event = 'Successful Signoff' then pvalue end) signoff_value,
max(case when event = 'Successful Signoff' then lcltime end) signoff_lcltime,
from (
select a.*, count(*) over(partition by actionid) cnt
from tablea
where uniqueid = 5742 and actionid <> 0 and len(phase) = 0
) t
where cnt > 2
group by actionid

Showing sum of conditions as one row

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;

Want to perform calculation and do group by on a field in SQL server

I Have a table A. Select Field1,field2 from A , I want to perform a group by on count(field1) and group by field 2. But the count will be a calculation of certain condition on same table like
Select (Count(All records) - count(Records which are Rejected)/Count(All records))*100 as [Rate] from A
Group by Field1,field2
Result should be
Field1 Count
A1 (calculation mentioned above)
B1 (calculation mentioned above)
My favorite way to do this uses AVG() with a conditional expression:
select field1,
avg(case when status = 'Rejected' then 0.0 else 100.0 end) as Rate
from A
group by Field1;
The first condition is however you decide that a row is rejected. Also, note that field1 goes in the group by clause if that is what you want in the unaggregated column.
I guess you need something like this:
Select
Field1,
sum(case when status = 'R' then 0 else 1 end) / sum(1)*100.0 as [Rate]
from A
Group by
Field1
This will calculate the percentage of rows that don't have 'R' as status for each value of Field1.
Edit: To calculate the percentages with the number of the rows from whole table you can use a variable:
declare #rows int
select #rows = count(*) from A
Select
Field1,
(#rows - sum(case when status = 'R' then 1 else 0 end)) / #rows*100.0 as [Rate]
from A
Group by
Field1