I’m having trouble summing up the column

Asked

Viewed 134 times

1

I want to sum up the column Value, I know that to use SUM() I also have to use GROUP BY, only I’m not able to make the sum. I don’t know which fields I have to put on GROUP BY.

SELECT DISTINCT z.sales_office AS kam, z.customer,g.sales_order,
d.nfnum, d.vlr_liq_nf AS valor FROM TPL_GUARDA g 
LEFT JOIN TPL_COLETAS c     ON g.SALES_ORDER   = c.SALES_ORDER 
LEFT JOIN humo h            ON h.HANDLING_UNIT = g.handling_unit 
LEFT JOIN DANFE_DELIVERY dd ON dd.delivery     = h.delivery 
LEFT JOIN danfe d           ON d.nfnum         = dd.nfnum 
LEFT JOIN zzcustmon z       ON z.sales_order   = g.sales_order 
where c.dt_exp is null and d.nfnum is not null and z.delivery is not null 
GROUP BY

3 answers

1

Basically, in the group by should enter all fields that were not the sum:

SELECT DISTINCT z.sales_office AS kam, z.customer, g.sales_order, d.nfnum, SUM(d.vlr_liq_nf) AS valor
FROM TPL_GUARDA g 
LEFT JOIN TPL_COLETAS c     ON g.SALES_ORDER   = c.SALES_ORDER 
LEFT JOIN humo h            ON h.HANDLING_UNIT = g.handling_unit 
LEFT JOIN DANFE_DELIVERY dd ON dd.delivery     = h.delivery 
LEFT JOIN danfe d           ON d.nfnum         = dd.nfnum 
LEFT JOIN zzcustmon z       ON z.sales_order   = g.sales_order 
WHERE c.dt_exp IS NULL
  AND d.nfnum IS NOT NULL
  AND z.delivery IS NOT NULL
GROUP BY z.sales_office, z.customer, g.sales_order, d.nfnum

In comment it was said that the field to be added is of the type varchar2. Ensuring that they would still be saved whole as text, you can use the to_number:

SUM(TO_NUMBER(d.vlr_liq_nf))

As quoted in comment, the values in the database were being separated by commas, which is not accepted by oracle as a valid number; it would then suffice to give a replace and change the comma by a dot (accepted character):

SUM(REPLACE(d.vlr_liq_nf,',','.')) 
  • I tried to execute the code and gave the error ORA-01722: invalid number.

  • d.vlr_liq_nf is not a numerical field?

  • It’s the prices of the materials that stay on it

  • @Guilhermewayne, could share the definitions of the column fields in the question, remembering that the summation fields only work in numerical fields.

  • The field I’m trying to add is varchar2 type. I just looked here. I thought it was numeric. It has how to make the sum of it?

  • @Guilhermewayne, the answer (edited) helped??

  • The TO_NUMBER did not work, after breaking the head trying, I found that the oracle does not treat the comma as separation of values, the correct is to use point. So as much as I tried to convert the value with TO_NUMBER, it wouldn’t work. So I used REPLACE to replace the comma by stitch, and then it worked

  • @Guilhermewayne, I get it. well, I’ll change the answer and add that you went through, but if you prefer, post an answer saying what you solved and mark as accepted.. ideal is that the question don’t go unanswered

  • @You just did that

Show 4 more comments

0

Use the SUM function that will solve your problem.

SELECT SUM (d.vlr_liq_nf) AS valor
FROM TPL_GUARDA g 
LEFT JOIN TPL_COLETAS c     ON g.SALES_ORDER   = c.SALES_ORDER 
LEFT JOIN humo h            ON h.HANDLING_UNIT = g.handling_unit 
LEFT JOIN DANFE_DELIVERY dd ON dd.delivery     = h.delivery 
LEFT JOIN danfe d           ON d.nfnum         = dd.nfnum 
LEFT JOIN zzcustmon z       ON z.sales_order   = g.sales_order 

This way, you will return only the result you need, the sum of the values of this column. Check the column type is numerical.

  • Danyel, snippet is for HTML, CSS, and JAVASCRIPT code. The OS makes it very clear what it’s for. Review your response.

  • It did not work, still gives the error of Invalid Number. This field stores the values of materials, stores money values

  • This error, Invalid Number, usually happens when you try to do an operation( "+", "-" , "/" , "*") using String data type ("9 + 1") Invalid Number.

  • Dear cyclist friend @Danyelpires, he is asking about the sum of the column, not the average .

0

I managed to solve the problem, in the oracle, the comma treatment is different, comma does not mean comma. It was giving the error Invalid Number, because of the comma, the oracle could not do the conversion. So I used replace to replace the comma with a dot, and I used another select so that I didn’t have to use group by. Follow the code working:

 SELECT SUM(REPLACE(valor,',','.')) FROM (SELECT DISTINCT z.sales_office AS 
 kam, z.customer,g.sales_order,
 d.nfnum, d.vlr_liq_nf AS valor FROM TPL_GUARDA g 
 LEFT JOIN TPL_COLETAS c     ON g.SALES_ORDER   = c.SALES_ORDER 
 LEFT JOIN humo h            ON h.HANDLING_UNIT = g.handling_unit 
 LEFT JOIN DANFE_DELIVERY dd ON dd.delivery     = h.delivery 
 LEFT JOIN danfe d           ON d.nfnum         = dd.nfnum 
 LEFT JOIN zzcustmon z       ON z.sales_order   = g.sales_order 
 where c.dt_exp is null and d.nfnum is not null and z.delivery is not null)
  • SUM(REPLACE(valor,',','.')) that valor is an alias or field name? if it is alias, I believe it will give execution error

  • 1

    It is an alias that is within the second select. It is not giving error, because I have already tested and implemented in the system. Before testing I also thought it would give error but it did not work. It worked correctly.

Browser other questions tagged

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