Union two tables with a table _name column - sql

I want to union two tables(Student1, Student2).
1 - Student1
| student_code | name |
--------------------------
| 1 | katia |
| 2 | roger |
| 3 | ken |
2 - Student2
| student_code | name |
--------------------------
| 3 | katia |
| 4 | roger |
| 5 | ken |
then I want get result like this.
result
|table_name| student_code | name |
-------------------------------------
|Student1 | 1 | katia |
|Student1 | 2 | roger |
|Student1 | 3 | ken |
|Student2 | 3 | katia |
|Student2 | 4 | roger |
|Student2 | 5 | ken |
I want to use only ANSI sql.

You could use
SELECT 'Student1' AS table_name, student_code, name FROM Student1
UNION ALL
SELECT 'Student2' AS table_name, student_code, name FROM Student2

select 'Student1' AS table_name,student_code,name from student1
union
select 'Student2' AS table_name,student_code,name from student2
I assume you know the difference betweenUNION and UNION ALL, union brings unique records, it is same as UNION PERFORMED ON SETS while union all will bring duplicate rows as well.
In your case, it will bring duplicates even with union because of the first column which differentiates the rows.

Use UNION ALL statement :
SELECT 'Student1' as table_name, student_code, name FROM Student1
UNION ALL
SELECT 'Student2' as table_name, student_code, name FROM Student2

Related

Add column to table with set values for every row expanding the table

I'm trying to add a column to a table in Redshift that adds multiple values to all the rows but has no relation, apart from all rows should be affected.
I'm not sure how to describe this so Google-ing is proving tough!
Visual example:
+----+-------+
| ID | Name |
+----+-------+
| 1 | James |
| 2 | Jane |
+----+-------+
Should become
+----+-------+-----+
| ID | Name | Sec |
+----+-------+-----+
| 1 | James | SG1 |
| 1 | James | SG2 |
| 1 | James | SG3 |
| 2 | Jane | SG1 |
| 2 | Jane | SG2 |
| 2 | Jane | SG3 |
+----+-------+-----+
Basically added "SG1, SG2, and SG3" to every row.
Thanks,
You can cross join a derived table that contains the three values:
select t.id, t.name, s.sec
from mytable t
cross join (select 'SG1' sec union all select 'SG2' union all select 'SG3') s
I am unsure whether Redshift supports values(), which would help shortening the syntax:
select t.id, t.name, s.sec
from mytable t
cross join (values ('SG1'), ('SG2'), ('SG3')) s(sec)

Query to fetch distinct rows with below requirement

Sample date in the table:
+--------+---------+---------+--------------+-----------+------------+---+
| School | Class | Student | Student desc | Section | Date | |
+--------+---------+---------+--------------+-----------+------------+---+
| ABC | Grade 2 | Stud 1 | AAA | Mango | 5/12/2015 | 1 |
| DEF | Grade 2 | Stud 1 | AAA | Mango | 12/25/2018 | |
| DEF | Grade 2 | Stud 1 | AAA | Orange | 9/8/2016 | |
| GHI | Grade 3 | Stud 2 | BBB | Apple | 12/28/2016 | 2 |
| JKL | Grade 3 | Stud 2 | BBB | Pear | 12/19/2016 | |
| ABC | Grade 2 | Stud 3 | CCC | Guava | 12/28/2016 | 3 |
| GHI | Grade 3 | Stud 4 | DDD | StarFruit | 9/8/2018 | 4 |
+--------+---------+---------+--------------+-----------+------------+---+
Ideally mapping should be 1 student is only get assigned to one section in a class.
I need to build the query to fetch the data to meet below requirement:-
Irrespective of the School need to show the distinct data for those students which get assigned to multiple sections within same class.
+--------+---------+---------+--------------+----------+------------+
| School | Class | Student | Student desc | Section | Date |
+--------+---------+---------+--------------+----------+------------+
| DEF | Grade 2 | Stud 1 | AAA | Mango | 12/25/2018 |
| DEF | Grade 2 | Stud 1 | AAA | Orange | 9/8/2016 |
| GHI | Grade 3 | Stud 2 | BBB | Apple | 12/28/2016 |
| JKL | Grade 3 | Stud 2 | BBB | Pear | 12/19/2016 |
+--------+---------+---------+--------------+----------+------------+
Below is the query that provides the correct data if school information is fetched:
select distinct a.class
,a.student
,a.Stud desc
,a.section
,to_date(max(a.date),'MM-DD-YYYY')"Date"
from Table1 a,
( select class
,student
,count(distinct section) cot
from Table1 c
where 1=1
and class is not null
and incoming_qty >= 1
group by class
,student
Having count(distinct section) > 1
) b
where 1=1
and a.class = b.class
and a.student=b.student
and b.cot > 1
and b.class is not null
and a.incoming_qty_new >= 1
group by a.class,a.student,a.Stud desc,a.section
order by a.class,a.student,a.Stud desc,a.section;
But query not working as per expectation while trying to fetch the school detail.
Please suggest.
Here is example of analytic functions usage for your data. Try to extend it for your specific case.
WITH t(School, Class, Student, StudentDesc, SectionName, Dates) AS
(
SELECT 'ABC','Grade 2','Stud 1','AAA','Mango',date'2015-05-12' FROM dual UNION ALL
SELECT 'DEF','Grade 2','Stud 1','AAA','Mango',date'2018-12-25' FROM dual UNION ALL
SELECT 'DEF','Grade 2','Stud 1','AAA','Orange',date'2016-09-08' FROM dual UNION ALL
SELECT 'GHI','Grade 3','Stud 2','BBB','Apple',date'2016-12-28' FROM dual UNION ALL
SELECT 'JKL','Grade 3','Stud 2','BBB','Pear',date'2016-12-19' FROM dual UNION ALL
SELECT 'ABC','Grade 2','Stud 3','CCC','Guava',date'2016-12-28' FROM dual UNION ALL
SELECT 'GHI','Grade 3','Stud 4','DDD','StarFruit',date'2018-09-08' FROM dual
)
SELECT *
FROM (
SELECT t.*,
COUNT(DISTINCT SectionName) OVER (PARTITION BY Class, Student) AS cntStudentSections,
ROW_NUMBER() OVER (PARTITION BY Class, Student ORDER BY Dates) AS StudentRowNumber
FROM t
)
WHERE cntStudentSections > 1 AND StudentRowNumber = 1;
You can use analytic functions:
select t1.*
from (select t1.*,
count(*) over (partition by class, student, section) as cnt
from table1 t1
) t1
where cnt >= 2;

SQL Join repeatedly (M:N)

How can I join one table with another table multiple times in SQL? Illustrative example:
Table "Couples":
+------+------+------+
| ID | ID_1 | ID_2 |
+------+------+------+
| 1 | 123 | 456 |
+------+------+------+
Table "Info":
+-----+-----------+-----------+--------+--------+
| ID | FirstName | LastName | Gender | Season |
+-----+-----------+-----------+--------+--------+
| 123 | Jon | Snow | Male | 6 |
| 456 | Daenerys | Targaryen | Female | 6 |
| 123 | Jon | Targaryen | Male | 7 |
+-----+-----------+-----------+--------+--------+
And now I need a combined result, that needs to be "up to date" (Info.Season must be the highest possible, but you cannot delete obsolete rows):
Desired result:
+-------------+------------+----------+-------------+------------+----------+
| FirstName_1 | LastName_1 | Gender_1 | FirstName_2 | LastName_2 | Gender_2 |
+-------------+------------+----------+-------------+------------+----------+
| Jon | Targaryen | Male | Daenerys | Targaryen | Female |
+-------------+------------+----------+-------------+------------+----------+
I have no clue how to solve the problem, that IDs are not unique and I need to join the Info table "multiple times".
You can select the most current state of a player by using an SQL Window Function to order each player by season. Then you need to join the information table to the couples table for each person in the couple.
actor_latest CTE
ID FIRSTNAME LASTNAME GENDER SEASON LAST_CHANGE
123 Jon Targaryen Male 7 1
123 Jon Snow Male 6 2
456 Daenerys Targaryen Female 6 1
Resulting SQL
with actor_latest (id, firstname,lastname,gender,season, last_change) as (
select
id
, firstname
, lastname
, gender
, season
, rank() over (partition by id order by season desc) as last_change
from info
)
select
left_partner.firstname as firstname_1
, left_partner.lastname as lastname_1
, left_partner.gender as gender_1
, left_partner.season as season_1
, right_partner.firstname as firstname_2
, right_partner.lastname as lastname_2
, right_partner.gender as gender_2
, right_partner.season as season_2
from
couples c
join actor_latest left_partner on c.id_1 = left_partner.id and left_partner.last_change=1
join actor_latest right_partner on c.id_2 = right_partner.id and left_partner.last_change=1
Results
FIRSTNAME_1 LASTNAME_1 GENDER_1 SEASON_1 FIRSTNAME_2 LASTNAME_2 GENDER_2 SEASON_2
Jon Targaryen Male 7 Daenerys Targaryen Female 6
SQL Fiddle

Joining multiple counts on the same table, from different columns?

I feel like this should be easy but it's late and I'm struggling.
Say (in an oracle 12 db) I have a table which represents which staff filled what roles in a bar, during different events, like this:
+----------+----------+-------+------------+----------+
| event_id | bar | doors | cloak_room | keg_room |
+----------+----------+-------+------------+----------+
| 2 | bob | bill | john | mary |
+----------+----------+-------+------------+----------+
| 3 | bob | bill | mary | kev |
+----------+----------+-------+------------+----------+
| 4 | bob | john | louise | mary |
+----------+----------+-------+------------+----------+
| 5 | kyle | kev | sarah | louise |
+----------+----------+-------+------------+----------+
| 6 | jennifer | bob | jay | john |
+----------+----------+-------+------------+----------+
| 7 | john | bill | mary | steve |
+----------+----------+-------+------------+----------+
and I want to get a count of, overall, how many events each staff member worked, like this:
+-------+--------+
| count | person |
+-------+--------+
| 4 | bob |
+-------+--------+
| 4 | john |
+-------+--------+
| 3 | bill |
+-------+--------+
| 3 | mary |
+-------+--------+
| 2 | kev |
+-------+--------+
| 2 | louise |
+-------+--------+
| 1 | jay |
+-------+--------+
| 1 | steve |
+-------+--------+
We see here that bob has a count of 4 - because he is associated with 4 distinct event_id: 3 as a barman, and 1 as a doorman.
(assuming no two staff members have the same name, and no one can work two jobs at once)
How do I do this?
for one 'role' it's clear:
select count(event_id), bar group by bar
but is there an elegant way to do this for all columns - without full joins and string concat?
thanks!
You should change the structure of your data, so you have one row per event/person/role. Then you could just use aggregation.
You can do that in a query as well:
select who, count(*)
from (select event_id, 'bar' as job, bar as who from t union all
select event_id, 'doors' as job, doors as who from t union all
select event_id, 'cloak_room' as job, cloak_room as who from t union all
select event_id, 'keg_room' as job, keg_room as who from t
) jw
group by who;
If someone could have multiple jobs in one event, then use count(distinct event_id).
EDIT:
I see you are using Oracle 12c. Then use a lateral join/cross apply:
select who, count(*)
from t cross apply
(select t.event_id, 'bar' as job, t.bar as who from dual union all
select t.event_id, 'doors' as job, t.doors as who from dual from dual union all
select event_id, 'cloak_room' as job, cloak_room as who from dual union all
select t.event_id, 'keg_room' as job, t.keg_room as who from dual
) jw
group by who;
You may count by string columns in the nested inner query and then sum them up outside with your desired order :
SELECT sum(count) count, person
FROM
(
SELECT count(event_id) count, bar person FROM mytable GROUP BY bar UNION ALL
--> P.S. Only aliasing as "person" is enough in this upper "select" for all
--> four "select" statements inside the parentheses.
SELECT count(event_id) , doors FROM mytable GROUP BY doors UNION ALL
SELECT count(event_id) , cloak_room FROM mytable GROUP BY cloak_room UNION ALL
SELECT count(event_id) , keg_room FROM mytable GROUP BY keg_room
)
GROUP BY person
ORDER BY 1 desc, 2;
COUNT PERSON
4 bob
4 john
3 bill
3 mary
2 kev
2 louise
1 jay
1 jennifer
1 kyle
1 mary2
1 sarah
1 steve
SQL Fiddle Demo

SQL select flag based on count and/or flag of joined table

I have a Customer table and an Address table.
The Address table has a flag which is either INVOICE, CORRESPONDENCE or DELIVERY.
A Customer can have 0 to many Address records.
I want to be able to query both tables and generate a flag for each customer based on the address data - no address records = NONE, 1 or more INVOICE records = HASINVOICE, no INVOICE but 1 or more others = HASOTHER
so, for the following data:
+------------+---------+
| CustomerID | Name |
+------------+---------+
| 1 | Peter |
| 2 | Ray |
| 3 | Egon |
| 4 | Winston |
| 5 | Dana |
+------------+---------+
+-----------+------------+----------------+
| AddressID | CustomerID | AddressType |
+-----------+------------+----------------+
| 1 | 1 | INVOICE |
| 2 | 1 | DELIVERY |
| 3 | 2 | DELIVERY |
| 4 | 2 | CORRESPONDENCE |
| 5 | 4 | INVOICE |
| 6 | 5 | CORRESPONDENCE |
+-----------+------------+----------------+
I would expect the following output:
+------------+---------+-------------+
| CustomerID | Name | AddressFlag |
+------------+---------+-------------+
| 1 | Peter | HASINVOICE |
| 2 | Ray | HASOTHER |
| 3 | Egon | NONE |
| 4 | Winston | HASINVOICE |
| 5 | Dana | HASOTHER |
+------------+---------+-------------+
Is this possible, for SQL 2000, using a single query and no cursors?
I don't have a 2000 instance handy (you really should upgrade, you're 4-5 releases behind), but I think that this should work:
declare #Customers table (CustomerID int,Name varchar(10))
insert into #Customers (CustomerID,Name)
select 1,'Peter' union all select 2,'Ray' union all
select 3,'Egon' union all select 4,'Winston' union all
select 5,'Dana'
declare #Addresses table (AddressID int, CustomerID int,
AddressType varchar(30))
insert into #Addresses (AddressID,CustomerID,AddressType)
select 1,1,'INVOICE' union all select 2,1,'DELIVERY' union all
select 3,2,'DELIVERY' union all select 4,2,'CORRESPONDENCE' union all
select 5,4,'INVOICE' union all select 6,5,'CORRESPONDENCE'
select
c.CustomerID,
c.Name,
CASE MAX(CASE
WHEN a.AddressType = 'Invoice' THEN 2
WHEN a.AddressType IS NOT NULL THEN 1
END
) WHEN 2 THEN 'HASINVOICE'
WHEN 1 THEN 'HASOTHER'
ELSE 'NONE'
END as AddressFlag
from
#Customers c
left join
#Addresses a
on
c.CustomerID = a.CustomerID
group by
c.CustomerID,
c.Name
Produces:
CustomerID Name AddressFlag
----------- ---------- -----------
5 Dana HASOTHER
3 Egon NONE
1 Peter HASINVOICE
2 Ray HASOTHER
4 Winston HASINVOICE