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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:28:04+00:00 2026-06-07T20:28:04+00:00

I need to create a new column dinamically in SQL with a counter depending

  • 0

I need to create a new column dinamically in SQL with a counter depending on certain conditions

i.e.

ID ||| COLUMN1 ||| COLUMN2 ||| COLUMN3 ||| COLUMN 4 |||NEW_COLUMN
1        null        null        null         12    
2         13         null        null         18           1
3        null        null        null         82            
4        null        null        null         81           
5        null        null        31           89           2
6        null        22          32           91           3
7        null        92          null         95           4

The counter is going to increase only when COLUMN1 or COLUMN2 or COLUMN 3 != null

The sentence would be something like:

SELECT COLUMN1,COLUMN2,COLUMN3,COLUMN4, (SELECT .....) FROM ... WHERE ...

I don’t know if that’s possible or if that’s the correct way to face this problem in terms of efficiency. Other possibilities are doing that with java or with Jasper Reports, but i think that SQL is the easiest way to do that

Any advise is welcome.

Thank you

  • 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-07T20:28:05+00:00Added an answer on June 7, 2026 at 8:28 pm
    select t.*, (@row := case 
                         when column1 is not null or column2 is not null or column3 is not null 
                         then @row
                         else null
                end) as NEW_COLUMN,
                (@row := case 
                         when column1 is not null or column2 is not null or column3 is not null 
                         then @row + 1 
                         else @row 
                end) as TEMP_COLUMN
    from your_table t, (select @row := 0) r
    

    I used a temp_column as helper.

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

Sidebar

Related Questions

I need to create an entirely new Sql Server 2008 database and want to
I need to create a new column of data, assigning every 15 minutes in
I have a Excel sheet with two columns and I need to create new
I am not looking to create new events. I need to create a canvas
I'm very new at this, but I need to create new tables from existing
I need to create a new line of data within an array where the
I am new to Zkoss and i need to create completely new css theme
I have the following three arrays and need to create a new two-dimensional array
I need an advice about how to create new string array from 4 different
I need to create a server which creates a new thread for each client

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.