SQL Query with multiple columns w/ different conditions - sql

How would I create a query that is querying 1 table and wanting to return multiple clauses but has different conditions for each column. I also need a group by clause at the end too. Something like below where my bracket is psuedocode:
Select date, sum(revenue) [where source = X], sum (revenue [where source = Y]
from table1
group by date
Is this possible or do I just need to do different queries

Use conditional aggregation:
select date,
sum(case when source = 'X' then revenue end) as x_revenue,
sum(case when source = 'Y' then revenue end) as y_revenue
from t
group by date;

Related

Hive SQL nested query use similar column

I have a query that includes two subqueries with similar column 'day'. I would like to show values in a following way:
day cnt1 cnt_total
But in a query I have it does not recognize that the day column is similar and makes a multiplication of all rows in nested statement one by all rows in nested statement two.
Is there a way to make it recognize that the day column is similar?
The query looks as follows:
SELECT p1.day, p1.count AS cnt1, p2.count AS cnt_total
FROM
(
SELECT day, COUNT(DISTINCT id) AS count FROM table
WHERE 1=1
AND service="service"
AND action="action"
AND path LIKE "%search%"
AND year="2021"
GROUP BY day
) p1,
(
SELECT day, COUNT(DISTINCT id) AS count FROM table
WHERE 1=1
AND service="service"
AND action="action"
AND year="2021"
GROUP BY day
) p2;
You should be able to do this with conditional aggregation, so only one SELECT is needed:
SELECT day,
COUNT(DISTINCT CASE WHEN action = 'mousedown' AND data["path"] LIKE '%go-to-latest-search%' THEN gsid END) AS count,
COUNT(DISTINCT CASE WHEN action = 'impress' THEN gsid END) as cnt_total
FROM hit
WHERE service = 'sauto' AND
year = '2021' AND
month = '07'
GROUP BY day

How to write SQL query without join?

Recently during an interview I was asked a question: if I have a table like as below:
The requirement is: how many orders and how many shipments per day (based on date column) - output needs to be like this:
I have written the following code, but interviewer ask me to write a SQL query without JOIN and UNION, achieve the same output.
SELECT
COALESCE(a.order_date, b.ship_date), orders, shipments
FROM
(SELECT
order_date, COUNT(1) AS orders
FROM
table
GROUP BY 1) a
FULL JOIN
(SELECT
ship_date, COUNT(1) AS shipments
FROM table) b ON a.order_date = b.ship_date
Is this possible? Could you guys please advice?
You can use UNION and GROUP BY with conditional aggregation as follows:
SELECT DATE_,
COUNT(CASE WHEN FLAG = 'ORDER' THEN 1 END) AS ORDERS,
COUNT(CASE WHEN FLAG = 'SHIP' THEN 1 END) AS SHIPMENTS
FROM (SELECT ORDER_DATE AS DATE_, 'ORDER' AS FLAG FROM YOUR_TABLE
UNION ALL
SELECT SHIP_DATE AS DATE_, 'SHIP' AS FLAG FROM YOUR_TABLE) T
In BigQuery, I would express this as:
select date, countif(n = 0) as orders, countif(n = 1) as numships
from t cross join
unnest(array[order_date, ship_date]) date with offset n
group by 1
order by date;
The advantage of this approach (over union all) is two-fold. First, it only scans the table once. More importantly, the unnest() is all on the same node where the data resides -- so data does not need to be moved for the unpivot.

Sum of multiple select count distinct with case function

I try to make a sum of multiple select count distinct with case function. For example:
SELECT id_dept,
count(DISTINCT case when e.statut='pub' then id_patients end) AS nb_patients_pub,
count(DISTINCT case when e.statut='priv' then id_patients end) AS nb_patients_priv
FROM venues
I would like to make of these two results in only one columns.
Is it possible?
I think that you want in:
SELECT
id_dept,
COUNT(DISTINCT CASE WHEN e.statut IN ('pub', 'priv') THEN id_patients END) AS nb_patients_pub_and_venues
FROM venues
GROUP BY id_dept
Note that I added a GROUP BY clause to the query, which was initially missing (this is a syntax error in almost all databases).
Depending on your data, this might not do exactly what you want; if a given id_patient has both statuses, then it will be counted only once, whereas your code counted it once in each count(distinct ...). If so, then you can just keep the two separated counts, and sum them:
SELECT
id_dept,
COUNT(DISTINCT CASE WHEN e.statut IN = 'pub' THEN id_patients END)
+ COUNT(DISTINCT CASE WHEN e.statut IN = 'priv' THEN id_patients END)
AS nb_patients_pub_and_venues
FROM venues
GROUP BY id_dept
If you're happy with current code, then either sum (using +) those counts, or use that query as a CTE (or an inline view) and
with test as
(SELECT id_dept,
count(DISTINCT case when e.statut='pub' then id_patients end)
AS nb_patients_pub,
count(DISTINCT case when e.statut='priv' then id_patients end)
AS nb_patients_priv
FROM venues
GROUP BY id_dept
)
select id, nb_patients_pub + nb_patients_priv as result
from test;

Join Two Count(*) Tables with No Relation in SQL Server

I am trying to combine the results of a count(*) statement and a count(*) with a where clause on a SQL Server Table into a single table.
I have a union statement that bring together the two queries one of top of another.
SELECT count(*) FROM [dbo].asma a
where [MLR] in ('y')) l
union
SELECT count (*) as 'Total' FROM [dbo].asma]
This post of solutions I looked at, but couldn't piece together a solution that would present these side by side. How would you do this?
What I need is this output:
You can do conditional aggregation instead :
select sum(case when MLR = 'y' then 1 else 0 end) as Active, count(*) as Total
from dbo.asma a;

unique count of the columns?

i want to get a unique count of the of multiple columns containing the similar or different data...i am using sql server 2005...for one column i am able to take the unique count... but to take a count of multiple columns at a time, what's the query ?
You can run the following selected, getting the data from a derived table:
select count(*) from (select distinct c1, c2, from t1) dt
To get the count of combined unique column values, use
SELECT COUNT(*) FROM TableName GROUP BY UniqueColumn1, UniqueColumn2
To get the unique counts of multiple individual columns, use
SELECT COUNT(DISTINCT Column1), COUNT(DISTINCT Column2)
FROM TableName
Your question is not clear what exactly you want to achieve.
I think what you're getting at is individual SUMS from two unique columns in one query. I was able to accomplish this be using
SELECT FiscalYear, SUM(Col1) AS Col1Total, SUM(Col2) AS Col2Total
FROM TableName
GROUP BY FiscalYear
If your data is not numerical in nature, you can use CASE statements
SELECT FiscalYear, SUM(CASE WHEN ColA = 'abc' THEN 1 ELSE 0 END) AS ColATotal,
SUM(CASE WHEN ColB = 'xyz' THEN 1 ELSE 0 END) AS ColBTotal
FROM TableName
GROUP BY FiscalYear
Hope this helps!