Use group by and with to produce special results

Source: Internet
Author: User

Raw data is as follows:

Date Status
2012-01-01 Win
2012-01-01 Win
2012-01-01 Win
2012-01-01 Lose
2012-01-01 Lose
2012-01-02 Win
2012-01-02 Win
2012-01-02 Lose

 

The query is shown as follows:

Date Win Lose
2012-01-01 3 2
2012-01-02 2 1

 

View code

 1 With tbdate
2 (
3 Select date from tbusers group by date
4 ),
5 Tbwin
6 (
7 Select date, count (statu) as win from tbusers Where Statu = ' Win ' Group by date
8 ),
9 Tblose
10 (
11 Select date, count (statu) as lose from tbusers where statu = ' Lose ' Group by date
12 )
13 Select D. Date, T. Win, L. Lose from tbwin T, tblose L, tbdate d Where D. Date = T. Date and L. Date = D. Date

Another type:

View code

 1       With T As 
2 (
3 Select Date, Count (Statu) As Win From Tbusers Where Statu = ' Win ' Group By Date
4 ),
5 DAs (
6 Select Date, Count (Statu) As Lose From Tbusers Where Statu = ' Lose ' Group By Date
7 )
8 Select T. Date, T. Win, D. Lose From T, d Where T. Date = D. Date

 

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.