SQL query distinct with case function - sql

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

Related

Merge the same records that have the same id together in sql resultat:db2

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;

show information in column depending on whether it is an adult or a child SQL

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

SQL Server, Find duplicate rows base on a column

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.

Oracle SQL Query TO return rows from a table matching two conditions

I have a table "Employees":
E_ID E_Name E_Salary Grade
01 Hansen, Ola 15,000 HC_1
02 Svendson, Tove 15,000 HC_2
03 Svendson, Stephen 32,000 HC_9
04 Pettersen, Kari 21,000 HC_1
05 Sachin, Tendulkar 21,000 HC_2
06 Brian, Lara 19,000 HC_3
I need to return the Employees Salary that have both HC_1 and HC_2 grades. Can someone help me forming the query for this.
Thank you for your time.
I think you want ...
Select e_salary
from employees
where grade in ('HC_1','HC_2')
group by e_salary
having count(distinct grade) = 2

can any one please tell me how the flow of records took place in this query in accordace with the output generated

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