Query on how to select data based on single condition - sql

Need your help to review the below request.
i am using the below query and what i want to achieve is as below
i am trying to extract the min value of the new_status_id and not fetch any other details of any other status at all. Basically i am getting more than one line as output for one v_id that i input where as the desired output is one line and min(timestamp) to get fetched under column name Upload as defined below
Select
to_char(to_date(date,'DY Mon DD YYYY'),'MM-DD')||' '||Away_team.name||' '||'#'||' '||home_team.name as "Title",
video_jobs.id as "Job Id",
markets.name as "Market",
round as "Round",
to_char(to_date(date,'DY Mon DD YYYY'),'DD-Mon-YY') as "Date Aired",
competitions.name as "Competition",
Home_team.name as "Home Team",
Away_team.name as "Away Team",
job_statuses.name as "Job Status",
networks.name as "Network",
stadiums.name as "Stadium",
Round(images_count/3600.0,2) as "Footage Hours",
round(sum(minutes_in_logo_finder)/60.0,2) as "Logo Picker Time",
Round(sum(minutes_in_hits_editor)/60.0,2) as "Hits Editing Time",
Round(sum(minutes_in_branding)/60.0,2) as "Branding Time",
Round(sum(minutes_in_tagging)/60.0,2) as "Tagging Time",
Round(sum(minutes_in_qc)/60.0,2)as "QC Time",
Round(sum(minutes_in_idle)/60.0,2) as "Idle Time",
Round(sum(loading_time)/60.0,2) as "Loading Time",
Round(sum(minutes_in_logo_finder+minutes_in_hits_editor + minutes_in_branding +minutes_in_tagging +minutes_in_qc+minutes_in_idle+loading_time)/60.0,2) as "Total"
from video_jobs
Join markets on video_jobs.market_id = markets.id
Join stadiums on video_jobs.stadium_id = Stadiums.id
Join job_statuses on video_jobs.status_id = job_statuses.id
Join competitions on video_jobs.competition_id = competitions.id
Join networks on video_jobs.network_id = networks.id
Join sessions on video_jobs.id = sessions.job_id
join teams as Home_team on video_jobs.home_team_id = home_team.id
join teams as Away_team on video_jobs.away_team_id = Away_team.id
Join video_job_status_history on video_jobs.id = video_job_status_history.video_job_id
Where video_jobs.id > 163026
group by
video_jobs.id,
markets.name,
video_jobs.round,
stadiums.name,
job_statuses.name,
competitions.name,
networks.name,
video_jobs.images_count,
home_team.name,
Away_team.name,
video_jobs.date
order by
video_jobs.id

Add the condition into the where clause:
select min(timestamp) as "Uplaod"
from v_jobs
where v_id = 1234 and new_status_id = 25;
EDIT:
In that case, what you want is conditional aggregation:
select min(case when new_status_id = 25 then timestamp end) as MinTimestamp25
. . .

Related

How do I select record with no pair?

Hi guys I hope you can help me, How do I pair the same record but also I want to select the record that has no pair like the 95 in the credit.
Here is my code I can actually pair the same record on Debit and Credit but also I want to show the record that has no pair.
select
A."ShortName" as "BP Code",
A."LineMemo" as "Remarks",
A."Debit",
A."Credit",
B."LineMemo" as "Remarks",
B."Debit",
B."Credit"
from JDT1 A
inner join
(
select
"ShortName",
"LineMemo",
"Debit",
"Credit"
from "JDT1" where "TransType" = '18' /*A/P Invoices*/
) B on A."Debit" = B."Credit" and A."ShortName" = B."ShortName"
Where "TransType" = '46' /*Outgoing Payments*/
and A."ShortName" = 'D-0003'
and A."RefDate" between '2020-01-01' and '2020-12-31'
Result:

Multiple Sub Queries with Multiple JOINS Oracle

I have a large ORACLE SQL file with multi joins and multiple sub queries. I am having issues joining two of the sub queries. I have tried many different methods but the closest that I have gotten is below. The piece that is giving me trouble is the first portion of the join with the multiple subqueries (the Address Eff Date and the Employee Eff Date. The error message that I am receiving is ORA-00907: missing right parenthesis 00907. 00000 - "missing right parenthesis". I can't find where the right parenthesis is missing and I suspect it is more than that or some other type of syntax issue. The two tables that I have had trouble joinging are the EMPLOYEE_EFF_DATE table and the ADDRESS_EFF_DATE table. Both of these tables have an EFF_DATE field, and for both tables I need to pull the record with the most recent effective date (by SSN field). I believe I have accounted for that with the ROW_NUMBER() OVER (PARTITION method but if there is a more efficient or easier way to to this I am absolutely open to suggestions.
/*Subscriber Code*/
eed.P_COMPANY_ID_I as "Client Company ID"
,cs.REAL_SSN as "Employee SSN"
,cs.REAL_SSN as "Member SSN"
,'Subscriber' as "Person Type"
,eed.LAST_NAME as "Last Name"
,eed.FIRST_NAME as "First Name"
,eed.BIRTHDATE as "Date of Birth"
,eed.SEX as "Gender"
,aed.Address_1 as "Address 1"
,aed.Address_2 as "Address 2"
,aed.City
,aed.State
,aed.Zip
,aed.Country as "Country Code"
,aed.Telephone as "Employee Home Phone"
,eed.EMAIL_ADDRESS as "Employee Email Address"
,CASE
WHEN eed.controlled_group_status = 'L'
OR eed.controlled_group_status = 'P'
then eed.EFF_DATE
ELSE NULL
END
as "Date Last Actively At Work"
/*Remove line below*/
,eed.CONTROLLED_GROUP_STATUS
,CASE
WHEN eed.controlled_group_status = 'L'
OR eed.controlled_group_status = 'P'
then 'LEAVE'
ELSE NULL
END
as "Leave Status"
,CASE
WHEN eed.controlled_group_status = 'L'
OR eed.controlled_group_status = 'P'
then eed.EFF_DATE + 1
ELSE NULL
END
as "Leave Begin Date"
,eed.LATEST_HIRE_DATE as "Employee Date of Hire"
,eed.LAST_TERM_DATE as "Employee Date of Termination"
,mcemd.RATE_1 as "Employee Salary"
,ele.LIFE_EVENT_ID as "Life Event ID"
,ele.LIFE_EVENT_DATE as "Loss of Coverage Date"
FROM
/*Employee_Eff_Date*/
(SELECT *
FROM
(SELECT *
FROM
(SELECT eed1.*,
ROW_NUMBER() OVER (PARTITION BY eed1.SSN ORDER BY EFF_DATE DESC) as seqnum
FROM EMPLOYEE_EFF_DATE eed1)
WHERE seqnum = 1) eed)
JOIN
/*Address_Eff_Date*/
(SELECT *
FROM
(SELECT *
FROM
(SELECT aed1.*,
ROW_NUMBER() OVER (PARTITION BY aed1.SSN ORDER BY EFF_DATE DESC) as seqnum
FROM ADDRESS_EFF_DATE aed1
) aed1
ON aed1.SSN = eed.SSN
WHERE aed1.seqnum = 1) aed)
INNER JOIN COMPANY_EMPLOYMENT_DATA ced
ON eed.SSN = ced.SSN
INNER JOIN MV_COMB_EMP_MAX_DTS mcemd
ON eed.SSN = mcemd.SSN
INNER JOIN EMPLOYEE_LIFE_EVENTS ele
ON ele.SSN = eed.SSN
WHERE eed.P_COMPANY_ID_I = 1234
/*Address_Eff_Date qualifying statement*/
AND aed.ADDRESS_KEY = 0
/*EMPLOYEE_LIFE_EVENTS qualifying statement*/
/*Below line indicates the Life Event Dates (set to the past week)*/
AND ele.LIFE_EVENT_DATE >= sysdate-7
AND ele.LIFE_EVENT_DATE <= sysdate
I think you actually want this. Note you have more level of query nesting than you actually need and I have removed the extra ones:
/*Employee_Eff_Date*/
(SELECT *
FROM
(SELECT eed1.*,
ROW_NUMBER() OVER (PARTITION BY eed1.SSN ORDER BY EFF_DATE DESC) as seqnum
FROM EMPLOYEE_EFF_DATE eed1) eed1
WHERE seqnum = 1) eed
JOIN
/*Address_Eff_Date*/
(SELECT *
FROM
(SELECT aed1.*,
ROW_NUMBER() OVER (PARTITION BY aed1.SSN ORDER BY EFF_DATE DESC) as seqnum
FROM ADDRESS_EFF_DATE aed1
) aed1
WHERE aed1.seqnum = 1) aed ON aed.SSN = eed.SSN

Trying to make a single query out of these 2 queries

I am trying to get the percentage of calls that fit the allowed time-frame to get answered. But due to one condition, having the call less than 30 seconds, I am having an issue getting it to work. I tried working out the 30 seconds condition in the Select statement but it did not work ( I kept getting 100% every time and after looking at the numbers individually, it was not possible.
Select date, count("speed of answer" < '00:00:30')/ count(calls) as SLA
From five9_data.calllog
Where "call type" = 'Inbound' and campaign in ('Eves Addiction', 'Brook and York') and "service level" = '1' and skill = 'Eves Sales V'
Group By date
Order By date desc
Limit 5000
Here are the 2 queries in full:
Select date, count(calls) as Total
From five9_data.calllog
Where
"call type" = 'Inbound'
and campaign in ('Eves Addiction', 'Brook and York')
and "service level" = '1'
and skill = 'Eves Sales V'
Group By date
Order By date desc
AND
Select date, count("speed of answer") as AnsweredInTime
From five9_data.calllog
Where
"call type" = 'Inbound'
and campaign in ('Eves Addiction', 'Brook and York')
and "service level" = '1'
and skill = 'Eves Sales V'
and "speed of answer" < '00:00:30'
Group By date
Order By date desc
It has the same data source so union did not work, and did not think Join would work.
End game I want to be able to make a query that allows the 2 queries above to work and finally divide AnsweredInTime by Total.
I would recommend writing the query as:
Select date,
count(*) as total_count,
sum( ("speed of answer" < '00:00:30')::int ) as num_AnsweredInTime,
avg( ("speed of answer" < '00:00:30')::int ) as ratio_AnsweredInTime
from five9_data.calllog
where "call type" = 'Inbound' and
campaign in ('Eves Addiction', 'Brook and York') and
"service level" = '1' and
skill = 'Eves Sales V'
Group By date
Order By date desc
The COUNT(exp) function will count the number of rows for which exp is not null -- it doesn't care about true or false. You can verify this by executing the command select count(false) (evaluates to 1) and select count(NULL) (evaluates to 0). So instead of
count("speed of answer" < '00:00:30')
you could try either
count(nullif("speed of answer" < '00:00:30', FALSE))
(if speed is < 00:30 this will NOT be null, and thus will be counted) or
sum(case when "speed of answer" < '00:00:30' then 1 else 0 end)
Select date,
count(*) as total_count,
sum( ("speed of answer" < '00:00:30')::int ) as num_AnsweredInTime,
avg( ("speed of answer" < '00:00:30')::int ) as ratio_AnsweredInTime
from five9_data.calllog
where "call type" = 'Inbound' and
campaign in ('Eves Addiction', 'Brook and York') and
"service level" = '1' and
skill = 'Eves Sales V'
Group By date
Order By date desc

Oracle SQL Select Date Time with Earliest Time

I have a query that returns this
And I would like to to return this, where earliest hour of the same day is chosen
This is my query so far --- MIN(I.CREATIONDATE) does return the date-time format I want, I was hoping that MIN would select the earliest time.
SELECT TO_CHAR(MIN(I.INCIDENTID)) AS "Incident ID",
MIN(I.CREATIONDATE) AS "Creation Date",
TO_CHAR(I.CREATIONDATE,'MM-DD-YYYY') AS "Date",
TRIM(MO.DOMAINUSERNAME) AS "Login ID",
TRIM(M.MESSAGESUBJECT) AS "Email Subject"
FROM MESSAGE M
JOIN INCIDENT I
ON M.MESSAGESOURCE = I.MESSAGESOURCE
AND M.MESSAGEID = I.MESSAGEID
AND M.MESSAGEDATE = I.MESSAGEDATE
JOIN MESSAGEORIGINATOR MO
ON M.MESSAGEORIGINATORID = MO.MESSAGEORIGINATORID
GROUP BY TO_CHAR(I.CREATIONDATE,'MM-DD-YYYY'),
TRIM(MO.DOMAINUSERNAME),
TRIM(M.MESSAGESUBJECT)
Use row_number()
with CTE as
(
select t1.*,
row_number() over (partition by trunc(creation_date) order by creation_date) rn
from Mytable t1
)
select *
from CTE
where rn = 1

sql to sum counts and add amounts from 2 tables

I am having getting the last sql to work.
The first two grouping gave me the right answer but the last one which involves joining 2 tables gave me a number thats too high when i manually add the first two together.
sql is to generate figures 1,2 and the (1+2) as 3.
Pls help.
sql below....
select [OrderDate] as "Date",
'Cobs' as "Payment Source",
COUNT(*) as "Quantity",
SUM([Amount]) as "Value in Pounds"
from cobaltins
where PN like 'BT%'
group by OrderDate
union all
select [OrderDate] as "Date",
'Cobs Adhoc' as "Payment Source",
COUNT(*) as "Quantity",
SUM([Amount]) as "Value in Pounds"
FROM cobaltins_adhoc
where name = 'Vauz'
group by OrderDate
union all
select cba.OrderDate as "Date",
'Cumulative' as "Payment Source",
COUNT(*) as "Quantity",
sum((cb.Amount)+(cba.Amount)) as "Value in Pounds"
FROM cobaltins as cb
left join cobaltins_adhoc as cba on cb.OrderDate = cba.OrderDate
where cb.PN like 'BT%'
or cba.name = 'Vauz'
group by cba.OrderDate
Rather than try and get it in one you might want to try and sum an inline view of your UNION
SELECT
"Date",
"Payment Source",
SUM(Quantity) Quantity,
SUM("Value in Pounds") "Value in Pounds"
FROM
(
select [OrderDate] as "Date",
'Cobs' as "Payment Source",
COUNT(*) as "Quantity",
SUM([Amount]) as "Value in Pounds"
from cobaltins
where PN like 'BT%'
group by OrderDate
union all
select [OrderDate] as "Date", '
Cobs Adhoc' as "Payment Source",COUNT(*) as "Quantity",
SUM([Amount]) as "Value in Pounds"
FROM cobaltins_adhoc
where name = 'Vauz'
group by OrderDate
) t
GROUP BY
"Date",
"Payment Source"
The way that you are doing your join
FROM cobaltins as cb
LEFT JOIN cobaltins_adhoc as cba
ON cb.OrderDate = cba.OrderDate
is only joining on the order date. if you have multiple orders per date then it's going to try to join those together arbitrarily. you will want to add something else into that join to make it distinct.
You are joining on OrderDate.
That means if there is more than one row in either table with the same order date, then you will get duplication of the matching rows in the other table.
A quick way to test this is to remove the COUNT(*) and the GROUP BY, and just look at the result of your join. Do you see any duplicates that you didn't want to count?
Why not do this with columns instead of rows?
select bt."Date", bt.Quantity as Cobs, vauz.Quantity as CobsAdhoc,
(bt.quantity + coalesce(vauz.Quantity)) as Total
from (select [OrderDate] as "Date", 'Cobs' as "Payment Source", COUNT(*) as "Quantity",
SUM([Amount]) as "Value in Pounds"
from cobaltins
where PN like 'BT%'
group by OrderDate
) bt left outer join
(select [OrderDate] as "Date", 'Cobs Adhoc' as "Payment Source",COUNT(*) as "Quantity",
SUM([Amount]) as "Value in Pounds"
FROM cobaltins_adhoc
where name = 'Vauz'
group by OrderDate
) vauz
on bt."Date" = vauz."Date";