Presto Pivoting Data - sql

I am really new to Presto and having trouble pivoting data in it.
The method I am using is the following:
select
distinct location_id,
case when role_group = 'IT' then employee_number end as IT_emp_num,
case when role_group = 'SC' then employee_number end as SC_emp_num,
case when role_group = 'HR' then employee_number end as HR_emp_num
from table
where 1=1
and id = 1234
This is fine, however, null values are also populated for the rows and I would like to pivot the data, to only return one row with the relevant info.
I have tried using the array_agg function, which will collapse the data but it also keeps the null values (e.g. it will return null,301166,null for the first colum)

If only one row per location is needed you can use max with group by:
select location_id,
max(IT_emp_num) IT_emp_num,
max(SC_emp_num) SC_emp_num,
max(HR_emp_num) HR_emp_num
from (
select location_id,
case when role_group = 'IT' then employee_number end as IT_emp_num,
case when role_group = 'SC' then employee_number end as SC_emp_num,
case when role_group = 'HR' then employee_number end as HR_emp_num
from table
where id = 1234)
group by location_id

Related

SQL query to return rows with one distinct field and use CASE to create new evaluation column

I want to write an SQL query to return rows with one distinct field and use CASE to create new evaluation column. Any help is appreciated. Deets below:
table
id
status
category
string
string
bigint
--------
--------
----------
pseudo query:
return (distinct id), time_created, NEW_COL
where category is 123123
and where new_col //create new col with these values
(
if status = 'good' then 'GOOD'
if status = 'bad' then 'BAD'
)
FROM table
result:
id
time_created
new_col
1
Jun-1
BAD
2
Jul-21
GOOD
3
Jun-12
GOOD
4
Aug-1
GOOD
--- I keep getting a lint error right on my CASE keyword:
"expecting " '%', '*', '+', '-', '.', '/', 'AT', '[', '||',""
one of queries I tried:
SELECT
ID, time_created
CASE
WHEN status = 'good' THEN 'GOOD'
WHEN status = 'bad' THEN 'BAD'
END
as STATUS_new
FROM TBL
WHERE CATEGORY = '871654671'
ORDER BY time_created
You just have a small syntax error (and bad column name in your sql fiddle). You just need a comma after the time created column.
SELECT
ID, time as time_created,
CASE
WHEN status = 'good' THEN 'GOOD'
WHEN status = 'bad' THEN 'BAD'
END
as STATUS_new
FROM TBL
WHERE CATEGORY = '871654671'
ORDER BY time_created
Here is the working query:
http://www.sqlfiddle.com/#!18/7293b5/11
SELECT
ID, TIME, 'STATUS_new' =
CASE STATUS
WHEN 'good' THEN 'GOOD'
WHEN 'bad' THEN 'BAD'
END
FROM TBL
WHERE CATEGORY = '871654671'
ORDER BY TIME
you must put the new name of the column before the CASE
the column you are defining the CASE must be defined directly behind the case and all the WHEN conditions are directly related to it.
in your fiddle you used the wrong column name of your TIME column

How to create a new column based on data out from a query

I have a quick question.
ATtaching the SS for reference.
How can i set a new column 'Status' as NO based the nvl condition if the id is null then i have to map to the corresponding of another table .
You can do that using case statement.
select
nvl(b.id,a.id) as id
,b.name
,case when nvl(b.id,a.id) is null then 'No' else 'Yes' End as Status
from dd b,
(select id, name from demo group by id, name)a
where a.id=b.id(+)

Oracle SQL Joining 2 Queries

I am trying to form a query using 2 other queries, but I don't know how I would do it exactly. I am using Oracle SQL. Here is the basic logic:
if (select supervisor_id from PS_EMPLOYEES where EMPLID = %This.sEMPLID)
is in
(select EMPLID from sysadm.PS_Z_RPT_TO_REL where Z_DIRECT_RPT = 'D' where manager_id = %EmployeeID)
return True else return False.
Basically the first query will have an ID and will search through a list formed from the second query. If the ID from the first query is in the list from the second query then return 'True' else return 'False' Any thoughts?
Something like the following perhaps (untested):
SELECT EXISTS (
SELECT supervisor_id
FROM PS_EMPLOYEES
WHERE EMPLID = %This.sEMPLID
AND supervisor_id IN (
SELECT EMPLID
FROM sysadm.PS_Z_RPT_TO_REL
WHERE Z_DIRECT_RPT = 'D'
AND manager_id LIKE %EmployeeID
)
)

Select an ID if count is equal to 1

I am trying to write a query which needs to find an ID number from 3 WHERE values based on the result only being equal to 1.
So say i want to find a patient's ID and my where clause matches the firstname, lastname and DOB. If there are 2 results because of duplicates, i need the output to be NIL else it should return the patient ID.
if(select count(*)
from patient
where last_name = 'JAMES'
and first_name = 'JONES'
and birth_DtTM = '1980-01-01') > 1
print 'NULL' else return Pat_ID1
This is kind of what i am leading towards.
Thanks guys
select case when count(*)> 1
then 'NULL' else Pat_ID1 end
from patient
where last_name = 'JAMES'
and first_name = 'JONES'
and birth_DtTM = '1980-01-01'
group by Pat_ID1
try below.
;WITH CTE(Pat_ID1,last_name,first_name,birth_DtTM,dup_rows)
as
(
SELECT Pat_ID1,last_name,first_name,birth_DtTM,ROW_NUMBER() OVER(PARTITION BY last_name,first_name,birth_DtTM ORDER BY Pat_ID1) AS dup_rows FROM patient
)
SELECT
case when dup_rows>1 then null
when dup_rows=1 then Pat_ID1
end
FROM CTE
You can do it like this:
SELECT
PatientID = CASE COUNT(*) WHEN 1 THEN MAX(Pat_ID1) END
FROM
patient
WHERE
last_name = 'JAMES'
AND first_name = 'JONES'
AND birth_DtTM = '1980-01-01'
;
The CASE expression will evaluate either to the single Pat_ID1 matching the request or to NULL (if COUNT(*) is anything but 1).
As you can see, the Pat_ID1 value is obtained with the help of an aggregate function (by the way, you can use MIN instead of MAX just as well). This is because the presence of COUNT(*) in the query automatically implies grouping and now, if you want to reference columns of the underlying row set, you must only access their aggregated values.

case statement in select query in sql

I have a stored procedure to load the data from one table to another table.
i need to set the column value of the destination table based on the two values of the select statement, some thing like the below example.
insert into table table_name
( value1, value 2,value 3)
select (value 1,value2 ,
case value3
when value1 = 'somevalue' &&* value2 = 'somevalue'
then 'x'
else 'y'
End
from table_name.
can any one help me to find out how to update the a column in based on the two previous column values in the same select query?
i have tried with the below sample example to understand but it was failed to parse.
INSERT INTO HumanResources.departmentcopy
( DepartmentID,GroupName,Name,temp)
SELECT DepartmentID,GroupName,Name,
CASE temp
WHEN DepartmentID = 1 && Name = 'Engineering and Research'
THEN 'sucessful'
ELSE 'unsucessful'
END
FROM HumanResources.department
Help me on this!!
thanks,
Venkat
You were very close:
INSERT INTO HumanResources.departmentcopy(DepartmentID, GroupName, Name, temp)
SELECT DepartmentID,
GroupName,
Name,
CASE WHEN DepartmentID = 1 AND Name = 'Engineering and Research'
THEN 'sucessful' ELSE 'unsucessful' END
FROM HumanResources.department
&& is not valid in SQL. Use AND to append a condition.
INSERT INTO HumanResources.departmentcopy( DepartmentID,GroupName,Name,temp)
SELECT DepartmentID,
GroupName,
Name,
CASE
WHEN DepartmentID = 1 AND Name = 'Engineering and Research' THEN 'sucessful'
ELSE 'unsucessful'
END
FROM HumanResources.department
INSERT INTO HumanResources.department(DepartmentID, GroupName, Name, temp)
SELECT DepartmentID,
GroupName,
Name,
CASE WHEN DepartmentID = 1 AND Name = 'Engineering and Research'
THEN 'sucessful' ELSE 'unsucessful' END
FROM HumanResources.department