Union and Aggregation in SQL - sql

I have 3 SQL queries I want to combine them in one table using Union I tried different queries but none of them worked, What am I missing?
SELECT DISTINCT
place.type AS type,
COUNT(place.type)AS place,
0 AS msd,
0 AS county
FROM place
GROUP BY place.type;
SELECT DISTINCT
mcd.type,
0 AS place,
COUNT(mcd.type) AS msd,
0 AS county
FROM mcd
GROUP BY mcd.type;
SELECT DISTINCT
county.type,
0 AS place,
0 AS msd,
COUNT(county.type) AS county
FROM county
GROUP BY county.type;
So the final output would be the scheme (type,place,mcd,county) where type contains all different values for types from the 3 tables and place contains the number times the value of type appears in place table and the same for mcs and county.

You'd need an outer query to get the type values from the three queries combined.
Here's an example, using the UNION ALL set operator to combine the results of the three queries. That query is an inline view, referenced by an outer query that does a GROUP BY on the type column, and SUM aggregate on the COUNT/0 columns.
SELECT t.type
, SUM(t.place)
, SUM(t.msd)
, SUM(t.county)
FROM ( SELECT place.type AS type
, COUNT(place.type) AS place
, 0 AS msd
, 0 AS county
FROM place
GROUP BY place.type
UNION ALL
SELECT mcd.type
, 0 AS place
, COUNT(mcd.type) AS msd
, 0 AS county
FROM mcd
GROUP BY mcd.type
UNION ALL
SELECT county.type
, 0 AS place
, 0 AS msd
, COUNT(county.type) AS county
FROM county
GROUP BY county.type
) t
GROUP BY t.type
With the GROUP BY clause in each query, the DISTINCT keyword isn't needed.

It seems join 3 table together would be more suitable for your requirement.
SELECT coalese(place.type,mcd.type,conty.type) AS type,
COUNT(place.type)AS place,
COUNT(mcd.type) AS msd,
COUNT(county.type) AS county
FROM place
FULL OUTER JOIN mcd ON place.type = mcd.type
FULL OUTER JOIN county ON place.type = county.type
GROUP BY coalese(place.type,mcd.type,conty.type)

Related

Union & Pivot multiple tables with different values for similar records

I have multiple tables with the same customer records, but each table has it's own cost currency.
Table 1:
User Country COST_USD
1 USA 10
2 USA 5
3 USA 3
Table 2:
User Country COST_EUR
1 USA 12
2 USA 7
3 USA 5
Table 3:
User Country COST_YEN
1 USA 100
2 USA 50
3 USA 30
What I am looking for is to Union those tables and then pivot the currencies to individual columns (or pivot then union) as follows:
User Country COST_USD COST_EUR COST_YEN
1 USA 10 12 100
2 USA 5 7 50
3 USA 3 3 30
I have tried union all and then pivot but that didn't work since I have differnt currency columns.
Is this what you were thinking of-
select user, country, MAX(COST_USD) COST_USD,MAX(COST_EUR) COST_EUR,MAX(COST_YEN ) COST_YEN
from
(
select user, country, COST_USD, NULL AS COST_EUR, NULL AS COST_YEN from table1
union all
select user, country, NULL AS COST_USD, COST_EUR, NULL AS COST_YEN from table2
union all
select user, country, NULL AS COST_USD, NULL AS COST_EUR, COST_YEN from table3
)T
group by user, country
but if you have many currency columns then you ought to maybe unpivot and union all and the pivot back.
select user, country, currency, amount
from
(select user, country, cost_curr from tableN)U
unpivot
(amount for currency in (COST_EUR, COST_USD,COST_YEN, cost_curr))UPVT
The above is done for all tables and the all resulting unpivots are unioned all and the pivoted back.
As you can see this is quite tedious.
or change you design if possible.
As I mentioned in the comments, this is just a JOIN. Based on your sample data, an INNER JOIN:
SELECT T1.[User], --USER is a reserved keyword, and should not be used for object names
T1.Country,
T1.COST_USD,
T2.COST_EUR,
T3.COST_YEN
FROM dbo.Table1 T1
JOIN dbo.Table2 T2 ON T1.[User] = T2.[User] --USER is a reserved keyword, and should not be used for object names
AND T1.Country = T2.Country
JOIN dbo.Table3 T3 ON T1.[User] = T3.[User] --USER is a reserved keyword, and should not be used for object names
AND T1.Country = T3.Country;
Of course, I doubt all your table have a value for a specific user, so you likely want a FULL OUTER JOIN:
SELECT COALESCE(T1.[User],T2.[User],T3.[User]) AS [User], --USER is a reserved keyword, and should not be used for object names
COALESCE(T1.Country,T2.Country,T3.Country) AS Country,
T1.COST_USD,
T2.COST_EUR,
T3.COST_YEN
FROM dbo.Table1 T1
FULL OUTER JOIN dbo.Table2 T2 ON T1.[User] = T2.[User] --USER is a reserved keyword, and should not be used for object names
AND T1.Country = T2.Country
FULL OUTER JOIN dbo.Table3 T3 ON T3.[User] IN (T1.[User],T2.[User])--USER is a reserved keyword, and should not be used for object names
AND T3.Country IN (T1.Country,T2.Country);
But, like I mentioned, the real solution is fix your design, and have a single table that looks something like this:
CREATE TABLE dbo.YourTable (UserID int,
Country nvarchar(50),
Currency char(3),
Value decimal(12,0));
Then your data would look like this:
INSERT INTO dbo.YourTable
VALUES(1,'USA','USD',10),
(1,'USA','EUR',12),
(1,'USA','YEN',100);
And finally you would get your results with conditional aggregation:
SELECT UserID,
Country,
MAX(CASE Currency WHEN 'USD' THEN Value END) AS COST_USD,
MAX(CASE Currency WHEN 'EUR' THEN Value END) AS COST_EUR,
MAX(CASE Currency WHEN 'YEN' THEN Value END) AS COST_YEN
FROM dbo.YourTable
GROUP BY UserID,
Country;

How to join 2 queries with different number of records and columns in oracle sql?

I have three tables:
Employee_leave(EmployeeID,Time_Period,leave_type)
Employee(EID,Department,Designation)
leave_eligibility(Department,Designation, LeaveType, LeavesBalance).
I want to fetch the number of leaves availed by a particular employee in each LeaveTypes(Category) so I wrote following query Query1
SELECT LEAVE_TYPE, SUM(TIME_PERIOD)
FROM EMPLOYEE_LEAVE
WHERE EMPLOYEEID=78
GROUP BY LEAVE_TYPE
order by leave_type;
output for Query1
Leave_Type | SUM(Time_Period)
Casual 1
Paid 4
Sick 1
I want to fetch the number of leaves an employee is eligible for each leave_type(category). Following query Query2 gives the desire result.
Select UNIQUE Leavetype,LEAVEBALANCE
from LEAVE_ELIGIBILITY
INNER JOIN EMPLOYEE
ON LEAVE_ELIGIBILITY.DEPARTMENT= EMPLOYEE.DEPARTMENT
AND LEAVE_ELIGIBILITY.DESIGNATION= EMPLOYEE.DESIGNATION
WHERE EID=78
order by leavetype;
output for Query2
LeaveType | LeaveBalance
Casual 10
Paid 15
Privlage 6
Sick 20
Now I want to join these 2 queries Query1 and Query2 or create view which displays records from both queries. Also as you can see from output there are different no. of records from different queries. For a record which is not present in output of query1, it should display 0 in final output. Like in present case there is no record in output of query1 like privlage but it should display 0 in Sum(time_period) in Privlage of final output. I tried creating views of these 2 queries and then joining them, but I'm unable to run final query.
Code for View 1
create or replace view combo_table1 as
Select UNIQUE Leavetype,LEAVEBALANCE,EMPLOYEE.DEPARTMENT,EMPLOYEE.DESIGNATION, EID
from LEAVE_ELIGIBILITY
INNER JOIN EMPLOYEE
ON LEAVE_ELIGIBILITY.DEPARTMENT= EMPLOYEE.DEPARTMENT
AND LEAVE_ELIGIBILITY.DESIGNATION= EMPLOYEE.DESIGNATION
WHERE EID='78';
Code for View 2
create or replace view combo_table2 as
SELECT LEAVE_TYPE, SUM(TIME_PERIOD) AS Leave_Availed
FROM EMPLOYEE_LEAVE
WHERE EMPLOYEEID='78'
GROUP BY LEAVE_TYPE;
Code for joining 2 views
SELECT combo_table1.Leavetype, combo_table1.LEAVEBALANCE, combo_table2.leave_availed
FROM combo_table1 v1
INNER JOIN combo_table2 v2
ON v1.Leavetype = v2.LEAVE_TYPE;
But I'm getting "%s: invalid identifier" while executing the above query. Also I know I can't use union as it requires same column which here it is not.
I'm using Oracle 11g, so please answer accordingly.
Thanks in advance.
Desired final output
LeaveType | LeaveBalance | Sum(Time_period)
Casual 10 1
Paid 15 4
Privlage 6 0
Sick 20 1
To get the final desired output ...
"For a record which is not present in output of query1, it should display 0 in final output. "
... use an outer join to tie the taken leave records to the other tables. This will give zero time_duration for leave types which the employee has not taken.
select emp.Employee_ID
, le.leavetype
, le.leavebalance
, sum (el.Time_Duration) as total_Time_Duration
from employee emp
inner join leave_eligibility le
on le.department= emp.department
and le.designation= emp.designation
left outer join Employee_leave el
on el.EmployeeID = emp.Employee_ID
and el.leave_type = le.leavetype
group by emp.Employee_ID
, le.leavetype
, le.leavebalance
;
Your immediate problem:
I'm getting "%s: invalid identifier"
Your view has references to a column EID although none of your posted tables have a column of that name. Likewise there is confusion between Time_Duration and time_period.
More generally, you will find life considerably easier if you use the exact same name for common columns (i.e. consistently use either employee_id or employeeid, don't chop and change).
Try this examle:
with t as (
select 'Casual' as Leave_Type, 1 as Time_Period, 0 as LeaveBalance from dual
union all
select 'Paid', 4,0 from dual
union all
select 'Sick', 1,0 from dual),
t1 as (
select 'Casual' as Leave_Type, 0 as Time_Period, 10 as LeaveBalance from dual
union all
select 'Paid', 0, 15 from dual
union all
select 'Privlage', 0, 6 from dual
union all
select 'Sick', 0, 20 from dual)
select Leave_Type, sum(Time_Period), sum(LeaveBalance)
from(
select *
from t
UNION ALL
select * from t1
)
group by Leave_Type
Ok, edit:
create or replace view combo_table1 as
Select UNIQUE Leavetype, 0 AS Leave_Availed, LEAVEBALANCE
from LEAVE_ELIGIBILITY INNER JOIN EMPLOYEE ON LEAVE_ELIGIBILITY.DEPARTMENT= EMPLOYEE.DEPARTMENT AND LEAVE_ELIGIBILITY.DESIGNATION= EMPLOYEE.DESIGNATION
WHERE EID='78';
create or replace view combo_table2 as
SELECT LEAVE_TYPE as Leavetype, SUM(TIME_PERIOD) AS Leave_Availed, 0 as LEAVEBALANCE
FROM EMPLOYEE_LEAVE
WHERE EMPLOYEEID='78'
GROUP BY LEAVE_TYPE, LEAVEBALANCE;
SELECT Leavetype, sum(LEAVEBALANCE), sum(leave_availed)
FROM (
select *
from combo_table1
UNION ALL
select * from combo_table2
)
group by Leavetype;

return count 0 with mysql group by

database table like this
============================
= suburb_id | value
= 1 | 2
= 1 | 3
= 2 | 4
= 3 | 5
query is
SELECT COUNT(suburb_id) AS total, suburb_id
FROM suburbs
where suburb_id IN (1,2,3,4)
GROUP BY suburb_id
however, while I run this query, it doesn't give COUNT(suburb_id) = 0 when suburb_id = 0
because in suburbs table, there is no suburb_id 4, I want this query to return 0 for suburb_id = 4, like
============================
= total | suburb_id
= 2 | 1
= 1 | 2
= 1 | 3
= 0 | 4
A GROUP BY needs rows to work with, so if you have no rows for a certain category, you are not going to get the count. Think of the where clause as limiting down the source rows before they are grouped together. The where clause is not providing a list of categories to group by.
What you could do is write a query to select the categories (suburbs) then do the count in a subquery. (I'm not sure what MySQL's support for this is like)
Something like:
SELECT
s.suburb_id,
(select count(*) from suburb_data d where d.suburb_id = s.suburb_id) as total
FROM
suburb_table s
WHERE
s.suburb_id in (1,2,3,4)
(MSSQL, apologies)
This:
SELECT id, COUNT(suburb_id)
FROM (
SELECT 1 AS id
UNION ALL
SELECT 2 AS id
UNION ALL
SELECT 3 AS id
UNION ALL
SELECT 4 AS id
) ids
LEFT JOIN
suburbs s
ON s.suburb_id = ids.id
GROUP BY
id
or this:
SELECT id,
(
SELECT COUNT(*)
FROM suburb
WHERE suburb_id = id
)
FROM (
SELECT 1 AS id
UNION ALL
SELECT 2 AS id
UNION ALL
SELECT 3 AS id
UNION ALL
SELECT 4 AS id
) ids
This article compares performance of the two approaches:
Aggregates: subqueries vs. GROUP BY
, though it does not matter much in your case, as you are querying only 4 records.
Query:
select case
when total is null then 0
else total
end as total_with_zeroes,
suburb_id
from (SELECT COUNT(suburb_id) AS total, suburb_id
FROM suburbs
where suburb_id IN (1,2,3,4)
GROUP BY suburb_id) as dt
#geofftnz's solution works great if all conditions are simple like in this case. But I just had to solve a similar problem to generate a report where each column in the report is a different query. When you need to combine results from several select statements, then something like this might work.
You may have to programmatically create this query. Using left joins allows the query to return rows even if there are no matches to suburb_id with a given id. If your db supports it (which most do), you can use IFNULL to replace null with 0:
select IFNULL(a.count,0), IFNULL(b.count,0), IFNULL(c.count,0), IFNULL(d.count,0)
from (select count(suburb_id) as count from suburbs where id=1 group by suburb_id) a,
left join (select count(suburb_id) as count from suburbs where id=2 group by suburb_id) b on a.suburb_id=b.suburb_id
left join (select count(suburb_id) as count from suburbs where id=3 group by suburb_id) c on a.suburb_id=c.suburb_id
left join (select count(suburb_id) as count from suburbs where id=4 group by suburb_id) d on a.suburb_id=d.suburb_id;
The nice thing about this is that (if needed) each "left join" can use slightly different (possibly fairly complex) query.
Disclaimer: for large data sets, this type of query might have not perform very well (I don't write enough sql to know without investigating further), but at least it should give useful results ;-)

help in sql count

Suppose I have a table with 2 columns (status and date) like the following:
status: U T U U L
date: 12 14 15 16 17
Can I (using only 1 SQL statement) count the number of distinct values in the status? That is:
count(U)=3
count(T)=1
count(L)=2
count(P)=0
Can I do this with 1 SQL query?
Note: I have static values in status. I can only have (U-T-L-P)
You need to use Group By:
SELECT Status, Count(Status)
FROM table
GROUP BY Status
This will not return P = 0 if P is not populated in the table. In your application logic you will need to check and if a certain status is not returned, it means there are no entries (i.e. 0).
SQL cannot query records that are not there.
This will return a row for every status and the count in the second column:
SELECT Status, COUNT(*) Cnt
FROM Tbl
GROUP BY Status
So it would return
Status Cnt
U 3
T 1
L 1
for your example (in no defined order). Use ORDER BY if you want to sort the results.
You can do this with a query which groups on your status column, e.g.
SELECT COUNT(*) as StatusCount, Status
FROM MyTable
GROUP BY Status
To get the zero for the status P, you have to do some devious stuff using a table that lists all the possible statuses.
SELECT COUNT(A.Status), B.Status
FROM AnonymousTable AS A RIGHT OUTER JOIN
(SELECT 'P' AS Status FROM Dual
UNION
SELECT 'U' AS Status FROM Dual
UNION
SELECT 'L' AS Status FROM Dual
UNION
SELECT 'T' AS Status FROM Dual
) AS B ON A.Status = B.Status
GROUP BY B.Status;
The 4-way UNION is one way of generating a list of values; your DBMS may provide more compact alternatives. I'm assuming that the table Dual contains just one row (as found in Oracle).
The COUNT(A.Status) counts the number of non-null values in A.Status. The RIGHT OUTER JOIN lists the row from B with Status = 'P' and joins it with a single NULL for the A.Status, which the COUNT(A.Status) therefore counts as zero. If you used COUNT(*), you'd get a 1 for the count.

Select values in SQL that do not have other corresponding values except those that i search for

I have a table in my database:
Name | Element
1 2
1 3
4 2
4 3
4 5
I need to make a query that for a number of arguments will select the value of Name that has on the right side these and only these values.
E.g.:
arguments are 2 and 3, the query should return only 1 and not 4 (because 4 also has 5). For arguments 2,3,5 it should return 4.
My query looks like this:
SELECT name FROM aggregations WHERE (element=2 and name in (select name from aggregations where element=3))
What do i have to add to this query to make it not return 4?
A simple way to do it:
SELECT name
FROM aggregations
WHERE element IN (2,3)
GROUP BY name
HAVING COUNT(element) = 2
If you want to add more, you'll need to change both the IN (2,3) part and the HAVING part:
SELECT name
FROM aggregations
WHERE element IN (2,3,5)
GROUP BY name
HAVING COUNT(element) = 3
A more robust way would be to check for everything that isn't not in your set:
SELECT name
FROM aggregations
WHERE NOT EXISTS (
SELECT DISTINCT a.element
FROM aggregations a
WHERE a.element NOT IN (2,3,5)
AND a.name = aggregations.name
)
GROUP BY name
HAVING COUNT(element) = 3
It's not very efficient, though.
Create a temporary table, fill it with your values and query like this:
SELECT name
FROM (
SELECT DISTINCT name
FROM aggregations
) n
WHERE NOT EXISTS
(
SELECT 1
FROM (
SELECT element
FROM aggregations aii
WHERE aii.name = n.name
) ai
FULL OUTER JOIN
temptable tt
ON tt.element = ai.element
WHERE ai.element IS NULL OR tt.element IS NULL
)
This is more efficient than using COUNT(*), since it will stop checking a name as soon as it finds the first row that doesn't have a match (either in aggregations or in temptable)
This isn't tested, but usually I would do this with a query in my where clause for a small amount of data. Note that this is not efficient for large record counts.
SELECT ag1.Name FROM aggregations ag1
WHERE ag1.Element IN (2,3)
AND 0 = (select COUNT(ag2.Name)
FROM aggregatsions ag2
WHERE ag1.Name = ag2.Name
AND ag2.Element NOT IN (2,3)
)
GROUP BY ag1.name;
This says "Give me all of the names that have the elements I want, but have no records with elements I don't want"