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

  • Home
  • SEARCH
  • 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 7818991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:45:31+00:00 2026-06-02T06:45:31+00:00

I need to run a huge query with the option to sum up one

  • 0

I need to run a huge query with the option to sum up one column. I’m wondering if it would be possible to do something like the following:

declare @sumIt bit
set @sumIt = 1

select ID, Name, CASE WHEN @sumIt=1 THEN sum(Time) ELSE Time END [timeCol]     
from Visits
where ID = 123
Group by ID, Name, CASE WHEN @sumIt=1 THEN '' ELSE Time END

Right now I get an error:
Msg 8120, Level 16, State 1, Line 4
Column ‘Visits.Time’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

  • 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-02T06:45:32+00:00Added an answer on June 2, 2026 at 6:45 am

    I think you can do the following:

    declare @sumIt bit
    set @sumIt = 1
    
    select ID, Name,
           (CASE WHEN @sumIt=1 THEN sum(Time) ELSE min(Time) END) [timeCol]
    from Visits
    where ID = 123
    Group by ID, Name, (CASE WHEN @sumIt=1 THEN '' ELSE Time END) 
    

    You can apply an aggregation function to the group by variables. This isn’t commonly done, but it solves your problem.

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

Sidebar

Related Questions

So, I have a huge query that I need to run on an Access
I need to run a query which will retrieve a huge amount of records.
I need to run a RLIKE query on a database mmoved from MySQL to
I have a query I need to run on almost 2000 strings where it
i need run code that will create a database and populate tables. i am
I need run ts:reindex when smth add in model or destroy from model. How
I need to run a JavaScript function onLoad(), but only do it if the
I need to run a stored procedure from a C# application. I use the
I need to run an equipment audit and to do that I need to
I need to run a Linux CLI command and get its stdout output from

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.