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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:18:29+00:00 2026-06-14T07:18:29+00:00

I need help rewriting the following query as I need to group by the

  • 0

I need help rewriting the following query as I need to group by the aliases and derived tables appear to be the only path open to me. I cannot group by Type and SalesDatabase as I get an invalid column error.

SELECT    'Transactions'        AS Type,  
          'SalesDb'             AS SalesDatabase,  
          COUNT (B.Id)          AS Total,  
          CASE 
              WHEN SB.name LIKE '%Online%'      THEN 'Online'   
              WHEN SB.name LIKE '%RetailSale%'  THEN 'Retail'   
              ELSE 'Bricks'   
          END AS SalesType,    
          ISNULL( SUM( CASE WHEN datediff( d, b.Inserted, getDate() ) <  1 THEN 1 ELSE 0 END ), 0 ) AS SalesPeriod0,          
          ISNULL( SUM( CASE WHEN datediff( d, b.Inserted, getDate() ) >=  1 and datediff( d, b.Inserted, getDate() ) < 8 THEN 1 ELSE 0 END ), 0 ) AS SalesPeriod1to7 ,         
          ISNULL( SUM( CASE WHEN datediff( d, b.Inserted, getDate() ) >= 8 THEN 1 ELSE 0 END ), 0 ) AS SalesPeriod8p  
FROM SalesItem B WITH (NOLOCK)  
INNER JOIN SalesBatch SB WITH (NOLOCK) ON SB.Id  = B.BatchId    
GROUP BY SB.NAME    

Any help appreciated.

  • 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-14T07:18:29+00:00Added an answer on June 14, 2026 at 7:18 am

    The query as you have written is correct, if you intend to produce one row PER SB.NAME. It means that you could have

    SB.NAME    | SalesType   | ...
    ABC        | Online      | ...
    DEF        | Online      | ...
    XYZ        | Retail      | ...
    DEF1       | Retail      | ...
    

    However, if you want to group by SalesType, then use this GROUP BY

    GROUP BY CASE 
              WHEN SB.name LIKE '%Online%'      THEN 'Online'   
              WHEN SB.name LIKE '%RetailSale%'  THEN 'Retail'   
              ELSE 'Bricks'   
          END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need help with a query that I wrote: I have three tables Company id
Need some quick help with url rewriting. Currently using the following in an htaccess
I need help optimizing or rewriting this complex query. The query worked great with
I need help rewriting online.php to -> /users/online and profile.php?id=3426 to -> /users/3426 and
I need little help for url rewriting in wordpress. basically what i need: mydomain.com/mysomewordpresspost
need help/guide for sql select query, I have 2 table stock and stock_history, in
I need help about URL rewriting for image gallery for site. This is problem,
Can anyone help with .htaccess URL rewriting rule. I need to redirect pages like
I need help with url-rewriting in .htaccess. So the issue is about different protocols:
Need help, function getFamily() { FB.api('/me/family', function(response) { alert(JSON.stringify(response)); }); } With the above

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.