How to retrieve data from SQL Server as required below?

老子叫甜甜 提交于 2019-12-11 06:02:13

问题


I have a table like this :

  CustName    Country    RecordedTime
 ---------------------------------------------
  Alex        Australia  2018-Jun-01 08:00 AM
  Alex        China      2018-Jun-01 10:00 AM
  Alex        Japan      2018-Jun-01 11:00 AM
  John        Australia  2018-Jun-01 08:00 AM
  John        China      2018-Jun-02 08:00 AM
  Bob         Australia  2018-Jun-02 09:00 AM
  Bob         Brazil     2018-Jun-03 09:50 AM

If the record is brand new in the system then it should show 'ADD' & 'NEW' in Audit and history fields (two additional fields in result set) for the given date.

If the record got edited twice that day then it should show two entries with 'ADD' & 'CHANGE ' in Audit fields and 'BEFORE' & 'CURRENT' in History state fields respectively for the given date.

For example this is how my result should appear;

When I pass input date as 2018-Jun-01 then the output should be as below:

   CustName    Country    RecordedTime           Audit    History
  ----------------------------------------------------------------
   Alex        Australia  2018-Jun-01 08:00 AM   ADD      NEW
   Alex        China      2018-Jun-01 10:00 AM   CHANGE   BEFORE
   Alex        Japan      2018-Jun-01 11:00 AM   CHANGE   CURRENT
   John        Australia  2018-Jun-01 08:00 AM   ADD      NEW

When I pass the input date as 2018-Jun-02 then the output should be as below:

   CustName    Country    RecordedTime           Audit    History
  -----------------------------------------------------------------
   John        China      2018-Jun-02 08:00 AM   CHANGE   CURRENT
   Bob         Australia  2018-Jun-02 09:00 AM   ADD      NEW

When I pass input date as 2018-Jun-02 then the output should be as below:

   CustName    Country    RecordedTime           Audit    History
  ----------------------------------------------------------------
   Bob         Brazil     2018-Jun-03 09:50 AM   CHANGE   CURRENT

I tried many ways but still I'm missing some scenarios to achieve this. Can someone please shed some light on this?


回答1:


One way to do it is via a cte like below where we have row_number() function to track the sequence both ways.

See live demo

; with cte as 
(
select *, rn= row_number() over(partition by CustName order by RecordedTime),
rn2=row_number() over(partition by CustName order by RecordedTime desc)
from records
    )
, cte2 as
(
select *, audit='New', History='Change' from cte where rn=1
    union 
select *, audit='Change', History='Current' from cte where rn2=1 and rn<>1
    union
select *, audit='Change', History='before' from cte where rn>1 and rn2<>1
)

select 
    CustName,
    Country,
    RecordedTime,
    audit,
    History
from cte2
order by  CustName,RecordedTime



回答2:


I would simply use case expressions.

select t.*,
       (case when seqnum = 1 then 'ADD' else 'CHANGE' end) as audit,
       (case when seqnum = 1 then 'NEW'
             when seqnum_day = 1 then 'CURRENT'
             else 'BEFORE'
        end) as history
from (select t.*,
             row_number() over (partition by custname order by recordedtime) as seqnum,
             row_number() over (partition by custname, cast(recordedtime as date) order by recordedtime desc) as seqnum_day
      from t
     ) t;

sqlfiddle:http://sqlfiddle.com/#!18/43c08/27




回答3:


You can try.

CASE WHEN and RANK with Windows function

;WITH CTE (CustName,Country,RecordedTime,rn) AS(
  SELECT *,RANK() OVER(PARTITION BY CustName ORDER BY RecordedTime) rn
  FROM T
)
SELECT t.*,
       (CASE WHEN rn = 1 then 'ADD' ELSE 'CHANGE' END) 'Audit',
       (CASE 
          WHEN rn = 1  then 'NEW'
          WHEN t2.mRn = rn then 'CURRENT' 
       ELSE 'BEFORE' END) 'History'
FROM CTE t LEFT JOIN  (
  SELECT MAX(rn) mRn,CustName  FROM CTE GROUP BY CustName
) t2 on t2.mRn = t.rn and t2.CustName = t.CustName
WHERE CONVERT(char(10), RecordedTime,126) = '2018-06-02'

sqlfiddle:http://sqlfiddle.com/#!18/43c08/26



来源:https://stackoverflow.com/questions/51030093/how-to-retrieve-data-from-sql-server-as-required-below

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!