question

Gelain avatar image
Gelain asked

Group values

Hi! Can you consider only one value per ID for the Value Sell column? It turns out that the sale of 1,000.00 was received twice in 500.00 and in the query I did, of course, the value of 1,000.00 was repeated in each low. Could the received value be grouped by ID? ID Sales Received 1 1.000,00 500,00 1 1.000,00 500,00 2 400,00 200,00 2 400,00 200,00 Total 1.400,00 1.400,00
grouping
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image ThomasRushton ♦♦ commented ·
Can you provide sample data that matches your database objects, and show the query that you have been using, and the actual & expected output together?
0 Likes 0 ·

1 Answer

·
Oleg avatar image
Oleg answered
If there is no possibility that there is yet another sale for the ***same amount*** and ***same ID*** in addition to the split sales records then this will work: select ID, sum(distinct Sales) Sales, sum(Received) Received from YourTable group by ID -- with rollup; If you uncomment the rollup part, it will show the grand totals in the last record. If there is a need to calculate just the grand totals for all IDs and all sales assuming that the condition I outlined in the beginning of this answer holds then this will work: select sum(Sales) TotalSales, sum(Received) TotalReceived from ( select ID, sum(distinct Sales) Sales, sum(Received) Received from YourTable group by id ) t; If the possibility exists that there are multiple sales for the same amount and same ID and each may or may not be split into multiple received then the solution above will not work as is. In this case, more details about the source data are needed in order to come up with the correct solution. Hope this helps. Oleg
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gelain avatar image Gelain commented ·
Hello, Thank you for your interest in helping me. In the actual situation, I applied the first SQL and returned two rows because the received values ​​are not the same for the invoice. How to ignore this and make the sum? I was confused because of the join. Thank you! SELECT A.IDLAN ID , B.CODCFO AS CUSTOMER , SUM(DISTINCT B.VALORORIGINAL) SALES , A.NUMCONTABILBX NUMBER , SUM(A.VALORORIGINAL) RECEIVED FROM FLANBAIXA AS A INNER JOIN FLAN AS B ON A.IDLAN = B.IDLAN AND A.CODCOLIGADA = B.CODCOLIGADA WHERE A.NUMCONTABILBX = '008198/01' GROUP BY A.IDLAN , B.CODCFO , B.VALORORIGINAL , A.NUMCONTABILBX , A.VALORORIGINAL
0 Likes 0 ·
sql1.jpg (84.0 KiB)
Oleg avatar image Oleg commented ·
@Gelain Because you are including the aggregates on B.VALORORIGINAL (sum of distinct values) and A.VALORORIGINAL (sum of values), you must not include these columns in the group by list. Once you remove them, the select should work. Here is how it should look: SELECT A.IDLAN ID, B.CODCFO AS CUSTOMER, SUM(DISTINCT B.VALORORIGINAL) SALES, A.NUMCONTABILBX NUMBER, SUM(A.VALORORIGINAL) RECEIVED FROM FLANBAIXA AS A INNER JOIN FLAN AS B ON A.IDLAN = B.IDLAN AND A.CODCOLIGADA = B.CODCOLIGADA WHERE A.NUMCONTABILBX = '008198/01' GROUP BY A.IDLAN, B.CODCFO, A.NUMCONTABILBX;
0 Likes 0 ·
Gelain avatar image Gelain commented ·
Hello @Oleg! I did not realize that detail. It's all right now. Helped me a lot. Thank you.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.