The sort ting technology refers to a technology that can convert rows into columns. unsort Ting can be seen as the reverse operation of sort ting, which will rotate columns into rows.
First, make a test table.
- Create TableFormatting(
- OrderidInt Not Null,
- OrderdateDate Not Null,
- EmpidInt Not Null,
- Custid vahrchar(10) Not Null,
- QtyInt Not Null,
- Primary Key(Orderid,Orderdate));
Insert some test data:
- Insert IntoFormattingSelect1,'2017-01-02',3,'A',10;
- Insert IntoFormattingSelect2,'2017-04-02',2,'B',20;
- Insert IntoFormattingSelect3,'2017-2010',1,'A',30;
- Insert IntoFormattingSelect4,'2017-07-02',3,'D',40;
- Insert IntoFormattingSelect5,'2017-01-02',4,'A',20;
- Insert IntoFormattingSelect6,'2017-01-02',3,'B',30;
- Insert IntoFormattingSelect7,'2017-01-02',1,'C',40;
- Insert IntoFormattingSelect8,'2017-01-02',2,'A',10;
- Insert IntoFormattingSelect9,'2017-01-02',3,'B',20;
The formatting table can be viewed as a summary table, such as the shopping details of the online store.
This summary table shows the order number, order date, employee number, consumer number, and order quantity.
To further count the order quantity of each consumer each year based on this summary table, you may think of using groups to obtain results, such:
SelectCustid,Year(Orderdate) Year,Sum(Qty) Sum
- FromFormatting
- Group by custid,Year(Orderdate);
Result:
This is not intuitive. If you can get this result through rotation, it will be more intuitive and clear:
- SelectCustid,
- Ifnull(Sum(Case WhenA=2009ThenQtyEnd),0)\"2009 \",
- Ifnull(Sum(Case WhenA=2010ThenQtyEnd),0)\"2010 \",
- Ifnull(Sum(Case WhenA=2011ThenQtyEnd),0)\"2011 \"
- From (SelectCustid,Year(Orderdate)A,Qty
- FromFormatting)P
- Group by custid;
However, when many rotating elements speak, a long query string is generated.
To shorten the query string, a matrix table can be created in advance, containing the attributes of each column to be rotated:
- Create TableFormatting_temp(
- OrderyearIntPrimary Key,
- Y2009Int,
- Y2010Int,
- Y2011Int);
- Insert IntoFormatting_tempSelect2009,1,0,0;
- Insert IntoFormatting_tempSelect2010,0,1,0;
- Insert IntoFormatting_tempSelect2011,0,0,1;
Connect formatting and formatting_temp
- SelectCustid,
- Sum(Qty*Y2009) "2009",
- Sum(Qty*Y2010) "2010",
- Sum(Qty*Y2011) "2011"
- From(SelectCustid,Year(Orderdate)A,Qty
- FromFormatting)O
- Join formatting_temp P
- On o.A=P.Orderyear
- Group by custid;
[Unmarshting]
The unmarshting operation is to convert columns into rows, which is the inverse operation of grouping ting.
- Create TableUnmarshting(
- Custid vahrchar(10),
- Y2009Int,
- Y2010Int,
- Y2011Int);
- Insert IntoUnmarshting
- SelectCustid,
- Ifnull(Sum(Case WhenA=2009ThenQtyEnd),0)\"2009 \",
- Ifnull(Sum(Case WhenA=2010ThenQtyEnd),0)\"2010 \",
- Ifnull(Sum(Case WhenA=2011ThenQtyEnd),0)\"2011 \"
- From (SelectCustid,Year(Orderdate)A,Qty
- FromFormatting)P
- Group by custid;
Here we import the content after the previous formatting rotation to the unmarshting table.
Currently, the content of the unsorted ting table
Solutions and ideas:
To solve this problem, you need to rotate the column into a row. The trick here is to generate three copies of each row of data.
Each copy generates a column to be rotated. This process can be completed through the following cross join:
- Select *
- FromUnmarshting,(Select2009AsOrderyear
- Union allSelect2010
- Union allSelect2011)A;
Result:
Next, you only need to follow the corresponding rotation column value of the orderyear column.
As you finally want to get
Filter out qty = 0
Therefore, the final solution to this problem is:
- SelectCustid,Orderyear,Qty
- From(
- SelectCustid,Orderyear
- CaseOrderyear
- When2009ThenY2009
- When2010ThenY2010
- When2011ThenY2011
- End AsQty
- FromUnmarshting,(Select2009AsOrderyear
- Union allSelect2010
- Union allSelect2011)A
- )B
- WhereQty<>0;
Grouping ting & ungrouping Ting