sqlite - distinct sum does not distinct values -


i have 2 tables, reservations , articles:

reservations

------------------------------ id     |   name   |     city | ------------------------------  1     |  mike    | stockholm  2     |  daniel  | gothenburg  2     |  daniel  | gothenburg  3     |  andre   | gothenburg (majorna) 

articles

------------------------------------------------------------- articleid    |    name       |    amount |    reservationid |  ------------------------------------------------------------- 10           |   coconuts    |    1      |    1          10           |   coconuts    |    4      |    2      11           |   apples      |    2      |    2 12           |   oranges     |    2      |    3 

i want select articles name , sum of articles.amount per articles.articleid , reservations.city.

my code:

select distinct r.id,a.name articlename,        sum(a.amount) articlesamount,        substr(r.city,1,3) tocityname  reservations r  inner join articles        on r.id = a.reservationid   a.name <> ''  group tocityname,a.articleid,a.name  order tocityname asc 

this gives me following result:

id | articlename | articlesamount | tocityname  2  |  coconuts   |   8            |   got  2  |  apples     |   4            |   got  3  |  oranges    |   2            |   got  1  |  coconuts   |   1            |   sto  

but want:

id | articlename | articlesamount | tocityname  2  |  coconuts   |   4            |   got  2  |  apples     |   2            |   got  3  |  oranges    |   2            |   got  1  |  coconuts   |   1            |   sto  

help appreciated, , explanation please :)

fiddle

have @ sqlfiddle

code:

select distinct r.id,a.name articlename,        sum(distinct a.amount) articlesamount,        substr(r.city,1,3) tocityname  reservations r  inner join articles        on r.id = a.reservationid   a.name <> ''  group tocityname,a.articleid,a.name  order tocityname asc 

you want ensure sum amount distinct number of times appears per group.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -