Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Response to query
Message
De
05/04/2007 07:44:31
 
 
À
05/04/2007 01:17:13
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01212334
Message ID:
01212378
Vues:
15
>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.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform