CASE 表达式中的嵌套 SELECT 语句

发布于 2024-10-07 22:03:44 字数 2122 浏览 5 评论 0原文

您好,

这是我的问题。
我需要从多行获取数据并将它们作为较大查询中的单个结果返回。

我已经在这里发布了类似的问题。 在主查询中的一列中返回多个值 但我怀疑我缺乏 SQL 知识使得问题过于模糊,因为答案不起作用。
我正在使用 Microsoft SQL 2005。
这是我所拥有的。

多个表以CaseID为PK,CaseID是唯一的。
一张表 (tblKIN),其中 CaseID 和 ItemNum(AutoInc) 作为组合 PK。
因为数据库中的每个人可能都有不止一个亲戚。
如果我在 SQL 查询窗口中运行以下命令,它就会起作用。

DECLARE @KINList varchar(1000)  
SELECT @KINList = coalesce(@KINList + ', ','') + KINRel from tblKIN  
WHERE CaseID = 'xxx' and Address = 'yyy'  
ORDER BY KINRel  
SELECT @KINList  

这将返回居住在同一地址的所有人员的关系。结果看起来像这样...
父亲、侄女、姐妹、儿子

现在,我的问题是如何将其添加到我的主查询中?
缩短为相关信息,主要查询如下所示。

SELECT DISTINCT  
c.CaseID,  
c.Name,  
c.Address,  
Relatives=CASE WHEN exists(select k.CaseID from tblKIN k where c.CaseID = k.CaseID)  
   THEN DECLARE @KINList varchar(1000)  
        SELECT @KINList = coalesce(@KINList + ', ','') + KINRel from tblKIN  
        WHERE CaseID = 'xxx' and Address = 'yyy'  
        ORDER BY KINRel  
        SELECT @KINList  
   ELSE ''  
   END
FROM tblCase c  
ORDER BY c.CaseID

我收到的错误是。
服务器:消息 156,级别 15,状态 1,第 13 行
关键字“DECLARE”附近的语法不正确。
服务器:消息 156,级别 15,状态 1,第 18 行
关键字“ELSE”附近的语法不正确。

我尝试在括号内嵌套从 DECLARE 到 SELECT @KINList 的末尾。
我尝试将 BEGIN 和 END 添加到 CASE 语句的 THEN 部分。
两者都不起作用。

源表数据看起来像这样。 (为了可读性而添加了句点
tblCase
案例 ID 名称 地址
10-001吉姆......100主街
10-002 汤姆....榆树街 150 号
10-003 Abe.....200 1st St.

tblKIN
CaseID ItemNum 名称 关系 地址
10-001 00001 史蒂夫...儿子........100 Main St.
10-002 00002 詹姆斯..父亲....150 Elm St.
10-002 00003 贝蒂....侄女......150 Elm St.
10-002 00004 Greta...妹妹.....150 Elm St.
10-002 00005 戴维..儿子........150 Elm St.
10-003 00006 Edgar...Brother...200 1st St.

如果我运行 CaseID = 10-002 的查询,它需要返回以下内容。
CaseID 姓名 地址......亲戚
10-002 Tom...150 Elm St. ..父亲,侄女,姐妹,儿子

我确信这可能是一个简单的修复,但我只是不知道该怎么做。
感谢您抽出宝贵的时间,对于问题的长度我深表歉意,但我想澄清一下。

谢谢 !!!

Greetings,

Here is my problem.
I need to get data from multiple rows and return them as a single result in a larger query.

I already posted a similar question here.
Return multiple values in one column within a main query but I suspect my lack of SQL knowledge made the question too vague because the answers did not work.
I am using Microsoft SQL 2005.
Here is what I have.

Multiple tables with CaseID as the PK, CaseID is unique.
One table (tblKIN) with CaseID and ItemNum(AutoInc) as the combined PK.
Because each person in the database will likely have more than one relative.
If I run the following, in a SQL query window, it works.

DECLARE @KINList varchar(1000)  
SELECT @KINList = coalesce(@KINList + ', ','') + KINRel from tblKIN  
WHERE CaseID = 'xxx' and Address = 'yyy'  
ORDER BY KINRel  
SELECT @KINList  

This will return the relation of all people who live at the same address. the results look like this...
Father, Niece, Sister, Son

Now, the problem for me is how do I add that to my main query?
Shortened to relevant information, the main query looks like this.

SELECT DISTINCT  
c.CaseID,  
c.Name,  
c.Address,  
Relatives=CASE WHEN exists(select k.CaseID from tblKIN k where c.CaseID = k.CaseID)  
   THEN DECLARE @KINList varchar(1000)  
        SELECT @KINList = coalesce(@KINList + ', ','') + KINRel from tblKIN  
        WHERE CaseID = 'xxx' and Address = 'yyy'  
        ORDER BY KINRel  
        SELECT @KINList  
   ELSE ''  
   END
FROM tblCase c  
ORDER BY c.CaseID

The errors I receive are.
Server: Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'DECLARE'.
Server: Msg 156, Level 15, State 1, Line 18
Incorrect syntax near the keyword 'ELSE'.

I tried nesting inside parenthesis from the DECLARE to the end of the SELECT @KINList.
I tried adding a BEGIN and END to the THEN section of the CASE statement.
Neither worked.

The source table data looks something like this. (periods added for readability)
tblCase
CaseID Name Address
10-001 Jim......100 Main St.
10-002 Tom....150 Elm St.
10-003 Abe.....200 1st St.

tblKIN
CaseID ItemNum Name Relation Address
10-001 00001 Steve...Son........100 Main St.
10-002 00002 James..Father....150 Elm St.
10-002 00003 Betty....Niece......150 Elm St.
10-002 00004 Greta...Sister.....150 Elm St.
10-002 00005 Davey..Son........150 Elm St.
10-003 00006 Edgar...Brother...200 1st St.

If I run the query for CaseID = 10-002, it needs to return the following.
CaseID Name Address.......Relatives
10-002 Tom...150 Elm St. ..Father, Niece, Sister, Son

I am sure this is probably a simple fix, but I just don't know how to do it.
Thank you for your time, and I apologize for the length of the question, but I wanted to be clear.

Thanks !!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

源来凯始玺欢你 2024-10-14 22:03:44

当我做类似的事情时,我必须创建一个标量函数来执行返回 varchar 结果的合并。然后在 select 中调用即可。

CREATE FUNCTION GetRelatives 
(
    @CaseID varchar(10)
)
RETURNS varchar(1000)
AS
BEGIN

    DECLARE @KINList varchar(1000)  
    SELECT @KINList = coalesce(@KINList + ', ','') + KINRel from tblKIN  
        WHERE CaseID = @CaseID  
    ORDER BY KINRel  

    RETURN @KINList 

END

然后你的选择

SELECT DISTINCT  
c.CaseID,  
c.Name,  
c.Address,  
database.dbo.GetRelatives(c.CaseID) AS Relatives
FROM tblCase c  
ORDER BY c.CaseID

When I did something similar I had to create a scalar function to do the coalesce that returns the varchar result. Then just call it in the select.

CREATE FUNCTION GetRelatives 
(
    @CaseID varchar(10)
)
RETURNS varchar(1000)
AS
BEGIN

    DECLARE @KINList varchar(1000)  
    SELECT @KINList = coalesce(@KINList + ', ','') + KINRel from tblKIN  
        WHERE CaseID = @CaseID  
    ORDER BY KINRel  

    RETURN @KINList 

END

Then your select

SELECT DISTINCT  
c.CaseID,  
c.Name,  
c.Address,  
database.dbo.GetRelatives(c.CaseID) AS Relatives
FROM tblCase c  
ORDER BY c.CaseID
玩物 2024-10-14 22:03:44

您可以创建一个 FUNCTION,它接受 caseID 作为参数并返回 truefalse

由于您多次调用嵌套查询,因此它肯定会影响性能。更好的解决方案是执行查询并将结果存储在临时表中。
然后将此临时表和 caseID 传递给 FUNCTION 并检查是否包含。

You can create a FUNCTION which takes in the caseID as the arguement and returns true or false.

Since you are calling the nested query multiple times, its definitely a performance hit. A better solution is to execute the query and store the results in a temporary table.
Then pass this temporary table and the caseID to the FUNCTION and check for containment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文