简体   繁体   中英

SQL query to select min date

I have a SQL table called transaction where different type of transactions are stored eg Payment arrangements, sent letter and so on. I have ran a query:

 SELECT TOP 6 Case_Ref as Case Ref,TrancRefNO as Tranc RefNO, Date_CCYYMMDD, LetterSent, Arr_Freq,
SMS_Sent_CCYYMMDD
    From Transaction
    Where  (LEN(LetterSent  ) >0 OR Arr_Freq >0)

The table looks something like this

Case Ref  Tranc RefNO   Date_CCYYMMDD  LetterSent  Arr_Freq      SMS_Sent_CCYYMMDD
--------  -----------   ----------     ----------  ----------    -----------------
15001     100           20140425       Stage1                       
15001     101           20140430       Stage2                     
15001     102           20140510                   30              
15001     104           20140610                   30       
15002     105           20140425       Stage1    
15002     106           20140610                   30  

From the table, I can clearly see that a letter was sent on '20140430' for the case 15001 and the person started arrangements on '20140510'. And a letter was sent on '20140425' for the case 15001 and the person made arrangements on on '20140610'.

I'm trying to create a excel report using C# which will show the total number of cases got arrangements after getting a letter and total number of cases for arrangements after receiving a SMS.

I have tried

select MAX(ROW_NUMBER() OVER(ORDER BY o3.Date_CCYYMMDD  ASC)), o3.
from
(
    select o.TrancRefNO, o.Date_CCYYMMDD  , sq.LetterSent  
    from Transaction o
    join Transaction sq on sq.TrancRefNO= o.TrancRefNO
    and sq.Date_CCYYMMDD  <= o.Date_CCYYMMDD  
    where o.Arr_Freq      >0
    and len(sq.LetterSent  ) > 0
) o2
join Transaction  o3 on o3.TrancRefNO= o2.TrancRefNO

But gives me an error :

Msg 4109, Level 15, State 1, Line 2
Windowed functions cannot be used in the context of another windowed function or aggregate.

Ps Title will need to be changed as I don't know what to call it.

SELECT * FROM table as t1
WHERE (LetterSent != '' OR SMS_SENT_CCYYMMDD != '')
AND (SELECT COUNT(*) FROM table AS t2
        WHERE t1.case_ref = t2.case_ref
        AND t1.DATE_CCYYMMDD < t2.DATE_CCYYMMDD
        AND Arr_freq > 0) > 1

My assumptions based on what I could glean from your post:

  1. ARR_FREQ!='' indicates that some time of arrangement was made at the specified date
  2. Since NULL is not shown, I'm assuming all values are ''. With null values you will have to use a coalesce command

Hope this helps. I'm not sure about your second question (max date) in the comments. You would need to explain it a bit more.

SELECT * FROM table where Date = 
               (SELECT MIN(Date) from table)                   
SELECT TOP 1 ROW_NUMBER() OVER(ORDER BY Date_CCYYMMDD ASC), mytable.*
FROM mytable

or just

SELECT TOP 1 * FROM mytable
ORDER BY Date_CCYYMMDD ASC

but i guess, you want to get not the MIN date overall, but group by first

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM