Avatar of erikTsomik
erikTsomikFlag for United States of America asked on

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
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
PortletPaul

8/22/2022 - Mon
Argenti

erik

for the given query output you provided, what would be the expected output after modifying the query?
ASKER
erikTsomik

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
Brian Crowe

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Argenti

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
ASKER CERTIFIED SOLUTION
Brian Crowe

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
PortletPaul

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
)