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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:37:41+00:00 2026-06-17T21:37:41+00:00

I have this table where I only want to look at AB ID CODE

  • 0

I have this table where I only want to look at AB

ID     CODE       COUNT   
102    AB         9
101    AB         8
100    AC         23    //not important!!!!
99     AB         7
98     AB         6
97     AB         5
96     AB         0

Conversed to this

ID    NEWID     CODE       COUNT   
102   102       AB         9
101   101       AB         8
99    100       AB         7
98    99        AB         6
97    98        AB         5
96    97        AB         0

Using

SELECT
t.ID, t.CODE, t.COUNT,
@PREVCOUNT - t.COUNT DIFFERENCE,
@PREVCOUNT := t.COUNT  -- Updates for the next iteration, so it
                       -- must come last! 
FROM
(SELECT ID, CODE, COUNT FROM some_table WHERE CODE = 'AB' ORDER BY ID DESC) t,
(SELECT @PREVCOUNT := NULL) _uv;

http://sqlfiddle.com/#!2/e0b8b/36/0

So

Step 1: 9 - 1 = 1
Step 2: 8 - 7 = 1
Step 3: 7 - 6 = 1
Step 4: 6 - 5 = 1
Step 5: 5 - 0 = 5

Now there could be a case where I have

ID    NEWID     CODE       COUNT   
102   102       AB         4
101   101       AB         2
99    100       AB         1
98    99        AB         0
97    98        AB         7
96    97        AB         0

Then I want to count

Step 1: 4 - 2 = 2
Step 2: 2 - 1 = 1
Step 3: 1 - 0 = 1
Step 4: 0 - 7 = -7      //want  to discard this negative value
Step 5: 5 - 0 = 7

Where I want to discard step 4 because this is negative.

Now I use this code to discard the negative value

SELECT
t.ID, t.CODE, t.COUNT,
@PREVCOUNT,
@PREVCOUNT - t.COUNT DIFFERENCE,
CASE @PREVCOUNT - t.COUNT WHEN @PREVCOUNT - t.COUNT >= 0 THEN 'equal or bigger' ELSE 'smaller' END,
@PREVCOUNT := t.COUNT  -- Updates for the next iteration, so it
                       -- must come last!
FROM
(SELECT ID, CODE, COUNT FROM some_table WHERE CODE = 'AB' ORDER BY ID DESC) t,
(SELECT @PREVCOUNT := NULL) _uv;

After executing this code I expect to see where the comparison made is bigger as or equal to 0, instead I get very different results.

Below a link to see what I mean:

http://sqlfiddle.com/#!2/6be4a/1

I would really like to know what is going wrong here, and I would love to have a solution.

Kind regards

  • 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-17T21:37:42+00:00Added an answer on June 17, 2026 at 9:37 pm

    How about this:

    http://sqlfiddle.com/#!2/6be4a/7

    select * from (
    SELECT
        t.ID, t.CODE, t.COUNT,
        @PREVCOUNT,
        @PREVCOUNT - t.COUNT DIFFERENCE,
        CASE @PREVCOUNT - t.COUNT WHEN @PREVCOUNT - t.COUNT >= 0 THEN 'equal or bigger' ELSE 'smaller' END,
        @PREVCOUNT := t.COUNT  -- Updates for the next iteration, so it
                               -- must come last!
    FROM
        (SELECT ID, CODE, COUNT FROM some_table WHERE CODE = 'AB' ORDER BY ID DESC) t,
        (SELECT @PREVCOUNT := NULL) _uv
    group by t.id, t.code
    )x
    where x.difference >= 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table where I only want to look at AB ID CODE
So I have this table where I only want to look at AB. ID
I have MySQL InnoDB table utf-8 encoded. This table has only id and name
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
I have this table Test as Id, SomeValue, SomeText contains about a mill records,
I have this table: UNIQUE_ID | WINNER_ID | FINALIST_ID ________1 | ________1 | __________2
I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE
I have this table [Table 1] cid | arrived | date_arrived The [arrived field
I have this table, which can be seen as a basic custom gantt chart:

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.