Solution to the Order form SQL encountered by the interview

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.