Sum amount by type of food in SQL Server -
i'm new sql, i'm using sql server 2008.
in database have 3 tables
- table
type
- table
invoice
- table
item
this sql command sum amount:
select tbl_type.type, ((tbl_item.price * qty) - (tbl_item.price * qty * discount)) totalafter, tbl_inv.dateinv tbl_type, tbl_item, tbl_inv tbl_inv.dateinv = '2013-03-26 00:00:00' , tbl_inv.id = tbl_item.id_invoice , tbl_type.id = tbl_item.id_type order tbl_inv.dateinv;
so when run query return
chicken 46.000000 2013-03-26 00:00:00.000 meat 3.000000 2013-03-26 00:00:00.000 chicken 69.000000 2013-03-26 00:00:00.000 chicken 46.000000 2013-03-26 00:00:00.000 chicken 69.000000 2013-03-26 00:00:00.000 meat 4.500000 2013-03-26 00:00:00.000
but how this?
chicken 230.000000 2013-03-26 00:00:00.000 meat 7.500000 2013-03-26 00:00:00.000
i error
column 'tbl_item.price' invalid in select list because not contained in either aggregate function or group clause.
when use group tbl_type.type
in sql
thanks help.
select tbl_type.type,sum((tbl_item.price * qty) - (tbl_item.price * qty * discount)) totalafter ,tbl_inv.dateinv tbl_type,tbl_item,tbl_inv tbl_inv.dateinv = '2013-03-26 00:00:00' , tbl_inv.id=tbl_item.id_invoice , tbl_type.id=tbl_item.id_type group tbl_type.type,tbl_inv.dateinv order tbl_inv.dateinv,tbl_type.type;
Comments
Post a Comment