Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Totals from a set of dates
Message
 
À
26/02/2010 17:48:49
Moacyr Zalcman
Independent Consultant
São Paulo, Brésil
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:
01451177
Message ID:
01451390
Vues:
69
This message has been marked as the solution to the initial question of the thread.
DECLARE @HistoricoProduto TABLE (id int, prodid int, caddate datetime)
INSERT INTO @HistoricoProduto VALUES (714640, 99608,'2010-02-21 17:27:38')
INSERT INTO @HistoricoProduto VALUES (714641,101899,'2010-02-22 17:27:38')
INSERT INTO @HistoricoProduto VALUES (714638,101226,'2010-02-23 17:20:03')
INSERT INTO @HistoricoProduto VALUES (714639,101234,'2010-02-24 17:20:03')

DECLARE @Romaneio TABLE (id int, valor int, caddate datetime)
INSERT INTO @Romaneio VALUES (769291,2459.06,'2010-02-21 16:57:14')
INSERT INTO @Romaneio VALUES (769290,3055.14,'2010-02-22 16:34:23')
INSERT INTO @Romaneio VALUES (769289,1064.73,'2010-02-23 15:34:03')
INSERT INTO @Romaneio VALUES (769288,2055.85,'2010-02-24 15:29:17')

SELECT Hist.ProdId     AS ProdId,
       Hist.CadDate    AS StartDate,
       SUM(Roma.Valor) AS Sales
FROM (SELECT ProdId,
             MAX(CadDate) AS CadDate
      FROM @HistoricoProduto
      GROUP BY prodid) Hist
INNER JOIN @Romaneio Roma ON Hist.CadDate <= Roma.CadDate
GROUP BY Hist.ProdId,
         Hist.CadDate
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