Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Response to query
Message
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01212334
Message ID:
01212378
Views:
14
>Walter, back in Message #1211591, you stated that many retrieval requirements did not fit well into a SET-oriented approach. You provided an example at the bottom of the message of some FoxPro code, and asked me to provide a SQL solution.
>
>I'm starting a new thread on the SQL Server forum - I've posted a response to your challenge/question on my newest blog entry, at www.TheBakersDozen.net. I've provided script for a sample table, so that anyone with SQL Server 2005 should be able to run it.
>
>Kevin

Kevin,
you have some typos in he BLOG:
AND  type  in  (N'FN', N'IF', N'TF', N        'FS', N'FT'))
/*must be*/
AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
/* because my SSMS raise an error on N        'FS' /*
also in the function you use Invoices:
 FROM Invoices 
but it must be
  FROM Invoice 
Also please add dates in the table with ANSI format ;-) because we, which are on the other part of the world, have different DATE FORMAT :-)
This is a great example how to use recursive queries and join the function to it, thank you.

For older versions:
DECLARE @DebtorID int
DECLARE @Threshold decimal (14,2)
SET @DebtorID = 1
SET @Threshold = 1000.00

SELECT Invoice.InvoiceNum,
       Invoice.DebtorID,
       Invoice.InvoiceDate,
       MAX(Invoice.InvoiceAmount)+
       SUM(ISNULL(Inv.InvoiceAmount,0)) AS InvoiceAmount
FROM Invoice
LEFT JOIN Invoice Inv ON Invoice.DebtorId    = Inv.DebtorId AND
                         Invoice.InvoiceDate > Inv.InvoiceDate

WHERE  Invoice.DebtorID = @DebtorID   

GROUP BY Invoice.InvoiceNum,
         Invoice.DebtorID,
         Invoice.InvoiceDate
 
HAVING MAX(Invoice.InvoiceAmount)+
       SUM(ISNULL(Inv.InvoiceAmount,0)) >= @Threshold
THIS EXAMPLE IS NOT TESTED very well, only against the example data you provided.
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform