sql server - 将一对多查询到一列

时间:2017-11-20 16:43:47

标签: sql sql-server database

在我们的数据库中,有一个起诉案例表,其中包含起诉ID和案例代码。还有另一个法院更新表((这是 一对多关系 ,因为一个起诉案件可以有多个法院更新)。

我正在尝试创建一个报告来显示案例及其多个法院更新,我不知道如何编写具有以下列的查询 - 诉讼案例代码&法院更新。

这是样本起诉表

╔════╦═════════════════════╗ ║ID ║ ProsecutionCaseCode ║ ╠════╬═════════════════════╣ ║ 1 ║CASE158SG ║ ╠════╬═════════════════════╣ ║ 2 ║CASE688ZY ║ ╠════╬═════════════════════╣ ║ 3 ║CASE999SS ║ ║ ║ ║ ╚════╩═════════════════════╝

这是法庭更新表的示例(CUid是法院更新ID)

╔════╦══════════════╦═════════════════════════════════════╗ ║CUid║ ProsecutionID║ UPDATE ║ ╠════╬════════════ ═╬═════════════════════════════════════╣ ║ 1 ║ 1 ║ 14th May - Judge wore pink ║ ╠════╬══════════════╬═════════════════════════════════════╣ ║ 2 ║ 1 ║ 13th May - Judge wore black ║ ╠════╬══════════════╬═════════════════════════════════════╣ ║ 3 ║ 1 ║ 12th May - Judge wore orange ║ ╠════╬══════════════╬═════════════════════════════════════╣ ║ 4 ║ 2 ║ 29th November - Judge was a no-show ║ ╠════╬══════════════╬═════════════════════════════════════╣ ║ 5 ║ 3 ║ 19th January - Judge is cute lol ║ ╠════╬══════════════╬═════════════════════════════════════╣ ║ 6 ║ 3 ║ 1st January - Judge was mean ║ ╚════╩══════════════╩═════════════════════════════════════╝

这是我要创建的报告

╔════╦═════════════════════╦════════════════════════════════════════╗ ║SNO ║ ProsecutionCaseCode ║ COURT UPDATE ║ ╠════╬═════════════════════╬════════════════════════════════════════╣ ║ 1 ║CASE158SG ║ 14th May - Judge wore pink ║ ║ ║ ║ 13th May - Judge wore black ║ ║ ║ ║ 12th May - Judge wore orange ║ ╠════╬═════════════════════╬════════════════════════════════════════╣ ║ 2 ║CASE688ZY ║ 29th November - Judge was a no-show ║ ╠════╬═════════════════════╬════════════════════════════════════════╣ ║ 3 ║CASE999SS ║ 19th January - Judge is cute lol ║ ║ ║ ║ 1st January - Judge was mean ║ ╚════╩═════════════════════╩════════════════════════════════════════╝

3 个答案:

答案 0 :(得分:0)

您可以使用以下代码

select u.ProsecutionID as sno,p.ProsecutionCaseCode  ,u.UPDATE as [COURT UPDATE] from prosecution as p inner join Update as u on p.id=u.ProsecutionID
order by u.ProsecutionID,u.CUid

答案 1 :(得分:0)

它不漂亮,但您可以在select子句中使用子查询。

困难的部分是将多行连接成一行。我已经使用了FOR XML

示例:

SELECT
    p.ID,
    p.ProsecutionCaseCode,
    REPLACE(
        REPLACE(
            (
                -- Here we hijack the FOR XML clause to concatenate multiple values into one string.
                -- XML will append opend and closed UPDATE tags, as this is the column name.
                -- Replace functions strip these out.
                SELECT
                    [UPDATE]
                FROM
                    @CourtUpdate AS cu
                WHERE
                    cu.ProsecutionID = p.ID
                FOR XML PATH('')
            )
        , '<UPDATE>', '')
    , '</UPDATE>', CHAR(13)) AS [COURT UPDATE]
FROM
    @prosecution AS p       
ORDER BY
    p.ID
;

此方法使用correlated subquery。这些可能会影响性能,因为它们会强制SQL Server逐行处理。啊。

默认情况下,SSMS仅显示结果网格中的第一行。按Ctrl + T切换到结果到文本模式。从这里您可以查看完整的结果,无需复制/粘贴。

SQL Server 2017包含一个名为string_agg的新功能。在撰写本文时,这是一个非常新的功能。这是一个(未经测试的)示例:

-- SQL Server 2017 or higher.
SELECT
    p.ID,
    p.ProsecutionCaseCode,
    STRING_AGG(cu.ProsecutionID,  CHAR(13)) AS [COURT UPDATE]
FROM
    @prosecution AS p   
        INNER JOIN @CourtUpdate AS cu       ON cu.ProsecutionID = p.ID
GROUP BY
    p.ID,
    p.ProsecutionCaseCode
ORDER BY
    p.ID
;

好多了。

答案 2 :(得分:0)

SQL Server 2012或更高版本

sum()

我强烈建议在这种情况下避免做 INNER JOIN 。使用 LEFT OUTER JOIN ,您将保证显示所有ProsecutionCaseCodes,无论它们是否包含CourtUpdateTable中的条目。