0

データベース内のすべての回答者の郵便番号と州の両方を見つける2つのクエリがあります。はい、どうぞ

郵便番号の場合:

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

これは機能しますが、すべてを1つのテーブル、つまり郵便番号と州にまとめたいと思います。

ありがとう!

質問スキーマ:

Column_nameタイプ計算された長さPrecScaleNullable

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 に答える