Employee Time Log: Calculating time between "Clock-In" and "Clock-Out" time stamps. SSRS - ssrs-2012

When all Employees are selected, this query will not return calculations for every employee only most. I am trying to return results for only the times between "Clock-In" and "Clock-Out". Any help would be appreciated!
SELECT DISTINCT i.SerialNumber, it.Name AS EmployeeName, cs.Value AS
StepValue, cs.ModifiedOnUtc AS TimeStamp, CASE WHEN cs.Value = 'Clocked-In'
THEN NULL WHEN cs.Value <> 'Clocked-In' THEN (CASE WHEN LAG(cs.Value, 1, 0)
OVER (ORDER BY it.Name, cs.modifiedonserverutc)
<> 'Clocked-In' THEN NULL WHEN LAG(cs.Value, 1, 0) OVER (ORDER BY it.name,
cs.ModifiedOnUtc) = 'Clocked-in' THEN DATEDIFF(minute, LAG(cs.ModifiedOnUtc,
1, 0) OVER (ORDER BY it.name, cs.ModifiedOnUtc), cs.ModifiedOnUtc) ELSE NULL
END) END AS MinutesWorked

Related

Oracle Query for grouping and percentage of Runs

I am looking for query that would give the totals and the percentage of runs based on a field .
I am able to get the query using two select statements and then a final where i join them based on Country Field .
Below is the raw data.
I am trying to get output where runs grouped by country and class along with the percentages of each class in the country.
Thanks in advance for the help.
You could use below sql to achieve your purpose.
I use grouping sets option within group by clause to make Oracle generate the require subtotals
I use ratio_to_report analytic function to calculate the Percentage column
Finally, I format the result according to your expected output format using ROUND function.
select COUNTRY, CLASS, RUNS
, NVL2(
PERCENTAGE
, ROUND ( PERCENTAGE, case when COUNTRY = 'Grand Total' then 2 else 0 end ) || '%'
, null
) PERCENTAGE
from (
select
decode( grouping(t.COUNTRY), 1, 'Grand Total', t.COUNTRY ) as COUNTRY
, t.CLASS
, sum(t.RUNS) runs
, 100 * RATIO_TO_REPORT(
case
when t.CLASS != 'Total(Batsman+Bowler+Allrounder)' then sum(t.RUNS)
else null
end
) OVER (PARTITION BY t.COUNTRY) PERCENTAGE
, grouping_id(t.COUNTRY, t.CLASS) grp_id /*just added for sorting in the outer view*/
from Your_table t
group by grouping sets ((t.COUNTRY, t.CLASS), (t.CLASS))
)
order by grp_id, COUNTRY, PERCENTAGE
;
db<>fiddle
You can try the below query -
SELECT T.Country, T.Class, T.Runs, ROUND((T.Runs/TOT_RUNS.Runs)*100, 2)||'%' Percentage
FROM (SELECT Country, Class, SUM(Runs) Runs
FROM YOUR_TABLE
WHERE UPPER(Class) IN ('BATSMAN', 'BOWLER', 'ALLROUNDER')
GROUP BY Country, Class) T
JOIN (SELECT Country, SUM(Runs) Runs
FROM YOUR_TABLE
WHERE UPPER(Class) IN ('BATSMAN', 'BOWLER', 'ALLROUNDER')
GROUP BY Country) TOT_RUNS ON T.Country = TOT_RUNS.Country
UNION ALL
SELECT Country, Class, SUM(Runs), NULL
FROM YOUR_TABLE
WHERE UPPER(Class) IN ('TOTAL(BATSMAN+BOWLER+ALLROUNDER)')
GROUP BY Country, Class
UNION ALL
SELECT 'Grand Total', 'Batsman', SUM(CASE WHEN UPPER(Class) = 'BATSMAN' THEN Runs END),
ROUND(SUM(CASE WHEN UPPER(Class) = 'BATSMAN' THEN Runs END)
/SUM(CASE WHEN UPPER(Class) IN ('BATSMAN', 'BOWLER', 'ALLROUNDER') THEN Runs END)*100, 2) || '%'
FROM YOUR_TABLE
UNION ALL
SELECT 'Grand Total', 'Bowler', SUM(CASE WHEN UPPER(Class) = 'BOWLER' THEN Runs END),
ROUND(SUM(CASE WHEN UPPER(Class) = 'BOWLER' THEN Runs END)
/SUM(CASE WHEN UPPER(Class) IN ('BATSMAN', 'BOWLER', 'ALLROUNDER') THEN Runs END)*100, 2) || '%'
FROM YOUR_TABLE
UNION ALL
SELECT 'Grand Total', 'Allrounder', SUM(CASE WHEN UPPER(Class) = 'ALLROUNDER' THEN Runs END),
ROUND(SUM(CASE WHEN UPPER(Class) = 'ALLROUNDER' THEN Runs END)
/SUM(CASE WHEN UPPER(Class) IN ('BATSMAN', 'BOWLER', 'ALLROUNDER') THEN Runs END)*100, 2) || '%'
FROM YOUR_TABLE
UNION ALL
SELECT 'Grand Total', 'TOTAL(BATSMAN+BOWLER+ALLROUNDER)', SUM(CASE WHEN UPPER(Class) = 'TOTAL(BATSMAN+BOWLER+ALLROUNDER)' THEN Runs END),NULL
FROM YOUR_TABLE

Oracle store procedure to retrieve dates ny passing column name as parameter

The below procedure returns thousands of lines by passing start date and end date. Added to that i need to add one parameter which should apply the start date and end date on different columns such as created date, planned date and end date which are available in result set.
For instance if i pass the value as (created date,'31/10/2020 00:00:00','01/11/2020 00:00:00'), the filter should be applied on created date column, if i pass (end,'31/10/2020 00:00:00','01/11/2020 00:00:00'), the filter should be applied on end date column.
How can i achieve this ?
create or replace PROCEDURE CVP_TEST(STARTDATE VARCHAR, ENDDATE VARCHAR) AS
st_dt TIMESTAMP := TO_TIMESTAMP(STARTDATE, 'dd/mm/yyyy hh24:mi:ss');
end_dt TIMESTAMP := TO_TIMESTAMP(ENDDATE, 'dd/mm/yyyy hh24:mi:ss');
cursor cur1(start_time timestamp, end_time timestamp)
is
SELECT
A.LGST_GRP_CD ,
(case when A.FRST_SHPG_LOC_CD like 'D%' then A.FRST_SHPG_LOC_CD
when (A.FRST_SHPG_LOC_CD not like 'D%' and A.DMCL_CD is not null and length(dmcl_t.shpgloc_cd) <= 4) then dmcl_t.shpgloc_cd
--when (A.FRST_SHPG_LOC_CD not like 'D%' and A.DMCL_CD is null and length(dom_carrier.shpgloc_cd) <= 4) then dom_carrier.shpgloc_cd
else A.FRST_SHPG_LOC_CD end) AS Dispatch_DC,
--ba_tmextra.FUN_GET_INT_DC(A.LD_LEG_ID) AS INT_DC,
'XXX' AS INT_DC,
A.CUR_OPTLSTAT_ID,
--V.CUR_STAT_ID,
A.TRIP_ID AS TRIP_ID,
A.LD_LEG_ID AS LOAD_ID,
A.CARR_CD AS CARRIER_ID,
A.SRVC_CD AS SERVICE_ID,
A.EQMT_TYP AS EQMT_TYP,
A.STRD_DTT AS START_DATE,
A.END_DTT AS END_DTT,
A.CRTD_DTT AS CRTD_DTT,
A.TRCTR_NUM as TRCTR_NUM,
A.DRVR as DRVR,
A.TRLR_NUM,
A.FRST_SHPG_LOC_CD,
A.LAST_SHPG_LOC_CD,
A.NUM_STOP as NUM_STOP,
--BA_TMEXTRA.FUN_GET_STOP_LOC(A.LD_LEG_ID, 0) STOP_LIST,
'XXX' AS STOP_LIST,
LD_MMO.PRTB_CTNT AS LD_COMMENTS,
A.MILE_DIST AS MILE_DIST,
A.TOT_SCLD_WGT AS TOTAL_WEIGHT,
A.ELPD_HRS,
CM.chrg_extl_code2 as chrg_extl_code2,
(case when CM.chrg_extl_code2 = 'HAUL' then C.CHRG_CD else null end) as Haul_Charge_ID,
(case when CM.chrg_extl_code2 = 'HAUL' then C.CHGD_UNIT_RATE else null end) as Planned_Rate,
(case when CM.chrg_extl_code2 = 'FUEL' then C.CHGD_UNIT_RATE else null end) as Fuel_Planned_Rate,
(case when CM.chrg_extl_code2 = 'FUEL' then C.CHRG_CD else null end) as Fuel_Surcharge_Type_ID,
(case when CM.chrg_extl_code2 = 'STOP' then C.CHRG_CD else null end) as Stop_Off_Charge_ID,
--C.CHRG_CD AS CHRG_CD,
--C.CHGD_UNIT_RATE AS PLANNED_RATE,
NVL(C.MNLY_OVRD_DLR,0) as CHRG_GRP_AMT
FROM JDATM_PROD.LD_LEG_T A, JDATM_PROD.CHRG_DETL_T C, JDATM_PROD.IA_DIST_MSTRCHRG CM,
JDATM_PROD.DMCL_T,
--DMCL_T DOM_CARRIER,
--VCHR_AP_T V,
JDATM_PROD.MMO_T LD_MMO
WHERE 1=1
AND A.LD_LEG_ID = C.LD_LEG_ID
AND C.CHRG_CD = CM.CHRG_CODE
AND C.CHRGDETL_TYP_ENU = 1
AND C.CHRG_LVL_ENU != 7
AND A.DMCL_CD=DMCL_T.DMCL_CD(+)
--AND DOM_CARRIER.CARR_CD(+)=A.CARR_CD
--AND V.VCHR_NUM (+) = C.VCHR_NUM_AP
--AND V.CUR_STAT_ID (+) != 825
and A.mmo_id = ld_mmo.mmo_id (+)
AND A.TRIP_ID IS NULL
--AND A.LD_LEG_ID = 1001038103
AND A.RATG_VLID_YN = 'T'
AND A.STRD_DTT BETWEEN start_time and end_time;
read_value cur1%ROWTYPE;
BEGIN
--dbms_output.put_line('Accepted:' || STARTDATE || ', ' || ENDDATE);
--dbms_output.put_line('Assigned:' || st_dt || ', ' || end_dt);
END;
You can use the OR condition as follows:
AND (
(P_IN_COLUMN_NAME = 'created date' AND A.CREATED_DTT BETWEEN start_time and end_time)
OR
(P_IN_COLUMN_NAME = 'planned date' AND A.PLANNED_DTT BETWEEN start_time and end_time )
)

procedure failurecompilation

I have an existing procedure which creates a table adwstg.switchhold_notificatn_stg
Suddenly it is throwing error in creating the table. I didn't change anything the statement.
First I thought the error is with ''No'' . So find & replace '' with '
But it is throwing error at line:
nvl(ba300_z51.sent_650_01, 'No') sent_650_01,
error(27,29): PLS-00103: Encountered the symbol "NO" when expecting one of the following: * & = - + ; < / > at in is mod remainder not rem <> or != or ~= >= <= <> and or like like2 like4 likec between || multiset member submultiset***
Below is the query :
create table adwstg.switchhold_notificatn_stg nologging parallel (degree 8) compress as
select distinct
bad3700.createdon,
bad3700.uc_pod_ext,
bpc.cacont_acc,
bad3700.notificatn,
bad3700.nfcat_code,
nvl(ba300_z51.sent_650_01, ''No'') sent_650_01,
decode(ba300_z51.sent_650_01,''No'',''--'',''Yes'',to_char(ba300_z51.ucswmsgdat,''yyyymmdd''),''--'') date_sent_650_01,
nvl(to_char(ba300_z51.ucswmsgtim,''hh24:mi:ss''),''--'') time_sent_650_01,
nvl(ba300_z52.received_650_02, ''No'') received_650_02,
decode(ba300_z52.received_650_02,''No'',''--'',''Yes'',to_char(ba300_z52.ucswmsgdat,''yyyymmdd''),''--'') date_received_650_02,
nvl(to_char(ba300_z52.ucswmsgtim,''hh24:mi:ss''),''--'') time_received_650_02,
nvl(ba300_z20.received_814_20, ''No'') received_814_20,
decode(ba300_z20.received_814_20,''No'',''--'',''Yes'',to_char(ba300_z20.ucswmsgdat,''yyyymmdd''),''--'') date_received_814_20,
nvl(to_char(ba300_z20.ucswmsgtim,''hh24:mi:ss''),''--'') time_received_814_20,
case
when trim(bad3700.nfcat_code) = ''SH01'' and zet.ext_ui is not null then ''ADDED''
when trim(bad3700.nfcat_code) = ''SH01'' and zet.ext_ui is null then ''NOT PROCESSED''
when trim(bad3700.nfcat_code) in (''SH02'',''SH03'') and zet.ext_ui is null then ''REMOVED''
when trim(bad3700.nfcat_code) in (''SH02'',''SH03'') and zet.ext_ui is not null then ''NOT PROCESSED''
else ''NOT PROCESSED''
end work_order_check
from
(select distinct *
from
(select
trunc(createdon) createdon,
trim(notificatn) notificatn,
trim(uc_pod_ext) uc_pod_ext,
trim(not_type) not_type,
trim(nfcat_code) nfcat_code,
row_number () over (partition by trim(uc_pod_ext),trunc(createdon) order by trim(notificatn) desc) rnum
from birpt.bic_azfc_ds3700
where upper(trim(not_type)) = ''SH''
and trim(bic_zdiscstat) = ''E0010'')
where rnum = 1
) bad3700
left outer join
(
select distinct ucinstalla, uc_pod_ext, datefrom, dateto
from birpt.bi0_qucinstalla
where objvers = ''A''
) bqi
on (trim(bad3700.uc_pod_ext) = trim(bqi.uc_pod_ext)
and trunc(bad3700.createdon) between bqi.datefrom and bqi.dateto)
left outer join
(
select distinct cacont_acc, ucinstalla, ucmovein_d, ucmoveoutd
from birpt.bi0_puccontract
where objvers = ''A''
) bpc
on (trim(bqi.ucinstalla) = trim(bpc.ucinstalla)
and trunc(bad3700.createdon) between bpc.ucmovein_d and bpc.ucmoveoutd)
left outer join
(select distinct *
from
(select
trim(ucswtpodex) ucswtpodex,
trunc(ucswmsgdat) ucswmsgdat,
ucswmsgtim,
case --650_01 CHECK
when trim(uc_mdcat) = ''Z51'' then ''Yes''
else ''No''
end sent_650_01,
row_number () over (partition by trim(ucswtpodex), trunc(ucswmsgdat) order by trunc(ucswmsgdat) desc, ucswmsgtim desc) rnum
from birpt.bic_azudeds0300
where trim(uc_mdcat) = ''Z51'')
where rnum = 1) ba300_z51
on (trim(bad3700.uc_pod_ext) = trim(ba300_z51.ucswtpodex)
and trunc(bad3700.createdon)= trunc(ba300_z51.ucswmsgdat))
left outer join
(select distinct *
from
(select
trim(ucswtpodex) ucswtpodex,
trunc(ucswmsgdat) ucswmsgdat,
ucswmsgtim,
case --650_02 CHECK
when trim(uc_mdcat) = ''Z52'' then ''Yes''
else ''No''
end received_650_02,
row_number () over (partition by trim(ucswtpodex), trunc(ucswmsgdat) order by trunc(ucswmsgdat) desc, ucswmsgtim desc) rnum
from birpt.bic_azudeds0300
where trim(uc_mdcat) = ''Z52'')
where rnum = 1) ba300_z52
on (trim(bad3700.uc_pod_ext) = trim(ba300_z52.ucswtpodex)
and trunc(bad3700.createdon)= trunc(ba300_z52.ucswmsgdat))
left outer join
(select distinct *
from
(select
trim(ucswtpodex) ucswtpodex,
trunc(ucswmsgdat) ucswmsgdat,
ucswmsgtim,
case --814_20 CHECK
when trim(uc_mdcat) = ''Z20'' then ''Yes''
else ''No''
end received_814_20,
row_number () over (partition by trim(ucswtpodex), trunc(ucswmsgdat) order by trunc(ucswmsgdat) desc, ucswmsgtim desc) rnum
from birpt.bic_azudeds0300
where trim(uc_mdcat) = ''Z20'')
where rnum = 1) ba300_z20
on (trim(bad3700.uc_pod_ext) = trim(ba300_z20.ucswtpodex)
and trunc(bad3700.createdon)= trunc(ba300_z20.ucswmsgdat))
left outer join
(select distinct ext_ui
from isurpt.zcs_esiid_tamper
) zet
on (trim(bad3700.uc_pod_ext) = trim(zet.ext_ui));
If you remove all doubled single quotes (perform replace function in any text editor), your code will be valid (as far as syntax is concerned).
I don't have your tables to test it, and there are too many of them with too many columns to create a test case by myself.
The question is: why did you double them in the first place?

Can't understand Oracle logic for Procedure

PROCEDURE DELETE_X1
IS
v_emp_unum VARCHAR2 (25);
BEGIN
/*FOR rec
IN (SELECT l2.*,
row_number ()
OVER (PARTITION BY case_uid,nvl(min_eff_date, eff_begin_date)
ORDER BY case_uid,nvl(min_eff_date, eff_begin_date))
rw,
ROWID rid
FROM (SELECT l1.*,
MIN (
CASE
WHEN eff_end_date = next_begin - 1
THEN
eff_begin_date
END)
OVER (PARTITION BY case_uid)
min_eff_date,
MAX (
CASE
WHEN previous_end <>
TO_DATE ('12/31/9999',
'mm/dd/yyyy')
AND previous_end + 1 = eff_begin_date
THEN
eff_end_date
END)
OVER (PARTITION BY case_uid)
max_end_date
FROM (SELECT GT.*,
LEAD (
EFF_begin_DATE)
OVER (PARTITION BY CASE_UID
ORDER BY EFF_BEGIN_DATE)
next_begin,
LAG (
EFF_end_DATE)
OVER (PARTITION BY CASE_UID
ORDER BY EFF_BEGIN_DATE)
previous_end
FROM TABLE_OUTPUT GT
WHERE SRC = 'ERICSSON' AND STATUS_CODE = 'X1') l1)
l2)*/
FOR rec
IN (SELECT l2.*,
ROW_NUMBER ()
OVER (PARTITION BY case_uid, min_eff_date, max_end_date
ORDER BY case_uid, min_eff_date, max_end_date)
rw,
ROWID rid
FROM (SELECT l1.*,
(MAX (
start_at)
OVER (
PARTITION BY case_uid
ORDER BY EFF_BEGIN_DATE
ROWS UNBOUNDED PRECEDING))
min_eff_date,
(MIN (
break_at)
OVER (
PARTITION BY case_uid
ORDER BY EFF_BEGIN_DATE
ROWS BETWEEN CURRENT ROW
AND UNBOUNDED FOLLOWING))
max_end_date
FROM (SELECT GT.*,
(CASE
WHEN LAG (
EFF_end_DATE)
OVER (
PARTITION BY CASE_UID
ORDER BY EFF_BEGIN_DATE) =
EFF_BEGIN_DATE
- 1
THEN
NULL
ELSE
EFF_BEGIN_DATE
END)
start_at,
(CASE
WHEN LEAD (
EFF_BEGIN_DATE)
OVER (
PARTITION BY case_uid
ORDER BY EFF_BEGIN_DATE) =
CASE
WHEN EFF_end_DATE <>
TO_DATE (
'12/31/9999',
'mm/dd/yyyy')
THEN
EFF_end_DATE
+ 1
ELSE
EFF_end_DATE
END
THEN
NULL
ELSE
EFF_end_DATE
END)
break_at
FROM TABLE_OUTPUT GT
WHERE SRC = 'ERICSSON' AND STATUS_CODE = 'X1') l1)
l2)
The part of code is commented out an re written.
commented out code output
OFF TIME 1/1/2017 1/7/2017 X1
OFF TIME 1/8/2017 2/1/2017 X1
New code output
OFF TIME 1/1/2017 2/1/2017 X1
NORMAL 2/2/2017 2/2/2017 AB
OFF TIME 2/20/2017
The LAG function is used to access data from a previous row.
The LEAD function is used to return data from rows further down the result set.
aggregate functions MIN , MAX
i am pretty confused with the flow of code.
I can't understand that code on the whole please explain the logic for the code

No Results returned for ROW_NUMBER() query

I am getting "no results returned" for the following query:
SELECT
Referer
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY CT.Referer ASC) AS RowNum,
CT.Referer, CT.LastModified
FROM
ClickTrack CT
JOIN
OrderTrack OT ON OT.ClickTrackID = CT.ClickTrackID
GROUP BY
CT.Referer, CT.LastModified
HAVING
LEN(CT.Referer) > 0) as num
WHERE
RowNum = 1
AND LastModified BETWEEN '07/06/2013' and '08/05/2013'
Curiously, when I leave off RowNum = 1, I get the full list of values. I need to get one at a time though to assign to a variable and drop into a temporary table.
The end query will be in a while loop using scalar variables in place of the date ranges and RowNum comparison.
Any help is appreciated. Thank you!
I'm thinking RowNum 1 may not have a date between your selections. Maybe put the date selection inside so that you know that the first one matches.
SELECT Referer
FROM (SELECT ROW_NUMBER() OVER (ORDER BY CT.Referer ASC)
AS RowNum, CT.Referer, CT.LastModified
FROM ClickTrack CT
JOIN OrderTrack OT ON OT.ClickTrackID = CT.ClickTrackID
WHERE CT.LastModified BETWEEN '07/06/2013' and '08/05/2013'
GROUP BY CT.Referer, CT.LastModified
HAVING LEN(CT.Referer) > 0) as num
WHERE RowNum = 1

Resources