Bill(Master)和BillDetail(Detail)表:使用表联接生成查询

时间:2019-04-10 08:31:39

标签: sql sql-server

我是SQL Server的新手,想从两个表-Bill(主表)和BillDetail(详细表)生成查询。这两个表数据如下:

帐单数据:

+------------------+--------------+---------------+------------+----------+-------------------------+
| ContractorBillId | ContractorId |    BillNo     |  BillDate  |IsDeleted |       UpdatedDate       |
+------------------+--------------+---------------+------------+----------+-------------------------+
|             1050 |            2 | 2-20190329-W  | 2019-03-29 |        0 | 2019-03-29 13:53:51.447 |
|             1051 |            2 | 2-20190329-W1 | 2019-03-29 |        0 | 2019-03-29 18:48:48.077 |
|             1052 |            2 | 2-20190402-W  | 2019-04-02 |        0 | 2019-04-02 15:54:16.267 |
|             1053 |            1 | 1-20190402-E  | 2019-04-02 |        0 | 2019-04-02 18:58:50.753 |
|             1078 |            1 | 1-20190403-A  | 2019-04-03 |        0 | 2019-04-03 10:59:18.083 |
|             1079 |            1 | 1-20190403-A1 | 2019-04-03 |        0 | 2019-04-03 11:00:37.197 |
|             1080 |            1 | 1-20190403-E  | 2019-04-03 |        0 | 2019-04-03 11:33:41.333 |
+------------------+--------------+---------------+------------+----------+-------------------------+

BillDetail表数据:

+----------------------+------------------+---------------------+------------+--------+-------------+
| ContractorBillItemId | ContractorBillId | ContractorPayTypeId | WeekEnding | Hours  | GrossAmount |
+----------------------+------------------+---------------------+------------+--------+-------------+
|                 1064 |             1050 |                   2 | 2019-03-29 | 145.00 | 725000.0000 |
|                 1065 |             1051 |                   1 | 2019-03-29 |  50.00 |  75000.0000 |
|                 1066 |             1052 |                   1 | 2019-04-05 |  10.00 |  15000.0000 |
|                 1067 |             1052 |                   2 | 2019-04-05 |  12.00 |  60000.0000 |
|                 1068 |             1053 |                   4 | 2019-04-02 |   1.00 |    100.0000 |
|                 1069 |             1053 |                   3 | 2019-04-03 |   1.00 |    100.0000 |
|                 1070 |             1053 |                   2 | 2019-04-04 |   1.00 |    100.0000 |
|                 1071 |             1053 |                   4 | 2019-04-05 |   1.00 |    100.0000 |
|                 1072 |             1053 |                   4 | 2019-04-06 |   1.00 |    100.0000 |
|                 1089 |             1078 |                1006 | 2019-04-05 |   1.00 |    100.0000 |
|                 1090 |             1079 |                1006 | 2019-04-05 |  12.00 |   1200.0000 |
|                 1091 |             1080 |                   4 | 2019-04-02 |   1.00 |    100.0000 |
|                 1092 |             1080 |                   4 | 2019-04-03 |   1.00 |    100.0000 |
|                 1093 |             1080 |                   4 | 2019-04-04 |   1.00 |    100.0000 |
|                 1094 |             1080 |                   4 | 2019-04-05 |   1.00 |    100.0000 |
|                 1095 |             1080 |                   4 | 2019-04-06 |   1.00 |    100.0000 |
+----------------------+------------------+---------------------+------------+--------+-------------+

从这两张表中,我想要下面的结果

+------------------+--------------+---------------+------------+----------+-------------------------+--------------------+------------+--------+
| ContractorBillId | ContractorId |    BillNo     |  BillDate  |IsDeleted |       UpdatedDate       | ContractorPayTypeId| WeekEnding | Hours  |
+------------------+--------------+---------------+------------+----------+-------------------------+--------------------+------------+--------+
|             1050 |            2 | 2-20190329-W  | 2019-03-29 |        0 | 2019-03-29 13:53:51.447 |                  2 | 2019-03-29 | 145.00 |
|             1051 |            2 | 2-20190329-W1 | 2019-03-29 |        0 | 2019-03-29 18:48:48.077 |                  1 | 2019-03-29 |  50.00 |
|             1052 |            2 | 2-20190402-W  | 2019-04-02 |        0 | 2019-04-02 15:54:16.267 |                  1 | 2019-04-05 |  22.00 |
|             1053 |            1 | 1-20190402-E  | 2019-04-02 |        0 | 2019-04-02 18:58:50.753 |                  4 | 2019-04-02 |   5.00 |
|             1078 |            1 | 1-20190403-A  | 2019-04-03 |        0 | 2019-04-03 10:59:18.083 |               1006 | 2019-04-05 |   1.00 |
|             1079 |            1 | 1-20190403-A1 | 2019-04-03 |        0 | 2019-04-03 11:00:37.197 |               1006 | 2019-04-05 |  12.00 |
|             1080 |            1 | 1-20190403-E  | 2019-04-03 |        0 | 2019-04-03 11:33:41.333 |                  4 | 2019-04-02 |   5.00 |
+------------------+--------------+---------------+------------+----------+-------------------------+--------------------+------------+--------+

请帮助我生成查询。结果,在明细表中,小时和总金额应该是总和,而ContractorPayTypeId和WeekEnding应该是所有项目的首位。

1 个答案:

答案 0 :(得分:1)

您可以使用高级SQL查询,例如SUM()OVER PARTITION BY,FIRST_VALUE()ETC。

 SELECT BillTable.* ,
   CS.SumGrossAmount ,
   CS.SumHours ,
   CS.FirstWeekEnding ,
   CS.FirstContractorPayTypeId
   FROM   dbo.BillTable
   CROSS APPLY 
     (   SELECT  TOP 1 S.SumGrossAmount ,
                        S.SumHours ,
                        S.FirstWeekEnding ,
                        S.FirstContractorPayTypeId
                FROM     (   SELECT * ,
                                    SUM(GrossAmount) OVER ( PARTITION BY ContractorBillId
                                                            ORDER BY ContractorBillItemId
                                                            ) AS SumGrossAmount ,
                                    SUM(Hours) OVER ( PARTITION BY ContractorBillId
                                                        ORDER BY ContractorBillItemId
                                                    ) AS [SumHours] ,
                                    FIRST_VALUE(WeekEnding) OVER ( PARTITION BY ContractorBillId
                                                                    ORDER BY ContractorBillItemId
                                                                ) AS [FirstWeekEnding] ,
                                    FIRST_VALUE(ContractorPayTypeId) OVER ( PARTITION BY ContractorBillId
                                                                            ORDER BY ContractorBillItemId
                                                                            ) AS [FirstContractorPayTypeId]
                            FROM   dbo.BillDetail
                        ) S
                WHERE    S.ContractorBillId = dbo.BillTable.ContractorBillId
                ORDER BY S.ContractorBillItemId DESC
               ) CS;