How to make a query to group by ranges?

-1

I need to group by ranges this is the result where I want to arrive

The query I am using is the following:

SELECT org_account, COUNT(RESULT) AS TOTAL_MT 
FROM TABLE 
WHERE FECHA = '02/12/2018' AND mt_msc_addr IS NOT NULL 
GROUP BY org_account 
ORDER BY TOTAL_MT DESC

The result of the query is as follows:

ORG_ACCOUNT     TOTAL
Gateway_G       34
WSMS            23
smsgw           23
VMSTRWI         22
sdp             22
OtaDMC          18
ocs             17
PcRf            15
MNP_icon        3
wapgw           1
    
asked by Juan Perez 03.12.2018 в 16:39
source

2 answers

0

We can use your query as the basis of everything we do to add that new column.

As it does not exist, we can not add it directly in your query.

Let's call your query A * (that means that every time you read that, you would have to be all your query as it is now)

But we could make a union for example to generate the output:

Select 'grafico 2 y datos 2' subtotal,
org_account,
TOTAL_MT 
from (A*)
where org_account In ('OtaDMC','otarca1')
UNION
Select 'grafico 3 y datos 3' subtotal,
org_account,
TOTAL_MT 
from (A*)
where org_account In ('Gateway_G','MNP_icon'.....)
UNION .....

Notice that you have to complete it where the ellipses are.

Also, we could use a case

Select 
    Case org_account 
        when 'OtaDMC' then 'grafico 2 y datos 2'
        when 'otarca1' then 'grafico 2 y datos 2'
        when 'Gateway_G' then 'grafico 3 y datos 3'
        when 'MNP_icon' then 'grafico 3 y datos 3'
        when .......
    end subtotal
    org_account,
    TOTAL_MT 
from (A*)

Where you would also have to complete the ellipses.

All this would be easier, if your ranges were already defined in another table, and only adjust them use a join. But as you decide that your table does not have an id, it would be more complex ..

Although the recommendation would be to create a table

Rango                nombre_dato
grafico 2 y datos 2  OtaDMC
grafico 2 y datos 2  otarca1

And then, with that, you would only make a join between your query and this table in the following way:

Select Rango, org_account, Total_MT
From (A*) inner join nuevatabla on nombre_dato = org_account
    
answered by 03.12.2018 в 17:24
0

basically in yourself Question you are answering, I show you:

SELECT org_account, COUNT(RESULT) AS TOTAL_MT FROM TABLE WHERE FECHA = '02/12/2018' AND mt_msc_addr IS NOT NULL GROUP BY org_account HAVING COUNT(RESULT) BETWEEN 15 AND 23 ORDER BY TOTAL_MT DESC

note: I could not see your image, so with the first comments I think I understood, I hope it serves you ...

    
answered by 05.12.2018 в 01:56