For the following requirements: User table: Users (user_id int)
Order table: ORDER_TB (user_id int, or_time date, Or_money double)
Ask the following users:
One month under single, February not under the order of March next single user
The following indicators:
March Order amount greater than 100, order amount for the first and last order in March. (per User's OH)
The following answers are available online, much better than the hundreds of lines I wrote earlier. I don't know if there's any other solution.
With US1 as (select User_id,or_time,or_money,
SUM (NVL (case when To_char (Or_time, ' MM ') = ' 1 end,0) "Over (partition by USER_ID) M1,
SUM (NVL (case when To_char (Or_time, ' MM ') = ' 1 end,0]) over (partition by user_id) M2,
SUM (NVL (case when To_char (Or_time, ' MM ') = ' 1 end,0) "Over (partition by user_id) M3,
SUM (case when To_char (Or_time, ' MM ') = Or_money > 1 end) over (partition by user_id) CNT
From ORDER_TB
where Or_time between To_date (' 20170101 ', ' YYYYMMDD ') and to_date (' 20170331 ', ' YYYYMMDD ')),
Mo1 as (select DISTINCT user_id,
case where To_char (Or_time, ' MM ') = First_value (Or_money) over (partition by user_id ORDER by Or_time) End F1V,
case where To_char (Or_time, ' MM ') = Last_value (Or_money) over (partition by user_id order by Or_time range between unbounded preceding and unbounded following) end F3L,
CNT from (SELECT * from US1 where m2=0 and M1 >0 and M3 >0 and or_time between To_date (' 20170301 ', ' YYYYMMDD ') and to _date (' 20170331 ', ' YYYYMMDD '))
Solution to the Order form SQL encountered by the interview