How to group by using 2 columns in mysql -


i have query in mysql database

select tbl_lab_reservations.full_desc, serial_number, rsvn_owner, reservation_id,  sum((select datediff(reservation_date_end, reservation_date_start)+1)) totalnumberofdaysreserve tbl_lab_reservations  join tbl_lab_assets on tbl_lab_assets.id = tbl_lab_reservations.lab_id  tbl_lab_reservations.full_desc = 'wmhd (masthead amplifier)'  , tbl_lab_reservations.asset_status = 'idle'  group rsvn_owner  order `tbl_lab_reservations`.`rsvn_owner` asc 

and result

query result

the result correct base on query not want. base on data save on database, there should rsvn_owner named payak problem payak uses serial_numbers used because grouped serial number totalnumberofdaysreserve of payak , added makes wrong. want query grouped serial_number if belong different rsvn_owner should create row.

hope undestands , can me this. in advance

so try group serial number , after owner? :)

group serial_number, rsvn_owner 

like this

select   tbl_lab_reservations.full_desc, serial_number, rsvn_owner, reservation_id,   sum((select datediff(reservation_date_end, reservation_date_start)+1)) totalnumberofdaysreserve tbl_lab_reservations join tbl_lab_assets on tbl_lab_assets.id = tbl_lab_reservations.lab_id tbl_lab_reservations.full_desc = 'wmhd (masthead amplifier)'   , tbl_lab_reservations.asset_status = 'idle' group serial_number, rsvn_owner order `tbl_lab_reservations`.`rsvn_owner` 

as syntax tell (see near comma)

[group {col_name | expr | position}       [asc | desc], ... [with rollup]] 

we can this

https://dev.mysql.com/doc/refman/5.0/en/select.html


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -