[英]MS Access or MSSQL query to combine multiple rows of data based on common value in a single column (data exported from survey monkey)
我有以下數據,並且想找到一種使用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查詢或MS SQL Server查詢是否可能?
還有更多這樣的記錄。 我無法控制其布局,因為它是每天從外部來源導出的內容。
到目前為止,我在MS Access查詢中擁有的是:
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
您可以嘗試以下方法:
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中檢查了上面的代碼,它工作正常(創建了表,填充了數據)。 附帶的屏幕截圖:帶有數據的初始查詢(您的)和帶有數據的結果查詢(我的)
建議Sandr發表是有效的我唯一需要做的更改是將x.Contact轉換為max(x.Contact)作為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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.