Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Removing the last child record from same parent if seque
Message
 
 
À
29/06/2015 09:24:24
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2012
Application:
Web
Divers
Thread ID:
01621484
Message ID:
01621549
Vues:
42
>I would like to revisit the SQL. From that approach,what I did to expand the support to remove additional items in the last sequence is this:
>
>
>;WITH InvoiceTemp AS
> (SELECT Invoice.Numero,Invoice.NoClient,Invoice.NoStatus,
>  ROW_NUMBER() OVER (PARTITION BY Invoice.NoClient ORDER BY AddDate DESC) AS RowNumber,
>  COUNT(*) OVER (PARTITION BY Invoice.NoClient) AS RowTotal
>  FROM Invoice)
>
>SELECT * FROM InvoiceTemp
> WHERE InvoiceTemp.RowNumber<RowTotal AND InvoiceTemp.NoStatus=7 AND
>  EXISTS (SELECT * FROM InvoiceTemp InvoiceTemp2
>           WHERE InvoiceTemp2.RowNumber=2 AND InvoiceTemp2.NoStatus=7 AND 
>            InvoiceTemp.NoClient=InvoiceTemp2.NoClient)
>
>
>But, that gets all Completed, in the child records, and even the first one.
>
>Do you see what I did wrong?
SELECT * FROM InvoiceTemp
 WHERE InvoiceTemp.RowNumberbetween 2 and RowTotal AND InvoiceTemp.NoStatus=7
However, this query will not select only consecutive last rows, it will select all rows except the very last one with the particular status. I need to think more about how to transform this query to only get consecutive last rows with that status and not all rows with that status if this is what you're after.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform