Try to do it every day. SQL (ii) Count sum case-then-group by

Source: Internet
Author: User

1.

Select SUM (case if a.[status]=0 then 1 ELSE 0 End) as A1,
SUM (case A.[status] When 1 then 1 ELSE 0 end) as A2,
SUM (case A.[status] When 2 then 1 ELSE 0 end) as A3,

A.userid,c.trainid
From Coursewarelogmiddle A join Lessondetail B
On A.lessonid=b.id
Join Lessonclass C on B.classid=c.classid
where a.userid=9143 and c.trainid=81
GROUP BY A.userid,c.trainid

Row-to-column, although the case is used and then it needs to be used in conjunction with group.

The first sum I wrote is count but Count is will count all.

So decided to sum up, it is correct.

This method can calculate the quantity.

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.