SELECT displaying the same field several times from a calculation

Asked

Viewed 48 times

1

I started studying databases, I am using Postgres and I came across the following need, I have field "packages" in one in the table "flow", where I need to separate the packages by quantity of "bytes" (another record of the same table), I can make this presentation separately with the following selects:

select idpontocli,sum(pacotes) as "<128"  from fluxo
where idpontocli= 51 and fluxo.time between '2020-05-23 11:15:00' and '2020-05-23 11:25:00' and (bytes/pacotes) <= 128 
group by dpontocli;

select idpontocli,sum(pacotes) as "<256"  from fluxo
where idpontocli= 51 and fluxo.time between '2020-05-23 11:15:00' and '2020-05-23 11:25:00' and (bytes/pacotes) > 128  and (bytes/pacotes) <= 256 
group by idpontocli;

I would like to know if there is a possibility in the same select to make this separation and present a result of the kind:

idpontocli  |  <128 | <256 | <512 ...

1 answer

0

This feature is called "PIVOT" where basically you make a grouping of row results into specific columns.

So we can test this for you to load the data into db-fiddle.

This example seems very simple to implemenar here.

In the official documentation you can find in this link.

  • Philip, first of all thank you for your reply. I tried to run select, I had to activate Function Crosstab, but I think my knowledge is too little and I did not succeed. As for putting the data on that link, I was left with some doubt, but I have to inform that the table is giant, taken five minutes are inserted around 15 to 20 thousand records, are network traffic information. I tried to give you more information, but the comment field is small. in case I can’t help you thank you.

  • @user190678 puts only one piece of the table so we can try to help. takes only 1000 records or something. This link that I gave him makes it easy to test in an environment so I can adjust the query for you. The PIVOT is a little complicated but after you get the hang of it is good. QQ thing puts a table create script somewhere and sends the link through here or in the body of your question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.