0

我有以下数据,并希望找到一种使用 MS Access SQL 或 MS SQL Server 创建查询以将所有内容合并到一行的方法。

RespondentID  Name             EID       Phone      Why     How          Contact
1809593812                                                  Testing      0
1809593812                                          Testing              0
1809593812                     19091193                                  0
1809593812    Jennifer                                                   0
1809593812                               8885555555                      0

我希望它看起来像:

RespondentID   Name             EID       Phone       Why     How      Contact
1809593812     Jennifer         19091193  8885555555  Testing Testing  0

这可以使用 MS Access SQL Query 或 MS SQL Server Query 吗?

这样的记录还有很多。我无法控制它的布局,因为它是每天从外部来源导出的。

到目前为止,我在 MS Access Query 中拥有的是:

SELECT DISTINCT dbo_ResponsesText.RespondentID,
IIf(dbo_ResponsesText.Key1=4383976121,ResponseText,Null) AS Name, 
IIf(dbo_ResponsesText.Key1=4383976120,ResponseText,Null) AS EID,
IIf(dbo_ResponsesText.Key1=4388819402,ResponseText,Null) AS Phone, 
IIf(dbo_ResponsesText.QuestionID=340372755,ResponseText,Null) AS Why, 
IIf(dbo_ResponsesText.QuestionID=340372805,ResponseText,Null) AS How, 
IIf(dbo_Responses.Key1=4305593988,-1,0) AS Contact
FROM dbo_ResponsesText
INNER JOIN dbo_Responses ON dbo_ResponsesText.RespondentID = dbo_Responses.RespondentID
ORDER BY dbo_ResponsesText.RespondentID

实际表结构:

dbo_ResponsesText Table
ID   RespondentID  CollectorID  QuestionID  Key1  ResponseText   DateAdded
1    1821607396    25982810     340372755   0     Name,EID,etc.  5/1/2012 3:29:00 PM

dbo_Responses Table:
RespondentID   CollectorID  QuestionID  Key1         Key2  Key3
1809593812     25982810     340372567   4308039090   0     0
4

2 回答 2

0

您可以尝试以下方法:

You can try the following:

select x.RespondentID, max(x.Name) as Name, max(x.EID) as EID,
       max(x.Phone) as Phone, max(x.Why) as Why, max(x.How) as How, x.Contact
from
(
  SELECT r.RespondentID,
    IIf(t.Key1=4383976121, ResponseText, Null) AS Name, 
    IIf(t.Key1=4383976120 ,ResponseText, Null) AS EID,
    IIf(t.Key1=4388819402, ResponseText, Null) AS Phone, 
    IIf(t.QuestionID=340372755, ResponseText, Null) AS Why, 
    IIf(t.QuestionID=340372805, ResponseText, Null) AS How, 
    IIf(r.Key1=4305593988,-1,0) AS Contact
    FROM dbo_ResponsesText t
    INNER JOIN dbo_Responses r ON t.RespondentID = r.RespondentID
) x
GROUP BY x.RespondentID, x.Contact
ORDER BY x.RespondentID

我在 MS Access 2010 中检查了上面的代码,它工作正常(创建了表,填充了数据)。附截图:使用数据的初始查询(你的)和使用数据的结果查询(我的) 原始查询 - 初始状态 修改后的查询

于 2013-10-30T16:38:53.717 回答
0

Sandr Posted 的建议是有效的,我需要做的唯一更改是更改以下将 x.Contact 转换为 max(x.Contact) 作为联系人

改变

选择 x.RespondentID,max(x.Name) 作为 Name,max(x.EID) 作为 EID,max(x.Phone) 作为 Phone,max(x.Why) 作为Why,max(x.How) 作为 How, x.联系方式

选择 x.RespondentID,max(x.Name) 作为 Name,max(x.EID) 作为 EID,max(x.Phone) 作为 Phone,max(x.Why) 作为Why,max(x.How) 作为 How, max(x.Contact) 作为联系人

这样最终的查询看起来像:

SELECT 
    x.RespondentID
    , Max(x.Name) AS Name
    , Max(x.EID) AS EID
    , Max(x.Phone) AS Phone
    , Max(x.Why) AS Why
    , Max(x.How) AS How
    , Max(x.Contact) As Contact
FROM (
     SELECT 
        r.RespondentID
            , IIf(t.Key1=4383976121,ResponseText, Null) AS Name
            , IIf(t.Key1=4383976120 ,ResponseText, Null) AS EID
            , IIf(t.Key1=4388819402,ResponseText, Null) AS Phone
            , IIf(t.QuestionID=340372755,ResponseText, Null) AS Why
            , IIf(t.QuestionID=340372805,ResponseText, Null) AS How
            , IIf(r.Key1=4305593988,-1,NULL) AS Contact 
     FROM dbo_ResponsesText AS t 
     INNER JOIN dbo_Responses AS r ON t.RespondentID = r.RespondentID
)  AS x
GROUP BY x.RespondentID
ORDER BY x.RespondentID;

这给了我以下所需的输出:

RespondentID  Name     EID       Phone         Why            How            Contact
1811504405    Jenn     123456    456456        Because        Nothing        -1
1820992008    ANDRIA   19289935  909-437-XXXX  Long Response  Long Response   0

对于其他参考,我在 MSSQL Server 格式中包含了相同类型的查询:

SELECT 
x.RespondentID
    , Max(x.Name) AS Name
    , Max(x.EID) AS EID
    , Max(x.Phone) AS Phone
    , Max(x.Why) AS Why
    , Max(x.How) AS How
    , Max(x.Contact) As Contact
FROM (
    SELECT
     RespondentID = r.RespondentID
     ,Name = 
              CASE WHEN t.[Key1] = '4383976121' THEN [ResponseText] 
              ELSE NULL END

     ,EID = 
              CASE WHEN t.[Key1] = '4383976120' THEN [ResponseText] 
              ELSE NULL END

     ,Phone = 
              CASE WHEN t.[Key1] = '4388819402' THEN [ResponseText] 
              ELSE NULL END

     ,Why = 
              CASE WHEN t.[QuestionID] = '340372755' THEN [ResponseText] 
              ELSE NULL END

     ,How = 
              CASE WHEN t.[QuestionID] = '340372805' THEN [ResponseText] 
              ELSE NULL END

     ,Contact = 
              CASE WHEN r.[Key1] = '4305593988' THEN 'True'
              ELSE 'False' END

     FROM [NPS].[dbo].[ResponsesText] t

     Join [NPS].[dbo].[Responses] r ON t.RespondentID=r.RespondentID
)  AS x
GROUP BY x.RespondentID
ORDER BY x.RespondentID;
于 2013-10-30T23:46:21.670 回答