Using UNION ALL to combine two queries into one table - sql

Trying to combine two queries that find the average value of column 'duration_minutes' broken down into two criteria (column 'member_casual' - for which there are only 2 options 'member' or 'casual'. I have been trying a the following syntax, which does display the data that I want, but in two rows, rather than two columns:
SELECT * FROM(
SELECT AVG(duration_minutes) as cas_avg
FROM `case-study-319921.2020_2021_Trip_Data.2020_2021_Rides_Merged`
WHERE member_casual = 'casual'
UNION ALL
SELECT AVG(duration_minutes) as mem_avg
FROM `case-study-319921.2020_2021_Trip_Data.2020_2021_Rides_Merged`
WHERE member_casual = 'member');
Resulting table:
Row
cas_avg
1
40.81073227046788
2
11.345919528176575
How would I combine those to queries so that the result from row 2 would instead display as a column with the header "mem_avg" (the alias that was given in the query)?

How would I combine those to queries so that the result from row 2 would instead display as a column with the header "mem_avg" (the alias that was given in the query)?
try below
SELECT
AVG(IF(member_casual = 'casual', duration_minutes, null) ) as cas_avg,
AVG(IF(member_casual = 'member', duration_minutes, null) ) as mem_avg,
FROM `case-study-319921.2020_2021_Trip_Data.2020_2021_Rides_Merged`
with output

You would use group by:
SELECT member_casual, AVG(duration_minutes) as cas_avg
FROM `case-study-319921.2020_2021_Trip_Data.2020_2021_Rides_Merged`
GROUP BY member_casual;
If there are more than two types, you may need to add:
member_casual in ('casual', 'member')

Related

Hive - Select distinct unique IDs per date without creating external tables or using JOINS

I am working on a data set which has the following columns :
unique_ID Date
a 2018_09_08
a 2018_09_18
a 2018_09_28
d 2018_09_08
I am looking to select those Unique_IDs which are occurring on all three dates i.e 2018_09_08, 2018_09_18 and 2018_09_28.
My output should be just 'a'.
There is a long solution to this problem - Extract unique_IDs per date and create external table on top of all three of them and then use join on three tables to get unique IDs for all three dates. I believe there should be a better solution as we have just 3 dates in this case which might rise later so I am looking for a more generalized solution.
Here is the query that I have written - select distinct(unique_ID) from table_name where Date = '2018_09_08' and Date = '2018_09_18' and Date = '2018_09_28' which is returning null.
I am also trying to write a sub-query but I doubt HIVE supports such sub queries in this case. Here is what I have written :
select count(distinct(unique_ID)) from (
(select distinct(unique_ID) from table_name where Date = '2018_09_08') a
union all
(select distinct(unique_ID) from table_name where Date = '2018_09_18') b
union all
(select distinct(unique_ID) from table_name where Date = '2018_09_28') c
);
and I am getting following parsing error : FAILED: ParseException line 3:0 missing ) at 'union' near ')' line 4:87 missing EOF at 'b' near ')'
How could we get the Unique_IDs in this case ?
This can be accomplished with group by and having.
select unique_id,count(distinct date)
from tbl
where date in ('2018_09_08','2018_09_18','2018_09_28')
group by id
having count(distinct date) = 3

single query to find difference in values in the three tables in oracle

Am having three similar tables
test_dev
test_qmg
test_prod
All the tables have same columns. i want single query to find difference in values in the three tables.
example:
select * from test_dev
minus
select * from test_qmg
minus
select * from test_prod
column names are same for all three tables. I want to find the difference in values in column.
select VALIDITY_DAYS_BEFORE_ENTRY,VALIDITY_DAYS_AFTER_ENTRY from visa_type_lk where visa_type_id=1 select VALIDITY_DAYS_BEFORE_ENTRY,VALIDITY_DAYS_AFTER_ENTRY from visa_type_lk_qmg where visa_type_id=1 select VALIDITY_DAYS_BEFORE_ENTRY,VALIDITY_DAYS_AFTER_ENTRY from visa_type_lk_prod where visa_type_id=1
here validity_days_before_entry,validity_days_before_entry column will change. i want to find that difference
I believe this is what you are looking for:
SELECT dev.visa_type_id,
(dev.VALIDITY_DAYS_BEFORE_ENTRY - qmg.VALIDITY_DAYS_BEFORE_ENTRY - prod.VALIDITY_DAYS_BEFORE_ENTRY) as difference_before,
(dev.VALIDITY_DAYS_AFTER_ENTRY - qmg.VALIDITY_DAYS_AFTER_ENTRY - prod.VALIDITY_DAYS_AFTER_ENTRY) as difference_after
FROM visa_type_lk dev INNER JOIN visa_type_lk_qmg qmg ON dev.visa_type_id = qmg.visa_type_id
INNER JOIN visa_type_lk_prod prod ON qmg.visa_type_id = prod.visa_type_id
WHERE dev.visa_type_id =1
Here's a link to an SQL fiddle to demonstrate: http://sqlfiddle.com/#!2/56e16/2
Are you sure this is really what you want to do though? I can't imagine how this data would be useful. By the way, all of these tables are in one database, right?
SELECT
MIN(environment_name) as environment_name,VISA_TYPE_ID,
VISA_TYPE_EN,
VISA_TYPE_AR,
VALIDITY_DAYS_BEFORE_ENTRY,
VALIDITY_DAYS_AFTER_ENTRY,
STAY_DAYS,
STAY_GRACE_DAYS,
EXTENSION1_DAYS,
EXTENSION1_GRACE_DAYS,
EXTENSION2_DAYS,
EXTENSION2_GRACE_DAYS,
IS_BORDER_VISA,
IS_MULTIPLE_ENTRY_VISA,
VIOLATION_GRACE_DAYS,
IS_ARCHIVED,
JOB_CLOSE_AFTER_DAYS,
IS_ALLOWED_FOR_ESTAB_QUOTA,
REPLACE_WITH_VISA_TYPE_ID
FROM
(
SELECT
'development' as environment_name, VISA_TYPE_ID,
VISA_TYPE_EN,
VISA_TYPE_AR,
VALIDITY_DAYS_BEFORE_ENTRY,
VALIDITY_DAYS_AFTER_ENTRY,
STAY_DAYS,
STAY_GRACE_DAYS,
EXTENSION1_DAYS,
EXTENSION1_GRACE_DAYS,
EXTENSION2_DAYS,
EXTENSION2_GRACE_DAYS,
IS_BORDER_VISA,
IS_MULTIPLE_ENTRY_VISA,
VIOLATION_GRACE_DAYS,
IS_ARCHIVED,
JOB_CLOSE_AFTER_DAYS,
IS_ALLOWED_FOR_ESTAB_QUOTA,
REPLACE_WITH_VISA_TYPE_ID
FROM visa_type_lk A
where visa_type_id in (select visa_type_id from visa_type_lk_prod)
UNION ALL
SELECT
'production' as environment_name, VISA_TYPE_ID,
VISA_TYPE_EN,
VISA_TYPE_AR,
VALIDITY_DAYS_BEFORE_ENTRY,
VALIDITY_DAYS_AFTER_ENTRY,
STAY_DAYS,
STAY_GRACE_DAYS,
EXTENSION1_DAYS,
EXTENSION1_GRACE_DAYS,
EXTENSION2_DAYS,
EXTENSION2_GRACE_DAYS,
IS_BORDER_VISA,
IS_MULTIPLE_ENTRY_VISA,
VIOLATION_GRACE_DAYS,
IS_ARCHIVED,
JOB_CLOSE_AFTER_DAYS,
IS_ALLOWED_FOR_ESTAB_QUOTA,
REPLACE_WITH_VISA_TYPE_ID
FROM visa_type_lk_prod B
)
tmp
GROUP BY VISA_TYPE_ID,
VISA_TYPE_EN,
VISA_TYPE_AR,
VALIDITY_DAYS_BEFORE_ENTRY,
VALIDITY_DAYS_AFTER_ENTRY,
STAY_DAYS,
STAY_GRACE_DAYS,
EXTENSION1_DAYS,
EXTENSION1_GRACE_DAYS,
EXTENSION2_DAYS,
EXTENSION2_GRACE_DAYS,
IS_BORDER_VISA,
IS_MULTIPLE_ENTRY_VISA,
VIOLATION_GRACE_DAYS,
IS_ARCHIVED,
JOB_CLOSE_AFTER_DAYS,
IS_ALLOWED_FOR_ESTAB_QUOTA,
REPLACE_WITH_VISA_TYPE_ID
HAVING COUNT(*) = 1
order by visa_type_id,environment_name

How to do an In Statement with a sub query returning 2 columns and one of the columns is a Count

I have this query:
SELECT *
FROM GUITARS.FENDER
WHERE FENDER.GUITARTYPE IN (
SELECT GUITARTYPE,Count(*)
FROM GUITARS.GUITAR_TYPE
WHERE GuitarColor = 'RED'
Group By GUITARTYPE
Having Count(*) = 1)
Basically I want to make sure I am only checking the Guitartypes that don't have duplicates with a count. The issue is the IN is only checking for 1 column, but i need the count(*)in there for instances of more than one guitar type. Is there a way to make this query work, or possible another way around doing the count.
You don't need to have the count() returned in the select statement, having the group by and the count() is sufficient.
SELECT *
FROM GUITARS.FENDER
WHERE FENDER.GUITARTYPE IN (
SELECT GUITARTYPE
FROM GUITARS.GUITAR_TYPE
WHERE GuitarColor = 'RED'
Group By GUITARTYPE
Having Count(*) = 1)
Adding the code so it looks right.

Narrowing sql query

I am trying to be more specific on my query as you can see (link to image below) from the cupID column there is groups A to H 3 times. All I am trying to do is have 3 queries, first query to output all groups A-H only once, second query from the second and third from the third if that makes sense?
This is the query
SELECT cupID, date, matchno,
clan1,
clan2,
si
FROM ws_bi2_cup_matches
WHERE ladID='0'
AND matchno = '6'
AND TYPE = 'gs'
GROUP BY clan1
ORDER BY cupID ASC
which shows: (take a look at picture)
http://s13.postimg.org/6rufgywcn/image.png
so query 1/2/3 should output separately like (a,b,c,d etc) instead of 1 query showing multiples (aaa,bbb,ccc,ddd etc)
Many thanks for help
Based on the assumption that you are performing a union all on your 3 queries, please add a dummy column viz SortOrder and order by on it.
In the following sample query (SQL Server), I assumed all 3 queries as same, please do change them accordingly with the dummy sortorder:
-- 1st query
SELECT cupID, date, matchno,
clan1,
clan2,
si,
1 as SortOrder -- dummy sort column
FROM ws_bi2_cup_matches
WHERE ladID='0'
AND matchno = '6'
AND TYPE = 'gs'
GROUP BY clan1
union all
-- 2nd query
SELECT cupID, date, matchno,
clan1,
clan2,
si,
2 as SortOrder -- dummy sort column
FROM ws_bi2_cup_matches
WHERE ladID='0'
AND matchno = '6'
AND TYPE = 'gs'
GROUP BY clan1
union all
-- 3rd query
SELECT cupID, date, matchno,
clan1,
clan2,
si,
3 as SortOrder -- dummy sort column
FROM ws_bi2_cup_matches
WHERE ladID='0'
AND matchno = '6'
AND TYPE = 'gs'
GROUP BY clan1
order by 7 -- dummy sort order column

Trouble with SQL UNION operation

I have the following table:
I am trying to create an SQL query that returns a table that returns three fields:
Year (ActionDate), Count of Built (actiontype = 12), Count of Lost (actiontype = a few different ones)
Bascially, ActionType is a lookup code. So, I'd get back something like:
YEAR CountofBuilt CountofLost
1905 30 18
1929 12 99
1940 60 1
etc....
I figured this would take two SELECT statements put together with a UNION.
I tried the following below but it only spits back two columns (year and countbuilt). My countLost field doesn't appear
My sql currently (MS Access):
SELECT tblHist.ActionDate, Count(tblHist.ActionDate) as countBuilt
FROM ...
WHERE ((tblHist.ActionType)=12)
GROUP BY tblHist.ActionDate
UNION
SELECT tblHist.ActionDate, Count(tblHist.ActionDate) as countLost
FROM ...
WHERE (((tblHist.ActionType)<>2) AND
((tblHist.ActionType)<>3))
GROUP BY tblHist.ActionDate;
Use:
SELECT h.actiondate,
SUM(IIF(h.actiontype = 12, 1, 0)) AS numBuilt,
SUM(IIF(h.actiontype NOT IN (2,3), 1, 0)) AS numLost
FROM tblHist h
GROUP BY h.actiondate
You should not use UNION for such queries. There are many ways to do what you want, for example
Updated to fit access syntax
SELECT tblHist.ActionDate,
COUNT(SWITCH(tblHist.ActionType = 12,1)) as countBuilt,
COUNT(SWITCH(tblHist.ActionType <>1 OR tblHist.ActionType <>2 OR ...,1)) as countLost
FROM ..
WHERE ....
GROUP BY tblHist.ActionDate