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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:34:28+00:00 2026-06-16T15:34:28+00:00

I got this table with some data: SELECT Id, SubId, N1, N2, N3 FROM

  • 0

I got this table with some data:

SELECT Id, SubId, N1, N2, N3 FROM table1 WHERE Id = 2

Id          SubId    N1          N2          N3
----------- -------- ----------- ----------- ----------------------
2           1        94917       97030       0
2           2        35087       41306       0
2           3        189671      194224      0
2           4        44905       46218       0
2           5        65760       70959       0
2           6        620         2395        0
2           7        60336       69531       0
2           8        65517       70666       0

I run this query

UPDATE table1
SET N3 = CASE WHEN N1 = 0 THEN 0 ELSE (( N2 * 1.0 / N1 ) -1) * 100 END

Id          SubId    N1          N2          N3
----------- -------- ----------- ----------- ----------------------
2           1        94917       97030       2.2261554832
2           2        35087       41306       17.7245133525
2           3        189671      194224      2.4004723969
2           4        44905       46218       2.9239505622
2           5        65760       70959       7.9060218978
2           6        620         2395        286.2903225806
2           7        60336       69531       15.2396579156
2           8        65517       70666       7.8590289543

So, when I group the table by Id i got this data:

SELECT  Id, 
        SUM(N1) AS N1, 
        SUM(N2) AS N2, 
        CAST(0 AS NUMERIC(10,2)) AS N3
INTO table2
FROM table1
GROUP BY Id

UPDATE table2
SET N3 = CASE WHEN N1 = 0 THEN 0 ELSE (( N2 * 1.0 / N1 ) -1) * 100 END

SELECT Id, N1, N2, N3 FROM table2 WHERE Id = 2

Id          N1          N2          N3
----------- ----------- ----------- ----------------------
2           556813      592329      6.38

The question is how I can get the same value of N3 when I group by ID (6.38) without recalculating the field (second update), I was trying to apply a reverse calculation ((N3/100) + 1) and SUM the N3 field but did not work.

  • 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-16T15:34:29+00:00Added an answer on June 16, 2026 at 3:34 pm

    Just replace N1 and N2 with SUM(N1) and SUM(N2) respectively..

    SELECT  Id, 
            SUM(N1) AS N1, 
            SUM(N2) AS N2, 
            N3 = CAST(CASE WHEN SUM(N1) = 0 THEN 0 ELSE (( SUM(N2) * 1.0 / SUM(N1) ) -1) * 100 END AS NUMERIC(10,2))
    FROM table1
    GROUP BY Id
    

    Demo at http://sqlfiddle.com/#!3/de849/4/0

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

Sidebar

Related Questions

Well ive got this code that creates a table from JSON data. If a
I've got this code I'm trying to use to export data from Excel to
I've got some data I'd like to pull off our SQL server. This old
empty (not null!)I want to create sql statement as this: SELECT * FROM table
I've got some data in a table that looks like so: Recipe | Category
I am using this select $sql1= SELECT pole FROM Table1 WHERE dva='d'; and it
I have got a table which is coming with some data. One of the
Got this: Table a ID RelatedBs 1 NULL 2 NULL Table b AID ID
I've got this table with an int(11) column and hundreds of millions of rows.
So I've got this audit table, looks like this: USE [DatabaseName] GO /****** Object:

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.