2013-10-03 96 views
0

我有以下查詢,我根據字段「必需」將一列分成不同的列。按字段拆分列並組合記錄MS Access 2007

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 

任何幫助將是巨大的。我很茫然。

回答

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 
+0

好的,這項工作一切順利,直到我們遇到這個問題... 由於這些條目更新,日期檢查有變化,但我寧願保留一個歷史記錄,並創建一個新的記錄,而不是改變一個。 Max通常會在日期之後正常工作,但用戶並不總是「最大」。所以拿上面的表並添加以下內容。 「Clearbrook 1 SCADA [30] .9 ML 7/12/2010 EGB' 我想保留那條記錄,但是現在它會提取日期並將WPB放在它上面。 – brans041