创建过程以生成连接不同表的表,其中表可以重复多行?

时间:2018-05-14 13:04:27

标签: sql sql-server join procedure

我有三个表T1T2T3,所有表中的每个表都为bpm_no。表T1是主表。 T1每行都有唯一的bpm_no(不重复)。表格T2包括两个第一列bpm_no,另一列user,此处一个bpm_no可以多次出现,同一个bpm_no上的用户不同。表格T3包括两个第一列bpm_no,另一列total_outstanding,此处还有一个bpm_no可以多次出现total_outstanding,一个{{1}不同的银行可以有不同的优秀。

现在,我必须编写将使用上述所有表(内部联接)构造表的过程,并且必须包含三列,一列包含bpm_no(每行唯一),另一列包含为每个人bpm_no分隔逗号的用户,以及总和为bpm_no的最后一列。想法是让每个total_outstanding的结束表都是唯一的,结果值以逗号分隔,贷款为总和。

我尝试使用视图,但它无法正常工作,因为我必须创建大量视图。请提出其他方法。

下面是表格结构,以便更好地理解:

bpm_no

2 个答案:

答案 0 :(得分:1)

如果您已经使用SQL Server 2017,则可以使用string_agg()获取用户列表:

SELECT [T3].[bpm_no],
       [T2].[user],
       [T3].[loan_os]
       FROM (SELECT [T3].[bpm_no],
                    sum([T3].[loan_os]) [loan_os]
                    FROM [T3]
                    GROUP BY [T3].[bpm_no]) T3
            LEFT JOIN (SELECT [T2].[bpm_no],
                              string_agg([T2].[user], ',') [user]
                              FROM [T2]
                              GROUP BY [T2].[bpm_no]) [T2]
                      ON [T2].[bpm_no] = [T3].[bpm_no];

答案 1 :(得分:0)

试试这个...

表格脚本和示例数据

CREATE TABLE [T1](
    [bpm_no] [nvarchar](50) NULL,
    [name] [nvarchar](50) NULL
) 


CREATE TABLE [T2](
    [bpm_no] [nvarchar](50) NULL,
    [user] [nvarchar](50) NULL
) 


CREATE TABLE [T3](
    [bpm_no] [nvarchar](50) NULL,
    [loan_os] [decimal](18, 0) NULL
) 

INSERT [T1] ([bpm_no], [name]) VALUES (N'abc_0011', N'john')

INSERT [T2] ([bpm_no], [user]) VALUES (N'abc_0011', N'abc')
INSERT [T2] ([bpm_no], [user]) VALUES (N'abc_0011', N'bcd')
INSERT [T2] ([bpm_no], [user]) VALUES (N'abc_0011', N'lmn')

INSERT [T3] ([bpm_no], [loan_os]) VALUES (N'abc_0011', CAST(14500 AS Decimal(18, 0)))
INSERT [T3] ([bpm_no], [loan_os]) VALUES (N'abc_0011', CAST(4000 AS Decimal(18, 0)))
INSERT [T3] ([bpm_no], [loan_os]) VALUES (N'abc_0011', CAST(5000 AS Decimal(18, 0)))

<强>查询

SELECT t1.bpm_no, 
       sq1.[user], 
       sq2.loan_os 
FROM   t1 
       INNER JOIN (SELECT bpm_no, 
                          Stuff((SELECT ', ' + [user] 
                                 FROM   t2 t21 
                                 WHERE  t21.bpm_no = t22.bpm_no 
                                 FOR xml path('')), 1, 2, '') AS [user] 
                   FROM   t2 t22 
                   GROUP  BY bpm_no) sq1 
               ON t1.bpm_no = sq1.bpm_no 
       INNER JOIN (SELECT bpm_no, 
                          Sum(loan_os) AS loan_os 
                   FROM   t3 
                   GROUP  BY bpm_no) sq2 
               ON t1.bpm_no = sq2.bpm_no 

注意:如果要获取bpm_no主表中所有T1的值(包括空值),请使用左连接而不是内连接。

查询(没有T1主表的预期结果相同)

SELECT sq1.bpm_no, 
       sq1.[user], 
       sq2.loan_os 
FROM   (SELECT bpm_no, 
               Stuff((SELECT ', ' + [user] 
                      FROM   t2 t21 
                      WHERE  t21.bpm_no = t22.bpm_no 
                      FOR xml path('')), 1, 2, '') AS [user] 
        FROM   t2 t22 
        GROUP  BY bpm_no) sq1 
       INNER JOIN (SELECT bpm_no, Sum(loan_os) AS loan_os 
                   FROM   t3 
                   GROUP  BY bpm_no) sq2 
               ON sq1.bpm_no = sq2.bpm_no 

<强>输出

+----------+---------------+---------+
|  bpm_no  |     user      | loan_os |
+----------+---------------+---------+
| abc_0011 | abc, bcd, lmn |   23500 |
+----------+---------------+---------+

演示:http://www.sqlfiddle.com/#!18/b6362/2/0

如果我误解了,请告诉我。