Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
tunc260
Starting Member
5 Posts |
Posted - 2009-05-29 : 08:03:56
|
| hello, i m newbie for mssqli try to write a sql querySELECT LNGCUSTCODE, (SUM(DBLGROSSAMOUNT) - SUM(DBLDISCOUNT)) AS SALESFROM TBLINVOICE TF , TBLCUSTOMER AS TMWHERE TF.LNGCUSTCODE = TM.LNGCUSTCODE AND (TF.TXTDATE >= '20090401') AND (TF.TXTTARIH <= '20090430')GROUP BY LNGCUSTCODEHAVING (SUM(DBLGROSSAMOUNT) - SUM(DBLDISCOUNT))> 2000some customers have some branches. that branch information save in another table ( TBLBRANCH )TBLBRANCH is like thatCUSTCODE BRANCHCODE100 101100 102100 103100 is the main customer101,102,103 are branches of it.soi want to write a query list all customers' sales as combining branches with main. our government want a report like above.thanks for helping. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-29 : 08:48:45
|
what's LNGCUSTCODE in table TBLINVOICE ? is it the main code or branch code ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
tunc260
Starting Member
5 Posts |
Posted - 2009-05-29 : 09:00:37
|
| i meant main code with main customer code, this customer have some branches in the city. actualy these , main customer code and its branches are same legally.for example, TBLINVOICE is like thatLNGCUSTCODE DDBLGROSSAMOUNT DBLDISCOUNT100 500 2101 100 3151 150 10102 200 0121 200 10legally 100,101,102 are the same customer. i should combine them when calculating sales.according to these table , i should get this report as a result of the query we build.customer sales 100 795 which gotten from ((500+100+200)-5)151 140 ( this customer doesnt have any branch so ( 150-10 )121 91i should compute customers' sales by combining its branchesthank u very much for helping. i can clarify more if u need |
 |
|
|
tunc260
Starting Member
5 Posts |
Posted - 2009-05-29 : 09:03:08
|
| main customer code and branch customer are different. ( 100 is the main customer code, 101,102,103 are its branches ) |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-29 : 09:30:24
|
[code]SELECT TB.CUSTCODE, (SUM(DBLGROSSAMOUNT) - SUM(DBLDISCOUNT)) AS SALESFROM TBLINVOICE AS TF INNER JOIN TBLCUSTOMER AS TM ON TF.LNGCUSTCODE = TM.LNGCUSTCODE INNER JOIN TBLBRANCH AS TB ON TF.LNGCUSTCODE = TB. BRANCHCODEWHERE (TF.TXTDATE >= '20090401') AND (TF.TXTTARIH <= '20090430')GROUP BY TB.CUSTCODEHAVING (SUM(DBLGROSSAMOUNT) - SUM(DBLDISCOUNT))> 2000[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
tunc260
Starting Member
5 Posts |
Posted - 2009-05-29 : 10:25:35
|
| thank u very much for ur interest, i can make use of it, but it s not the result i want,cos result of ur query only show customers exists in TBLBRANCH table , other customers , for example, 151 (cust_code) couldnt be seen in the result of ur query. 151 doesnt have any branch. it doesnt exist in TBLBRANCH table.have a nice day |
 |
|
|
|
|
|
|
|