Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Query help needed
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
01296873
Message ID:
01296931
Vues:
16
>Consider the table:
>
>Sn   Bal  DateSaved  TimeSaved
>1    200  2/5/2008   10:00
>1    175  2/5/2008   11:00
>1    123  2/5/2008   13:00
>1    111  2/6/2008   08:00
>1    101  2/6/2008   18:00
>2    500  2/8/2008   10:00
>2    475  2/8/2008   11:00
>
>
>I am trying write a query that will give me the following result set
>
>Sn   Bal  DateSaved  TimeSaved
>1    101  2/6/2008   18:00
>2    475  2/8/2008   11:00
>
>
>1 record per Sn where the DateSaved and the TimeSaved (together) is the newest datetime.
>
>Thanks,
>Einar

And because it should be SQL Server compliant try:
SELECT YourTable.*;
       FROM YourTable;
INNER JOIN (SELECT Sh, MAX(CAST(DTOC(DateSaved)+' '+TimeSaved as DateTime)) AS DateTimeSaved;
                   FROM YourTable;
            GROUP BY Sh) Tbl1;
ON YourTable.Sh = Tbl1.Sh AND;
   CAST(DTOC(YourTable.DateSaved)+' '+YourTable.TimeSaved as DateTime) = Tbl1.DateTimeSaved
<pre>
not tested.
Is SQL Server it should be:
<pre>
SELECT YourTable.*;
       FROM YourTable;
INNER JOIN (SELECT Sh, MAX(CAST(CONVERT(varchar(8),DateSaved,112)+' '+TimeSaved as DateTime)) AS DateTimeSaved;
                   FROM YourTable;
            GROUP BY Sh) Tbl1;
ON YourTable.Sh = Tbl1.Sh AND;
   CAST(CONVERT(varchar(8),DateSaved,112)+' '+TimeSaved as DateTime) = Tbl1.DateTimeSaved
That is of course if DateSaved is datetime field.
BTW Why not just use Datetime field?
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