I have a table for showing target and completion times and all of the information is from one table "job" - however I now want to add some more SQL in to provide some information from another table "job_type" but whenever I keep getting error messages saying
"SQLSTATE = S0022 [Oracle][ODBC][Ora]ORA-00904: "JOB_TYPE"."JOB_TYPE_CODE": invalid identifier.
The code I have that works (without the bit I want adding is)
select
job_number, priority_code, job_entry_date, clock_start,
site_code,
TO_CHAR(job_entry_date, 'Dy') as DAY_LOGGED,
TO_CHAR(actual_start_date, 'IW') as WEEK_ON_SITE,
actual_start_date,
actual_comp_date,
-- kpi tracker
CASE
WHEN actual_start_date is null AND current_date < target_time THEN 'Not arrived on site yet'
WHEN actual_start_date is null AND current_date > target_time THEN 'FAIL (2)'
when actual_start_date <= target_time then 'Pass'
when actual_start_date > target_time then 'FAIL'
ELSE 'Overdue' END as Arrived_on_time_check,
-- start of correct target time SQL
case
when to_char(target_time, 'Dy', 'NLS_DATE_LANGUAGE=ENGLISH') = 'Fri'
and floor((target_time - trunc(target_time)) * 24) >= 17
then target_time + 2 + 63/24
when floor((target_time - trunc(target_time)) * 24) >= 17
then target_time + 15/24
else target_time
end as target_time
from
(
select job_number, priority_code, job_entry_date, clock_start, site_code, actual_start_date, actual_comp_date,
CASE
WHEN PRIORITY_CODE IN ('GC01','GC02','GC03','GC04','GC05','GC06','GC07')
THEN
clock_start + case priority_code
when 'GC01' then 1
when 'GC02' then 2
when 'GC03' then 0.5
when 'GC04' then 1
when 'GC05' then 2
when 'GC06' then 4
when 'GC07' then 24
end / 24
ELSE TARGET_COMP_DATE END as target_time
from
(
select job_number, priority_code, job_entry_date, target_comp_date, site_code, actual_start_date, actual_comp_date,
case
when to_char(job_entry_date, 'Dy', 'NLS_DATE_LANGUAGE=ENGLISH') = 'Fri'
and floor((job_entry_date - trunc(job_entry_date)) * 24) >= 17
then trunc(job_entry_date) + 80/24
when to_char(job_entry_date, 'Dy', 'NLS_DATE_LANGUAGE=ENGLISH') = 'Sat'
then trunc(job_entry_date) + 56/24
when to_char(job_entry_date, 'Dy', 'NLS_DATE_LANGUAGE=ENGLISH') = 'Sun'
or floor((job_entry_date - trunc(job_entry_date)) * 24) >= 17
then trunc(job_entry_date) + 32/24
when floor((job_entry_date - trunc(job_entry_date)) * 24) < 8
then trunc(job_entry_date) + 8/24
else job_entry_date
end as clock_start
from job
)
)
where
priority_code in ('GC01','GC02','GC03','GC04','GC05','GC06','GC07')
The code I want to add in is:
-- JOB KPI ATTRIBUTE LOOK UP IN GJOB TABLE
(SELECT attribute_value.attrib_value_name FROM attribute_value WHERE
attribute_value.attrib_type_code = 'GJOB'
AND attribute_value.attrib_value_code = job_type.job_type_code) as JOB_KPI,
-- JOB KPI PENALTY POINTS LOOK UP IN GJOB TABLE
(SELECT attribute_value.attrib_value_nom FROM attribute_value WHERE
attribute_value.attrib_type_code = 'GJOB'
AND attribute_value.attrib_value_code = job_type.job_type_code) as KPI_POINTS,
How do I add in the above code to the existing code without the error messages saying that it's an invalid identifier?
Related
i have this query
SELECT case
when {{aggtime}} = 'HOURLY' then parsedatetime(formatdatetime("PUBLIC"."ORDERS"."CREATED_AT", 'yyyyMMddHH'), 'yyyyMMddHH')
when {{aggtime}} = 'DAILY' then CAST("PUBLIC"."ORDERS"."CREATED_AT" AS date)
when {{aggtime}} = 'WEEKLY' then dateadd('day', CAST((1 - CASE WHEN ((iso_day_of_week("PUBLIC"."ORDERS"."CREATED_AT") + 1) % 7) = 0 THEN 7 ELSE ((iso_day_of_week("PUBLIC"."ORDERS"."CREATED_AT") + 1) % 7) END) AS long), CAST("PUBLIC"."ORDERS"."CREATED_AT" AS date))
when {{aggtime}} = 'MONTHLY' then parsedatetime(formatdatetime("PUBLIC"."ORDERS"."CREATED_AT", 'yyyyMM'), 'yyyyMM')
when {{aggtime}} = 'YEARLY' then parsedatetime(formatdatetime("PUBLIC"."ORDERS"."CREATED_AT", 'yyyy'), 'yyyy')
END
AS "CREATED_AT", sum("PUBLIC"."ORDERS"."QUANTITY") AS "sum"
FROM "PUBLIC"."ORDERS"
WHERE year(CREATED_AT)=2020 and month(CREATED_AT) = 1 and day(CREATED_AT) = 01
GROUP BY "CREATED_AT"
i need the where clause only when {{aggtime}}='HOURLY', otherwise i need only the year
How can i achieve it ?
ps :. {{aggtime}} is a parameter for Metabase
I have two working reports / data-sources, but want to combine them into one.
I'm not too sure about which sections should go where, and what needs to go inside what bracket.
Some help regarding combining these into one report would be appreciated, the link field between both reports is the job.job_number field.
Edit: What I would like to see at the end is a report that gives me the following headings:
ALL FROM REPORT / DATASOURCE ONE
Job.Job_number 410000378
job.site_code 1372
job.job_entry_date 31/01/2019 17:45:23
job.actual_comp_date 04/06/2019
central_site.site_name HIGH STREET, NEWPORT
area.area_name NEWPORT
zone_1_area_check ZONE 1 AREA
day_logged Thu
week_on_site 5
job_type.job_type_code FLY1
job_type.job_type_name FLYTIP ZONE 1 AREA
Gang_Photos Photos Linked
Photo_Count 2
Photo Check Pass
Arrived_on_time_check Pass
KPI_PASS_FAIL Pass
KPI_PENALTY_INSTANCES 0
KPI_STATUS_CHECK Pass
JOB_KPI KPI 2 - FLY TIPPING
KPI_POINTS 25
Week_dashboard Current Week
Month_dashboard Current Month
Quarter_dashboard Current Quarter
ALL FROM DATA-SOURCE TWO:
clock_start 01/02/2019 08:00:00
target_time 01/02/2019 12:00:00
priority.priority_code GC06
First report / data-source code:
select
job.job_number,
job.job_entry_date,
job.site_code,
central_site.site_name,
area.area_name as Parish,
JOB.JOB_ENTRY_DATE as Job_logged,
priority.priority_code,
priority.priority_name,
job.target_comp_date as Target_On_Site,
job.actual_START_date as Actual_On_Site,
JOB.actual_COMP_DATE as Job_Completed_Time,
CASE
WHEN central_site.site_code in
('456','1372','1373','1426','1432','2251',
'3202','3206','4017','9978','9979','9980',
'9981','9982','9983','9984','9985','9986',
'9987','9988','9989','9990','9991','9992',
'9993','9994','9995','9996','9997','9998',
'9999')
THEN 'Zone 1 Area'
ELSE ''
END as ZONE_1_AREA_CHECK,
TO_CHAR(job.job_entry_date, 'Dy') as DAY_LOGGED,
TO_CHAR(job.actual_start_date, 'IW') as WEEK_ON_SITE,
job_type.job_type_code,
job_type.job_type_name,
-- CHECK TO SEE IF 2 PHOTOS HAVE BEEN TAKEN ON JOB
CAST(CASE WHEN document_link.entity_key IS NULL THEN 'No Photos' ELSE
'Photos Linked' END as VARCHAR (20)) as gang_photos,
(SELECT COUNT(document_link.entity_key) FROM document_link WHERE
document_link.entity_key =CAST( job.job_number as VARCHAR (15)) AND
document_link.entity_type = 'JOB' AND
document_link.document_notes LIKE 'Photo added by_%' ) as Photo_count,
CASE
WHEN ROUND ((SELECT COUNT(document_link.entity_key) FROM document_link WHERE
document_link.entity_key =CAST( job.job_number as VARCHAR (15)) AND
document_link.entity_type = 'JOB'),2) >=2 then 'Pass' else 'FAIL' end as
Photo_Check,
-- kpi tracker
CASE
WHEN job.actual_start_date is null AND current_date < job.target_comp_date
THEN 'Not arrived on site yet'
WHEN job.actual_start_date is null AND current_date > job.target_comp_date
THEN 'FAIL (2)'
when job.actual_start_date <= job.target_comp_date then 'Pass'
when job.actual_start_date > job.target_comp_date then 'FAIL'
ELSE 'Overdue' END as Arrived_on_time_check,
-- KPI arrive on site and 2 photos check
CASE
WHEN job.actual_start_date <= job.target_comp_date AND
ROUND ((SELECT COUNT(document_link.entity_key) FROM document_link WHERE
document_link.entity_key =CAST( job.job_number as VARCHAR (15)) AND
document_link.entity_type = 'JOB'),2) >=2 then 'Pass' else 'Fail' end as
KPI_PASS_FAIL,
-- KPI Instances of penalty
CASE
-- arrived on site in time and 2+ photos = 0 penalty (pass)
WHEN job.actual_start_date <= job.target_comp_date AND
ROUND ((SELECT COUNT(document_link.entity_key) FROM document_link WHERE
document_link.entity_key =CAST( job.job_number as VARCHAR (15))
AND document_link.entity_type = 'JOB'),2) >=2 then '0'
-- arrived on time but no photos attached = 1 penalty (FAIL PHOTOS)
WHEN job.actual_start_date <= job.target_comp_date AND
ROUND ((SELECT COUNT(document_link.entity_key) FROM document_link WHERE
document_link.entity_key =CAST( job.job_number as VARCHAR (15))
AND document_link.entity_type = 'JOB'),2) <=1 then '1'
--arrived on site late BUT 2+ PHOTOS = manual calculation (FAIL TIME)
WHEN job.actual_start_date > job.target_comp_date AND
ROUND ((SELECT COUNT(document_link.entity_key) FROM document_link WHERE
document_link.entity_key =CAST( job.job_number as VARCHAR (15))
AND document_link.entity_type = 'JOB'),2) >=2 THEN 'Manual Calculation -
Time Fail'
-- ARRIVED LATE AND NOT ENOUGH PHOTOS (FAIL TIME AND PHOTOS)
WHEN job.actual_start_date > job.target_comp_date AND
ROUND ((SELECT COUNT(document_link.entity_key) FROM document_link WHERE
document_link.entity_key =CAST( job.job_number as VARCHAR (15))
AND document_link.entity_type = 'JOB'),2) <=1 THEN 'Manual Calculation -
Time and Photo Fail'
else 'Check' end as KPI_PENALTY_INSTANCES,
-- KPI STATUS CHECK
CASE
-- arrived on site in time and 2+ photos = 0 penalty (pass)
WHEN job.actual_start_date <= job.target_comp_date AND
ROUND ((SELECT COUNT(document_link.entity_key) FROM document_link WHERE
document_link.entity_key =CAST( job.job_number as VARCHAR (15))
AND document_link.entity_type = 'JOB'),2) >=2 then 'PASS'
-- arrived on time but no photos attached = 1 penalty (FAIL PHOTOS)
WHEN job.actual_start_date <= job.target_comp_date AND
ROUND ((SELECT COUNT(document_link.entity_key) FROM document_link WHERE
document_link.entity_key =CAST( job.job_number as VARCHAR (15))
AND document_link.entity_type = 'JOB'),2) <=1 then 'FAIL - PHOTOS'
--arrived on site late BUT 2+ PHOTOS = manual calculation (FAIL TIME)
WHEN job.actual_start_date > job.target_comp_date AND
ROUND ((SELECT COUNT(document_link.entity_key) FROM document_link WHERE
document_link.entity_key =CAST( job.job_number as VARCHAR (15))
AND document_link.entity_type = 'JOB'),2) >=2 THEN 'FAIL - ARRIVED LATE'
-- ARRIVED LATE AND NOT ENOUGH PHOTOS (FAIL TIME AND PHOTOS)
WHEN job.actual_start_date > job.target_comp_date AND
ROUND ((SELECT COUNT(document_link.entity_key) FROM document_link WHERE
document_link.entity_key =CAST( job.job_number as VARCHAR (15))
AND document_link.entity_type = 'JOB'),2) <=1 THEN 'FAIL - TIME AND PHOTOS'
WHEN job.actual_START_date is NULL then 'FAIL - ARRIVED TIME BLANK'
else 'Check' end as KPI_STATUS_CHECK,
-- JOB KPI ATTRIBUTE LOOK UP IN GJOB TABLE
(SELECT attribute_value.attrib_value_name FROM attribute_value WHERE
attribute_value.attrib_type_code = 'GJOB'
AND attribute_value.attrib_value_code = job_type.job_type_code) as JOB_KPI,
-- JOB KPI PENALTY POINTS LOOK UP IN GJOB TABLE
(SELECT attribute_value.attrib_value_nom FROM attribute_value WHERE
attribute_value.attrib_type_code = 'GJOB'
AND attribute_value.attrib_value_code = job_type.job_type_code) as
KPI_POINTS,
-- FIELDS FOR USE IN DASHBOARD WIDGETS
CASE
when job.actual_comp_date >= trunc(trunc(SYSDATE,'IW')-1,'IW') AND
job.actual_comp_date <=trunc(SYSDATE,'IW') then 'Previous Week'
when job.actual_comp_date >=trunc(SYSDATE,'IW') AND job.actual_comp_date <=
SYSDATE then 'Current Week'
else ''
end as Week_Dashboard,
CASE
when job.actual_comp_date >= trunc(trunc(SYSDATE,'MM')-1,'MM') AND
job.actual_comp_date <=trunc(SYSDATE,'MM') then 'Previous Month'
when job.actual_comp_date >=trunc(SYSDATE,'MM') AND job.actual_comp_date <=
SYSDATE then 'Current Month'
else ''
end as Month_Dashboard,
CASE
when job.actual_comp_date >= trunc(trunc(SYSDATE,'IW')-1,'Q') AND
job.actual_comp_date <=trunc(SYSDATE,'Q') then 'Previous Quarter'
when job.actual_comp_date >=trunc(SYSDATE,'Q') AND job.actual_comp_date <=
SYSDATE then 'Current Quarter'
else ''
end as Quarter_Dashboard
from
job
left join enquiry on job.job_number = enquiry.job_number
left join defect on job.job_number = defect.job_number
left join defect_type on defect_type.defect_type_code = defect.defect_type
inner join central_site on job.site_code = central_site.site_code
inner join area on central_site.area_code = area.area_code
left join central_enquiry on central_enquiry.enquiry_number =
enquiry.enquiry_number
left join enquiry_cust_link on central_enquiry.enquiry_number =
enquiry_cust_link.enquiry_number
left join enquiry_subject on enquiry_subject.subject_code =
central_enquiry.subject_code
left join type_of_service on type_of_service.service_code =
enquiry_subject.service_code
inner join priority on priority.priority_code = job.priority_code
inner join JOB_STATUS_LOG on JOB.JOB_NUMBER = JOB_STATUS_LOG.JOB_NUMBER
and job.job_log_number = job_status_log.job_log_number
inner join JOB_STATUS on JOB_STATUS.STATUS_CODE = JOB_STATUS_LOG.STATUS_CODE
inner join job_type on job_type.job_type_key = job.job_type_key
inner join priority on priority.priority_code = job.priority_code
LEFT OUTER JOIN
(SELECT DISTINCT
job.job_number,
document_link.entity_key,
(SELECT COUNT(document_link.entity_key) FROM document_link WHERE
document_link.entity_key =CAST( job.job_number as VARCHAR (15)) AND
document_link.entity_type = 'JOB' AND
document_link.document_notes LIKE 'Photo added by_%' ) as Photo_count
FROM
job
LEFT OUTER JOIN
document_link
ON
document_link.entity_key =CAST( job.job_number as VARCHAR (15)) AND
document_link.entity_type = 'JOB' AND
document_link.document_notes LIKE 'Photo added by_%') document_link
ON
document_link.job_number = job.job_number
where
job_status_log.allocated_officer = 'IDVE' and
job_status_log.status_code in ('5100','5200','5300','5400')
order by
job.job_number
Second Report / Data-Source Code:
select job_number, priority_code, job_entry_date, clock_start,
target_comp_date,
case
when to_char(target_time, 'Dy', 'NLS_DATE_LANGUAGE=ENGLISH') = 'Fri'
and floor((target_time - trunc(target_time)) * 24) >= 17
then target_time + 2 + 63/24
when floor((target_time - trunc(target_time)) * 24) >= 17
then target_time + 15/24
else target_time
end as target_time
from (
select job_number, priority_code, job_entry_date, clock_start,
TARGET_COMP_DATE,
CASE
WHEN PRIORITY_CODE IN ('GC01','GC02','GC03','GC04','GC05','GC06','GC07')
THEN
clock_start
+ case priority_code
when 'GC01' then 1
when 'GC02' then 2
when 'GC03' then 0.5
when 'GC04' then 1
when 'GC05' then 2
when 'GC06' then 4
when 'GC07' then 24
end
/ 24
ELSE
TARGET_COMP_DATE END as target_time
from (
select job_number, priority_code, job_entry_date, target_comp_date,
case
when to_char(job_entry_date, 'Dy', 'NLS_DATE_LANGUAGE=ENGLISH') = 'Fri'
and floor((job_entry_date - trunc(job_entry_date)) * 24) >= 17
then trunc(job_entry_date) + 80/24
when to_char(job_entry_date, 'Dy', 'NLS_DATE_LANGUAGE=ENGLISH') = 'Sat'
then trunc(job_entry_date) + 56/24
when to_char(job_entry_date, 'Dy', 'NLS_DATE_LANGUAGE=ENGLISH') = 'Sun'
or floor((job_entry_date - trunc(job_entry_date)) * 24) >= 17
then trunc(job_entry_date) + 32/24
when floor((job_entry_date - trunc(job_entry_date)) * 24) < 8
then trunc(job_entry_date) + 8/24
else job_entry_date
end as clock_start
from job
)
)
I am trying to retrieve data from a database in a particular format to hook into a dashboard app. I need one column called "Import" with counts beneath it, and another called "Export" with relevant counts in that. I have the following query right now but it is only returning an Import column and I've verified there is relevant data to show as Export as well. Any ideas? Adding a screenshot of the result I'm getting...
SELECT count(*) as "Import", a.ship_id "Ship"
FROM SERVICE_EVENTS a JOIN CONTAINERS b ON a.eq_nbr = b.nbr
WHERE (
(to_char(sysdate, 'HH24') between '07' and '17' and a.performed between trunc(sysdate) + 7/24 and sysdate and category = 'I')
OR
(to_char(sysdate, 'HH24') between '18' and '23' and a.performed between trunc(sysdate) + 18/24 and sysdate and category = 'I')
OR
(to_char(sysdate, 'HH24') between '00' and '06' and a.performed between trunc(sysdate - 1) + 18/24 and sysdate and category = 'I')
)
AND a.TSERV_ID in ('LOAD', 'DISCHARGE') group by a.ship_id
UNION
SELECT count(*) as "Export", a.ship_id "Ship"
FROM SERVICE_EVENTS a JOIN CONTAINERS b ON a.eq_nbr = b.nbr
WHERE (
(to_char(sysdate, 'HH24') between '07' and '17' and a.performed between trunc(sysdate) + 7/24 and sysdate and category = 'E')
OR
(to_char(sysdate, 'HH24') between '18' and '23' and a.performed between trunc(sysdate) + 18/24 and sysdate and category = 'E')
OR
(to_char(sysdate, 'HH24') between '00' and '06' and a.performed between trunc(sysdate - 1) + 18/24 and sysdate and category = 'E')
)
AND a.TSERV_ID in ('LOAD', 'DISCHARGE') group by a.ship_id
;
You can do a conditional aggregation and simplify your query to this:
SELECT
a.ship_id AS Ship,
COUNT(CASE WHEN category = 'I' THEN 1 END) AS Import,
COUNT(CASE WHEN category = 'E' THEN 1 END) AS Export
FROM SERVICE_EVENTS a
JOIN CONTAINERS b
ON a.eq_nbr = b.nbr
WHERE
(
(to_char(sysdate, 'HH24') BETWEEN '07' AND '17' AND a.performed BETWEEN trunc(sysdate) + 7/24 AND sysdate)
OR (to_char(sysdate, 'HH24') BETWEEN '18' AND '23' AND a.performed BETWEEN trunc(sysdate) + 18/24 AND sysdate)
OR (to_char(sysdate, 'HH24') BETWEEN '00' AND '06' AND a.performed BETWEEN trunc(sysdate - 1) + 18/24 AND sysdate)
)
AND a.TSERV_ID in ('LOAD', 'DISCHARGE')
GROUP BY a.ship_id
You can explicitly select null as the column not being counted and do one more aggregation.
select max(Import) as Import, max(Export) as Export, Ship
from (
SELECT count(*) as "Import", null as "Export", a.ship_id "Ship"
FROM SERVICE_EVENTS a JOIN CONTAINERS b ON a.eq_nbr = b.nbr
WHERE (
(to_char(sysdate, 'HH24') between '07' and '17' and a.performed between trunc(sysdate) + 7/24 and sysdate and category = 'I')
OR
(to_char(sysdate, 'HH24') between '18' and '23' and a.performed between trunc(sysdate) + 18/24 and sysdate and category = 'I')
OR
(to_char(sysdate, 'HH24') between '00' and '06' and a.performed between trunc(sysdate - 1) + 18/24 and sysdate and category = 'I')
)
AND a.TSERV_ID in ('LOAD', 'DISCHARGE') group by a.ship_id
UNION
SELECT null as import, count(*) as "Export", a.ship_id "Ship"
FROM SERVICE_EVENTS a JOIN CONTAINERS b ON a.eq_nbr = b.nbr
WHERE (
(to_char(sysdate, 'HH24') between '07' and '17' and a.performed between trunc(sysdate) + 7/24 and sysdate and category = 'E')
OR
(to_char(sysdate, 'HH24') between '18' and '23' and a.performed between trunc(sysdate) + 18/24 and sysdate and category = 'E')
OR
(to_char(sysdate, 'HH24') between '00' and '06' and a.performed between trunc(sysdate - 1) + 18/24 and sysdate and category = 'E')
)
AND a.TSERV_ID in ('LOAD', 'DISCHARGE') group by a.ship_id
) t
group by Ship
I would like to create a trendline based on the values returned from a query. The logic needs to be applicable for other cases of placing a trendlines on series in Oracle PLSQL with Apex.
My series has X values that are dates and Y values that are time values. Here is my query that outputs my dates and Y values:
Select
null link,
(START_DATE) label,
Round(Avg(Run_TIME),3) "Average Length"
from
(Select job_id,
(Case :P4_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END) "START_DATE",
1440*(END_TIME - START_TIME) "RUN_TIME"
from apps.NI_INFA_ACTIVITY_LOG_V#util.world
WHERE
(:P4_JOB_SIZE_CHOOSER = 'Tiny' AND (1440*(END_TIME - START_TIME)) <= 1
OR
:P4_JOB_SIZE_CHOOSER = 'Small' AND (1440*(END_TIME - START_TIME)) > 1 AND
(1440*(END_TIME - START_TIME)) <= 5
OR
:P4_JOB_SIZE_CHOOSER = 'Medium' AND (1440*(END_TIME - START_TIME)) > 5 AND
(1440*(END_TIME - START_TIME)) <= 20
OR
:P4_JOB_SIZE_CHOOSER = 'Large' AND ((1440*(END_TIME - START_TIME)) > 20)
OR
:P4_JOB_SIZE_CHOOSER NOT IN('Small','Medium','Large','Tiny') AND (1440*(END_TIME - START_TIME)) > 0)
AND
(INFA_TYPE_CODE = 'WORKFLOW')
AND
OBJECT_NAME = :P4_WORKFLOWNAMES
AND
(
:P4_Data_Volume_Chooser = 'Small' AND SUCCESS_SOURCE_ROWS < 100
OR
:P4_Data_Volume_Chooser = 'Medium' AND SUCCESS_SOURCE_ROWS > 99 AND SUCCESS_SOURCE_ROWS < 10000
OR
:P4_Data_Volume_Chooser = 'Large' AND SUCCESS_SOURCE_ROWS > 9999
OR
:P4_Data_Volume_Chooser NOT IN ('Small','Medium','Large')
)
)
WHERE START_DATE BETWEEN
(Case :P4_DATE_CHOOSER
WHEN 'Daily' THEN trunc(to_date(:P4_BEGIN_DATES, 'MM-DD-YY'))
WHEN 'Weekly' THEN trunc(to_date(:P4_BEGIN_DATES, 'MM-DD-YY'), 'WW')
WHEN 'Monthly' THEN trunc(to_date(:P4_BEGIN_DATES, 'MM-DD-YY'), 'MM') END)
AND
(Case :P4_DATE_CHOOSER
WHEN 'Daily' THEN trunc(to_date(:P4_END_DATE, 'MM-DD-YY'))
WHEN 'Weekly' THEN trunc(to_date(:P4_END_DATE, 'MM-DD-YY'), 'WW')
WHEN 'Monthly' THEN trunc(to_date(:P4_END_DATE, 'MM-DD-YY'), 'MM') END)
group by START_DATE
order by START_DATE
;
Ideally, I'd be able to use my existing query as the source for a new subquery.
This query only works if i replace the top select columns with "*". I've been reading through the other questions that are similar to mine but i haven't been able to apply their logic to my situation. All of the subqueries are the same except that they change based on the state in the "where" clauses.
There are far more successes everyday. Failures and warnings don't happen happen everyday so they have some null values that I would like to make into 0's. I want all of the successes, warnings, and failures to be related to the a.Start_date.
Select a.START_DATE, a.Successes, b.START_DATE, b.Failures, c.START_DATE, c.WARNINGS
FROM
(
Select
(Case :P1_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END) "START_DATE",
NVL(count(job_id),0) as "Successes"
from NI_INFA_ACTIVITY_LOG_V
where State = 1
and
(:P1_JOB_SIZE_CHOOSER = 'Tiny' AND (1440*(END_TIME - START_TIME)) <= 1
OR
:P1_JOB_SIZE_CHOOSER = 'Small' AND (1440*(END_TIME - START_TIME)) > 1 AND
(1440*(END_TIME - START_TIME)) <= 5
OR
:P1_JOB_SIZE_CHOOSER = 'Medium' AND (1440*(END_TIME - START_TIME)) > 5 AND
(1440*(END_TIME - START_TIME)) <= 20
OR
:P1_JOB_SIZE_CHOOSER = 'Large' AND ((1440*(END_TIME - START_TIME)) > 20)
OR
:P1_JOB_SIZE_CHOOSER NOT IN('Small','Medium','Large','Tiny') AND (1440*(END_TIME - START_TIME)) > 0)
group by Case :P1_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END
order by Case :P1_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END
) a
left outer join
(
Select
(Case :P1_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END) "START_DATE",
nvl(count(job_id),0) as "Failures"
from NI_INFA_ACTIVITY_LOG_V
where State = 3
and
(:P1_JOB_SIZE_CHOOSER = 'Tiny' AND (1440*(END_TIME - START_TIME)) <= 1
OR
:P1_JOB_SIZE_CHOOSER = 'Small' AND (1440*(END_TIME - START_TIME)) > 1 AND
(1440*(END_TIME - START_TIME)) <= 5
OR
:P1_JOB_SIZE_CHOOSER = 'Medium' AND (1440*(END_TIME - START_TIME)) > 5 AND
(1440*(END_TIME - START_TIME)) <= 20
OR
:P1_JOB_SIZE_CHOOSER = 'Large' AND ((1440*(END_TIME - START_TIME)) > 20)
OR
:P1_JOB_SIZE_CHOOSER NOT IN('Small','Medium','Large','Tiny') AND (1440*(END_TIME - START_TIME)) > 0)
group by Case :P1_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END
order by Case :P1_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END
) b
on
a.START_DATE = b.START_DATE
left outer join
(
Select
(Case :P1_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END) "START_DATE",
nvl(count(job_id),0) as "Warnings"
from NI_INFA_ACTIVITY_LOG_V
where State = 2
and
(:P1_JOB_SIZE_CHOOSER = 'Tiny' AND (1440*(END_TIME - START_TIME)) <= 1
OR
:P1_JOB_SIZE_CHOOSER = 'Small' AND (1440*(END_TIME - START_TIME)) > 1 AND
(1440*(END_TIME - START_TIME)) <= 5
OR
:P1_JOB_SIZE_CHOOSER = 'Medium' AND (1440*(END_TIME - START_TIME)) > 5 AND
(1440*(END_TIME - START_TIME)) <= 20
OR
:P1_JOB_SIZE_CHOOSER = 'Large' AND ((1440*(END_TIME - START_TIME)) > 20)
OR
:P1_JOB_SIZE_CHOOSER NOT IN('Small','Medium','Large','Tiny') AND (1440*(END_TIME - START_TIME)) > 0)
group by Case :P1_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END
order by Case :P1_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END
) c
on
a.START_DATE = c.START_DATE
ORDER BY
a.START_DATE
At the end, I would like to have the results be something like:
Start_Date Successes Warnings Failures
6/1/2015 5 0 3
........ 8 15 4
6/30/2015 9 1 0
You've used quoted identifiers for (some of) the aliases in your subqueries, so you have to quote those in the outer select too, and exactly match the case you used:
Select a.START_DATE, a."Successes",
b.START_DATE, b."Failures",
c.START_DATE, c."Warnings"
...
Quoted identifiers are a pain. If you want the column titles to be mixed-case you could use unquoted identifiers in the subqueries and apply a final quoted name in ther outer query as another alias; or let the client handle that display issue.
And since start_date is the join condition, you probably don't want all three references; and to get the zeros in the result set you'll need to NVL the failure and warning counts, so you might as well use unquoted identifiers anyway:
Select a.start_date "Start date", a."Successes",
nvl(b.failures, 0) "Failures", nvl(c.warnings, 0) "Warnings"
...
NVL(count(job_id),0) as successes
...
The NVL you already have around the count isn't doing anything though - it needs to be in the outer query instead as shown - because there is no start date value to group by if the count is null.
If the subqueries really are so similar you could simplify this to a single query level with three selective counts; something like:
Select to_char(Case :P1_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END, 'MM/DD/YYYY') as "Start date",
count(case when State = 1 then job_id end) as "Successes",
count(case when State = 3 then job_id end) as "Failures",
count(case when State = 2 then job_id end) as "Warnings"
from NI_INFA_ACTIVITY_LOG_V
where (:P1_JOB_SIZE_CHOOSER = 'Tiny'
AND (1440*(END_TIME - START_TIME)) <= 1)
OR (:P1_JOB_SIZE_CHOOSER = 'Small'
AND (1440*(END_TIME - START_TIME)) > 1
AND (1440*(END_TIME - START_TIME)) <= 5)
OR (:P1_JOB_SIZE_CHOOSER = 'Medium'
AND (1440*(END_TIME - START_TIME)) > 5
AND (1440*(END_TIME - START_TIME)) <= 20)
OR (:P1_JOB_SIZE_CHOOSER = 'Large'
AND (1440*(END_TIME - START_TIME)) > 20)
OR (:P1_JOB_SIZE_CHOOSER NOT IN ('Small','Medium','Large','Tiny')
AND (1440*(END_TIME - START_TIME)) > 0)
group by Case :P1_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END
order by Case :P1_DATE_CHOOSER
WHEN 'Daily' THEN trunc(start_time)
WHEN 'Weekly' THEN trunc(start_time, 'WW')
WHEN 'Monthly' THEN trunc(start_time, 'MM')
END;
Your parentheses seem to be out of whack too, so I've attempted to correct those, but you may have been doing something intentional I didn't follow...
Replace
Select a.START_DATE, a.Successes, b.START_DATE, b.Failures, c.START_DATE, ...
by
Select a."START_DATE", a."Successes", b."START_DATE", b."Failures", c."START_DATE", ...