If I case when then select it by first_value saparately then it works, but it I combine First_value(case when ....) then it not work althought the code still run without syntax error
My try, I want to find latest overdue date, it only show null
FIRST_VALUE(
CASE
WHEN inv.aging_period = 0
AND is_tad_paid = 0
AND is_mad_paid = 0
AND inv.min_amount_due > 0 THEN
inv.due_date
ELSE
NULL
END
)
OVER(PARTITION BY inv.account_id
ORDER BY inv.DUE_DATE DESC NULLS LAST
) AS latest_overdue_date,
If I try this saparately, it works:
select sub.*, first_value(ALL_OVER_DUE_DAY) over (partition by account_id order by ALL_OVER_DUE_DAY desc nulls last) as latest_over_due2
from (select
CASE
WHEN inv.aging_period = 0
AND is_tad_paid = 0
AND is_mad_paid = 0
AND inv.min_amount_due > 0 THEN
inv.due_date
ELSE
NULL
END AS ALL_OVER_DUE_DAY from t1 ) SUB
sample data, my resul column is the last column
Replace First_value with MAX(), add unbounded preceeding... like bellow
max(
CASE
WHEN inv.aging_period = 0
AND is_tad_paid = 0
AND is_mad_paid = 0
AND inv.min_amount_due > 0 THEN
inv.due_date
ELSE
NULL
END
)
OVER(PARTITION BY inv.account_id
ORDER BY inv.DUE_DATE DESC NULLS LAST
ROWS BETWEEN
UNBOUNDED PRECEDING
AND
UNBOUNDED following
)
Related
My query is as follows
SELECT HEADER_TABLE.SEGMENT1,
LINES_TABLE.LINE_NUM,
CASE
WHEN ( HEADER_TABLE.REVISION_NUM = '0'
AND HEADER_TABLE.PRINT_COUNT = '0')
THEN
'Unavailable'
ELSE
NVL (ACK_TABLE.ACK_TYPE, 'Absent')
END
AS X_ACK_TYPE,
ACK_TABLE.GXS_DATE
FROM HEADER_TABLE,
LINES_TABLE,
(SELECT po_number,
po_line_number,
gxs_date,
po_ack_filename,
ack_type
FROM (SELECT po_number,
po_line_number,
gxs_date,
po_ack_filename,
ack_type,
ROW_NUMBER ()
OVER (PARTITION BY po_number ORDER BY gxs_date DESC)
rn
FROM xxcmst_po_ack_from_gxs_stg)
WHERE rn = 1) ACK_TABLE,
(SELECT PO_NUMBER FROM XXCMST.XXCMST_ACTION_TABLE_ACKNOWLEDGEMENT) ACTION_TABLE
WHERE HEADER_TABLE.PO_HEADER_ID = LINES_TABLE.PO_HEADER_ID
AND HEADER_TABLE.SEGMENT1 = ACK_TABLE.PO_NUMBER(+)
AND HEADER_TABLE.SEGMENT1 = ACTION_TABLE.PO_NUMBER(+)
AND LINES_TABLE.LINE_NUM = ACK_TABLE.PO_LINE_NUMBER(+)
AND HEADER_TABLE.SEGMENT1 = '100';
This is giving me 6 records with 1 GXS_DATE and X_ACK_TYPE = 'Absent'. The RN function is needed here to pull 1 record only from the subquery but the requirement is to have all the 6 records have the same date and ACK_TYPE which is not happening. How can I achieve this? Please refer to the below screenshot and I need X_ACK_TYPE = AK for all the 6 LINE_NUMs and GXS_DATE = 3/6/2020 for all these 6 records.
My current data screenshot here
Instead of
ACK_TABLE.GXS_DATE
in SELECT clause use the LAG function as follows:
CASE WHEN ACK_TABLE.GXS_DATE IS NOT NULL
THEN ACK_TABLE.GXS_DATE
ELSE LAG(ACK_TABLE.GXS_DATE IGNORE NULLS)
OVER (PARTITION BY HEADER_TABLE.SEGMENT1 ORDER BY LINES_TABLE.LINE_NUM )
END AS GXS_DATE
or If there will be always one value of ACK_TABLE.GXS_DATE exists per HEADER_TABLE.SEGMENT1 then you can simply write it as
MIN(ACK_TABLE.GXS_DATE)
OVER (PARTITION BY HEADER_TABLE.SEGMENT1) AS GXS_DATE
-- Update --
for ACK_TYPE, You need to apply the same logic in ELSE portion of your CASE statement from the original query as follows:
Replace this:
ELSE
NVL (ACK_TABLE.ACK_TYPE, 'Absent')
END
With this:
ELSE
NVL (MIN(ACK_TABLE.ACK_TYPE)
OVER (PARTITION BY HEADER_TABLE.SEGMENT1), 'Absent')
END
I need to find the date when the hit = 1, and when the hit moved from
1 to 0.Here is the table
Once I use this code. For the following table
select t1.id,t1.hit,min(datehit_1) as Hit_1, min(dateHit_0)as modelHit_0 from (
select
id,
dateHit_0 = (case when hits = 0 then date else null end),
datehit_1 = (case when hits = 1 then date else null end),
datacontrol_modelhits
from dmt.dqm_std_model_output_history
where id = 10 ) as t1
group by t1.id, t1.hit
But Unfortunately, I am getting the this output:
But I need to get this output. Want to find the last change for hit =0 which is 23/10/2019.
I need to find the date when the hit = 1, and when the hit moved from 1 to 0.Here is the table
I think you want:
select t.*
from (select t.*,
lag(hit) over (partition by id order by coalesce(datehit_0, datehit_1) as prev_hit,
lead(hit) over (partition by id order by coalesce(datehit_0, datehit_1) as next_hit
from t
) t
where (hit = 1 and next_hit = 0) or
(hit = 0 and prev_hit = 1);
If you want only the most recent two rows:
select top (2) t.*
from (select t.*,
lag(hit) over (partition by id order by coalesce(datehit_0, datehit_1) as prev_hit,
lead(hit) over (partition by id order by coalesce(datehit_0, datehit_1) as next_hit
from t
) t
where (hit = 1 and next_hit = 0) or
(hit = 0 and prev_hit = 1)
order by coalesce(datehit_0, datehit_1) desc;
I have the following SQL:
SELECT *
FROM forum_l.entries
WHERE bindningstid = 1
ORDER BY
CASE
WHEN rabatt IS NULL THEN 1
ELSE 0
END, rabatt DESC,
CASE
WHEN paslag IS NULL THEN 1
ELSE 0
END, paslag ASC
So I first have a value (rabatt) which I want to order desc, then I want to order the field paslag Asc but still keep null last.
You should replace cases:
SELECT *
FROM forum_l.entries
WHERE bindningstid = 1
ORDER BY
CASE
WHEN rabatt IS NULL THEN 0
ELSE 1
END, rabatt DESC,
CASE
WHEN paslag IS NULL THEN 0
ELSE 1
END, paslag ASC
I have the following SQL code:
SELECT EmployeeID,
SUM(CASE
WHEN Error1 = '0'
THEN 1
ELSE 0
END + CASE
WHEN Error2 = '0'
THEN 1
ELSE 0
END + CASE
WHEN Error3 = '0'
THEN 1
ELSE 0
END) AS TotalErrors
FROM SubmittedDocuments
GROUP BY EmployeeID
The statement should calculate the number of errors in the table for each employee. However, there is another column in the table SubmittedDocuments named "DocumentName". How could I write a statement that only counts errors for the first instance of each DocumentName? (Or only counts for the one with the lowest "SubmittedID", the unique identifier)
Sorry if anything in unclear, I will attempt to clear up any confusion in the comments.
I might have not got this right from your question, but I think this should work. If you could show some sample data and expected output then we can definitely have a sure answer.
SELECT EmployeeID
SUM(CASE WHEN DocIDErr > 1 THEN 0 ELSE 1 END)
FROM
(SELECT EmployeeID
, Error1
, ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY DocumentName) AS DocIDErr
FROM SubmittedDocuments) AS RS
GROUP BY EmployeeID
I don't think you want aggregation. I think you just want to select the first document for each name.
If so, this may be what you want:
select sd.*,
((case when error1 = '0' then 1 else 0 end) +
(case when error2 = '0' then 1 else 0 end) +
(case when error3 = '0' then 1 else 0 end)
) as numerrors
from (select sd.*,
row_number() over (partition by documentname order by submittedid) as seqnum
from SumittedDocuments sd
) sd
where seqnum = 1;
I'm getting the error " ORA-00907: missing right parenthesis" at row: 7, column: 6
when I launch this query on ORACLE 11g DB:
SELECT B.ID_COND AS IDCOND,B.ID_PAYMENT AS IDPAYMENT, B.DT_SCAD AS DATASCAD, B.DT_PAYMENT AS DATAPAYMENT,B.DE_CHANNELPAG AS CHANNELPAYMENT, B.DT_STARTVALID AS DATASTART, B.DT_ENDVALID AS DATAEND,
B.IM_TOTAL AS TOTAL, B.ST_PAYMENT AS STATECOND, B.CO_CIP AS codCIP, B.ID_PEND AS IDPEND, B.TI_PAYMENT AS TIPOPAYMENT,
B.cause_PAYMENT AS causaleCOND, B.IM_PAYMENT AS amountPAYMENT, B.DE_MIDDLEPAYMENT AS MIDDLEPAYMENT, B.DE_NOTEPAYMENT as notePAYMENT,
(select st_PAYMENT from QLT.paymnts P
where P.ID_COND=B.ID_COND
AND ROWNUM <= 1
order by (CASE WHEN P.st_PAYMENT='ES' THEN 1 ELSE 0 END) DESC, TS_INSMNT DESC
) AS STATEPAYMENT
FROM QLT.JLTCOPD B
WHERE B.ID_PEND = '269' AND B.TI_PAYMENT = 'S'
ORDER BY B.DT_SCAD ASC, DT_STARTVALID ASC
Trying to delete this row: 'order by (CASE WHEN P.st_PAYMENT='ES' THEN 1 ELSE 0 END) DESC, TS_INSMNT DESC' no errors are shown and a result is returned.
I'm trying to convert a DB2 query to ORACLE, the original DB2 inner query was:
(select st_PAYMENT from QLT.paymnts P where P.ID_COND=B.ID_COND order by (CASE WHEN P.st_PAYMENT='ES' THEN 1 ELSE 0 END) DESC, TS_INSMNT DESC fetch first 1 rows only)
How can I change it in order to do the porting without errors?
try replacing the inner query with:
(
select st_PAYMENT FROM
(
select st_PAYMENT from QLT.paymnts P
where P.ID_COND=B.ID_COND
order by (CASE WHEN P.st_PAYMENT='ES' THEN 1 ELSE 0 END) DESC, TS_INSMNT DESC
)
WHERE ROWNUM <= 1
)