Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Returning Last Tranaction Date
Message
 
 
À
03/12/2013 17:11:19
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2005
Application:
Desktop
Divers
Thread ID:
01589211
Message ID:
01589225
Vues:
48
>Good question, my first response would be the value only. However it might be nice to be able to provide the origin of the transaction as well.

In this case I suggest to use table valued function (inline), e.g.
create function GetLastTransDate (@RepId int)

RETURN table 
AS

RETURN

with cte as (select max(TransDate) as LastDate, 'T1' as tableName from TransTable1 
WHERE RepID = @RepID
UNION ALL
select max(TransDate) as LastDate, 'T2' as tableName from TransTable2 
WHERE RepID = @RepID
UNION ALL
..),
cte1 as (select *, row_number() over (order by LastDate DESC) as Rn from cte)

select LastDate, tableName from cte1 WHERE Rn = 1
and invoke it this way:

select ..., F.LastDate, F.TableName from myMainTable T CROSS APPLY dbo.GetLastTransDate(T.RepID) F
If it's not broken, fix it until it is.


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

Click here to load this message in the networking platform