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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:35:51+00:00 2026-05-19T00:35:51+00:00

I m sry if the title is not clear. Here’s my proble. I created

  • 0

I m sry if the title is not clear.

Here’s my proble.

I created a new table which will show total, average and maximum values.

I have to insert the results into that table.

That table will have only 4 rows. No Appointment, Appointment Early, Appointment Late and Appointment Punctual.

So.. I have sth like..

insert into newTable
select
'No Appointment' as 'Col1',
avg statement,
total statement,
max statement 
from orgTable
where (general conditions) and (unique condition to check NO APPOINTMENT);

I have to do that same thing for another 3 rows.. where only the unique condition is different to check early, punctual or late..

So..the statement is super long.

I wanna reduce the size.. How can I achieve that?

  • 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-05-19T00:35:52+00:00Added an answer on May 19, 2026 at 12:35 am

    Complete proof-of-concept (now in Sql Server), and also we use CTE(the WITH clause) so we don’t need to repeat the specific conditions on GROUP BY clause; basically the logic of the program is to tag first the rows where specific condition they belong to, then from there we group them outside of WITH clause.

    -- drop table attendance;
    -- drop table yourtable;
    create table attendance
    (
    arrived datetime
    );
    
    
    
    declare @d datetime;
    set @d = GETDATE();
    
    
    
    
    declare @t integer;
    set @t = 0;
    
    while @t < 50 begin
    
       insert into attendance select dateadd(hour, @t, @d);
       set @t = @t + 1;
    end
    
    select * from attendance;
    
    
    create table yourtable
    (
    segment varchar(max),
    total int,
    the_max datetime
    );
    
    
    with tagger as
    (
    select
    
        -- specific conditions here
        case 
        when DATEPART(hour,arrived) between 0 and 7 then 
           'First Shift' 
        when DATEPART(hour,arrived) between 8 and 15 then
           'Second Shift' 
        when DATEPART(hour,arrived) between 16 and 23 then
           'Third Shift'         
        else
           'Hurry, inform the programmer of the anomaly!'
        end 
           as segment,
    
        *
    
        from attendance
    )
    insert into yourtable(segment, total, the_max)
    select segment, count(*), max(arrived)
    from tagger
    group by segment;
    
    
    
    select * from yourtable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sry about my english :) Im new to Java programming and i have a
I'm new to Magento so sry if it's a newbie question. I have a
sry if this is trivial which files from my asp.net solution should i upload
I began to like the following pattern (sry, Coffeescript here cause it's more readable
sry about the stupid way I phrased the question here's some explanation: I am
On a prominent news site (sry, can't remember exactly which one) I saw a
sry for this imprecise topic name. I am querying a dataset a lot of
I am stuck with my web application. As known from the title its a
at first, sry for my english... :) I'm using the YAJL Framework http://github.com/gabriel/yajl-objc My
Again I m sry fr that non-sense way of asking the question what i

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.