SELECT RefundTicketId,
PSerialNumber ,
PaymentAmount,
PosStatus,
count(*) over( partition by PSerialNumber) AS TotalCnt,
sum(PaymentAmount) over( partition by PSerialNumber) AS TotalAmt,
sum(case when PosStatus='已保全' then 1 else 0 end)over(partition by PSerialNumber) AS POSCnt,
sum(case when PosStatus='已转账' then 1 else 0 end)over(partition by PSerialNumber) AS TranCnt,
sum(case when PosStatus='已转账' then PaymentAmount else 0 end) over( partition by PSerialNumber) AS TranAmt
FROM [CHCPay_DB].[dbo].[tb_ReClaimsTransferRefundTicket]
这是现在的sql语句,请问下该怎么实现啊
16 个解决方案
#1
select * from (
SELECT
RefundTicketId,
PSerialNumber ,
PaymentAmount,
PosStatus,
count(*) over( partition by PSerialNumber) AS TotalCnt,
sum(PaymentAmount) over( partition by PSerialNumber) AS TotalAmt,
sum(case when PosStatus='已保全' then 1 else 0 end)over(partition by PSerialNumber) AS POSCnt,
sum(case when PosStatus='已转账' then 1 else 0 end)over(partition by PSerialNumber) AS TranCnt,
sum(case when PosStatus='已转账' then PaymentAmount else 0 end) over( partition by PSerialNumber) AS TranAmt,
row_number()over(partition by PSerialNumber order by PSerialNumber)as ID
FROM [CHCPay_DB].[dbo].[tb_ReClaimsTransferRefundTicket]
)as a where a.id=1
这是方式可以实现,但因某种原因无法使用,请问下还有他方法吗
CREATE VIEW V_ReClaimsTransferRefundTicket
SELECT PSerialNumber ,
count(*) over( partition by PSerialNumber) AS TotalCnt,
sum(PaymentAmount) over( partition by PSerialNumber) AS TotalAmt,
sum(case when PosStatus='已保全' then 1 else 0 end)over(partition by PSerialNumber) AS POSCnt,
sum(case when PosStatus='已转账' then 1 else 0 end)over(partition by PSerialNumber) AS TranCnt,
sum(case when PosStatus='已转账' then PaymentAmount else 0 end) over( partition by PSerialNumber) AS TranAmt
FROM [CHCPay_DB].[dbo].[tb_ReClaimsTransferRefundTicket]
GROUP BY PSerialNumber
CREATE VIEW V_ReClaimsTransferRefundTicket
SELECT PSerialNumber ,
count(*) over( partition by PSerialNumber) AS TotalCnt,
sum(PaymentAmount) over( partition by PSerialNumber) AS TotalAmt,
sum(case when PosStatus='已保全' then 1 else 0 end)over(partition by PSerialNumber) AS POSCnt,
sum(case when PosStatus='已转账' then 1 else 0 end)over(partition by PSerialNumber) AS TranCnt,
sum(case when PosStatus='已转账' then PaymentAmount else 0 end) over( partition by PSerialNumber) AS TranAmt
FROM [CHCPay_DB].[dbo].[tb_ReClaimsTransferRefundTicket]
GROUP BY PSerialNumber
SELECT a.* FROM
(SELECT RefundTicketId,
PSerialNumber ,
PaymentAmount,
PosStatus,
count(*) over( partition by PSerialNumber) AS TotalCnt,
sum(PaymentAmount) over( partition by PSerialNumber) AS TotalAmt,
sum(case when PosStatus='已保全' then 1 else 0 end)over(partition by PSerialNumber) AS POSCnt,
sum(case when PosStatus='已转账' then 1 else 0 end)over(partition by PSerialNumber) AS TranCnt,
sum(case when PosStatus='已转账' then PaymentAmount else 0 end) over( partition by PSerialNumber) AS TranAmt
FROM [CHCPay_DB].[dbo].[tb_ReClaimsTransferRefundTicket]) a,
(SELECT max(RefundTicketId) AS RefundTicketId FROM [CHCPay_DB].[dbo].[tb_ReClaimsTransferRefundTicket] GROUP BY PSerialNumber) b
WHERE a.RefundTicketId=b.RefundTicketId
#1
select * from (
SELECT
RefundTicketId,
PSerialNumber ,
PaymentAmount,
PosStatus,
count(*) over( partition by PSerialNumber) AS TotalCnt,
sum(PaymentAmount) over( partition by PSerialNumber) AS TotalAmt,
sum(case when PosStatus='已保全' then 1 else 0 end)over(partition by PSerialNumber) AS POSCnt,
sum(case when PosStatus='已转账' then 1 else 0 end)over(partition by PSerialNumber) AS TranCnt,
sum(case when PosStatus='已转账' then PaymentAmount else 0 end) over( partition by PSerialNumber) AS TranAmt,
row_number()over(partition by PSerialNumber order by PSerialNumber)as ID
FROM [CHCPay_DB].[dbo].[tb_ReClaimsTransferRefundTicket]
)as a where a.id=1
这是方式可以实现,但因某种原因无法使用,请问下还有他方法吗
CREATE VIEW V_ReClaimsTransferRefundTicket
SELECT PSerialNumber ,
count(*) over( partition by PSerialNumber) AS TotalCnt,
sum(PaymentAmount) over( partition by PSerialNumber) AS TotalAmt,
sum(case when PosStatus='已保全' then 1 else 0 end)over(partition by PSerialNumber) AS POSCnt,
sum(case when PosStatus='已转账' then 1 else 0 end)over(partition by PSerialNumber) AS TranCnt,
sum(case when PosStatus='已转账' then PaymentAmount else 0 end) over( partition by PSerialNumber) AS TranAmt
FROM [CHCPay_DB].[dbo].[tb_ReClaimsTransferRefundTicket]
GROUP BY PSerialNumber
CREATE VIEW V_ReClaimsTransferRefundTicket
SELECT PSerialNumber ,
count(*) over( partition by PSerialNumber) AS TotalCnt,
sum(PaymentAmount) over( partition by PSerialNumber) AS TotalAmt,
sum(case when PosStatus='已保全' then 1 else 0 end)over(partition by PSerialNumber) AS POSCnt,
sum(case when PosStatus='已转账' then 1 else 0 end)over(partition by PSerialNumber) AS TranCnt,
sum(case when PosStatus='已转账' then PaymentAmount else 0 end) over( partition by PSerialNumber) AS TranAmt
FROM [CHCPay_DB].[dbo].[tb_ReClaimsTransferRefundTicket]
GROUP BY PSerialNumber
SELECT a.* FROM
(SELECT RefundTicketId,
PSerialNumber ,
PaymentAmount,
PosStatus,
count(*) over( partition by PSerialNumber) AS TotalCnt,
sum(PaymentAmount) over( partition by PSerialNumber) AS TotalAmt,
sum(case when PosStatus='已保全' then 1 else 0 end)over(partition by PSerialNumber) AS POSCnt,
sum(case when PosStatus='已转账' then 1 else 0 end)over(partition by PSerialNumber) AS TranCnt,
sum(case when PosStatus='已转账' then PaymentAmount else 0 end) over( partition by PSerialNumber) AS TranAmt
FROM [CHCPay_DB].[dbo].[tb_ReClaimsTransferRefundTicket]) a,
(SELECT max(RefundTicketId) AS RefundTicketId FROM [CHCPay_DB].[dbo].[tb_ReClaimsTransferRefundTicket] GROUP BY PSerialNumber) b
WHERE a.RefundTicketId=b.RefundTicketId