简体   繁体   English

如何在SQL Server select语句中将单列拆分为多列?

[英]How to split a single column into multiple columns in SQL Server select statement?

I am new to SQL Server . 我是SQL Server的新手。 I have a single long column with names starting from a, b, c and d. 我有一长列,名称以a,b,c和d开头。 I want to show them in separate columns of NameA, NameB, NameC and NameD. 我想在NameA,NameB,NameC和NameD的单独列中显示它们。 I tried union but it shows in one column. 我试过工会,但它显示在一列中。 I tried case but I dont know how to use it. 我尝试过案例,但我不知道如何使用它。 Please help. 请帮忙。

Existing column 现有栏

names
A1
B1
A2
C1
A3
A4

A_names| B_names | C_names
A1     | B1      | C1
A2
A3
A4

just for fun and curious why you want that: 只是出于乐趣和好奇,为什么要这样:

select * 
from 
( select idx = left(names,1)
       , names
       , rn = row_number() over (partition by left(names,1) order by names) 
  from 
  ( values ('A1'),('B1'),('A2'),('C1'),('A3'),('A4'),('B2')) 
  v(names)
) dat
pivot 
( max(names)
  for idx in ([A],[B],[C],[D])
) p              

http://sqlfiddle.com/#!6/9eecb/4013/0 http://sqlfiddle.com/#!6/9eecb/4013/0

I don't think this is something that should be solved in SQL. 我认为这不是SQL应该解决的问题。 It's a representational thing that should probably be done in the application. 这是代表性的事情,应该在应用程序中完成。

However, if you insist to use SQL for this, this is how you could do it. 但是,如果您坚持要为此使用SQL,则可以使用此方法。 The main problem with this query is that the ROW_NUMBER function will be quite bad for performance. 此查询的主要问题是ROW_NUMBER函数的性能会很差。

WITH nameA
(
   SELECT name, ROW_NUMBER() OVER(ORDER BY name) AS rn
   FROM t1
   WHERE name LIKE 'a%'
), nameB AS
(
   SELECT name, ROW_NUMBER() OVER(ORDER BY name) AS rn
   FROM t1
   WHERE name LIKE 'b%'
)
SELECT name FROM nameA
FULL OUTER JOIN nameB
ON nameA.rn = nameB.rn
ORDER BY nameA.rn,nameB.rn;

You can use CASE ( https://msdn.microsoft.com/en-us/library/ms181765.aspx ) 您可以使用CASE( https://msdn.microsoft.com/en-us/library/ms181765.aspx

This Query should work: 此查询应工作:

SELECT 
    CASE WHEN users.name like 'a%' THEN users.name  ELSE NULL END AS NameA,
    CASE WHEN users.name like 'b%' THEN users.name  ELSE NULL END AS NameB,
    CASE WHEN users.name like 'c%' THEN users.name  ELSE NULL END AS NameC,
    CASE WHEN users.name like 'd%' THEN users.name  ELSE NULL END AS NameD
FROM users

This isn't really the way relational databases work. 这并不是关系数据库的真正工作方式。 When you have data in the same row, it's supposed to be related in some way - a common ID, at the least. 当数据在同一行中时,它应该以某种方式关联-至少是一个公共ID。 What is it that would connect the person whose name happens to begin with A to the person whose name happens to begin with B? 将名字刚好以A开头的人与名字刚好以B开头的人联系起来是什么? Why would you ever want the RDBMS to make such an arbitrary connection? 为什么要让RDBMS建立这样的任意连接?

If you have a requirement to display users in such a way, you'd just want to write several queries and have your presentation layer deal with laying them out properly, eg 如果您需要以这种方式显示用户,则只想编写几个查询并让您的表示层处理正确的布局,例如

SELECT name FROM users WHERE name LIKE 'a%'
SELECT name FROM users WHERE name LIKE 'b%'
SELECT name FROM users WHERE name LIKE 'c%'
etc...

The presentation layer could run each query and then populate a table appropriately. 表示层可以运行每个查询,然后适当地填充表。 Even better would be to have the presentation layer just run this query: 更好的是让表示层只运行以下查询:

SELECT name FROM users

And then appropriately sort and display the data, which is probably going to be less expensive than multiple scans on your users table by SQL Server. 然后适当地排序和显示数据,这可能比SQL Server对用户表的多次扫描便宜。

Look at this post, it is very close to your problem. 看这篇文章,它非常接近您的问题。 Itzik Ben-Gan | Itzik Ben-Gan | SQL Server Pro SQL Server专业版

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

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