I have this data with the following information:
AcctNumber VisitID TxnType TxnJournal TxnSvcDate TxnBchDate TxnNonChgCode TxnAmount TxnCount TxnInsurance ChgTotal
-------------------------------------------------------------------------------------------------------------------------------
csc V123456789 PAY CASUCL 2/8/2019 2/8/2019 CHECK -181.22 1 SP 3386.6
csc V123456789 PAY CASUCL 2/9/2019 2/9/2019 CHECK -175.48 1 SP 3386.6
csc V123456789 PAY CASUCL 2/8/2019 2/8/2019 CHECK -68.06 1 SP 3386.6
But I need the results to be as below, Where you can see all the columns are same except charges total in the below result, i want these charges to populate only once for same VisitID
OR AccountNumber
and remaining to be populated as Zero , Let me know how can we solve this through the query.
AcctNumber VisitID TxnType TxnJournal TxnSvcDate TxnBchDate TxnNonChgCode TxnAmount TxnCount TxnInsurance ChgTotal
-------------------------------------------------------------------------------------------------------------------------------
csc V123456789 PAY CASUCL 2/8/2019 2/8/2019 CHECK -181.22 1 SP 3386.6
csc V123456789 PAY CASUCL 2/9/2019 2/9/2019 CHECK -175.48 1 SP 0
csc V123456789 PAY CASUCL 2/8/2019 2/8/2019 CHECK -68.06 1 SP 0
In the inner query I have added a Row_Number function on the column AcctNumber. This function will return a row number group by AcctNumber. So you will be start from 1 for every AcctNumber. In the outer query I have added a Case When to get the ChgTotal based on ROw Number.
SELECT
AcctNumber, VisitID, TxnType, TxnJournal, TxnSvcDate, TxnBchDate,
TxnNonChgCode, TxnAmount, TxnCount, TxnInsurance, ChgTotal,
CASE
WHEN RowNumber = 1 THEN ChgTotal ELSE 0
END
FROM
(SELECT
ROW_NUMBER() OVER (PARTITION BY AcctNumber ORDER BY AcctNumber) RowNumber,
AcctNumber, VisitID, TxnType, TxnJournal, TxnSvcDate, TxnBchDate,
TxnNonChgCode, TxnAmount, TxnCount, TxnInsurance, ChgTotal
FROM
YOUR_TABLE_NAME) AS A
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.