3
How do I get the Valor Unit of the last purchase in Crystal Reports in a resulting table as the following?
+-------------+---------------+------+------------+---------------+
|   CodItem   |   DataCompra  | Qtde |  ValorUnit |   ValorTotal  |
+-------------+---------------+------+------------+---------------+
|    Cod1     |    17/01/15   |  25  |    10,00   |     250,00    |  
|    Cod1     |    09/01/15   |  35  |    20,00   |     700,00    |
|    Cod1     |    11/01/15   |  50  |     5,00   |     250,00    |
|    Cod1     |    22/01/15   |  30  |    10,00   |     300,00    |
|    Cod2     |    19/01/15   |  10  |     5,00   |      50,00    |
|    Cod2     |    15/01/15   |  15  |    10,00   |     150,00    |
+-------------+---------------+------+------------+---------------+
The SQL of the resulting table is as follows:
SELECT CodItem
      ,DataCompra
      ,'Qtde' = SUM(Qtde)
      ,ValorUnit
      ,ValorTotal
FROM...
WHERE DataCompra BETWEEN '2015-01-01' AND '2015-01-31'
GROUP BY CodItem
      ,DataCompra
      ,ValorUnit
      ,ValorTotal
And my result I would like so:
+-------------+---------------+------+------------+---------------+---------------+
|   CodItem   |   DataCompra  | Qtde |  ValorUnit |   ValorTotal  |  UltimoPreco  |
+-------------+---------------+------+------------+---------------+---------------+
|    Cod1     |    17/01/15   |  25  |    10,00   |     250,00    |     10,00     |
|    Cod1     |    09/01/15   |  35  |    20,00   |     700,00    |     10,00     |
|    Cod1     |    11/01/15   |  50  |     5,00   |     250,00    |     10,00     |
|    Cod1     |    22/01/15   |  30  |    10,00   |     300,00    |     10,00     |
|    Cod2     |    19/01/15   |  10  |     5,00   |      50,00    |      5,00     |
|    Cod2     |    15/01/15   |  15  |    10,00   |     150,00    |      5,00     |
+-------------+---------------+------+------------+---------------+---------------+
Preferably (and only if possible) I would like to know how to do this in Crystal Reports. But in SQL it would also help me a lot.
But this would result in a table with only the largest purchase dates, right? I need the result as a new Query column. I’ll edit the question and add how I need the result.
– Cassio Milanelo
Use subselect as a column, just "move" the solution
– Motta
https://technet.microsoft.com/en-us/library/hh213018(v=sql.110). aspx depending on the bd use an "analityc Function" can solve , know that Oracle and Sql Server have.
– Motta