简体   繁体   English

将列值转换为行

[英]convert column values to rows

uid   pid   mail   value
 1     78    27     Nairobi
 2     78    27     Milimani
 3     78    27     Criminal
 4     78    27     1427932800

I have a DB table above and only need the 'value' column values. 我上面有一个数据库表,只需要'value'列值。 I want to have the column values display in rows (not comma separated) for a cross-tab report. 我想让交叉表报表的行中显示列值(不用逗号分隔)。 my ideal result would be: 我理想的结果是:

        **Nairobi  Milimani  Criminal  1427932800**

The matching 'pid' and 'mail' means that the corresponding 'value' is from a single submission and a change in pid and mail (not captured here) is a new submission! 匹配的“ pid”和“ mail”表示相应的“值”来自单个提交,而pid和mail(此处未捕获)的更改是新提交!

so how do I write an sql for converting the 'value' column values to row values? 所以我怎么写一个SQL来将“值”列值转换为行值? any help much appreciated. 任何帮助,不胜感激。

'Pivot' has not really helped or i'm probably doing it wrongly.!! 'Pivot'并没有真正帮助,或者我可能做错了。

In SQL Server, you could easily pivot your columns something like this: 在SQL Server中,您可以轻松地对列进行如下操作:

select 
     pvt.[Nairobi],
     pvt.[Milimani],
     pvt.[Criminal],
     pvt.[1427932800] 
from 
(select * from test) t
PIVOT
(
count(uid) for [value] in ([Nairobi],[Milimani],[Criminal],[1427932800]) 
        --change the count function and column to match your needs
) as pvt;

Also, as you may see above, you would need to use some kind of aggregate function to use pivot. 同样,如您在上面看到的,您将需要使用某种聚合函数来使用数据透视。 Hope this helps! 希望这可以帮助!

SQL Fiddle Demo SQL小提琴演示

UPDATE UPDATE

After re-reading your question, I figured that you might be instead looking for something like this: 重新阅读您的问题后,我发现您可能正在寻找这样的东西:

SELECT 
     ltrim(substring(t.concat_values, 1, charindex(' ', t.concat_values, 1))) AS first_col
    ,ltrim(substring(t.concat_values, dbo.GetNthCharacterOccurrence(t.concat_values, ' ', 1), (dbo.GetNthCharacterOccurrence(t.concat_values, ' ', 2) - dbo.GetNthCharacterOccurrence(t.concat_values, ' ', 1)))) AS second_col
    ,ltrim(substring(t.concat_values, dbo.GetNthCharacterOccurrence(t.concat_values, ' ', 2), (dbo.GetNthCharacterOccurrence(t.concat_values, ' ', 3) - dbo.GetNthCharacterOccurrence(t.concat_values, ' ', 2)))) AS third_col
    ,ltrim(substring(t.concat_values, dbo.GetNthCharacterOccurrence(t.concat_values, ' ', 3), len(t.concat_values) - dbo.GetNthCharacterOccurrence(t.concat_values, ' ', 3)+1)) AS fourth_col
FROM (
    SELECT DISTINCT stuff((
                SELECT ' ' + t2.[value]
                FROM test t2
                WHERE t1.pid = t2.pid
                    AND t1.mail = t2.mail
                FOR XML path('')
                ), 1, 1, '') AS concat_values
    FROM test t1
    ) t;

SQL Fiddle Demo 2 SQL小提琴演示2

The trick for this method is to initially create a comma separated list of values using the STUFF function with XML Path . 此方法的窍门是,首先使用带有XML PathSTUFF函数创建一个逗号分隔的值列表。 Then, break the string based on the position of the string separator which in this case I used space (' '). 然后,根据字符串分隔符的位置断开字符串,在这种情况下,我使用space ('')。 To find the nth occurrence of space, I "borrowed" a function that was written by Tavis Lovell originally in a blog. 为了找到第n个出现的空间,我“借用了” Tavis Lovell最初在博客中编写的函数。 To split the values into multiple columns, I used substring , charindex and the user defined function above as needed. 要将值分成多个列,我根据需要使用了substringcharindex和上面的用户定义函数。

MySQL Version MySQL版本

Using group_concat and substring_index functions, you could easily convert your string values into multiple columns like this: 使用group_concatsubstring_index函数,您可以轻松地将字符串值转换为多个列,如下所示:

SELECT 
     SUBSTRING_INDEX(SUBSTRING_INDEX(concat_values, ',',1), ',', -1) as first_col,
     SUBSTRING_INDEX(SUBSTRING_INDEX(concat_values, ',',2), ',', -1) as second_col,
     SUBSTRING_INDEX(SUBSTRING_INDEX(concat_values, ',',3), ',', -1) as third_col,
     SUBSTRING_INDEX(SUBSTRING_INDEX(concat_values, ',',4), ',', -1) as fourth_col

FROM (
     SELECT GROUP_CONCAT(value) as concat_values
    FROM test t1  --change this to match your table name
    ) t;

MySQL Demo MySQL演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM