I would like to get a pivot table with a different status and has count days from a table without a prepared pivot column in it by using SQL query. I have try use UNION ALL to insert the column, but the query was too long and not practicable and efficiency.
My Table
Data Table
Status |date
-------------
New |25/2/2017
Confirm |29/1/2017
Status Table
Status
-------
New
Confirm
Finish
Results I want:
Status | 0-7|8-14|15-21|<30 ---->Days
------------------------------
New | 1 | 0 | 0 | 1
Confirm| 0 | 0 | 0 | 1
Finish | 0 | 0 | 0 | 0
SOLVED
Solution
Using left join status table and data table, then use case statement and pivot to solved it. Thanks to all who try to help me ^^