0

我有以下查询,其中我根据“必填”字段将一列分成不同的列。

SELECT 
Station, 
Line, 
[Tag Reference], 
FTN, 
IIF(Required = "TF", Required, "") AS [TF Required], 
IIF(Required = "TF", [Date Checked], "") AS [TF Date], 
IIF(Required = "TF", [User Checked], "") AS [TF User], 
IIF(Required = "ML", Required, "") AS [ML Required], 
IIF(Required = "ML", [Date Checked], "") AS [ML Date], 
IIF(Required = "ML", [User Checked], "") AS [ML User], 
Format

FROM [Tag Information]

ORDER BY 
Station, 
Line, 
[Tag Reference]

我得到的项目是每条记录都是自己的行,但想将记录组合​​在一起。

IE。我得到这张表,其中行被拆分,但我想查看按标签引用分组的值。(如第二张表)

Station Line    Tag Reference   FTN TF Required TF Date TF User ML Required ML Date ML User Format
Clearbrook  1   SCADA[30].0                 ML  7/12/2006   WPB 
Clearbrook  1   SCADA[30].0     TF                      
Clearbrook  1   SCADA[30].1                 ML  7/12/2006   WPB 
Clearbrook  1   SCADA[30].1     TF                      
Clearbrook  1   SCADA[30].10                    ML  7/12/2006   WPB 
Clearbrook  1   SCADA[30].2                 ML  7/12/2006   WPB 
Clearbrook  1   SCADA[30].4                 ML  7/12/2006   WPB 
Clearbrook  1   SCADA[30].4     TF  7/12/2006   WPB             
Clearbrook  1   SCADA[30].5     TF  7/12/2006   WPB             
Clearbrook  1   SCADA[30].5                 ML  7/12/2006   WPB 
Clearbrook  1   SCADA[30].6                 ML  7/12/2006   WPB 
Clearbrook  1   SCADA[30].8                 ML  7/12/2006   WPB 
Clearbrook  1   SCADA[30].8     TF  7/12/2006   WPB             
Clearbrook  1   SCADA[30].9     TF  7/12/2006   WPB             
Clearbrook  1   SCADA[30].9                 ML  7/12/2006   WPB 

我想看看这张表:

    Station Line    Tag Reference   FTN TF Required TF Date TF User ML Required ML Date ML User Format
    Clearbrook  1   SCADA[30].0     TF          ML  7/12/2006   WPB 
    Clearbrook  1   SCADA[30].1     TF          ML  7/12/2006   WPB 
    Clearbrook  1   SCADA[30].10                    ML  7/12/2006   WPB 
    Clearbrook  1   SCADA[30].2                 ML  7/12/2006   WPB 
    Clearbrook  1   SCADA[30].4     TF  7/12/2006   WPB ML  7/12/2006   WPB 
    Clearbrook  1   SCADA[30].5     TF  7/12/2006   WPB ML  7/12/2006   WPB 
    Clearbrook  1   SCADA[30].6                 ML  7/12/2006   WPB 
    Clearbrook  1   SCADA[30].8     TF  7/12/2006   WPB ML  7/12/2006   WPB 
    Clearbrook  1   SCADA[30].9     TF  7/12/2006   WPB ML  7/12/2006   WPB 

任何帮助都会很棒。我很茫然。

4

1 回答 1

1

您应该能够使用 GROUP BY 查询来合并结果,如下所示:

假设您有一个名为 [ThingStatus] 的表,其中包含数据...

ThingName  Status1  Status2
---------  -------  -------
foo        thing1          
foo                 thing2 

您可以使用查询合并条目...

SELECT 
    ThingStatus.ThingName, 
    Max(ThingStatus.Status1) AS Status1, 
    Max(ThingStatus.Status2) AS Status2
FROM ThingStatus
GROUP BY ThingStatus.ThingName;

...这给了你

ThingName  Status1  Status2
---------  -------  -------
foo        thing1   thing2 
于 2013-10-03T20:31:12.550 回答