lundi 29 février 2016

how to get actual result by using date rage in linked tables

Table : item

+---------+------------+
| item_id |  item_sc   |
+---------+------------+
|   63247 | 313150069  |
|   47558 | 2S27500EVW |
+---------+------------+

Table: incident

+-------------+-------------+---------+
| incident_id | date_logged | Item_id |
+-------------+-------------+---------+
|    10074329 | 1-Nov-2015  |   63247 |
|    10074869 | 31-Jan-2016 |   63247 |
|    10074399 | 20-Jan-2016 |   63247 |
|    10075274 | 7-Feb-2016  |   63247 |
|    10035727 | 2-Aug-2013  |   47558 |
|    10050239 | 8-Jul-2014  |   47558 |
|    10076340 | 25-Feb-2016 |   47558 |
|             |             |         |
+-------------+-------------+---------+

Table: item_maint

+---------------+---------+---------------+-------------+-------------+
| item_maint_id | item_id | maint_crct_id | start_date  |  end_date   |
+---------------+---------+---------------+-------------+-------------+
|         71748 |   63247 |          2132 | 11-Nov-2015 | 10-Nov-2016 |
|         62281 |   63247 |          1819 | 11-Nov-2014 | 10-Nov-2015 |
|         40786 |   47558 |           921 | 1-May-2013  | 8-Sep-2016  |
+---------------+---------+---------------+-------------+-------------+

Table: maint_crtc

+---------------+----------------+----------------+-------------+
| maint_crct_id | maint_crct_sc  | effective_date | expiry_date |
+---------------+----------------+----------------+-------------+
|          1819 | ACSS-2015-0011 | 11-Nov-2014    | 10-Nov-2015 |
|          2132 | ACSS-2015-0091 | 11-Nov-2015    | 10-Nov-2016 |
|           921 | ACSS-2013-0066 | 1-May-2013     | 8-Sep-2016  |
+---------------+----------------+----------------+-------------+

Expected Result

+-------------+-------------+-----------+----------------+
| incident_id | date_logged |  item_sc  | maint_crct_sc  |
+-------------+-------------+-----------+----------------+
|    10074869 | 31-Jan-2016 | 313150069 | ACSS-2015-0091 |
|    10074399 | 20-Jan-2016 | 313150069 | ACSS-2015-0091 |
|    10075274 | 7-Feb-2016  | 313150069 | ACSS-2015-0091 |
+-------------+-------------+-----------+----------------+

My Query

select DISTINCT incident.incident_id,incident.date_logged,ITEM.item_id, item.item_sc,maint_crct.maint_crct_id, maint_crct.maint_crct_sc
FROM 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 maint_crct.maint_crct_n='ACSS-2015-0091'

As per my above query I am getting result like below,

+-------------+-------------+-----------+----------------+
| incident_id | date_logged |  item_sc  | maint_crct_sc  |
+-------------+-------------+-----------+----------------+
|    10074869 | 31-Jan-2016 | 313150069 | ACSS-2015-0091 |
|    10074399 | 20-Jan-2016 | 313150069 | ACSS-2015-0091 |
|    10075274 | 7-Feb-2016  | 313150069 | ACSS-2015-0091 |
|    10074329 | 1-Nov-2015  | 313150069 | ACSS-2015-0091 |
+-------------+-------------+-----------+----------------+

How can I get the result without 10074329.

Aucun commentaire:

Enregistrer un commentaire