使用多个变量选择相同的列值

时间:2014-08-16 09:00:33

标签: sql

我有一张桌子tags。两列tag_namevalue

我想根据value选择tag_name。例如。如果tag_name是"输入"我想选择value作为" type_value"如果tag_name是"材料"我想选择它作为" material_value"。我尝试使用以下case语句,但语法错误:

select
case tag_name
when 'type'
then value as type_value
when 'material'
then value as material_value
end

这样做的最佳方式是什么?

标签表中的示例数据:

  id            tag_name                   value                 taggable_id
1671223  collection          Chunky                         1060528713
1671224  type                Chains                         1060528713
1671225  occasion            Special Occasions or Gifts     1060528713
1671226  material            Alloy                          1060528713
1671227  gemstones           Pearl                          1060528713
1671228  home_page_link      Statement_Necklaces            1060528713
1671217  collection      Traditional and Imitation      1060528712
1671218  type            Bangles                        1060528712
1671219  occasion        Special Occasions or Gifts     1060528712
1671220  material        Alloy                          1060528712
1671221  plating         Yellow Gold Plating            1060528712
1671222  gemstones       Cubic Zirconia                 1060528712

我正在遍历表格并希望根据tag_name

存储值

所以我想要像

这样的东西
taggable_id    collection_value            type_value   occasion_value               material_value      plating_value       gemstones_value
1060528712   Traditional and Imitation       Bangles     Special Occasions or Gifts      Alloy        Yellow Gold Plating    Cubic Zrconia             
1060528713   Chunky                          Chains      Special Occasions or Gifts      Alloy                               Pearl       

2 个答案:

答案 0 :(得分:0)

如果您不需要让查询变为动态,并且如果您可以在查询中使用硬编码值,我相信这应该可行:

select 
    taggable_id, 
    max(collection_value) as collection_value,
    max(type_value) as type_value,
    max(occasion_value) as occasion_value,
    max(material_value) as material_value,
    max(plating_value) as plating_value,
    max(gemstones_value) as gemstone_value
from (
    select 
        taggable_id,
        case tag_name when 'collection' then value end as collection_value,
        case tag_name when 'type' then value end as type_value,
        case tag_name when 'occasion' then value end as occasion_value,
        case tag_name when 'material' then value end as material_value,
        case tag_name when 'plating' then value end as plating_value,
        case tag_name when 'gemstones' then value end as gemstones_value
    from tags 
) subquery 
group by taggable_id
order by taggable_id

如果您需要所有标记成为列,则另一个选项是使用动态数据透视查询:

DECLARE @Query AS NVARCHAR(MAX)
DECLARE @Column AS NVARCHAR(MAX)

SELECT @Column= ISNULL(@Column + ',','') + QUOTENAME(tag_name)
FROM (SELECT DISTINCT tag_name FROM tags) AS Tags

SET @Query =
  N'SELECT taggable_id, ' + @Column + '
    FROM (SELECT taggable_id, value, tag_name FROM tags) tb
    PIVOT(MAX(value)
          FOR tag_name IN (' + @Column + ')) AS PivotTable'

EXEC sp_executesql @Query

虽然排序和列标签不同,但它不会产生您要求的确切输出。

答案 1 :(得分:0)

试试这个

    WITH Pivoted
    AS
    (
        select taggable_id,collection ,type,occasion,material,plating,gemstones 
        from tags 
        pivot
        (
          max(value)
          for tag_name in (collection,type,occasion,material,plating,gemstones)
        ) piv
    ) 
    SELECT 
       taggable_id,
       MAX(collection) AS collection_Value,
       MAX(type) AS type_Value,
       MAX(occasion) AS occasion_Value,
       MAX(material) AS material_Value,
       MAX(plating) AS plating_Value,
       MAX(gemstones) AS gemstones_Value
    FROM Pivoted
    GROUP BY taggable_id;