Say if I have a datatable as follow:
ID Name Birthday
------ -------- ------------
01 Eric 06/20
02 Judy 03/15
03 Mary 04/01
04 John 03/15
05 Judy 06/20
06 John 09/11
How can I get the result base on Name:
ID Name Birthday
------ -------- ------------
02 Judy 03/15
04 John 03/15
05 Judy 06/20
06 John 09/11
Because both Judy and John appeared more than once (duplicates).
Base on Birthday:
ID Name Birthday
------ -------- ------------
01 Eric 06/20
02 Judy 03/15
04 John 03/15
05 Judy 06/20
Because both 06/20 and 03/15 appeared more than once!
You should look up WHERE, GROUP BY clauses as well as INNER queries and JOINs in SQL server.
Here are the link to MSDN documentation
https://learn.microsoft.com/en-us/sql/t-sql/queries/where-transact-sql
https://learn.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql?view=sql-server-2017
Using GROUP BY and WHERE your query for case 1 should be:
Select * from Tbl where Name in
(
Select Name
from Tbl
Group by Name
HAVING COUNT(1)>1
)
Similarly second result should be like:
Select * from Tbl where Birthday in
(
Select Birthday
from Tbl
Group by Birthday
HAVING COUNT(1)>1
)
based on Name
select * from myTable where name in(
select Name from myTable group by Name having count(*)>1)
you can make small change on that for birtday
this is the SQL Fiddle link where you can change the table Name or field data type based on your real DB
SQL Fiddle Link
Why not use exists ?
select *
from table t
where exists (select 1
from table
where Name = t.name
having count(1) > 1
);
In order to get date based on Birthday, use Birthday column in inner query.
Related
Assuming I have a table containing the following information:
ID NAME Boss Main responsibility
01 Tommy x3 Yes
02 Elis x2 Yes
02 Elis x3 No
03 John x65 yes
04 Lille x50 yes
is there a way I can perform a select on the table to get the following(sql :DB2)
ID NAME main responsibility
01 Tommy X3
02 Elis X2(main responsibility) AND X3
03 John X65
04 Lille x50
Thanks
If your version of DB2 support it, you may aggregate and use the LISTAGG() function:
SELECT
ID,
NAME,
LISTAGG(CONCAT(Boss, CASE WHEN main = 'Yes' THEN ' (main)' ELSE '' END), ', ')
WITHIN GROUP(ORDER BY main DESC) AS main
FROM yourTable
GROUP BY ID, NAME
ORDER BY ID;
I have two tables in my SQL database:
Table 1: Person:
Application
NoPerson
Name
Lastname
Salary
DateofBirth
01
01
name1
lastname1
2000
1990-03-03
01
02
name2
lastname2
NULL
2010-01-01
02
01
name1
lastname1
2000
1993-03-03
02
02
name2
lastname2
NULL
2012-01-01
Table 2 : EducationInfo:
Application
NoPerson
schoolgrade
school
goals
01
02
8. grade
St. Luis
Be Engineer
02
02
3. grade
norcross
Be Professor
I need to make a query that returns something like this: If the person is an adult, then show in column occupation his salary. And if it is a child, then show in same column the school grade.
Like this:
Try to use something like this:
select ...., coalesce(p.Salary, e.schoolgrade) as ocupation
from Person p
left join EducationInfo e on e.Aplication=p.Aplication and e.NoPerson=p.NoPerson
We join two tables and then show in ocupation column value Salary or schoolgrade depending on Salary is null or not.
thaks for help, i does this finally
Select *,
CASE WHEN
(P.Salary=0)THEN e.schoolgrade
ELSE CAST(p.Salary AS VARCHAR)
END AS Ocupation
FROM v_APP_Person AS P
LEFT JOIN v_APP_Education AS e ON p.Aplication= e.Aplication and p.NoPerson=e.NoPerson
I have the following data:
ID Owner
--------
01 Sarah
01 Andrew
02 Sarah
03 Andrew
04 Andrew
04 Sarah
05 Andrew
And I want to get to the following output:
ID Grouped_Owners
---------
01 Andrew and Sarah
02 Sarah
03 Andrew
04 Andrew and Sarah
05 Andrew
And I've tried this query (and other iterations of similar approaches):
select
distinct(id)
, case
when count(id) > 1 then 'Andrew and Sarah'
else owner
end as Grouped_Owners
from Ads
group by id, Owner
order by id
... but I can't get to the results. I'm suspecting that I might need to use windowing/partition function, but that's seems overkill. I've searched around the interwebs, but I can't seem to find anything that fits this use case :/
I'm sure this will be a very quick answer for the gurus here! :) Thanks!
You need to use GROUP_CONCAT along with GROUP BY:
SELECT
ID,
GROUP_CONCAT(Owner SEPARATOR ' and ') AS Grouped_Owners
FROM Ads
GROUP BY ID
ORDER BY ID
Check Demo
I have four tables
Pay,
Location,
deparment,
job
each one of them has 3 colums
EmpId, Code, Date.
some records in each table are unique to that table, whereas some records exists in multiple tables.
i want all the data together in one table without duplicates and also there should be a new column having name of the tables that the record exists in
So our final data will be something like
#Sr.no------------EmpID----------Code------Date----------Tables in which the record exists
1.--------------- E001 --------- C1 ------ 1 Feb 2014 ---------------- Pay, Department
2.--------------- E002 --------- C2 ------ 2 Jan 2014 ---------------- Location
3.--------------- E003 --------- C3 ------ 3 Mar 2014 ---------------- Job
4.--------------- E004 --------- C4 ------ 4 Jan 2014 ---------------- Location, Pay
5.--------------- E005 --------- C5 ------ 6 Mar 2014 ---------------- Dept, Job
6.--------------- E006 --------- C6 ------ 3 Feb 2014 ---------------- Pay, Job, Location
7.--------------- E007 --------- C7 ------ 2 Aug 2014 ---------------- Pay, Dept, Job, Loc
8.--------------- E008 --------- C8 ----- 19 sep 2014 ---------------- Department
9.--------------- E009 --------- C9 ----- 22 dec 2014 ---------------- Pay, Dept, Job, Loc
Use Union all to combine the results of different tables and use a additional column to differentiate the rows.
After that use ListAgg function to group the duplicate rows into comma separated values.Try this.
SELECT EmpId,Code,Date,Listagg(Tablename,',') within GROUP (ORDER BY empid) AS Tablename
FROM (SELECT EmpId,Code,Date,'Pay' AS Tablename
FROM Pay
UNION ALL
SELECT EmpId,Code,Date,'Location' AS Tablename
FROM Location
UNION ALL
SELECT EmpId,Code,Date,'deparment' AS Tablename
FROM deparment
UNION ALL
SELECT EmpId,Code,Date,'job' AS Tablename
FROM job) A
GROUP BY EmpId,Code,Date
ListAgg Function referred from this answer
SQL> select * from employee;
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
03 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
03 Alison Mathews 21-MAR-76 21-FEB-86 13323.56 Vancouver Tester
04 James Smith 12-DEC-78 15-MAR-90 52358.24 Vancouver Tester
05 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
03 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
03 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
6 rows selected.
SQL> select id,first_name
2 from employee
3 group by id, first_name;
ID FIRST_NAME
---- ----------
03 Linda
03 Jason
04 James
05 Celia
03 David
03 Alison
I want to know the flow of records ...
In what manner these records are displayed ...
Please tell me how groups are created here ...
I'm not sure I understand the question correctly, but I'll attempt an answer.
You second query is this:
select id,first_name
from employee
group by id,first_name;
This means, find all employees and group them together based on the id and the first_name. Then return these results in any order whatsoever.
The order that the results come out depends internally how Oracle decided to execute the query (hash group by or sort group by) and perhaps where the data was physically stored on disk. It could change from day to day, Oracle version to Oracle version etc.
If you want them to come out in a particular order, you must use ORDER BY