0

我有两个查询可以找到我们数据库中所有受访者的邮政编码和州。他们来了

对于邮政编码:

select top 100  S.ID as SurveyID, S.SID, S.SurveyNumber, S.ABCSurveyName, SE.RespondentID, Q.name as QuestionName, rp.Condition as ZipCode
from Surveys S 
    join Sessions SE 
        on S.id = SE.SurveyID 
    join RespondentProfiles rp
        on RP.RespondentID = SE.RespondentID
    join Questions Q 
        on Q.ID = rp.QuestionID
where q.name = 'ZIP'
        and S.ID = 13900
        and Q.LK_RecordStatusID = 1

对于状态:

select VW.ID as SurveyID, VW.SID, SurveyNumber, ABCSurveyName, RespondentID, VW.Name as QuestionName, st.Code as State
from (
    select top 100 S.ID, S.SID, S.SurveyNumber, S.ABCSurveyName, SE.RespondentID, Q.name, rp.Condition 
    from Surveys S 
        join Sessions SE 
            on S.id = SE.SurveyID 
        join RespondentProfiles rp
            on RP.RespondentID = SE.RespondentID
        join Questions Q 
            on Q.ID = rp.QuestionID
    where S.ID = 13900
            and q.name = 'STATE'
            and Q.LK_RecordStatusID = 1

) VW
    join LK_States st
        on st.ID = vw.Condition

这可行,但我想将它们全部放在一张表中,即邮政编码和州。

谢谢!

问题架构:

Column_name 类型 计算长度 Prec Scale Nullable

TrimTrailingBlanks  FixedLenNullInSource    Collation
ID  int no  4   10      0       no  (n/a)   (n/a)   NULL
SID nvarchar    no  128                 yes (n/a)   (n/a)   SQL_Latin1_General_CP1_CI_AS
Name    nvarchar    no  64                  yes (n/a)   (n/a)   SQL_Latin1_General_CP1_CI_AS
QuestionIdentifier  nvarchar    no  128                 yes (n/a)   (n/a)   SQL_Latin1_General_CP1_CI_AS
ParentID    int no  4   10      0       yes (n/a)   (n/a)   NULL
LK_QuestionTypeID   int no  4   10      0       yes (n/a)   (n/a)   NULL
LK_QuestionCategoryID   int no  4   10      0       yes (n/a)   (n/a)   NULL
LK_IndustryID   int no  4   10      0       yes (n/a)   (n/a)   NULL
OptionMask  nvarchar    no  512                 yes (n/a)   (n/a)   SQL_Latin1_General_CP1_CI_AS
MetaTags    ntext   no  16                  yes (n/a)   (n/a)   SQL_Latin1_General_CP1_CI_AS
Order   int no  4   10      0       yes (n/a)   (n/a)   NULL
Rows    int no  4   10      0       yes (n/a)   (n/a)   NULL
Columns int no  4   10      0       yes (n/a)   (n/a)   NULL
IsDisplay   bit no  1                   yes (n/a)   (n/a)   NULL
AnswerLifespan  int no  4   10      0       yes (n/a)   (n/a)   NULL
CreateUserID    int no  4   10      0       yes (n/a)   (n/a)   NULL
CreateDate  datetime    no  8                   yes (n/a)   (n/a)   NULL
UpdateUserID    int no  4   10      0       yes (n/a)   (n/a)   NULL
UpdateDate  datetime    no  8                   yes (n/a)   (n/a)   NULL
LK_RecordStatusID   bit no  1                   yes (n/a)   (n/a)   NULL
LK_QuestionClassID  int no  4   10      0       yes (n/a)   (n/a)   NULL
LK_QuestionVisibilityID int no  4   10      0       yes (n/a)   (n/a)   NULL
DisplayLK_QuestionTypeID    int no  4   10      0       yes (n/a)   (n/a)   NULL
4

2 回答 2

2

在不完全了解您的结构的情况下,请尝试以下操作:

select top 100 
    S.ID, 
    S.SID, 
    S.SurveyNumber, 
    S.FEDSurveyName, 
    SE.RespondentID, 
    qState.name as QuestionName, 
    rp.Condition as ZipCode, 
    st.Code as State
from [Surveys] S
    inner join [Sessions] SE 
        on S.id = SE.SurveyID 
    inner join [RespondentProfiles] rp
        on RP.RespondentID = SE.RespondentID
    inner join [Questions] qState
        on qState.ID = rp.QuestionID
    inner join [Questions] qZip 
        on qZip.ID = rp.QuestionID
    inner join [LK_States] st
        on st.ID = rp.Condition
where 
    S.ID = 13900
    and qState.name = 'STATE'
    and qZip.name = 'ZIP'
    and qState.LK_RecordStatusID = 1
于 2012-07-06T14:46:02.047 回答
2

好吧,我没有花时间删除不必要的选定字段,但这是一个丑陋的查询,应该非常接近。本质上,您的“状态”查询无论如何都将大多数连接重新转换为子查询:

SELECT ... FROM
(SELECT ... FROM ... JOIN ... JOIN ... WHERE Q.Name = 'State') VW
JOIN LK_States ...

我所做的只是添加一个额外的子查询以在顶层加入。我认为可能会有更有效的查询,但由于它是SELECT TOP 100,我不确定性能是否会成为问题。

SELECT ... FROM
(SELECT ... FROM ... JOIN ... JOIN ... WHERE Q.Name = 'State') VW
JOIN
(SELECT ... FROM ... JOIN ... JOIN ... WHERE Q.Name = 'Zip') VW2
ON VW2.SurveyID = VW.SurveyID
JOIN LK_States ...

没有检查错误,但这是整个怪物:

select VW.SurveyID as SurveyID, VW.SID, VW.SurveyNumber, VW.FEDSurveyName, VW.RespondentID, VW.Name as QuestionName, st.Code as State, VW2.Condition as ZipCode
from (
    select top 100 S.ID as SurveyID, S.SID, S.SurveyNumber, S.FEDSurveyName, SE.RespondentID, Q.name, rp.Condition 
    from Surveys S 
        join Sessions SE 
            on S.id = SE.SurveyID 
        join RespondentProfiles rp
            on RP.RespondentID = SE.RespondentID
        join Questions Q 
            on Q.ID = rp.QuestionID
    where S.ID = 13900
            and q.name = 'STATE'
            and Q.LK_RecordStatusID = 1

    ) VW
    join (
    select top 100  S.ID as SurveyID, S.SID, S.SurveyNumber, S.FEDSurveyName, SE.RespondentID, Q.name, rp.Condition
    from Surveys S 
        join Sessions SE 
            on S.id = SE.SurveyID 
        join RespondentProfiles rp
            on RP.RespondentID = SE.RespondentID
        join Questions Q 
            on Q.ID = rp.QuestionID
    where q.name = 'ZIP'
            and S.ID = 13900
            and Q.LK_RecordStatusID = 1
    ) VW2
        on VW2.SurveyID = VW.SurveyID
    join LK_States st
        on st.ID = vw.Condition
于 2012-07-06T15:13:58.013 回答