Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Grouping in SQL to find a maximum cutoff

Author  Topic 

BIGGY
Starting Member

17 Posts

Posted - 2012-03-14 : 13:27:47
Good afternoon!

I have two tables that I'm trying to coordinate between. Matter and Invoice which I'm trying to incorporate into a Crystal Report, however I believe that the calculation needs to happen in the SQL rather than in the VB of the report.

The report must ultimately GROUP1 by YEAR(InvoiceDate) and GROUP2 by MatterNumber. Each matter may have one or many invoices. What I need to do in SQL is to select the MATTER.MATTERNUMBER, INVOICE.INVOICEAMOUNT for a given date range and other various select criteria.

As the query is pulling in the INVOICEAMOUNT field for each matter, I need it to cut off once the invoices hit the $1,000,000 mark regardless of the date range or anything else. As soon as an invoice dollar amount causes a matter to go over 1 million, the dollar amount should be capped at $1,000,000 and the rest of the invoice and future invoices disregarded. Any suggestions on how to accomplish this would be greatly appreciated.

Thanks,
Mike

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-14 : 15:41:41
so how should be your output? can you post sample?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

BIGGY
Starting Member

17 Posts

Posted - 2012-03-14 : 16:09:27
Sure here's a sample of what it would look like...

Currently if I just run the basic SELECT query then I will see three fields.

MatterNumber | InvoiceDate | InvoiceAmount | InvoiceID

1000 3-1-2012 $500,000 1
1000 3-3-2012 $400,000 2
1000 3-5-2012 $500,000 3
1000 3-6-2012 $100,000 4

Totaling the invoice amounts up, the matter number 1000 would go over $1 million for a grand total of $1.5 mil. What should be displayed and passed into the Crystal report is...

MatterNumber | InvoiceDate | InvoiceAmount | InvoiceID

1000 3-1-2012 $500,000 1
1000 3-3-2012 $400,000 2
1000 3-5-2012 $100,000 3

So two things happened...Invoice #3 was cut so that the grand total does not exceed $1 mil and Invoice #4 was dropped all together (this would apply to any invoices after #4 as well).

Thanks!
Mike
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-14 : 21:05:51
[code]
;with cte as
(
select MatterNumber, InvoiceDate, InvoiceAmount, InvoiceID,
InvAmt = case when InvoiceAmount < MatterNumber
then InvoiceAmount
else 0
end,
CummAmt = case when InvoiceAmount < MatterNumber
then InvoiceAmount
else MatterNumber
end
from yourtable
where InvoiceID = 1

union all

select d.MatterNumber, d.InvoiceDate, d.InvoiceAmount, d.InvoiceID,
InvAmt = case when c.CummAmt + d.InvoiceAmount < d.MatterNumber
then d.InvoiceAmount
else d.MatterNumber - c.CummAmt
end,
CummAmt = case when c.CummAmt + d.InvoiceAmount < d.MatterNumber
then c.CummAmt + d.InvoiceAmount
else d.MatterNumber
end
from cte c
inner join yourtable d on c.InvoiceID + 1 = d.InvoiceID
)
select MatterNumber, InvoiceDate, InvAmt, InvoiceID
from cte
where InvAmt <> 0
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-15 : 15:27:55
see similar logic used here. Its in lines of what Tan posted

http://visakhm.blogspot.com/2012/03/iterative-queries-using-common-table.html



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-15 : 15:41:31
Well maybe you can explain the logic of your request for your readers... because it makes no sense to me



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -