高级SQL Server排序依据

时间:2013-03-11 07:24:22

标签: sql sql-server tsql sql-order-by

我正在尝试使用SQL查询执行高级订单

我有一个用于跟踪工作流及其相关活动的数据库

之前的活动(已完成)处于closed状态。他们同时拥有Active_DateClosed_Date设置

当前活动处于active状态。设置了Active_Date,但Closed_DateNULL

对于每个ECR.item_numberactive州只有一项活动

我想通过

订购
  1. ECR.item_number但我希望订单基于active状态中哪个活动按降序排列
  2. A.ACTIVE_DATE
  3. 查询:

    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-000123  |   ITEM 2  |   6/12/2012       |   Check CR            |   6/12/2012           |   21/12/2012      |   15                          |   0                           |
    |CR-000123  |   ITEM 2  |   6/12/2012       |   Submit CR           |   6/12/2012           |   6/12/2012       |   0                           |   0                           |
    |CR-000123  |   ITEM 2  |   6/12/2012       |   Review CR           |   21/12/2012          |   17/01/2013      |   27                          |   0                           |
    |CR-000123  |   ITEM 2  |   6/12/2012       |   Technical Review    |   17/01/2013          |   6/03/2013       |   48                          |   0                           |
    |CR-000123  |   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-000123  |   ITEM 2  |   6/12/2012       |   Check CR            |   6/12/2012           |   21/12/2012      |   15                          |   0                           |
    |CR-000123  |   ITEM 2  |   6/12/2012       |   Submit CR           |   6/12/2012           |   6/12/2012       |   0                           |   0                           |
    |CR-000123  |   ITEM 2  |   6/12/2012       |   Review CR           |   21/12/2012          |   17/01/2013      |   27                          |   0                           |
    |CR-000123  |   ITEM 2  |   6/12/2012       |   Technical Review    |   17/01/2013          |   6/03/2013       |   48                          |   0                           |
    |CR-000123  |   ITEM 2  |   6/12/2012       |   CRB Meeting         |   6/03/2013           |   NULL            |   NULL                        |   5                           |
    |-----------+-----------+-------------------+-----------------------+-----------------------+-------------------+-------------------------------+-------------------------------|
    

2 个答案:

答案 0 :(得分:2)

在这种情况下,我发现ECR.item_number处于活动状态,并通过MAX()OVER()子句将此日期分配给整个组ECR.item_number。在SELECT语句中使用位置10的数字对新列进行进一步排序(升序)。您还可以使用别名作为排序列,而不是SELECT语句中位置10的数量

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],
     MAX(CASE WHEN A.CLOSED_DATE IS NULL THEN A.ACTIVE_DATE END) OVER (PARTITION BY ECR.item_number)
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
ORDER BY 10 ASC, ECR.ITEM_NUMBER, A.ACTIVE_DATE ASC

SQLFiddle

上的简单演示

答案 1 :(得分:2)

关键是获取每个项目的Active活动的日期。这可以使用分析函数来完成:

[DateOfActiveActivity] = MIN(CASE WHEN A.CLOSED_DATE IS NULL THEN A.ACTIVE_DATE END) OVER(PARTITION BY ECR.item_number)

因此,要将其集成到您的查询中,您可以使用:

WITH CTE AS
(   SELECT  [CRNumber] = ECR.item_number,
            ECR.title,
            [DateCRCreated] = CAST(ECR.CREATED_ON AS DATE),
            [ActivityActivated] = CAST(A.ACTIVE_DATE AS DATE),
            [ActivityClosed] = CAST(A.CLOSED_DATE AS DATE),
            A.STATE,
            [ActivityName] = A.NAME,
            [DAYSACTIVITYOPEN] = CASE WHEN A.STATE = 'Closed' THEN DATEDIFF(DAY, A.ACTIVE_DATE, A.CLOSED_DATE) END,
            [DAYSACTIVITYHASBEENOPEN] = CASE WHEN A.STATE = 'Active' THEN DATEDIFF(DAY, A.ACTIVE_DATE, GETDATE()) END, 
            [DateOfActiveActivity] = MIN(CASE WHEN A.CLOSED_DATE IS NULL THEN A.ACTIVE_DATE END) OVER(PARTITION BY ECR.item_number)
    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
)
SELECT  [CR Number] = [CRNumber],
        Title,
        [Date CR Created] = CONVERT(VARCHAR(10), DateCRCreated, 120),
        [Activity Activated] = CONVERT(VARCHAR(10), ActivityActivated, 120),
        [Activity Closed] = CONVERT(VARCHAR(10), ActivityClosed, 120),
        [STATE],
        [Activity Name] = ActivityName,
        [DAYS ACTIVITY (WAS) OPEN] = [DAYSACTIVITYOPEN],
        [DAYS ACTIVITY HAS BEEN OPEN] = [DAYSACTIVITYHASBEENOPEN]
FROM    CTE
ORDER BY DateOfActiveActivity ASC, ActivityActivated, ActivityClosed; 

<强> Demo with sample data on SQL Fiddle