Avatar of Brogrim
Brogrim
Flag for Ireland asked on

Subquery grouping

I am attempting to group data from a sub query

The 1st part is to filter teh records works fine

SELECT     YEAR(dbo.tblMemberROC.CommensementofService) AS Expr1, dbo.tblMemberROC.ROCSUStatusID AS Expr2
FROM         dbo.tblMemberROC INNER JOIN
                      dbo.tblMember ON dbo.tblMemberROC.MemberID = dbo.tblMember.MemberID
WHERE     (dbo.tblMemberROC.ROCSUStatusID = 3 OR
                      dbo.tblMemberROC.ROCSUStatusID = 4))

I now want to group the records using this sql


SELECT     YEAR(dbo.tblMemberROC.CommensementofService) AS Expr3, COUNT(dbo.tblMemberROC.ROCSUStatusID) AS Expr4
GROUP BY YEAR(dbo.tblMemberROC.CommensementofService)

Where (
SELECT     YEAR(dbo.tblMemberROC.CommensementofService) AS Expr1, dbo.tblMemberROC.ROCSUStatusID AS Expr2
FROM         dbo.tblMemberROC INNER JOIN
                      dbo.tblMember ON dbo.tblMemberROC.MemberID = dbo.tblMember.MemberID
WHERE     (dbo.tblMemberROC.ROCSUStatusID = 3 OR
                      dbo.tblMemberROC.ROCSUStatusID = 4))
Microsoft SQL Server

Avatar of undefined
Last Comment
tim_cs

8/22/2022 - Mon
tim_cs

SELECT     YEAR(a.Expr1) AS Expr3, COUNT(a.Expr2) AS Expr4
FROM (
SELECT     YEAR(dbo.tblMemberROC.CommensementofService) AS Expr1, dbo.tblMemberROC.ROCSUStatusID AS Expr2
FROM         dbo.tblMemberROC INNER JOIN
                      dbo.tblMember ON dbo.tblMemberROC.MemberID = dbo.tblMember.MemberID
WHERE     (dbo.tblMemberROC.ROCSUStatusID = 3 OR
                      dbo.tblMemberROC.ROCSUStatusID = 4)) a
GROUP BY YEAR(a.Expr1)
Brogrim

ASKER
that is returning 2 records on the group

the sub query returns 1953 records of which ether is 33 groups
ASKER CERTIFIED SOLUTION
tim_cs

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.
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
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