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

The Archive Base Latest Questions

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

I couldn’t figure out how to optimize this query and I didn’t find a

  • 0

I couldn’t figure out how to optimize this query and I didn’t find a similar query as example.

What would be the best way to rewrite this query in order not to have so many sub-queries in the select clause achieving the same result grouped by month/brand_id?

The DBMS is SQL Server.

select
    o.brand_id,
    count(o.item_id) as "Total bought items",
    count(o.sell_date) as "Total sold items",
     /* Amount of sold items grouped by month per item */
    (select count(obj.sell_date) from tab_object as obj where month(obj.install_date) = 1 and obj.brand_id = b.brand_id) as "Jan",
    (select count(obj.sell_date) from tab_object as obj where month(obj.install_date) = 2 and obj.brand_id = b.brand_id) as "Feb",
    (select count(obj.sell_date) from tab_object as obj where month(obj.install_date) = 3 and obj.brand_id = b.brand_id) as "Mar",
    (select count(obj.sell_date) from tab_object as obj where month(obj.install_date) = 4 and obj.brand_id = b.brand_id) as "Apr",
    (select count(obj.sell_date) from tab_object as obj where month(obj.install_date) = 5 and obj.brand_id = b.brand_id) as "May",
    (select count(obj.sell_date) from tab_object as obj where month(obj.install_date) = 6 and obj.brand_id = b.brand_id) as "Jun",
    (select count(obj.sell_date) from tab_object as obj where month(obj.install_date) = 7 and obj.brand_id = b.brand_id) as "Jul",
    (select count(obj.sell_date) from tab_object as obj where month(obj.install_date) = 8 and obj.brand_id = b.brand_id) as "Aug",
    (select count(obj.sell_date) from tab_object as obj where month(obj.install_date) = 9 and obj.brand_id = b.brand_id) as "Sep",
    (select count(obj.sell_date) from tab_object as obj where month(obj.install_date) = 10 and obj.brand_id = b.brand_id) as "Oct",
    (select count(obj.sell_date) from tab_object as obj where month(obj.install_date) = 11 and obj.brand_id = b.brand_id) as "Nov",
    (select count(obj.sell_date) from tab_object as obj where month(obj.install_date) = 12 and obj.brand_id = b.brand_id) as "Dec"
from  tab_brand as b
left join tab_object as o on b.brand_id = o.brand_id
                            and year(o.sell_date) = 2012
where b.brand_id in (1234, 1324, 1423, 2314)
group by b.brand_id
order by b.brand_id;
  • 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-12T07:30:30+00:00Added an answer on June 12, 2026 at 7:30 am

    You’re looking for a PIVOT http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx and join the results of that to a query to provide the totals

    select TotalBought, TotalSold, months.* from
    (
    select 
        o.brand_id,
        count(o.item_id) as TotalBought, 
        count(o.sell_date) as TotalSold
    from tab_object o   
    where year(sell_date)=2012
    group by brand_id
    ) totals
        left join
    (    
    select * 
      from (select brand_id, sell_date, left(datename(month,install_date),3) as InstallMonth 
            from tab_object where brand_id in (1234,1324,1423,2314) and year(sell_date)=2012
      ) src 
      pivot 
      ( count(sell_date) for installmonth in ([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])) p
    ) months
        on totals.brand_id = months.brand_id
    

    You don’t seem to use the brand table either

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

Sidebar

Related Questions

I couldn't find anything similar to this anywhere. I have an array of pointers
I couldn't find a suitable title for this. I'm going to express my query
couldn't find a similar topic but this may boil down to not knowing how
Couldn't figure out how to word the question accurately, so here's an example: Given
Couldn't find an answer to this solution, so once I figured it out I
Couldn't find anything about this on the internet, or stackoverflow?!? Basic Example: A great
I couldn't find this in the documentation. If I have a complex object in
I couldn't find anything on this, but probably its just because I don't know
Couldn't find similar enough question so I'll ask. I'm trying to use facebook log-in
Couldn't find a better place to ask this so I hope you guys can

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.