用于组合列的SQL [英] SQL to combine columns

查看:51
本文介绍了用于组合列的SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这已经多次提出了,但无论如何我都会问。


假设我有两张相关的表:


所有者:

---------

o_id

o_name


狗:

---------

d_id

d_name

o_id - 对于所有者表。


如果数据布局为


o_id o_name

1 John


d_id d_name o_id

1 Skippy 1

2 Fido 1


如何我可以进行一个会产生以下结果的查询:


o_id o_name拥有狗的名字

1 John Skippy,Fido


我认为它与工会有关但我似乎无法得到它。我是使用SQL Server Compact Edition的

I''m sure this has been brought up many times, but I will ask anyway.

Let''s say I have 2 tables related:

Owner:
---------
o_id
o_name

Dog:
---------
d_id
d_name
o_id - for Owner table.

If the data is laid out as

o_id o_name
1 John

d_id d_name o_id
1 Skippy 1
2 Fido 1

How can I make a query that will produce the following results:

o_id o_name owned dog names
1 John Skippy, Fido

I think it has something to do with unions but I can''t seem to get it. I''m
using SQL Server Compact Edition.

推荐答案

Steve London(sy ****** @ optonline.net)写道:
Steve London (sy******@optonline.net) writes:

让我们说我有2个相关的表:


所有者:

---------

o_id

o_name


狗:

---------

d_id

d_name

o_id - 用于所有者表。


如果数据布局为


o_id o_name

1 John


d_id d_name o_id

1 Skippy 1

2 Fido 1


如何进行产生以下结果的查询:


o_id o_name拥有狗的名字

1 John Skippy,Fido


我认为这与某事有关工会,但我似乎无法得到它。

我正在使用SQL Server Compact Edition。
Let''s say I have 2 tables related:

Owner:
---------
o_id
o_name

Dog:
---------
d_id
d_name
o_id - for Owner table.

If the data is laid out as

o_id o_name
1 John

d_id d_name o_id
1 Skippy 1
2 Fido 1

How can I make a query that will produce the following results:

o_id o_name owned dog names
1 John Skippy, Fido

I think it has something to do with unions but I can''t seem to get it.
I''m using SQL Server Compact Edition.



没有任何直接的方法可以做到这一点。这可以做到这一点

一个语句带有一个XML的怪癖,如下例所示:


选择CustomerID,

substring( OrdIdList,1,datalength(OrdIdList)/ 2 - 1)

- 从列表中删除最后一个'',''

来自

客户c交叉申请

(选择转换(nvarchar(30),OrderID)+'',''作为[text()]

来自Orders o

其中o.CustomerID = c.CustomerID

由o.OrderID订购

为xml路径('''))为Dummy(OrdIdList)

go


我不知道所有这些语法是否可用,但是在Compact Edition中。

(虽然叫做SQL Server,它是一个完全不同的引擎。)


否则你将运行游标来实现这个结果。



-

Erland Sommarskog,SQL Server MVP, es **** @ sommarskog.se


SQL Server 2005联机丛书
http://www.microsoft.com/technet/pro ... ads / books.mspx

SQL Server 2000联机丛书
http://www.microsoft.com/sql/prodinf...ons/books.mspx

There isn''t any straight-forward way of doing this. It is possible do this
one statement with a quirk of XML as in this example:

select CustomerID,
substring(OrdIdList, 1, datalength(OrdIdList)/2 - 1)
-- strip the last '','' from the list
from
Customers c cross apply
(select convert(nvarchar(30), OrderID) + '','' as [text()]
from Orders o
where o.CustomerID = c.CustomerID
order by o.OrderID
for xml path('''')) as Dummy(OrdIdList)
go

I don''t know if all this syntax is available, in Compact Edition, though.
(While called SQL Server, it''s an entirely different engine.)

Else you will have run a cursor to achieve this result.


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


" Erland Sommarskog" < es **** @ sommarskog.sewrote in message

news:Xn ********************** @ 127.0.0.1 ...
"Erland Sommarskog" <es****@sommarskog.sewrote in message
news:Xn**********************@127.0.0.1...

>

我不知道Compact Edition中是否所有这些语法都可用。 />
(虽然称为SQL Server,它是一个完全不同的引擎。)


否则你将运行游标来实现这个结果。
>
I don''t know if all this syntax is available, in Compact Edition, though.
(While called SQL Server, it''s an entirely different engine.)

Else you will have run a cursor to achieve this result.



我最近不得不使用Compact Edition,它支持真正的一套有限的T-SQL。没有存储过程,用户函数,对流构造的控制(如IF..ELSE,但CASE可用),视图,
触发器和游标。 SQL 2005的新排名函数都不是可用的,而是特定于XML的处理(如FOR XML)。此外,不支持CROSS

APPLY,并且不支持变量DECLARE ...


它实际上是紧凑的。并且保持这种方式它只有基本的查询

支持数据库引擎(我的意思是非常基本 - 我甚至无法使用
在FROM中使用派生表SELECT列表中的子查询)。对于所有其他

功能,它取决于ADO.NET和/或您的

应用程序层的丰富功能。所以,解决这个问题的最好方法可能是在应用程序逻辑中解决它。


HTH,


Plamen Ratchev
http://www.SQLStudio.com


是的,我已经在Application Layer中做了。删除一些代码并让SQL执行它只是一个过时的幻想




我想我意外地给你发了一封电子邮件。只需删除它。很抱歉。

Windows Mail非常愚蠢。我真的需要转换。


Plamen Ratchev < Pl **** @ SQLStudio.com写了留言

新闻:Ta ***************** @ newsread1.news.pas.earthl ink .net ...
Yea, I already do it in the Application Layer. It was just a passing fancy
to remove some code and let SQL do it.

I think I sent you an email accidently. Just delete it. Sorry about that.
Windows Mail is so stupid. I really need to switch.

"Plamen Ratchev" <Pl****@SQLStudio.comwrote in message
news:Ta*****************@newsread1.news.pas.earthl ink.net...

" Erland Sommarskog" < es **** @ sommarskog.sewrote in message

news:Xn ********************** @ 127.0.0.1 ...
"Erland Sommarskog" <es****@sommarskog.sewrote in message
news:Xn**********************@127.0.0.1...

>>
我不知道Compact Edition中是否所有这些语法都可用。
(虽然称为SQL Server,它是一个完全不同的引擎。)

否则你将运行游标来实现这个结果。
>>
I don''t know if all this syntax is available, in Compact Edition, though.
(While called SQL Server, it''s an entirely different engine.)

Else you will have run a cursor to achieve this result.



我最近不得不使用Compact Edition,它支持真正的一套有限的T-SQL。没有存储过程,用户函数,对流构造的控制(如IF..ELSE,但CASE可用),视图,
触发器和游标。 SQL 2005的新排名函数都不是可用的,而是特定于XML的处理(如FOR XML)。此外,不支持CROSS

APPLY,并且不支持变量DECLARE ...


它实际上是紧凑的。并且保持这种方式它只有基本的查询

支持数据库引擎(我的意思是非常基本 - 我甚至无法使用
在FROM中使用派生表SELECT列表中的子查询)。对于所有

其他功能,它取决于ADO.NET的丰富功能和/或

您的应用程序层。所以,可能是解决这个问题的最好方法

就是在应用程序逻辑中解决它。


HTH,


Plamen Ratchev
http://www.SQLStudio.com


I had to work with the Compact Edition recently and it supports really a
limited set of T-SQL. There are no stored procedures, user functions,
control of flow constructs (like IF..ELSE, but CASE is available), views,
triggers, and cursors. None of the new ranking functions for SQL 2005 are
available as well as the XML specific handling (like FOR XML). Also, CROSS
APPLY is not supported, and no DECLARE for variables...

It is really "compact" and to keep it that way it has only the basic query
support of the database engine (and I mean really basic - I could not even
use derived tables in FROM and subqueries in the SELECT list). For all
other functionality it depends on the rich functionality of ADO.NET and/or
your application layer. So, probably the best way to approach this problem
is to solve it in the application logic.

HTH,

Plamen Ratchev
http://www.SQLStudio.com


这篇关于用于组合列的SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆