[英]Pivoting and merging data from a sql query
This is My Query:这是我的查询:
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)
My Result Looks Like This:我的结果如下所示:
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
What I would like to do is Rotate the data so that the information from column “Name” becomes the column header and I need to combine the values from attribute 1 & 2 This is what my Result Should Look Like:我想做的是旋转数据,以便“名称”列中的信息成为 header 列,我需要组合属性 1 和 2 中的值这是我的结果应该看起来像:
*Childs Age Attended Camp Before? Childs T-Shirt Size Allergies Description Phone# Pickup*
10 yes large none 212-555-1212 Mary Jordan
In Oracle DB I am using CASE statement to convert rows into columns.在 Oracle DB 中,我使用 CASE 语句将行转换为列。
Take a look on this example:看看这个例子:
select event_date ,sum(case when service_type = 'OCSAC_DEG' then service_count end) ocsac_deg ,sum(case when service_type = 'SMS_ONL' then service_count end) sms_onl ,sum(case when service_type = 'SMS_DEG' then service_count end) sms_deg ,sum(case when service_type = 'DATA_ONL' then service_count end) data_onl ,sum(case when service_type = 'DATA_DEG' then service_count end) data_deg from STATS where to_char(event_date, 'yyyymm') = to_char(add_months(sysdate,-1), 'yyyymm') group by event_date order by event_date desc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.