显示行像列

时间:2018-09-25 11:39:50

标签: sql sql-server

如何像列一样显示行?我需要为每个meta_key值打开该表。

我有下表:

CREATE TABLE wp_postmeta (
  meta_id int  NOT NULL,
  post_id int NOT NULL DEFAULT '0',
  meta_key varchar(255) DEFAULT NULL,
  meta_value varchar(255) 
)
INSERT INTO wp_postmeta ([meta_id], [post_id], [meta_key], [meta_value]) VALUES
(14454, 1614, 'price_bin', '2'),
(14453, 1614, 'price_current', '1'),
(14452, 1614, 'post_tags', ''),
(14451, 1614, 'price_reserve', '3'),
(14450, 1614, 'price_shipping', '4'),
(14449, 1614, 'condition', '1'),
(14448, 1614, 'auction_type', '1'),
(14447, 1614, 'listing_expiry_days', '1'),
(14446, 1614, 'city', '2'),
(14445, 1614, 'listing_price_due', '25'),
(14444, 1614, 'showgooglemap', 'no'),
(14443, 1614, 'topcategory', 'yes'),
(14442, 1614, 'visitorcounter', 'no'),
(14441, 1614, 'html', 'yes'),
(14440, 1614, 'featured', 'no'),
(14439, 1614, 'listing_price', '25'),
(14724, 1658, '_edit_last', '1'),
(14438, 1614, 'listing_expiry_date', ''),
(14437, 1614, 'hits', '0')

我正在尝试使用以下脚本:

 DECLARE @idList varchar(500),
        @sqlToRun varchar(1000)

SET @idList = STUFF((SELECT (meta_key)
            FROM wp_postmeta
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

SET @sqlToRun ='
SELECT post_id,  ' + @idList + ',
FROM (
    SELECT 
        post_id, meta_key, meta_value
    FROM wp_postmeta
        WHERE post_id = 1614
) as s
PIVOT
(
    Max(meta_value)
    FOR [meta_key] IN ('+ @idList +')
)AS pvt'

EXEC (@sqlToRun)

enter image description here

但是我得到NULL结果。如何解决?

输出应如下所示:

post_id, price_bin, price_current, price_shipping
1614        2          1            4

2 个答案:

答案 0 :(得分:1)

您必须使用meta_keyIN的{​​{1}}中的PIVOT作为要显示的内容,它也会返回meta_value行,因此如果希望它不显示,然后添加一个条件,休息就可以了

NULL

动态查询SELECT * FROM ( SELECT post_id, meta_key, meta_value FROM @wp_postmeta ) as s PIVOT ( Max(meta_value) FOR [meta_key] IN ([price_bin], [price_current], [price_shipping], [price_reserve]) )AS pvt 以使用所有pivoting作为列标题

meta_keys

答案 1 :(得分:1)

CREATE TABLE #wp_postmeta (
  meta_id int  NOT NULL,
  post_id int NOT NULL DEFAULT '0',
  meta_key varchar(255) DEFAULT NULL,
  meta_value varchar(255) 
)
INSERT INTO #wp_postmeta ([meta_id], [post_id], [meta_key], [meta_value]) VALUES
(14454, 1614, 'price_bin', '2'),
(14453, 1614, 'price_current', '1'),
(14452, 1614, 'post_tags', ''),
(14451, 1614, 'price_reserve', '3'),
(14450, 1614, 'price_shipping', '4'),
(14449, 1614, 'condition', '1'),
(14448, 1614, 'auction_type', '1'),
(14447, 1614, 'listing_expiry_days', '1'),
(14446, 1614, 'city', '2'),
(14445, 1614, 'listing_price_due', '25'),
(14444, 1614, 'showgooglemap', 'no'),
(14443, 1614, 'topcategory', 'yes'),
(14442, 1614, 'visitorcounter', 'no'),
(14441, 1614, 'html', 'yes'),
(14440, 1614, 'featured', 'no'),
(14439, 1614, 'listing_price', '25'),
(14724, 1658, '_edit_last', '1'),
(14438, 1614, 'listing_expiry_date', ''),
(14437, 1614, 'hits', '0')


   DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX),
    @cols1 AS NVARCHAR(MAX) ;

select @cols = STUFF((SELECT ',' + QUOTENAME([meta_key]) 
                from #wp_postmeta

        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')


select @cols1 = STUFF((SELECT ',MAX(' + QUOTENAME([meta_key]) + ') as ' + QUOTENAME([meta_key])
                from #wp_postmeta

        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)')
    ,1,1,'')




set @query = 'SELECT post_id, ' + (@cols1) + ' from 
            (
                select *
                from #wp_postmeta
           ) x
            pivot 
            (
                 Max(meta_value)
                for meta_key in (' + @cols + ')
            ) p 
                group by post_id '
--print(@query)
exec(@query)

输出

post_id price_bin   price_current   post_tags   price_reserve   price_shipping  condition   auction_type    listing_expiry_days city    listing_price_due   showgooglemap   topcategory visitorcounter  html    featured    listing_price   _edit_last  listing_expiry_date hits
1614    2   1       3   4   1   1   1   2   25  no  yes no  yes no  25  NULL        0
1658    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    1   NULL    NULL