SQL Server查询多对多关系

[英]SQL Server Query for Many to Many Relationship


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

我在我的SQL服务器中有以下多对多关系(见下图)。

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)

在大多数情况下,表tblWavelengths中有2行与表tblSensors相关,(在某些情况下只有1行,在极端情况下可能有20行)

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

我做了以下简单查询来从这3个表中检索数据:

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

这是我的问题。我想写一个查询,它只过滤那些传感器(SensorName),它在给定的时刻(DateTimeID)在tblWavelengths表中有两行(两个不同的波长)。因此,例如,我希望得到没有77902/001传感器的结果 - 因为它在给定时刻只有一行(一个波长)与tblSensors相关

1 个解决方案

#1


10  

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

您可以使用窗口函数来查找每个sensorname / datetimeid组合的波长数:

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

后来我意识到你可能有两个结果,同时一个传感器具有相同的波长,这将返回2个记录,但没有两个不同的波长。由于窗口函数不支持使用DISTINCT,因此下面是另一种选择

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;
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/02/01/16f35191f99a3b5854e72fa359a4cda3.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告