SQL Cross Table

Source: Internet
Author: User

Before doing the product horizontal display, have seen the comments said to use the cross-table.

The company recently needed to make a data summary of the order form, and colleagues gave a reference to the SQL

SELECT * FROM (select COUNT (1) as is locked from tbl_order where Orderlock = 1) as A,
(select COUNT (1) as unlocked from tbl_order where Orderlock = 0) as B,
(select COUNT (1) as is not shipped from Tbl_order where Pushstatus=1 and statusid=14) as C,
(select COUNT (1) as pending delivery from Tbl_order where Pushstatus=1 and statusid=14 and Isshipments=1) as D,
(select COUNT (1) as has been shipped from Tbl_order where statusid=4) as E,
(select COUNT (1) as is unassigned from tbl_order where Statusid = Pushstatus<>3 and pushstatus=0) as F,
(select COUNT (1) as has been assigned from Tbl_order where Pushstatus=1) as G,
(select COUNT (1) as has been pushed from Tbl_order where pushstatus=2) as H

Read the following SQL

SELECT
COUNT (case if Orderlock=1 then 1 ELSE NULL END) as ' locked ',
COUNT (case if orderlock=0 then 1 ELSE NULL END) as ' unlocked ',
COUNT (case if Pushstatus=1 and statusid=14 then 1 ELSE NULL END) as ' not shipped ',
COUNT (case if Pushstatus=1 and statusid=14 and Isshipments=1 then 1 ELSE NULL END) as ' pending shipment ',
COUNT (case if statusid=4 then 1 ELSE NULL END) as ' shipped ',
COUNT (case if Statusid = pushstatus<>3 and pushstatus=0 then 1 ELSE NULL END) as ' not assigned ',
COUNT (case if Pushstatus=1 then 1 ELSE NULL END) as ' allocated ',
COUNT (case if pushstatus=2 then 1 ELSE NULL END) as ' pushed '
From Tbl_order

The query table is used only once compared to the reference SQL above, and if there is a where condition that is more conducive to expansion.

Direct SQL Execution Plan reference:

This gap is obvious, and you are welcome to propose more optimized SQL.

SQL Cross Table

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.