How to query for get customer account status in SQL Server? -


i have these tables in database:

*custmrstble: (custid: pk,int) - (name: varchar(20)) ============= custid      name ________________ 1           sam  2           tom   productstble: (prodid: pk,int) - (prodname: varchar(20)) - (soldprice: money) =========== prodid   prodname  soldprice ____________________________ 1        biscuits   20 2        butter     30 3        milk       10  orderstbl:  (orderid: pk,int) - (orderdate: smalldatetime) - (custid: fk,int)  ========== orderid   orderdate   custid ____________________________ 1         2013/4/2     1 2         2013/4/2     2 3         2013/4/3     1 orderdetails:  (orderdetailsid : pk,int)- (orderid: fk,int) - (prodid: fk,int) - (qntty: int) ============= orderdetailsid   orderid  prodid  qntty _______________________________________ 1                 1        1      2 2                 1        2      1 3                 1        3      2 4                 2        1      5 5                 3        1      1 cashmoventstble:  (cashid : pk,int)- (orderid: fk,int) - (cashdate : smalldatetime) - (cashvalue money) ================ cashid   orderid  cashdate  cashvalue  _____________________________________ 1        1       2013/4/2   30 2        2       2013/4/2   100 3        1       2013/4/5   20 4         

so, want query return customers status this:

name    totalpurchase     totalpayments     _______________________________________ sam     110                50 tom     100                100 

totalpurchase = sum(qntty) * soldprice ----> purchase every customer
totalpayments = sum(cashvalue ) -----> payments every customer

but not sure how it. please me achieve this. thanks.

select  c.name     ,totalpurchase = (         select sum(p.soldprice * d.qntty)         orderdetails d          inner join  orderstbl o              on d.orderid = o.orderid         inner join  productstbl p              on p.prodid = d.prodid         o.custid = c.custid )     ,totalpayments =  (         select  sum(cm.cashvalue)            cashmoventstble cm         inner join orderstbl o             on o.orderid = cm.orderid          o.custid = c.custid)    custmrstble c 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -