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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:27:59+00:00 2026-06-17T08:27:59+00:00

I need multiple counts from multiple tables, but all tables join with the same

  • 0

I need multiple counts from multiple tables, but all tables join with the same tables and have the same where clause. I get the correct result, but, in a very large and slow query, and, I’d like to learn how to improve this.

I have to get a count from tables:

  • Ocorrencias
  • Followups
  • PropostasComerciais
  • PropostasDeLocacao
  • PedidosDeVendas

And 3 SUM, all from PedidosDeVendas table.

all this querys joins with:
– Empresas
– PedidosDeVendaXItens

and all querys have the same WHERE clause:
– WHERE SolicitanteID = {User ID} AND Data >= ‘{Initial Date}’ AND Data <= ‘{Final Date}’

This is the query that return all results I need:

SELECT U.[ID] AS UniqKey, U.NomeCompleto, R.Regiao,    

(SELECT Count(*)    
FROM Ocorrencias O    
INNER JOIN Nomes N ON (O.ClientesID = N.Codigo AND O.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
WHERE O.UsuariosID = U.[ID]    
AND MR.ID_Regiao = R.uniqKey    
AND O.Data >= '2012-12-01 00:00:00' AND O.Data <= '2012-12-31 23:59:59') AS Ocorrencias,    

(SELECT Count(*)    
FROM FollowUp F    
INNER JOIN Nomes N ON (F.ClienteID = N.Codigo AND F.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
WHERE SolicitanteID = U.[ID]    
AND MR.ID_Regiao = R.uniqKey    
AND Data >= '2012-12-01 00:00:00' AND Data <= '2012-12-31 23:59:59') AS FollowUps,    

(SELECT Count(*)    
FROM v_PropostaComercial PC    
INNER JOIN Nomes N ON (PC.ClienteID = N.Codigo AND PC.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
WHERE PC.SolicitanteID = U.[ID]    
AND MR.ID_Regiao = R.uniqKey    
AND PC.Data >= '2012-12-01 00:00:00' AND PC.Data <= '2012-12-31 23:59:59') AS PropostasComerciais,                  

(SELECT Count(*)    
FROM PropostaDeLocacao PL    
INNER JOIN Nomes N ON (PL.ClienteID = N.Codigo AND PL.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
WHERE PL.ResponsavelID = U.[ID]    
AND MR.ID_Regiao = R.uniqKey    
AND PL.Data >= '2012-12-01 00:00:00' AND PL.Data <= '2012-12-31 23:59:59') AS PropostasDeLocacao,    

(SELECT Count(*)    
FROM PedidosDeVenda PV    
INNER JOIN Nomes N ON (PV.ClienteID = N.Codigo AND PV.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
WHERE PV.SolicitanteID = U.[ID]    
AND MR.ID_Regiao = R.uniqKey    
AND PV.Data >= '2012-12-01 00:00:00' AND PV.Data <= '2012-12-31 23:59:59') AS PedidosDeVenda,                   

(SELECT SUM(PVI.Valor)    
FROM PedidosDeVenda PV    
INNER JOIN Empresas N ON (PV.ClienteID = N.Codigo AND PV.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
INNER JOIN PedidosDeVendaXItens PVI ON PVI.PedidoDeVendaID = PV.[ID]    
INNER JOIN Modelos M ON PVI.ProdutoID = M.[ID]    
WHERE PV.SolicitanteID = U.[ID]    
AND MR.ID_Regiao = R.uniqKey
AND PV.Data >= '2012-12-01 00:00:00' AND PV.Data <= '2012-12-31 23:59:59') As TotalPedidosDeVenda,    

(SELECT SUM(PVI.Valor)    
FROM PedidosDeVenda PV    
INNER JOIN Nomes N ON (PV.ClienteID = N.Codigo AND PV.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
INNER JOIN PedidosDeVendaXItens PVI ON PVI.PedidoDeVendaID = PV.[ID]    
INNER JOIN Modelos M ON PVI.ProdutoID = M.[ID]    
WHERE PV.SolicitanteID = U.[ID]    
AND N.TipoEmpresa = 'PU'    
AND PV.ClienteID <> U.ClienteID    
AND MR.ID_Regiao = R.uniqKey    
AND PV.Data >= '2012-12-01 00:00:00' AND PV.Data <= '2012-12-31 23:59:59') As TotalVendasPublicas,    


(SELECT SUM(PVI.Valor)    
FROM PedidosDeVenda PV    
INNER JOIN Nomes N ON (PV.ClienteID = N.Codigo AND PV.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
INNER JOIN PedidosDeVendaXItens PVI ON PVI.PedidoDeVendaID = PV.[ID]    
INNER JOIN Modelos M ON PVI.ProdutoID = M.[ID]    
WHERE PV.SolicitanteID = U.[ID]    
AND N.TipoEmpresa = 'PR'    
AND PV.ClienteID <> U.ClienteID    
AND MR.ID_Regiao = R.uniqKey    
AND PV.Data >= '2012-12-01 00:00:00' AND PV.Data <= '2012-12-31 23:59:59') As TotalVendasPrivadas,    

(SELECT SUM(PVI.Valor)    
FROM PedidosDeVenda PV    
INNER JOIN Nomes N ON (PV.ClienteID = N.Codigo AND PV.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
INNER JOIN PedidosDeVendaXItens PVI ON PVI.PedidoDeVendaID = PV.[ID]    
INNER JOIN Modelos M ON PVI.ProdutoID = M.[ID]    
WHERE PV.SolicitanteID = U.[ID]    
AND PV.ClienteID = U.ClienteID    
AND MR.ID_Regiao = R.uniqKey    
AND PV.Data >= '2012-12-01 00:00:00' AND PV.Data <= '2012-12-31 23:59:59') As TotalVendasProprias    

FROM Usuarios U    
INNER JOIN Regioes_Usuario RU ON U.[ID] = RU.ID_Usuario    
INNER JOIN Regioes R ON RU.ID_Regiao = R.uniqKey    
WHERE U.Representante = 64        
ORDER BY R.Regiao, U.NomeCompleto;

is there any trick to turn my query faster and shorter?

  • 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-17T08:28:01+00:00Added an answer on June 17, 2026 at 8:28 am

    You should not be doing intensive nested queries inside of a select inside of a main query as each nested I believe will execute each time a join on the outer occurs.

    Instead think of declaring variables and assigning these first.

    Like

    declare @outsidepredicate int, @Count1 int, @Count 2 int;
    
    select @outsidepredicate = value from sharedtable for expressions
    
    select @Count1 = count(*) 
    from table1 t (nolock) 
     join table2 tt (nolock) on t.idshared = tt.idshared
    where outsidepredicatecolumn = @outsidepredicate
    
    select @Count2 = count(*) 
    from table3 t (nolock) 
     join table4 tt (nolock) on t.idshared = tt.idshared
    where outsidepredicatecolumn = @outsidepredicate
    

    Then you could do your regular test like this:

    select 
        @Count1
    ,   @Count2
    ,   valuefrommain
    from tableMain m (nolock) 
     join tableSide s (nolock) on m.idshared = s.idshared
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get data from multiple tables from a database and I need
In my case, I'm deleting from multiple tables using LEFT JOIN and need to
I have 4 tables that I need to pull data from. I need to
I have table that has multiple data for the same Id. I need to
I'm trying to get some aggregate values from different tables, but my problem is
I am using the following query to get the count from multiple tables:- SELECT
I have a dynamic amount of tables with the same column structure. I wish
I need to do a left join on multiple conditions where the conditions are
I need to display multiple images from a database table and bind it to
I need to get summary data from many many rows. The summary fields are

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.