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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:38:05+00:00 2026-06-08T18:38:05+00:00

I have a complex stored procedure that has to return Sum of AmountSold as

  • 0

I have a complex stored procedure that has to return Sum of AmountSold as AmountSold and sum of AmountCollected as AmountCollected and also i have rows that has neither Sold nor collected and instead has Upgraded and UpgradedCollected colmns. Depending on the below conditions, that amount has to added into Sold or collected or UpgradedCollected. I dont really understand the syntax error which says “Incorrect Syntax error near keyword as” and for “keyword then”. What am i doing wrong here? Thank you in advance!

In the first 2 if’s, i have written different code assuming either of them should be correct. This is in SQL 05.

Select sum(cast(RDC.AmountSold as numeric(10,2))) as AmountSold, 
sum(cast(RDC.AmountCollected as numeric(10,2))) as AmountCollected,


case when RDC.AmountUpgraded = RDC.AmountUpgradedCollected
        then sum(cast((AmountSold + RDC.AmountUpgraded)as numeric(10,2))) as AmountSold
        and sum(cast((AmountCollected + RDC.AmountUpgradedCollected)as numeric(10,2))) as AmountCollected
        else if RDC.AmountUpgraded > RDC.AmountUpgradedCollected
        then AmountSold = AmountSold + RDC.AmountUpgraded 
        and AmountCollected = AmountCollected + RDC.AmountUpgradedCollected
        else
        then AmountSold = AmountSold + RDC.AmountUpgraded 
        and AmountCollected = AmountCollected + RDC.AmountUpgraded 
        and AmountUpgradedCollected = AmountUpgradedCollected + (RDC.AmountUpgradedCollected - RDC.AmountUpgraded)
        as AmountUpgradedCollected

end
  • 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-08T18:38:08+00:00Added an answer on June 8, 2026 at 6:38 pm

    Unfortunately SQL case statements don’t work the way you’re trying to use them. When used in a SELECT statement like how you are using them, each case expression can only define one column at a time. So, something like the following should work for you.

    Select sum(cast(RDC.AmountSold as numeric(10,2))) as AmountSold, 
    sum(cast(RDC.AmountCollected as numeric(10,2))) as AmountCollected,
    
    SUM(CASE WHEN RDC.AmountUpgraded = RDC.AmountUpgradedCollected
      THEN CAST(AmountSold + RDC.AmountUpgraded as numeric(10,2))
      ELSE CAST(AmountSold + RDC.AmountUpgraded as numeric(10,2))
    END) AS AmountSold,
    
    SUM(CASE WHEN RDC.AmountUpgraded = RDC.AmountUpgradedCollected
      THEN cast(AmountCollected + RDC.AmountUpgradedCollected as numeric(10,2))
      ELSE cast(AmountCollected + RDC.AmountUpgraded  as numeric(10,2))
    END) AS AmountCollected
    

    You’ll notice when you write it this way there is a bit of duplicated logic in the AmountSold case statement that you can probably simplify.

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

Sidebar

Related Questions

I have a complex sql query that I have in a stored procedure and
I have a rather complex SELECT statement in a stored procedure that I am
I have a stored procedure that goes something like this (pseudo code) storedprocedure param1,
I have a stored procedure that is performing some ddl dml operations. It retrieves
We have a complex Stored Procedure which first does INSERTS and then does UPDATES
I'm decoding a very complex stored procedure and part of that is matching up
I have imported a stored procedure that returns a dataset into my Entity Framework.
I have a stored procedure (I cannot edit) that I am calling via linq.
Environment: MySQL 5.1, Linux I have a stored procedure that computes several values from
Say I have a stored procedure that returns data from a SELECT query. I

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.