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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:13:56+00:00 2026-06-04T00:13:56+00:00

SQL Server 2008 I have a query with several local variables that does some

  • 0

SQL Server 2008

I have a query with several local variables that does some easy math in the result set. When I copy and paste the query to try to save it as a view, it fails telling me there’s incorrect syntax. (in this case it’s near the declare statement of the variables.) If needed I’ll post the query, just wondering if there’s a reason for this to work one way and not the other.

declare @totalpop float,
@totalMales float,
@totalFemales float,
@percentMales float,
@percentFemales float;
select @totalmales=sum(case when sex='m' then 1 else 0 end),
@totalfemales = sum(case when sex='f' then 1 else 0 end),
@totalpop=count(*)

from tblVisits
select @percentmales = round(100 * @totalmales/@totalpop,2),
@percentFemales = round(100*@totalfemales/@totalpop,2)

select @totalmales,@percentmales,@totalfemales, @percentfemales, @totalpop
  • 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-04T00:13:57+00:00Added an answer on June 4, 2026 at 12:13 am

    You don’t need any of the declared variables, you can do this in plain-old sql with a nested select:

    SELECT totalmales, round(1e2*totalmales/totalpop, 2) percentmales,
           totalfemales, round(1e2*totalfemales/totalpop, 2) percentfemales,
           totalpop
    FROM (SELECT sum(case when sex='m' then 1 else 0 end) totalmales,
                 sum(case when sex='f' then 1 else 0 end) totalfemales,
                 count(*) totalpop
          FROM tblVisits) innerquery
    

    Which should be usable on most any database that supports views and subselects (which is basically all of them.

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

Sidebar

Related Questions

Using Microsoft SQL server 2008 I have a query that is in need of
I have a Microsoft SQL Server 2008 query that returns data from three tables
I am using SQL Server 2008. I have a query that pulls two random
I am using SQL Server 2008 and ColdFusion 9.1.2. I have a query that
Does the text query editor in MS SQL Server 2008 R2 have a SQL
I have the following SQL Server 2008 query that works for all of my
I have a query like this on Sql Server 2008: DECLARE @START_DATE DATETIME SET
I have a SQL Query running on SQL Server 2008 R2 that returns a
I have a query in sql server 2008. That I want to either pass
I have created a T-SQL query in SQL Server 2008 R2 that is a

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.