我想使用group by作为nvarchar数据类型

时间:2017-06-09 01:35:49

标签: mysql reporting-services ssrs-2008-r2

我的目标是创建一个查询,该查询可以按3种相应的调查类型及其各自的值进行分组。列是日期每隔几天自动更新,调查类型分为调查a,调查b和调查c的不同调查类型。这些调查可以标记为已发送(意味着客户拥有并且尚未完成)或者可以标记为已接收(意味着客户已完成并且收到信息。每个调查字符串以相同的4个字符开头XXX-。这是我使用通配符%的地方。调查类型的格式是XXX-YY-SENT或" XXX-YY-RECIEVED" .YY与特定调查类型不同。值列表给出了每两周由系统更新的日期完成的调查总数。我的目标是按调查类型对数据进行分组,以便能够在ssrs中创建堆积柱形图,显示发送和接收调查之间的差异。(我已经对如何执行有了一个很好的想法。问题是开发查询以便通过Survey X提取数据并将其分组(正在调查的组x-SENT和调查x-recieved )和调查Y(正在调查y-SENT和调查y-收到的组)和调查Z(正在调查z-SENT组和调查z-recieved)。表SurveyInfo中有示例数据。这是我用来提取数据的查询:

 SELECT [Date]  ,[SurveyType]  ,[Value]
 FROM SurveyInfo]
  WHERE Date IN (SELECT max(Date) FROM SurveyInfo) and SurveyType like '%XXX%'

(这是我想按调查类型对数据进行分组的地方) 这是数据结果。

Date ,                          SurveyType,              Value 

2017-06-02 08:00:02.270     XXX-AA-SENT-WITHIN-2YR          1000.0000
2017-06-02 08:00:02.270     XXX-AA-RECEIVED-WITHIN-2YR        900.0000
2017-06-02 08:00:02.270     XXX-BB-SENT-WITHIN-2YR          1200.0000
2017-06-02 08:00:02.270     XXX-BB-RECEIVED-2YR             800.0000
2017-06-02 08:00:02.270     XXX-CC-SENT-WITHIN-2YR          500.0000
2017-06-02 08:00:02.270     XXX-CC-RECEIVED-WITHIN-2YR       400.0000

1 个答案:

答案 0 :(得分:0)

这很难读懂,但我想我明白你想要什么。以下是否开始帮助您?

if object_id('tempdb.dbo.#test') is not null drop table #test
create table #test
(
    dateVal datetime,
    SurveyType nvarchar(500),
    SurveyValue decimal(36,4)
)

insert into #test
values('2017-06-02 08:00:02.270','XXX-AA-SENT-WITHIN-2YR',1000.0000),
 ('2017-06-02 08:00:02.270','XXX-AA-RECEIVED-WITHIN-2YR', 900.0000),
 ('2017-06-02 08:00:02.270','XXX-BB-SENT-WITHIN-2YR',          1200.0000),
 ('2017-06-02 08:00:02.270','XXX-BB-RECEIVED-2YR',             800.0000),
 ('2017-06-02 08:00:02.270','XXX-CC-SENT-WITHIN-2YR',          500.0000),
 ('2017-06-02 08:00:02.270','XXX-CC-RECEIVED-WITHIN-2YR',       400.0000)

SELECT  
    substring(SurveyType,5,2) as SurveyType,
    case when SurveyType like '%SENT%' then 'SENT' else 'RECEIVED' END as SENT_RECEIVED,
    sum(SurveyValue) as SumSurveyValue
 FROM #test 
GROUP BY 
    substring(SurveyType,5,2)  ,
    case when SurveyType like '%SENT%' then 'SENT' else 'RECEIVED' END
ORDER BY 
    SurveyType,
    SENT_RECEIVED