需要对sql数据进行排序

时间:2018-04-10 06:36:10

标签: sql sql-server tsql

我使用sql在报表生成器中构建报表,我使用的SQL查询如下

tf.train.SequenceExample

从查询

显示的数据
    select count(*) [Total Clients], li.title,
 SUBSTRING(li.title,CHARINDEX('_',li.title,CHARINDEX('_',li.title)+1)+1,2) as month1,
 CASE SUBSTRING(li.title,CHARINDEX('_',li.title,CHARINDEX('_',li.title)+1)+1,5) 
            WHEN '01' THEN 'Jan'
            WHEN '02' THEN 'Feb'
            WHEN '03' THEN 'Mar'
            WHEN '04' THEN 'Apr'
            WHEN '05' THEN 'May'
            WHEN '06' THEN 'June'
            WHEN '07' THEN 'Jul'
            WHEN '08' THEN 'Aug'
            WHEN '09' THEN 'Sep'
            WHEN '10' THEN 'Oct'
            WHEN '11' THEN 'Nov'
            WHEN '12' THEN 'Dec'
            END As [Month],
            SUBSTRING(li.title,CHARINDEX('_',li.title)+1,4),
li.CI_UniqueID,coll.name,coll.CollectionID,
SUM (CASE WHEN ucs.status=3 or ucs.status=1  then 1 ELSE 0 END ) as 'Installed / Not Applicable',
sum( case When ucs.status=2 Then 1 ELSE 0 END ) as 'Required',
sum( case When ucs.status=0 Then 1 ELSE 0 END ) as 'Unknown',
round((CAST(SUM (CASE WHEN ucs.status=3 or ucs.status=1 THEN 1 ELSE 0 END) as float)/count(*) )*100,2) as 'Compliant%',
    round((CAST(count(case when ucs.status not in('3','1') THEN '*' end) as float)/count(*))*100,2) as 'NotCompliant%'
    From v_Update_ComplianceStatusAll UCS
inner join v_r_system sys on ucs.resourceid=sys.resourceid
inner join v_FullCollectionMembership fcm on ucs.resourceid=fcm.resourceid
inner join v_collection coll on coll.collectionid=fcm.collectionid
inner join v_AuthListInfo LI on ucs.ci_id=li.ci_id
where coll.CollectionID like '%SMS00001%' and
--title like '%SUG%' 
 Title like '%P1%' and
Title like '%SUG_' + '' + CAST(year(getdate())-1 as varchar) +  '' + '%'
--or Title like '%SUG_' + '' + CAST(year(getdate())-1 as varchar) +  '' + '%'
group by li.title,li.CI_UniqueID,coll.name,coll.CollectionID

标题显示为

+---------------+---------------------------+--------+-------+------------------+---------------------------------------------------------------------------------------------+----------------------+---------------+-----------------------------+----------+---------------------+---------------+
| Total Clients |           title           | month1 | Month | (No column name) |                                         CI_UniqueID                                         |         name         |  CollectionID |  Installed / Not Applicable | Required |  Unknown Compliant% | NotCompliant% |
+---------------+---------------------------+--------+-------+------------------+---------------------------------------------------------------------------------------------+----------------------+---------------+-----------------------------+----------+---------------------+---------------+
|            30 | SUG_2017_01_P1_RFC3456    |     01 | NULL  |             2017 |  ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_E586ED3A-EDD5-4145-98FB-C0B373F7E4CA | All Systems SMS00001 |            26 |                           0 |        4 |               86.67 |         13.33 |
|            30 | SUG_2017_01-03_P1_RFC2781 |     01 | NULL  |             2017 |  ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_5AEB5495-8913-4541-B29E-7D55C16E6B68 | All Systems SMS00001 |            26 |                           0 |        4 |               86.67 |         13.33 |
|            30 | SUG_2017_03_P1_RFC2781    |     03 | NULL  |             2017 |  ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_ED07143C-B357-454E-B02E-7D81AEE40869 | All Systems SMS00001 |             0 |                           0 |       30 |                   0 |           100 |
|            30 | SUG_2017_04_P1_RFC3103    |     04 | NULL  |             2017 |  ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_2722FDDB-6D6B-407F-A0CE-063372571E82 | All Systems SMS00001 |            26 |                           0 |        4 |               86.67 |         13.33 |
|            30 | SUG_2017_04-05_P1_RFC2781 |     04 | NULL  |             2017 |  ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_10D742F8-FB4B-4E19-BF05-5210C790F440 | All Systems SMS00001 |            26 |                           0 |        4 |               86.67 |         13.33 |
|            30 | SUG_2017_06_P1_RFC3123    |     06 | NULL  |             2017 |  ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_3063A272-0DF9-4033-94E2-C52AF1CFD4BC | All Systems SMS00001 |            25 |                           1 |        4 |               83.33 |         16.67 |
|            30 | SUG_2017_10_P1_RFC3103    |     10 | NULL  |             2017 |  ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_9C3F338B-E8BA-4AB5-8ECF-1EA8729825DA | All Systems SMS00001 |            26 |                           0 |        4 |               86.67 |         13.33 |
|            30 | SUG_2017_11_P1_RFC3103    |     11 | NULL  |             2017 |  ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_B42D69FC-2564-4542-8D5B-F5348A4080FF | All Systems SMS00001 |            26 |                           0 |        4 |               86.67 |         13.33 |
|            30 | SUG_2017_12_P1_RFC3103    |     12 | NULL  |             2017 |  ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_5C3AD217-7747-4BAC-AD06-3851014BCB94 | All Systems SMS00001 |            26 |                           0 |        4 |               86.67 |         13.33 |
+---------------+---------------------------+--------+-------+------------------+---------------------------------------------------------------------------------------------+----------------------+---------------+-----------------------------+----------+---------------------+---------------+

我需要在01-03 SUG_2017_01-03_P1_RFC2781上显示我需要的这3行中的01-03个月,我需要丢弃jan行SUG_2017_01_P1_RFC3456并且行进行意味着SUG_2017_03_P1_RFC2781

适用于我需要的所有行

我需要组合行而不是单独的行。

新数据

SUG_2017_01_P1_RFC3456
SUG_2017_01-03_P1_RFC2781
SUG_2017_03_P1_RFC2781 

1 个答案:

答案 0 :(得分:1)

尝试以下查询

WITH commonQueryCTE AS(
  -- your query is here
),
paramQueryCTE AS(
  SELECT
    *,
    SUBSTRING(Title,5,4) Y,
    SUBSTRING(Title,10,2) M1,
    IIF(SUBSTRING(Title,12,1)='-',SUBSTRING(Title,13,2),NULL) M2
  FROM commonQueryCTE
)
SELECT *
FROM paramQueryCTE c
WHERE NOT EXISTS(SELECT * FROM paramQueryCTE p WHERE c.Y=p.Y AND c.M1 BETWEEN p.M1 AND p.M2 AND p.M2 IS NOT NULL)
   OR c.M2 IS NOT NULL
ORDER BY c.Y,c.M1

SQL小提琴演示 - http://www.sqlfiddle.com/#!18/bf900/1

作为变体,您还可以使用SELECT ... INTO #TempTable FROM ...,然后在该查询中使用#TempTable