【发布时间】:2016-03-09 19:00:17
【问题描述】:
请查看我的表和查询以及数据检索方案。 我无法比较/检查与 from 到另一个表之间的日期。 我的逻辑是在合同的开始日期和结束日期之间记录事件(它应该在该合同的生效日期和到期日期之间)
Table:Incident:
+-------------+-------------+---------+
| incident_id | date_logged | item_id |
+-------------+-------------+---------+
| 10074329 | 2015-09-01 | 63247 |
| 10074869 | 2016-01-31 | 63247 |
| 10074399 | 2016-01-20 | 63247 |
| 10075274 | 2016-02-07 | 63247 |
| 10035727 | 2013-08-02 | 47558 |
| 10050239 | 2014-07-08 | 47558 |
| 10076340 | 2016-02-25 | 47558 |
+-------------+-------------+---------+
Table:Item:
+---------+-------------+
| item_id | item_sc |
+---------+-------------+
| 63247 | 313150069 |
| 47558 | 2S27500EVW |
+---------+-------------+
Table:item_maint
+---------------+---------+---------------+-------------+-------------+
| item_maint_id | item_id | maint_crct_id | start_date | end_date |
+---------------+---------+---------------+-------------+-------------+
| 701 | 63247 | 2132 | 2015-11-11 | 2016-11-10 |
| 702 | 63247 | 1819 | 2014-11-11 | 2015-11-10 |
| 703 | 47558 | 921 | 2013-05-01 | 2016-09-08 |
+---------------+---------+---------------+-------------+-------------+
Table:maint_crct
+---------------+-----------------+----------------+-------------+
| maint_crct_id | maint_crct_sc | effective_date | expiry_date |
+---------------+-----------------+----------------+-------------+
| 1819 | ACSS-2015-0011 | 2014-11-11 | 2015-11-10 |
| 2132 | ACSS-2015-0091 | 2015-11-11 | 2016-11-10 |
| 921 | ACSS-2013-0066 | 2013-05-01 | 2016-09-08 |
+---------------+-----------------+----------------+-------------+
我的查询:
Declare @contract varchar(50); set @contract='ACSS-2015-0011'
declare @from date;set @from='2014-01-01'
declare @To date; set @To='2015-09-01'
select dbo.incident.incident_id,dbo.incident.date_logged,dbo.item.item_id,
dbo.item.item_sc,dbo.maint_crct.maint_crct_id, dbo.maint_crct.maint_crct_sc
FROM dbo.incident INNER JOIN
dbo.item ON dbo.incident.item_id = dbo.item.item_id INNER JOIN
dbo.item_maint INNER JOIN dbo.maint_crct ON dbo.item_maint.maint_crct_id =
dbo.maint_crct.maint_crct_id
ON dbo.incident.item_id = dbo.item_maint.item_id
WHERE dbo.maint_crct.maint_crct_sc=@contract
如何在 WHERE 子句中添加 @from 和 @To 日期以检查 maint_crct 表的有效日期和到期日期之间的记录日期。
Expected Result:
+-------------+-------------+---------+-----------+---------------+----------------+
| incident_id | date_logged | item_id | item_sc | maint_crct_id | maint_crct_sc |
+-------------+-------------+---------+-----------+---------------+----------------+
| 10074329 | 2015-09-01 | 63247 | 313150069 | 1819 | ACSS-2015-0011 |
+-------------+-------------+---------+-----------+---------------+----------------+
【问题讨论】:
-
这是一个很好的起点。 spaghettidba.com/2015/04/24/…
-
您将不得不更好地解释预期结果背后的逻辑。 “预期结果:”之前的最后一段对我来说没有任何意义。
-
@TabAlleman :我的逻辑是从合同的 from 和 To 日期之间记录的 incidnet 表中获取数据,并且需要比较该合同的生效日期和到期日期之间的日期。谢谢
-
好的,那么我下面的回答应该涵盖它。如果您进行了尝试但未得到预期结果,请使用新查询编辑您的问题,以便我们对其进行调试。
-
@TabAlleman 感谢您的建议,我在 WHERE 子句中添加了脚本“和 dbo.item_maint.start_date = dbo.maint_crct.effective_date”但是没有得到真正的答案..再次感谢
标签: sql-server sql-server-2008 sql-server-2008-r2 qsqlquery