微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

透视和合并来自sql查询的数据

如何解决透视和合并来自sql查询的数据

| 这是我的查询
SELECT  ad.Name,av.Value AS Attribute1,ctv.Value AS Attribute2
FROM    AttributeDeFinitions AS ad WITH (nolock) 
INNER JOIN AttributeValues AS av WITH (nolock) 
ON      ad.AttributeDeFinitionID = av.AttributeDeFinitionID 
INNER JOIN AttributeCategories 
ON      ad.AttributeCategoryID = AttributeCategories.AttributeCategoryID 
LEFT OUTER JOIN CodeTableValues AS ctv WITH (nolock) 
ON      av.CodeTableValueID = ctv.CodeTableValueID
WHERE   (AttributeCategories.Name = \'Camp\') AND (av.AttributeValueGroupID = 9840)
我的结果看起来像这样:
Name                     Attribute1      Attribute2
Childs Age:              10 
Attended Camp Before?:   Yes
Childs T-Shirt Size:     large           NULL
Allergies Description    none            NULL
Phone #                  212-555-1212    NULL
Pickup                   Mary Jordan     NULL

Name= Name of Attribute Column
Attribute1 = Data is from a free Form
Attribute2 = Data is from a Drop down Menu   
我想做的是旋转数据,以便“名称”列中的信息成为列标题,我需要合并属性1和2中的值 这是我的结果应为:
*Childs Age  Attended Camp Before?  Childs T-Shirt Size  Allergies Description Phone#        PickuP*
10           yes                    large                none                  212-555-1212  Mary Jordan
    

解决方法

        在Oracle DB中,我使用CASE语句将行转换为列。 看一下这个例子: 选择event_date       ,sum(当service_type = \'OCSAC_DEG \'然后service_count结束的情况)ocsac_deg       ,sum(当service_type = \'SMS_ONL \'则service_count结束的情况)sms_onl       ,sum(当service_type = \'SMS_DEG \'则service_count结束的情况)sms_deg       ,sum(当service_type = \'DATA_ONL \'则service_count结束的情况)data_onl       ,sum(当service_type = \'DATA_DEG \'然后service_count结束的情况)data_deg  来自STATS 其中to_char(event_date,\'yyyymm \')= to_char(add_months(sysdate,-1),\'yyyymm \') 按event_date分组 按event_date desc排序     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。