Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8182193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:46:45+00:00 2026-06-07T00:46:45+00:00

I have two queries that find both the Zip codes, and the States for

  • 0

I have two queries that find both the Zip codes, and the States for all the respondents in our database. Here they are

For ZIP code:

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

For state:

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

This works, but I’d like to have them all in one table, i.e. Zip Code and State.

Thanks!

questions schema:

Column_name Type Computed Length 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:46:47+00:00Added an answer on June 7, 2026 at 12:46 am

    Well, I did not take time to remove unnecessary selected fields but here is an ugly query that should get pretty close. Essentially your ‘State’ query was recasting most of the joins as a sub query anyway:

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

    All I did was add an extra sub-query to join on at the top level. I think there may be a more efficient query but since it is a SELECT TOP 100, I’m not sure performance will be an issue.

    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 ...
    

    Not checked for errors but here is the entire monster:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As seen below the two queries, we find that they both work well. Then
I have two queries that produce the same columns but different rows. The first
A quick technical question- I have two queries that output some of the same
I'm seeing OraclePreparedStatement executeQuery() exhibit serialization. That is, I have two queries that I
I have two relatively complex queries that I am trying to join together into
I have the following two queries, I believe that the one that uses the
I have two complex rails (AR) queries coming from two different methods that I
I have two SQL queries that I'm running from a C# winform, and I
I have two queries that I thought meant the same thing, but I keep
I have two queries with subqueries for dateparts which I like to join. SELECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.