TSQL连接两个表以获得所需的结果

时间:2017-01-18 02:02:06

标签: sql sql-server sql-server-2008 tsql

我有一个问题,为此获得所需的结果。例如,我有这样的表

表1

+----------------------------------------------------------------------------------------+
|SerialId |serialidtagging |unifiedserial |     CreatedDatetime     | Wrkctrid | Embedded|  
|-----------------------------------------------------------------------------------------
|89250432 |   89250432-1   |    3266AD    | 2016-11-19 18:51:06.000 | Entry    |DRC53030 |
|89250432 |   89250432-2   |    3266AD    | 2017-01-05 17:30:45.000 | Entry    |F1461350 |
+----------------------------------------------------------------------------------------+

表2

+---------------------------------------------------------------+
|unifiedserial |Pool| PoNum  |       audit_date        |Row_Num |
-----------------------------------------------------------------
|    3266AD    | IP |07664594| 2016-11-22 18:42:07.000 |   1    |
|    3266AD    | PL |07733189| 2016-11-26 02:24:12.000 |   2    |
|    3266AD    | PL |07771562| 2016-11-29 02:01:28.000 |   3    |
|    3266AD    | Re |07784874| 2016-11-29 14:42:05.000 |   4    |
|    3266AD    | IP |08394765| 2017-01-09 09:56:59.000 |   5    |
|    3266AD    | Re |08427441| 2017-01-10 19:04:37.000 |   6    |
+---------------------------------------------------------------+

所需输出

+---------------------------------------------------------------------------------------------------------------------+
|unifiedserial |Pool|  PoNum   |      audit_date        |Row_num| serialidtagging |serialid |    createddatetime      |
-----------------------------------------------------------------------------------------------------------------------
|   3266AD     |IP  | 07664594 |2016-11-22 18:42:07.000 |   1   |   89250432-1    |89250432 | 2016-11-19 18:51:06.000 |
|   3266AD     |PL  | 07733189 |2016-11-26 02:24:12.000 |   2   |   89250432-1    |89250432 | 2016-11-19 18:51:06.000 |
|   3266AD     |PL  | 07771562 |2016-11-29 02:01:28.000 |   3   |   89250432-1    |89250432 | 2016-11-19 18:51:06.000 |
|   3266AD     |Re  | 07784874 |2016-11-29 14:42:05.000 |   4   |   89250432-1    |89250432 | 2016-11-19 18:51:06.000 |
|   3266AD     |IP  | 08394765 |2017-01-09 09:56:59.000 |   5   |   89250432-2    |89250432 | 2017-01-05 17:30:45.000 |
|   3266AD     |Re  | 08427441 |2017-01-10 19:04:37.000 |   6   |   89250432-2    |89250432 | 2017-01-05 17:30:45.000 |
----------------------------------------------------------------------------------------------------------------------+

加入表格时,我的参考是 unifiedserial 是否可以获得我想要的输出。如果仅 unifiedserial 是参考?正如您在表1 createddatetime = 2016-11-19 18:51:06.000中看到的那样,我不知道是否有可能仅从audit_date月份获得 table2 audit_date = createddatetime。据我所知,我可以通过><= Month of createddatetime-audit_date

来解决这个问题

很抱歉,如果没有解释。但请依靠Sample表和输出。

TIA!

1 个答案:

答案 0 :(得分:5)

SELECT *
FROM table1 t1 JOIN table2 t2
ON t1.unifiedserial = t2.unifiedserial
AND DATEPART(month,t1.createddatetime) = DATEPART(month,t2.audit_date)
AND DATEPART(year,t1.createddatetime) = DATEPART(year,t2.audit_date)