如果2个展平列中的一个未返回,则排除行

时间:2009-11-23 16:03:32

标签: sql sql-server tsql

我正在加入一个视图,在存储过程中尝试返回记录。

在一张表中,我有类似的内容:

Measurement | MeasurementType | Date Performed

我需要绘制TireHeightTireWidth

所以我把那张桌子弄成一排。

我只想在该日期测量TireHeightTireWidth时返回一行。除了加入TireWidthTireHeight之外,我没有使用其他任何日期。我对我的图表点的这两个数字进行了计算,并使用TireAge作为另一个轴。

如果TireHeightTireWidth不可用,如何排除结果行?

谢谢!

1 个答案:

答案 0 :(得分:3)

您使用INNER JOIN仅返回两个表中存在的行。例如:

SELECT   th.DatePerformed
,        th.Measurement as TireHeight
,        tw.Measurement as TireWidth
FROM (
    SELECT DatePerformed, Measurement
    FROM   Measurements
    WHERE  MeasurementType = 'TireHeight'
) th
INNER JOIN (
    SELECT DatePerformed, Measurement
    FROM   Measurements
    WHERE  MeasurementType = 'TireWidth'
) tw
ON tw.DatePerformed = th.DatePerformed