我試圖通過與SQL查詢做了一個先進的訂單高級SQL Server的訂購
我有一個用來跟蹤工作流程及其相關活動
以前的活動(已完成的)數據庫處於closed
狀態。他們同時擁有Active_Date
和Closed_Date
集合
當前活動處於active
狀態。有一個Active_Date
集,但Closed_Date
是NULL
對於每個ECR.item_number
只會出現在active
狀態的一個活動
我想通過
ECR.item_number
訂購,但我想爲了根據active
狀態中哪個活動降序排列A.ACTIVE_DATE
查詢:
SELECT
ECR.item_number,
ECR.title,
convert(VARCHAR(10),ECR.CREATED_ON,120) AS [Date CR Created],
convert(VARCHAR(10),A.ACTIVE_DATE,120) AS [Activity Activated],
convert(VARCHAR(10),A.CLOSED_DATE,120) AS [Activity Closed],
A.STATE,
A.NAME AS [Activity Name],
(CASE
WHEN
(A.STATE='Closed')
THEN
DATEDIFF(DAY, A.ACTIVE_DATE, A.CLOSED_DATE)
END) AS [DAYS ACTIVITY (WAS) OPEN],
(CASE
WHEN
(A.STATE='Active')
THEN
DATEDIFF(DAY, A.ACTIVE_DATE, Getdate())
END) AS [DAYS ACTIVITY HAS BEEN OPEN]
FROM
innovator.ecr AS ECR
INNER JOIN innovator.workflow AS WF
ON CR.id = WF.source_id
INNER JOIN innovator.workflow_process AS WFP
ON WF.related_id = WFP.id
INNER JOIN innovator.workflow_process_activity AS WPA
ON WFP.id = WPA.source_id
INNER JOIN innovator.activity AS A
ON WPA.related_id = A.id
--Shown for clarity
ORDER BY
ECR.ITEM_NUMBER,
[Activity Activated]
採樣返回數據
|-----------+-----------+-------------------+-----------------------+-----------------------+-------------------+-------------------------------+-------------------------------|
|CR Number | Title | Date CR Created | Activity Name | Activity Activated | Activity Closed | DAYS ACTIVITY (WAS) OPEN | DAYS ACTIVITY HAS BEEN OPEN |
|-----------+-----------+-------------------+-----------------------+-----------------------+-------------------+-------------------------------+-------------------------------|
|CR-000119 | ITEM 1 | 4/12/2012 | Submit CR | 4/12/2012 | 31/01/2013 | 58 | 0 |
|CR-000119 | ITEM 1 | 4/12/2012 | Check CR | 31/01/2013 | 13/02/2013 | 13 | 0 |
|CR-000119 | ITEM 1 | 4/12/2012 | Review CR | 13/02/2013 | 21/02/2013 | 8 | 0 |
|CR-000119 | ITEM 1 | 4/12/2012 | Technical Review | 21/02/2013 | 28/02/2013 | 7 | 0 |
|CR-000119 | ITEM 1 | 4/12/2012 | CRB Meeting | 28/02/2013 | NULL | NULL | 11 |
|CR-00| ITEM 2 | 6/12/2012 | Check CR | 6/12/2012 | 21/12/2012 | 15 | 0 |
|CR-00| ITEM 2 | 6/12/2012 | Submit CR | 6/12/2012 | 6/12/2012 | 0 | 0 |
|CR-00| ITEM 2 | 6/12/2012 | Review CR | 21/12/2012 | 17/01/2013 | 27 | 0 |
|CR-00| ITEM 2 | 6/12/2012 | Technical Review | 17/01/2013 | 6/03/2013 | 48 | 0 |
|CR-00| ITEM 2 | 6/12/2012 | CRB Meeting | 6/03/2013 | NULL | NULL | 5 |
|CR-000136 | ITEM 3 | 11/01/2013 | Submit CR | 11/01/2013 | 15/01/2013 | 4 | 0 |
|CR-000136 | ITEM 3 | 11/01/2013 | Check CR | 15/01/2013 | 16/01/2013 | 1 | 0 |
|CR-000136 | ITEM 3 | 11/01/2013 | Review CR | 16/01/2013 | 21/01/2013 | 5 | 0 |
|CR-000136 | ITEM 3 | 11/01/2013 | Technical Review | 21/01/2013 | 25/01/2013 | 4 | 0 |
|CR-000136 | ITEM 3 | 11/01/2013 | CRB Meeting | 25/01/2013 | 31/01/2013 | 6 | 0 |
|CR-000136 | ITEM 3 | 11/01/2013 | Technical Review | 31/01/2013 | 27/02/2013 | 27 | 0 |
|CR-000136 | ITEM 3 | 11/01/2013 | CRB Meeting | 27/02/2013 | NULL | NULL | 12 |
|-----------+-----------+-------------------+-----------------------+-----------------------+-------------------+-------------------------------+-------------------------------|
所需的結果
|-----------+-----------+-------------------+-----------------------+-----------------------+-------------------+-------------------------------+-------------------------------|
|CR Number | Title | Date CR Created | Activity Name | Activity Activated | Activity Closed | DAYS ACTIVITY (WAS) OPEN | DAYS ACTIVITY HAS BEEN OPEN |
|-----------+-----------+-------------------+-----------------------+-----------------------+-------------------+-------------------------------+-------------------------------|
|CR-000136 | ITEM 3 | 11/01/2013 | Submit CR | 11/01/2013 | 15/01/2013 | 4 | 0 |
|CR-000136 | ITEM 3 | 11/01/2013 | Check CR | 15/01/2013 | 16/01/2013 | 1 | 0 |
|CR-000136 | ITEM 3 | 11/01/2013 | Review CR | 16/01/2013 | 21/01/2013 | 5 | 0 |
|CR-000136 | ITEM 3 | 11/01/2013 | Technical Review | 21/01/2013 | 25/01/2013 | 4 | 0 |
|CR-000136 | ITEM 3 | 11/01/2013 | CRB Meeting | 25/01/2013 | 31/01/2013 | 6 | 0 |
|CR-000136 | ITEM 3 | 11/01/2013 | Technical Review | 31/01/2013 | 27/02/2013 | 27 | 0 |
|CR-000136 | ITEM 3 | 11/01/2013 | CRB Meeting | 27/02/2013 | NULL | NULL | 12 |
|CR-000119 | ITEM 1 | 4/12/2012 | Submit CR | 4/12/2012 | 31/01/2013 | 58 | 0 |
|CR-000119 | ITEM 1 | 4/12/2012 | Check CR | 31/01/2013 | 13/02/2013 | 13 | 0 |
|CR-000119 | ITEM 1 | 4/12/2012 | Review CR | 13/02/2013 | 21/02/2013 | 8 | 0 |
|CR-000119 | ITEM 1 | 4/12/2012 | Technical Review | 21/02/2013 | 28/02/2013 | 7 | 0 |
|CR-000119 | ITEM 1 | 4/12/2012 | CRB Meeting | 28/02/2013 | NULL | NULL | 11 |
|CR-00| ITEM 2 | 6/12/2012 | Check CR | 6/12/2012 | 21/12/2012 | 15 | 0 |
|CR-00| ITEM 2 | 6/12/2012 | Submit CR | 6/12/2012 | 6/12/2012 | 0 | 0 |
|CR-00| ITEM 2 | 6/12/2012 | Review CR | 21/12/2012 | 17/01/2013 | 27 | 0 |
|CR-00| ITEM 2 | 6/12/2012 | Technical Review | 17/01/2013 | 6/03/2013 | 48 | 0 |
|CR-00| ITEM 2 | 6/12/2012 | CRB Meeting | 6/03/2013 | NULL | NULL | 5 |
|-----------+-----------+-------------------+-----------------------+-----------------------+-------------------+-------------------------------+-------------------------------|
非常感謝你,這個偉大的工程 – Spacko 2013-03-11 10:22:58
你」對於一個好問題,歡迎..thx;) – 2013-03-11 10:32:21