patex1987
patex1987

Reputation: 417

SQL Server Query for Many to Many Relationship

I have the following Many to many relationship (See the picture below) in my SQL server.

Many to many relationship

In most cases there's are 2 rows in table tblWavelengths related to the table tblSensors, (in some cases only 1, and in extreme cases there can be 20 rows)

I made the following simple query to retrieve the data from those 3 tables :

select W.DateTimeID,S.SensorName,S.SensorType,W.Channel,W.PeakNr,W.Wavelength
from tblWavelengths as W
    Left Join tblSensorWavelengths as SW on W.tblWavelengthID = SW.WavelengthID
    Left Join tblSensors as S on SW.SensorID = S.SensorID
order by W.DateTimeID

After running this query I got the following results :

Result

Here comes my problem. I want to write a query which filters only those Sensors (SensorName) which at a given moment in time (DateTimeID) has two rows (two different wavelengths) in the tblWavelengths table. So for example I want to have the results without the 77902/001 Sensor - because it has only one row (one Wavelength) related to the tblSensors at a given moment in time

Upvotes: 9

Views: 18199

Answers (1)

GarethD
GarethD

Reputation: 69769

You could use a windowed function to find out the number of wavelengths for each sensorname/datetimeid combination:

WITH Data AS
(   SELECT  W.DateTimeID,
            S.SensorName,
            S.SensorType,
            W.Channel,
            W.PeakNr,
            W.Wavelength,
            [Wcount] = COUNT(*) OVER(PARTITION BY s.SensorName, d.DateTimeID)
    from    tblWavelengths as W
            LEFT JOIN tblSensorWavelengths as SW 
                ON W.tblWavelengthID = SW.WavelengthID
            LEFT JOIN tblSensors as S 
                ON SW.SensorID = S.SensorID
)
SELECT  DateTimeID, SensorName, SensorType, Channel, PeakNr, WaveLength
FROM    Data
WHERE   Wcount = 2
ORDER BY DateTimeID;

ADDENDUM

As an after thought I realised that you might have two results for one sensor at the same time with the same wavelength, which would return 2 records, but not have two different wavelengths. Since windowed functions don't support the use of DISTINCT an alternative is below

WITH Data AS
(   SELECT  W.DateTimeID,
            S.SensorName,
            S.SensorType,
            W.Channel,
            W.PeakNr,
            W.Wavelength,
            W.tblWaveLengthID
    from    tblWavelengths as W
            LEFT JOIN tblSensorWavelengths as SW 
                ON W.tblWavelengthID = SW.WavelengthID
            LEFT JOIN tblSensors as S 
                ON SW.SensorID = S.SensorID

)
SELECT  d.DateTimeID, d.SensorName, d.SensorType, d.Channel, d.PeakNr, d.WaveLength
FROM    Data d
        INNER JOIN
        (   SELECT  DateTimeID, SensorName
            FROM    Data
            GROUP BY DateTimeID, SensorName
            HAVING  COUNT(DISTINCT tblWaveLengthID) = 2
        ) t
            ON t.DateTimeID = d.DateTimeID
            AND t.SensorName = d.SensorName
ORDER BY d.DateTimeID;

Upvotes: 10

Related Questions