sql query grouping

I have a query which works just fine. I wan to add 2 additional columns to calculate the records with the same ID.

The query output is attached

I need to count number of the same records AreaID and CLTID, and put them in the separate column.
qry.txt
LVL 19
erikTsomikSystem Architect, CF programmer Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ArgentiCommented:
erik

for the given query output you provided, what would be the expected output after modifying the query?
0
erikTsomikSystem Architect, CF programmer Author Commented:
the same output .I just need 2 columns which will do the count. I know the number will be repeated multiple times, i will sort it out on the output
0
Brian CroweDatabase AdministratorCommented:
Please provide your initial query and what version of SQL Server you are operating in.

To provide some sort of advice:

Turn your original query into a common table expression and use multiple references...

WITH cte (itemID, ID, ItemCLID, name, Descr, iconID, CLTname, AreaID, CLTID)
AS
(
   <your query here>
)
SELECT itemID, ID, ItemCLID, name, Descr, iconID, CLTname, AreaID, CLTID,
   (SELECT COUNT(*) FROM cte AS cteAreaID WHERE cteAreaID.AreaID = cte.AreaID),
   (SELECT COUNT(*) FROM cte AS cteCLTID WHERE cteCLTID.CLTID = cte.CLTID)
FROM cte
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

ArgentiCommented:
select
   itemID, ID, ItemCLID, name, Descr, iconID, CLTname, AreaID, CLTID,
   (select count(*) from my_table T1 where T1.AreaID = T.AreaID) as CountAreaID,
   (select count(*) from my_table T1 where T1.CLTID = T.CLTID) as CountCLTID
from
   my_table T
where
   my_conditions
0
Brian CroweDatabase AdministratorCommented:
Alternatively add COUNT...OVER clauses below which may need to be altered depending on your query.

SELECT itemID, ID, ItemCLID, name, Descr, iconID, CLTname, AreaID, CLTID,
   COUNT(*) OVER(PARTITION BY AreaID) AS AreaIDCount,
   COUNT(*) OVER(PARTITION BY CLTID) AS CLTIDCount
FROM myTable
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
both MySQL & MS SQL Server are chosen as topics, but the syntax options for your request can differ depending on which dbms you are actually using - please only select the topics that are relevant to your question.

& Providing your existing query would:
a. reveal which dbms you are using
b. enable experts to propose a precise solution quickly
c. save you time as you don't have to choose from options that can't be used
d. saves the voluntary time of experts from proposing options you cannot use

nb: if you need a "cross platform" solution for both MySQL & MS SQL Server I would suggest you state that in the question.

(Argenti's correlated subqueries would be cross platform btw, but
we don't know the full extent of the existing query and those subqueries may require more details in the where clauses
there may be options giving better performance
)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.