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 9010929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:35:35+00:00 2026-06-16T02:35:35+00:00

I am using MS-SQL 2008. I have a table with different columns based on

  • 0

I am using MS-SQL 2008. I have a table with different columns based on locations in it that will have a ‘Y’ or Null value. The table also has other data other than location from survey results. I have set up a temptable @TempLocation to hold the location based on the one or all. I need to select rows from the table based on ‘Y’ from one or more location rows within a date range.

TableID Northwest Northeast Southwest Southeast Batchno first_choice date_completed
1       Y                   Y         Y         1       A            2012-11-10
2                 Y                   Y         1       SA           2012-19-10       
3       Y         Y                             1       N            2012-07-10
4       Y         Y                   Y         2       A            2012-10-10
5                           Y                   2       A            2012-16-10
6       Y                             Y         2       D            2012-21-10
7                 Y                             NULL    A            2012-19-10
8       Y         Y         Y         Y         3       SA           2012-11-10
9       Y                                       3       A            2012-10-10
10                          Y         Y         3       A            2012-07-10  

I have created a Dynamic SQL statement to pull one location successfully but is it possible to pull all of them?

select  ''' + (SELECT * FROM @TempLocation) + ''',
count(batchno),
count(case when first_choice is not null then batchno end),
count(case when t.First_choice =''SD'' then 1 end) ,
count(case when t.First_choice=''D'' then 1 end) ,
count(case when t.First_choice=''N'' then 1 end) ,
count(case when t.First_choice=''A'' then 1 end) ,
count(case when t.First_choice=''SA'' then 1 end) 
from    customer_satisfaction_survey t
where   t.date_completed>= ''' + CAST(@beg_date AS VARCHAR) + '''
and     t.date_completed < ''' + CAST(dateadd(day,1,@end_date) AS Varchar) + '''
and     t.' + (SELECT * FROM @TempLocation) + ' = ''Y'''

An All result would look like this.

Number  Location   Total  Total2  SA  A  N  D  SD
1       Northwest  6      6       1   3  1  1  0
2       Northeast  5      4       2   2  1  0  0
3       Southwest  4      4       1   3  0  0  0
4       Southeast  6      6       2   3  0  1  0
  • 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-16T02:35:36+00:00Added an answer on June 16, 2026 at 2:35 am

    I have to think that you are approaching this in the wrong way, because your data is not normalized. The first thing you should do is to normalize the data using UNPIVOT. I’m assuming that you are using SQL Server, since your syntax suggests that. It is a good idea to tag all questions with the database, though.

    You can unpivot your data with a statement such as:

    select BatchNo, FirstChoice, DateCompleted, Location
    from d
    unpivot (val for location in (Northwest, Northeast, Southwest, Southeast)) as unpvt
    

    Next, set up your temporary table to have a separate row for each location. Then, you can do the join with no dynamic SQL. Something like:

    with dnorm as (
        THE NORMALIZATION QUERY HERE
    )
    select dnorm.location, count(*) as total,
           sum(case when dnorm.first_choice is not null then 1 else 0 end) as total2,
           sum(case when dnorm.first_choice = 'SA' then 1 else 0 end) as SA,
           . . .
    from dnorm join
         @TempLocation tl
         on dnorm.location = tl.location
    where ALL YOUR WHERE CONDITIONS HERE
    

    The final query looks something like:

    with dnorm as (
        select BatchNo, FirstChoice, DateCompleted, Location
        from d
        unpivot (val for location in (Northwest, Northeast, Southwest, Southeast)) as unpvt
    )
    select dnorm.location, count(*) as total,
           sum(case when dnorm.first_choice is not null then 1 else 0 end) as total2,
           sum(case when dnorm.first_choice = 'SA' then 1 else 0 end) as SA,
           . . .
    from dnorm join
         @TempLocation tl
         on dnorm.location = tl.location
    where ALL YOUR WHERE CONDITIONS HERE  
    

    The dynamic SQL approach is quite clever, but I don’t think it is the simplest way to approach this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008 and I have a table that has a 'Status'
I'm using SQL Server 2008 and I have a table with three columns: Length
We are using SQL Server 2008. We have a table called response which has
I'm using SQL Server 2008. I have a database table that looks like this
I have a SQL Server 2008-R2 database that has a table that is storing
Ok I'm using SQL Server 2008 and have a table field of type VARCHAR(MAX)
I am using T/SQL in SQL Server 2008 I have a table MyTable with
I'm using SQL Server 2008. I have a table with x amount of rows.
I am using SQL Server 2008 spatial data types. I have a table with
Using Microsoft SQL server 2008 I have a query that is in need of

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.