0
---------------colete--------------------
| id | id_comanda  | status | id_lista |
----------------------------------------
|    | 21775       | 0      | 3820     |
----------------------------------------
|    | 21776       | 0      | 3820     |
----------------------------------------
|    | 21777       | 0      | 3820     |
----------------------------------------


-----comenzi--------
| id  | taxComanda |
--------------------
|21775| 16.00      |
--------------------
|21776| 00.00      |
--------------------
|21777| 16.00      |
--------------------

I want to get SUM from column taxaComand from table comenzi by making the selection from column id_lista from table colete, so the SUM at the end to be 32.00

This is what i have, but is not good.:

SELECT a.id_comnda AS a_id_comanda, a.id_lista AS a_id_lista,
b.id AS b_id, b.taxaComanda AS b_taxaComanda
(SELECT sum(taxaComanda) FROM comenzi WHERE b.id = a.id_comanda AS totalTaxaComanda)
FROM colete a
INNER JOIN comenzi b ON b.id = a.id_comanda
WHERE a.id_lista = 3820
GROUP BY a.id_comanda
3
  • What is your expected output ? Commented Nov 16, 2016 at 15:22
  • taxaComanda: 16.00 + 00.00 + 16.00 = 32.00 Commented Nov 16, 2016 at 15:24
  • Why? How does it make sense? Commented Nov 16, 2016 at 15:30

2 Answers 2

1

Based on your comment I see no other answer than this :

SELECT SUM(b.taxComanda )
FROM colete a
INNER JOIN comenzi b ON b.id = a.id_comanda
WHERE a.id_lista = 3820
Sign up to request clarification or add additional context in comments.

1 Comment

you are a god among mortals :D This mini query works, now i need to apply it in my query is slightly more complex. Thank you!
0

Using subquery and grouping id_lista:

SELECT id_lista, SUM(taxComanda ) as result,
FROM(
     SELECT id_comanda  , status , id_lista, taxComanda 
     from colete
     left join comenzi  on colete.id = comenzi.id_comanda)as sq
group by sq.id_lista

1 Comment

Thak you @tyro, i will try this to! Great people here!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.