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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:47:03+00:00 2026-06-12T00:47:03+00:00

i have the following table generated by SQL TABLE A timeinterval count(exclusive range) 0-6

  • 0

i have the following table generated by SQL TABLE A

timeinterval   count(exclusive range)
0-6             2
0-12            5
0-18            10

i want a table like this TABLE B

timeinterval   count(exclusive range)  count(inclusive range)
1-6             2                       2
1-12            5                       3
1-18            10                      5

i have already generated table A and need table B. can i do something in SQL where i can add a query in the code for table A and do something like this (0-12)-(0-6) for 2nd row in table B.

code used for generating table A is

with ranges as 
  (
    select 6 as val, 1 as count_all
 union all
    select 12, 1
    union all
    select 18, 1
    union all
 select 24, 1
    union all
 select 30, 1
    union all  
 select 36, 1
    union all 
 select 42, 1
    union all
 select 48, 1
    union all   
 select 1, 0
  )
select case when ranges.count_all = 0
            then 'more'
            else  convert (varchar(10), ranges.val) 
        end [MetLifeExperienceMonths],
       sum (case when (ranges.count_all = 0 and GoldListHistogram.MetLifeExperienceMonths>=1)
                   or
           (GoldListHistogram.MetLifeExperienceMonths<= ranges.val and  GoldListHistogram.MetLifeExperienceMonths>=1)
                 then 1 end) [count],
count(EmployeeID) as 'Total'
into yy
from GoldListHistogram
cross join ranges
where MetLifeExperienceMonths > 0
group by ranges.val, ranges.count_all

i need to modify the query such that i can subtract first two rows value for “count(exclusive range)” for every row staring from the 2nd row..like for 0-12(time interval) row i need to output a value that is difference of the first two rows..like row(i)=count(i)-count(i-1).

first column gives the time interval in 5 years (in months) second column calculates no. of employees in the exclusive range like (0-6,0-12,0-18)..6 ,12,18 being no. of months third column calculates no. of employees in the exclusive range like (0-6,6-12,12-18)

  • 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-12T00:47:04+00:00Added an answer on June 12, 2026 at 12:47 am

    Could you not just add a start value to ranges? Something like:

    
    with ranges as 
      (
        select 6 as val, 0 as start, 1 as count_all
     union all
        select 12, 7, 1
        union all
        select 18, 13, 1
        union all
     select 24, 19, 1
        union all
     select 30, 25, 1
        union all  
     select 36, 31, 1
        union all 
     select 42, 37, 1
        union all
     select 48, 43, 1
        union all   
     select 1, 49, 0
      )
    select case when ranges.count_all = 0
                then 'more'
                else  convert (varchar(10), ranges.val) 
            end [MetLifeExperienceMonths],
           sum (case when (ranges.count_all = 0 and GoldListHistogram.MetLifeExperienceMonths>=1)
                       or
               (GoldListHistogram.MetLifeExperienceMonths=1)
                     then 1 else 0 end) [count inclusive],
           sum (case when (ranges.count_all = 0 and GoldListHistogram.MetLifeExperienceMonths>=1)
                       or
               (GoldListHistogram.MetLifeExperienceMonths=ranges.start)
                     then 1 else 0 end) [count exclusive],
    count(EmployeeID) as 'Total'
    into yy
    from GoldListHistogram
    cross join ranges
    where MetLifeExperienceMonths > 0
    group by ranges.val, ranges.count_all;
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Netbean 6.9 generated the following JPA entity class from this SQL Server 2008 table:
I have SQL statements generated like the following. Note that the parameters @p1 and
I have the following HTML generated by an ASP.NET repeater: <table> <tr> <td><input type=hidden
I have the following html structure (which is generated and I can't change): <table
Framework: Rails 2.3.8 Database; PostgreSQL I currently have the following SQL statements (generated by
I have a multi-table SQL query. My need is: The query should I generate
I have the following need I have a logging table which logs som leads
I have a few tables in SQL that are pretty much like this A
I have the following code to generate a table listing brand/model/submodel/style for a group

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.