Totaling/Grouping in SQL query

My data returned looks like this:

Description      StatuteReference               Page   Sort Order   GLAccountId      Total
ClientX           4875                              1      1215            10                      3752.84
ClientX           4875                              1      1215            11                      3425.84
ClientX           4875                              1      1215            12                      2134.84

How would I change my query to have these merged into a single record and have a "total" on the Total column above?

Thanks.
SELECT
  str.Description, str.StatuteReference, str.Page, str.SortOrder
,STRA.SourceCodeId
,GLT.GLAccountId, GLT.Amount
FROM
  rp.StateRemittance as STR
  
  INNER JOIN rp.StateRemittanceAccount STRA on str.StateRemittanceId = STRA.StateRemittanceId

INNER JOIN
  (SELECT 
     GlAccountId, PostingDate, Amount
   FROM
     ac.GLTransaction
   GROUP BY 
     GlAccountId, PostingDate, Amount) GLT
ON
  STRA.GLAccountId = GLT.GLAccountId

WHERE GLT.PostingDate BETWEEN '3/01/2010' AND '3/31/2010'

ORDER BY SortOrder ASC

Open in new window

LVL 8
ApexCoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Daniel WilsonCommented:
What kind of a "merge" do you want?  The real question is, what do you want done with the data that are different -- that is the GLAccountID field?

If you want to pick the minimum or the maximum, a SUM operation becomes easy.

Or are you looking to have each GLAccountID in its own column?  That gets into PIVOT operations ...
0
ApexCoAuthor Commented:
I'm looking to really group this by the Description field. All of the clients needs to have their totals summed up.

Even if the GLAccount Id is different. Still possible to do?
0
Daniel WilsonCommented:
OK, then we'll just grab the minimum GLAccountID and sum the Total, grouping by Description.

Please try:

SELECT
  str.Description, min(str.StatuteReference) as StatuteReference, min(str.Page) as Page, str.SortOrder
,STRA.SourceCodeId
,min(GLT.GLAccountId) as GLAccountID, Sum(GLT.Amount) as Total
FROM
  rp.StateRemittance as STR
  
  INNER JOIN rp.StateRemittanceAccount STRA on str.StateRemittanceId = STRA.StateRemittanceId

INNER JOIN
  (SELECT 
     GlAccountId, PostingDate, Amount
   FROM
     ac.GLTransaction
   GROUP BY 
     GlAccountId, PostingDate, Amount) GLT
ON
  STRA.GLAccountId = GLT.GLAccountId

WHERE GLT.PostingDate BETWEEN '3/01/2010' AND '3/31/2010'
Group By str.Description, str.SortOrder ,STRA.SourceCodeId
ORDER BY SortOrder ASC

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

ApexCoAuthor Commented:
That's a definite improvement.

However, the "Total" column has the same value for every record.
0
Daniel WilsonCommented:
I expected only a single record to be returned in place of the 3 you posted.

Would you post the result of my query?
0
ApexCoAuthor Commented:
Description StatuteReference Page SortOrder SourceCodeId Total
Victim Rights-Juvenile 08-418;41-191-08 1 32 1114 4895.58
 
Description StatuteReference Page SortOrder SourceCodeId Total
JCEF-Time Payment 12-113G;12-116B 1 60 309 4895.58
 
Description StatuteReference Page SortOrder SourceCodeId Total
JCEF-Probation Surcharge 12-114.01 1 65 375 4895.58
 
Those are 3 different records from the result set. You can see everything looks good, except the total.
0
Daniel WilsonCommented:
hmm ... let's try grouping a little more and aggregating a little less ...

SELECT
  str.Description, str.StatuteReference, str.Page, str.SortOrder
,STRA.SourceCodeId
,min(GLT.GLAccountId) as GLAccountID, Sum(GLT.Amount) as Total
FROM
  rp.StateRemittance as STR
  
  INNER JOIN rp.StateRemittanceAccount STRA on str.StateRemittanceId = STRA.StateRemittanceId

INNER JOIN
  (SELECT 
     GlAccountId, PostingDate, Amount
   FROM
     ac.GLTransaction
   GROUP BY 
     GlAccountId, PostingDate, Amount) GLT
ON
  STRA.GLAccountId = GLT.GLAccountId

WHERE GLT.PostingDate BETWEEN '3/01/2010' AND '3/31/2010'
Group By str.Description, str.StatuteReference, str.Page, str.SortOrder ,STRA.SourceCodeId
ORDER BY SortOrder ASC

Open in new window

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
ApexCoAuthor Commented:
Thanks for the help, you put me on the right path.
Final result:

SELECT str.DESCRIPTION, 
       MIN(str.statutereference) AS statutereference, 
       MIN(str.page)             AS page, 
       str.sortorder, 
       SUM(glt.amount)           AS total 
FROM   rp.stateremittance AS str 
       INNER JOIN rp.stateremittanceaccount stra 
         ON str.stateremittanceid = stra.stateremittanceid 
       INNER JOIN ac.gltransaction glt 
         ON stra.glaccountid = glt.glaccountid 
            AND stra.sourcecodeid = glt.sourcecodeid 
WHERE  glt.postingdate BETWEEN '1/01/2010' AND '1/31/2010' 
GROUP  BY str.DESCRIPTION, 
          str.sortorder 
ORDER  BY sortorder ASC

Open in new window

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

From novice to tech pro — start learning today.