0

我编写了一个查询来从表中的一列获取值,并根据 where 子句在数据网格视图的两列中表示它们。以下是查询,

Select ElectricityMachinePanelDetails.MachineDescription AS "Machine Name",
    case when (ReadingCategoryID = 'RC001'  AND ReadingTypesDetails.ReadingTypeID = 'RT001')
    then ReadingValue
    end as [Last Day Meter Reading],
    case when (ReadingCategoryID = 'RC001'  AND ReadingTypesDetails.ReadingTypeID = 'RT002')
    then ReadingValue
    end as [Current Day Meter Reading]
    from ElectricityDailyMeterReadingDetails
    INNER JOIN ElectricityMachinePanelDetails on ElectricityMachinePanelDetails.MachinePanelID = ElectricityDailyMeterReadingDetails.MachinePanelID
    INNER JOIN ReadingTypesDetails ON ElectricityDailyMeterReadingDetails.ReadingTypeID = ReadingTypesDetails.ReadingTypeID 
    WHERE ReadingCategoryID = 'RC001' AND ReadingTypesDetails.ReadingTypeID = 'RT001'

基于读取类型 ID,列的值会发生变化..例如

如果我使用ReadingTypesDetails.ReadingTypeID = 'RT002'

Machine Name           Last Day Meter Reading    Current Day Meter Reading                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Transformer 01           NULL                               60.000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
Transformer 02           NULL                               60.000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
Transformer 03           NULL                               80.000000  

如果我使用ReadingTypesDetails.ReadingTypeID = 'RT001'

Machine Name           Last Day Meter Reading    Current Day Meter Reading                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Transformer 01           52.000000                  NULL                                                                                                                                                                                                                                                                                
Transformer 02           55.000000                  NULL                     
Transformer 03           70.000000                  NULL

*总是有一列显示为 NULL * .. 有人可以帮我吗?

4

2 回答 2

1

尝试这个 :

Select  ElectricityMachinePanelDetails.MachineDescription AS "Machine Name",
        case when (ReadingTypesDetails.ReadingTypeID = 'RT001') then ReadingValue
        end as [Last Day Meter Reading],
        case when (ReadingTypesDetails.ReadingTypeID = 'RT002') then ReadingValue
        end as [Current Day Meter Reading]
from ElectricityDailyMeterReadingDetails
INNER JOIN ElectricityMachinePanelDetails on ElectricityMachinePanelDetails.MachinePanelID = ElectricityDailyMeterReadingDetails.MachinePanelID
INNER JOIN ReadingTypesDetails ON ElectricityDailyMeterReadingDetails.ReadingTypeID = ReadingTypesDetails.ReadingTypeID 
WHERE ReadingCategoryID = 'RC001'
于 2013-10-29T04:37:54.277 回答
1
case when (ReadingCategoryID = 'RC001'  AND ReadingTypesDetails.ReadingTypeID = 'RT002')

This will never evaluate to "true", as long as you are selecting rows...

WHERE ... ReadingTypesDetails.ReadingTypeID = 'RT001'
于 2013-10-29T04:34:59.377 回答