ORDER BY, how to order salary amount

3

good day guys my doubt is this I put the syntax with its respective order by desc to show me the gain from highest to lowest but I keep ordering them randomly: (

SELECT month(s.fInicio)AS [MES],a.Nombre_auto,
   SUM(DATEDIFF(d, dateadd(d, -1, s.fInicio), s.fTermino) * a.Tarifa_Diaria) [GANANCIA]
FROM dbo.Arriendo_Solicitud s
INNER JOIN dbo.Arriendo_Autos a ON a.Patente = s.Patente
GROUP BY month(s.fInicio),a.Nombre_Auto ,a.Tarifa_Diaria
ORDER BY month(s.fInicio),a.Tarifa_Diaria DESC

    
asked by Daniela 31.10.2018 в 16:55
source

2 answers

4

It is that you are not ordering by the field that you should.

SELECT month(s.fInicio)AS [MES],a.Nombre_auto,
   SUM(DATEDIFF(d, dateadd(d, -1, s.fInicio), s.fTermino) * a.Tarifa_Diaria) [GANANCIA]
FROM dbo.Arriendo_Solicitud s
INNER JOIN dbo.Arriendo_Autos a ON a.Patente = s.Patente
GROUP BY month(s.fInicio),a.Nombre_Auto ,a.Tarifa_Diaria
ORDER BY SUM(DATEDIFF(d, dateadd(d, -1, s.fInicio), s.fTermino) * a.Tarifa_Diaria) DESC

Since I do not know which database engine you are using, I recommend you sort through the entire field (operations included) instead of the alias

    
answered by 31.10.2018 / 16:59
source
2

You just have to put the daily rate in the first place and take into account the first field then the next one and so on, your code would be like this,

SELECT month(s.fInicio)AS [MES],a.Nombre_auto,
   SUM(DATEDIFF(d, dateadd(d, -1, s.fInicio), s.fTermino) * a.Tarifa_Diaria) [GANANCIA]
FROM dbo.Arriendo_Solicitud s
INNER JOIN dbo.Arriendo_Autos a ON a.Patente = s.Patente
GROUP BY month(s.fInicio),a.Nombre_Auto ,a.Tarifa_Diaria
ORDER BY month(s.fInicio) ASC,a.Tarifa_Diaria DESC

I hope it works for you

    
answered by 31.10.2018 в 16:58