我有以下数据,并希望找到一种方法,使用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
答案 0 :(得分: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中检查了上述代码并且工作正常(已创建表,已填充数据)。随附屏幕截图:初始查询数据(您的)和结果查询数据(我的)
答案 1 :(得分:0)
建议Sandr已发布有效我需要做的唯一更改是更改以下转换x.Contact为max(x.Contact)为联系人
更改强>
选择x.RespondentID,max(x.Name)作为名称,max(x.EID)作为EID, max(x.Phone)为Phone,max(x.Why)为Why,max(x.How)为How,x .Contact
以强>
选择x.RespondentID,max(x.Name)作为名称,max(x.EID)作为EID, max(x.Phone)为Phone,max(x.Why)为Why,max(x.How)为How,max(x.Contact)As 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
For Additional Reference我在MSSQL Server格式中包含了相同类型的Query:
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;