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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:58:38+00:00 2026-06-10T11:58:38+00:00

First off: total SQL newbie here. I unfortunately had this thrust on me on

  • 0

First off: total SQL newbie here. I unfortunately had this thrust on me on a deadline. :/ My apologies if this is a dumb question.

So I have a DB with a view that has various columns. It has two columns I’m interested in: UserID and Age.

  • Age stores the age of a report in days from the day it was created.
  • UserID is the user that created the report.

The view lists all reports for a user.

We I need to do is create a new table/view from the existing view that lists each unique user, and then breaks down their total documents into various age groups. So the columns would look something like:

User || # rprts <14 days || # rprts 14-17 days || # rprts 18-28 days || # rprts 29-44 days || # rprts 45+ days

I know I can get the set of all the users I want with:

SELECT DISTINCT [UserID] FROM [DB].[dbo].[DB_View]

And I can run the following and get one row of what I want for the new table/view by hardcoding a UserID #:

SELECT DISTINCT 

    [UserID],
    (SELECT COUNT(*) FROM [DB].[dbo].[DB_View] WHERE (Age < 14) AND UserID = '9999') AS "Less Than 14 Days",
    (SELECT COUNT(*) FROM [DB].[dbo].[DB_View] WHERE (Age BETWEEN 14 AND 17) AND UserID = '9999') AS "14 to 17 Days",
    (SELECT COUNT(*) FROM [DB].[dbo].[DB_View] WHERE (Age BETWEEN 18 AND 28) AND UserID = '9999') AS "18 to 28 Days",
    (SELECT COUNT(*) FROM [DB].[dbo].[DB_View] WHERE (Age BETWEEN 29 AND 44) AND UserID = '9999') AS "29 to 44 Days",
    (SELECT COUNT(*) FROM [DB].[dbo].[DB_View] WHERE (Age >= 45) AND UserID = '9999') AS "Over 45 Days"

FROM [DB].[dbo].[DB_View]
WHERE UserID = '9999'

The result set of users is about ~600 rows, and the view usually has about 6,000-15,000 rows. My initial thought was just get my result set of users, then store each user to a parameter, and then pass that into the query where I’ve hardcoded a User ID. Sadly, I can’t even figure out the SQL to do that. (And I’m guessing there’s a much more efficient way to do this if you know your SQL….) I’ve seen stuff on using cursors and stuff, but I don’t really understand it that well.

Any ideas?

  • 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-10T11:58:40+00:00Added an answer on June 10, 2026 at 11:58 am

    If I’m understanding you right:

    SELECT [UserID],
        COUNT(CASE WHEN Age < 14 THEN 1 ELSE NULL END) AS [Less Than 14 Days],
        COUNT(CASE WHEN Age BETWEEN 14 AND 17 THEN 1 ELSE NULL END) AS [14 to 17 Days],
        COUNT(CASE WHEN Age BETWEEN 18 AND 28 THEN 1 ELSE NULL END) AS [18 to 28 Days],
        COUNT(CASE WHEN Age BETWEEN 29 AND 44 THEN 1 ELSE NULL END) AS [29 to 44 Days],
        COUNT(CASE WHEN Age >= 45 THEN 1 ELSE NULL END) AS [Over 45 Days]
    
    FROM [DB].[dbo].[DB_View]
    GROUP BY [UserID]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off I'm a total Rails newbie (front-end guy looking to learn rails), so
First off, fairly new to JS but getting better :-) This question is similar
first off I'm a noob to PHP but here is my problem. I am
First off, I'm coming (back) to Java from C#, so apologies if my terminology
First off I use this code to make the navigation bar always stay fixed;
First off, the function in question. bool Animation::loadFrames() { sf::Texture frame[frameCount]; std::string pathToFrame; for(int
First off, I've never used GWT before. I have good experience in HTML/CSS/JS/JSP. I'm
First off, I'm using XCode 4.0.2. Okay, here is my issue. I can build
It's been ages since I've worked in SQL, so apologies if this is a
First off, I've looked at several other questions about optimizing sql queries, but I'm

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.