以行作为列名称分组的行到

时间:2015-07-02 09:25:35

标签: sql sql-server excel

我有一个查询,我需要将返回的行转置为单独的列,但我需要从行值返回的列名,而另一个字段作为这些列下的值。

最终这将进入Excel。我最初认为我可以使用一个数据透镜,但是如果没有一些工作,值字段就无法接受文本,并且有太多的电影权限类型"跟...共事。我当前知道具体的权利类型,但正确的描述和权利数量可能会在未来发生变化,因此我希望它尽可能灵活。

我希望按生产者,电影和国家分组,以及#34; Film Right Type"正如列中所见,以及"可用性"作为右类型列的值。

我的查询。

Select product_detail.pd_product_code as 'ProductCode',
product_analysis.pa_m_9 as 'Producer',
product_detail.pd_abbv_description as 'Film',
product_detail.pd_commodity_code as 'Country',
variant_analysis.vaa_c_10 as 'Film Right Type',
case when isnull(variant_analysis.vaa_l_1,0) = 0 then  'Non-Exclusive' 
Else 
    case when dateadd(day,datediff(DAY,0,isnull(orders.LastDate,getdate())),0) > dateadd(day,datediff(DAY,0,getdate()),0) 
    Then 'Available On '+convert(varchar(10),Orders.LastDate, 103)
    Else 'Available' 
    End
end as 'Availability'

From product_detail
join variant_detail on variant_detail.vad_pd_id = product_detail.pd_id
left join variant_analysis on variant_analysis.vaa_vad_id = variant_Detail.vad_id
left join product_analysis on product_analysis.pa_pd_id = product_detail.pd_id
join (select count(variant_detail.vad_id) as CountVariants, variant_Detail.vad_pd_id from variant_Detail group by variant_detail.vad_pd_id) as Variants on Variants.vad_pd_id = product_detail.pd_id
Outer Apply
    (select top 1
    customer_detail.cd_statement_name as Customer,
    order_header_analysis.oha_d_2 as LastDate, 
    system_order_type.sot_description as OrderType, 
    order_header.oh_order_number as OrderNo
    from order_header
    join customer_detail on customer_detail.cd_id = order_header.oh_cd_id
    join order_line_item on order_line_item.oli_oh_id = order_header.oh_id
    join system_order_type on system_order_type.sot_id = order_header.oh_sot_id
    left join order_header_analysis on order_header_analysis.oha_oh_id = order_header.oh_id
    where order_header_analysis.oha_d_2 >= getdate()
    and order_header.oh_sot_id in (1, 2, 3)
    and order_line_item.oli_vad_id = variant_detail.vad_id
    ORder By order_header_analysis.oha_d_2 desc) as Orders 
where isnull(variant_analysis.vaa_l_2,0) = 0

这是我目前获得的数据: current data

这就是我想要最终结果的方式。 enter image description here

修改: 在使用枢轴后,我几乎得到了我想要的东西。 我的下面的查询给出了我所需要的内容:

Select * 

From(   
Select product_detail.pd_product_code as 'ProductCode',
product_analysis.pa_m_9 as 'Producer',
product_detail.pd_abbv_description as 'Film',
product_detail.pd_commodity_code as 'Country',
variant_analysis.vaa_c_10 as 'FilmRightType',
case when isnull(variant_analysis.vaa_l_1,0) = 0 then  'Non-Exclusive' 
Else 
    case when dateadd(day,datediff(DAY,0,isnull(orders.LastDate,getdate())),0) > dateadd(day,datediff(DAY,0,getdate()),0) 
    Then 'Available On '+convert(varchar(10),Orders.LastDate, 103)
    Else 'Available' 
    End
end as 'Availability'

From product_detail
join variant_detail on variant_detail.vad_pd_id = product_detail.pd_id
left join variant_analysis on variant_analysis.vaa_vad_id = variant_Detail.vad_id
left join product_analysis on product_analysis.pa_pd_id = product_detail.pd_id
join (select count(variant_detail.vad_id) as CountVariants, variant_Detail.vad_pd_id from variant_Detail group by variant_detail.vad_pd_id) as Variants on Variants.vad_pd_id = product_detail.pd_id
Outer Apply
    (select top 1
    customer_detail.cd_statement_name as Customer,
    order_header_analysis.oha_d_2 as LastDate, 
    system_order_type.sot_description as OrderType, 
    order_header.oh_order_number as OrderNo
    from order_header
    join customer_detail on customer_detail.cd_id = order_header.oh_cd_id
    join order_line_item on order_line_item.oli_oh_id = order_header.oh_id
    join system_order_type on system_order_type.sot_id = order_header.oh_sot_id
    left join order_header_analysis on order_header_analysis.oha_oh_id = order_header.oh_id
    where order_header_analysis.oha_d_2 >= getdate()
    and order_header.oh_sot_id in (1, 2, 3)
    and order_line_item.oli_vad_id = variant_detail.vad_id
    ORder By order_header_analysis.oha_d_2 desc) as Orders 
where isnull(variant_analysis.vaa_l_2,0) = 0
) as FilmVals

pivot
(
  max(Availability)
  for FilmRightType in (
    [Theatrical], [DVD], [Blu ray], [Free TV], [Pay TV], [Video on Demand Catch Up], [Subscription Video on Demand], [Advertising Video on Demand], [TV On Demand Download to Own], [TV On Demand Download to Rent]
  )
) piv;

我想从数据库中将RightTypes列表设置为动态。 我使用XML Path创建了另一个查询,它以正确的格式返回数据:

(Select left(RightType.RightTypesList,len(RightType.RightTypesList)-1) From (Select
    (Select
    DISTINCT
    '['+variant_analysis.vaa_c_10+']'+', '
    from variant_analysis
    where variant_analysis.vaa_c_10  is not null
    order by 1
    For XML Path ('')) as RightTypesList) as RightType)

但是当我把它替换为硬编码列表时,我收到以下错误:

Msg 102, Level 15, State 1, Line 44
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 52
Incorrect syntax near ')'.

1 个答案:

答案 0 :(得分:1)

尝试在SQL中使用PIVOT运算符 Here就是一个例子