Question

I have 2 SQL Server tables

equip_info

equip_id | asset_no
-------------------
1        | CAL-1 
2        | MIC-1
...

equip_insp

insp_id | equip_id | insp_name   | due_date
----------------------------------------------
1       | 1        | Calibration | 2014-01-10
2       | 1        | Maintenance | 2014-01-10
3       | 1        | Check       | 2014-02-10
4       | 2        | Calibration | 2014-11-04
5       | 2        | Maintenance | 2014-05-04
...

equip_id doesn't have 2 similar insp_name, but this is less important.

In one sentence, I would like to help me with the T-SQL syntax for getting a list of distinct equipments and with the "next" inspection or inspections (if due_date's are the same) and the due_date.

It could be the case when for the same due_date there are several inspections, so in this case this must be concatenated and shown in one field (ex: "Calibration, Maintenance")

Result of this query should be something like:

equip_id | asset_no | inspection               | due_date
-----------------------------------------------------------
1        | CAL-1    | Calibration, Maintenance | 2014-01-10
2        | MIC-1    | Maintenance              | 2014-05-04
Was it helpful?

Solution

Using the SqlFiddle of @Shiva:

create table equip_info (
  equip_id int not null,
   asset_no nvarchar(10) not null
  );

insert into equip_info (equip_id, asset_no) values
(1, 'CAL-1'),
(2, 'MIC-1');

create table equip_insp (
  insp_id  int not null,
  equip_id  int not null,
  insp_name nvarchar(50) not null,
  due_date datetime not null
  );

insert into equip_insp values
(1,1,'Calibration','10/01/2014'),
(2,1,'Maintenance','10/01/2014'),
(3,1,'Check','10/02/2014'),
(4,2,'Calibration','04/11/2014'),
(5,2,'Maintenance','04/05/2014');

This is what you need:

;WITH DataSource AS
(
  select inf.equip_id
        ,asset_no
        ,MIN(due_date) as [due_date]
  from equip_info inf
  inner join equip_insp ins
    on inf.[equip_id] = ins.[equip_id]
  GROUP BY inf.equip_id
          ,asset_no
)
SELECT equip_id
      ,asset_no
      ,SUBSTRING((SELECT DISTINCT ', ' + insp_name 
        FROM equip_insp 
        WHERE due_date = DS.due_date
          AND equip_id = DS.equip_id
        FOR XML PATH('')
       ),2,4000) as inspection
      ,due_date
FROM DataSource DS

This is the ouput:

enter image description here

Here is the full working example.

OTHER TIPS

SELECT DISTINCT   Ins.equip_id
               ,  info.asset_no
               , STUFF(List.Inspections, 1 , 2, '') AS Inspections
               , ins.due_date 
FROM #equip_insp Ins INNER JOIN #equip_info Info
ON Ins.equip_id = info.equip_id CROSS APPLY 
                                    (
                                    SELECT ', ' + insp_name [text()]
                                    FROM #equip_insp
                                    WHERE equip_id = Ins.equip_id
                                    AND due_date = Ins.due_date
                                    FOR XML PATH('')
                                    ) List(Inspections)

Results

equip_id    asset_no    Inspections                     due_date
1           CAL-1       Calibration, Maintenance    2014-10-01 00:00:00.000
1           CAL-1       Check                       2014-10-02 00:00:00.000
2           MIC-1       Calibration                 2014-04-11 00:00:00.000
2           MIC-1       Maintenance                 2014-04-05 00:00:00.000
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top