Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sql Server aggregation problem

Posted on 2013-05-21
3
Medium Priority
?
318 Views
Last Modified: 2013-05-21
I have an interesting problem.  I have to present data with a single sql statement from invoice header and invoice transaction tables.

Here's my current SQL statement:
select 
Base.CASENO as [Matter Number],
INVOICES.INV_NO as [Invoice Number],
sum(INVTRANS.TRANS_AMT) as [Total Fees],
ISNULL(INVTRANS.TRANS_HOURS, '') as [Units],
CASE
	WHEN INVTRANS.TRANS_HOURS IS NULL THEN 0
	WHEN INVTRANS.TRANS_HOURS = 0 THEN 0
	ELSE INVTRANS.TRANS_AMT/INVTRANS.TRANS_HOURS 
END as [Rate],
INVTRANS.TRANS_AMT as [Item Total],
INVTRANS.INVTRANS_ID
from INVTRANS inner join INVOICES on INVTRANS.INVOICE_ID = INVOICES.INVOICE_ID
left outer join BASE on INVTRANS.MATTER_ID = BASE.MATTER_ID
group by
	base.CASENO, invoices.INV_NO, INVTRANS.TRANS_HOURS, INVTRANS.TRANS_AMT, INVTRANS.INVTRANS_ID
order by 2, 1

Open in new window


Here's the result I get from my data:
SQL table result
The problem is that the Total Fees column should be the sum of that column, where the invoice number and matter number are the same, redundantly shown for each row.  For example, in my image above, rows 5 & 6 are the same invoice number and matter number, and so each should show 60.00 (the total of Item Total for both rows).  And rows 8 and 9 should each have 150.00 as Total Fees, since they have the same invoice number and matter number.

Normally, this would be the invoice header record's total, but in this instance, my client needs the equivalent of a subtotal by matter number within the invoice numbers.

any ideas?
0
Comment
Question by:bjones8888
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 39186335
If I have understood your request I think sum(..) over (partiton by ...) would suit the need. Please try this
SELECT
      Base.CASENO AS [Matter Number]
    , INVOICES.INV_NO AS [Invoice Number]
    , sum(INVTRANS.TRANS_AMT) AS [Total Fees]
    , sum(INVTRANS.TRANS_AMT) OVER (
            PARTITION BY INVOICES.INV_NO, Base.CASENO
            ) AS [Total Fees Per Inv/Matter]
    , ISNULL(INVTRANS.TRANS_HOURS, '') AS [Units]
    , CASE 
        WHEN INVTRANS.TRANS_HOURS IS NULL
            THEN 0
        WHEN INVTRANS.TRANS_HOURS = 0
            THEN 0
        ELSE INVTRANS.TRANS_AMT / INVTRANS.TRANS_HOURS
        END AS [Rate]
    , INVTRANS.TRANS_AMT AS [Item Total]
    , INVTRANS.INVTRANS_ID
FROM INVTRANS
INNER JOIN INVOICES ON INVTRANS.INVOICE_ID = INVOICES.INVOICE_ID
LEFT JOIN BASE ON INVTRANS.MATTER_ID = BASE.MATTER_ID
GROUP BY
      base.CASENO
    , invoices.INV_NO
    , INVTRANS.TRANS_HOURS
    , INVTRANS.TRANS_AMT
    , INVTRANS.INVTRANS_ID
ORDER BY INVOICES.INV_NO, Base.CASENO

Open in new window

0
 

Author Closing Comment

by:bjones8888
ID: 39186347
Brilliant!  That's exactly what I needed.  

Thank you.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39186351
:) good guess then (kidding)
thanks

Cheers, Paul
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question