Query does not sort records by Asc / Desc

2

I have a table called cadcha (call register) in the database where all the calls made by the employees of a certain company are stored.

cadcha
--------------------------------------------------
nreg    |telefone      |telpretot    |ramaldestino
1110     35420000       0,79          5065

telpretot = connection value

I made a query that brings the fashion statistic by the value of the connections, and needed that it be ordered by the decreasing amount, that is, the record with the larger quantity, comes first.

SELECT COUNT(telpretot) AS qtde, 
(CAST(telpretot AS DECIMAL(18,0))) as       preco
FROM cadcha
WHERE teldata = '08/03/2015'
GROUP BY (CAST(telpretot AS DECIMAL(18,0)))
ORDER BY qtde DESC;

It works fine, however, the ordered record is returned from the smallest to the largest. I've already tried to change DESC to ASC but the result displayed is the same.

How can I correct the script so that records are sorted from highest to lowest?

    
asked by anonymous 16.12.2015 / 13:45

2 answers

1

This happens because the qtde column is being populated at runtime, because you are using COUNT(telpretot) , try it that way.

declare @cadcha table
(
  nreg int,
  telefone int,
  telpretot numeric(18,2),
  ramaldestino int,
  teldata date

)

insert into @cadcha values
(1110,     35420000 ,      0.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      0.3   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      0.5   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      5.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      6.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      10.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      110.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      32.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      32.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      34.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015'),
(1110,     35420000 ,      44.79   ,       5065, '08/03/2015')

select * from 
(
    SELECT COUNT(telpretot) AS qtde, 
    (CAST(telpretot AS DECIMAL(18,0))) as       preco
    FROM @cadcha
    WHERE teldata = '08/03/2015'
    GROUP BY (CAST(telpretot AS DECIMAL(18,0)))
)D
ORDER BY qtde DESC;

    
16.12.2015 / 16:34
0

And this group by ai, remove it

SELECT COUNT(telpretot) AS qtde, 
(CAST(telpretot AS DECIMAL(18,0))) as preco
FROM cadcha
WHERE teldata = '08/03/2015'
ORDER BY qtde DESC;
    
16.12.2015 / 14:19