| Author |
Topic |
|
ranganaath
Starting Member
18 Posts |
Posted - 2011-02-13 : 15:22:53
|
| i am having following tablestblSales ------> party,qty,invoiceno etctblIncentive --> party,fromCBS,ToCBS,Amount (each party may have more than 1 rows )i want following out putparty,sum(qty) from tblSales and Amount from tblIncentive where sum(qty) between fromcbs and tocbs and tblsales.party=tblincentive.partythanks in advanceranganath |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-02-13 : 23:43:56
|
| come up with some sample data for tblSales and tblIncentive along with the desired output format in light of your two tables data. this will help us to help you out accordingly..CheersMIK |
 |
|
|
ranganaath
Starting Member
18 Posts |
Posted - 2011-02-14 : 02:24:35
|
| tblSalesParty========Qty=========InvoiceNo=========ABC==========10===========1================ABC==========20===========2================XYZ==========15===========101==============XYZ==========10===========123==============XYZ==========12===========134==============tblIncentiveparty========FromCBS=====ToCBS=======AmountABC==========10==========50==========100ABC==========51==========1000========200XYZ==========10==========50==========50XYZ==========51==========1000========100Out putParty=====sum(QTY)======fromCBS====ToCBS====AmountABC=======30============10=========50=======100XYZ=======37============10=========50=======50thank youranganath |
 |
|
|
chris_n_osborne
Starting Member
34 Posts |
Posted - 2011-02-14 : 09:16:49
|
| [code]CREATE TABLE tblSales(Party NVARCHAR(3),Qty INTEGER,InvoiceNo INTEGER)CREATE TABLE tblIncentive(Party NVARCHAR(3),FromCBS INTEGER,ToCBS INTEGER,Amount INTEGER)INSERT INTO tblSales(Party, Qty, InvoiceNo)SELECT 'ABC', 10, 1 UNIONSELECT 'ABC', 20, 2 UNIONSELECT 'XYZ', 15, 101 UNIONSELECT 'XYZ', 10, 123 UNIONSELECT 'XYZ', 12, 134INSERT INTO tblIncentive(Party, FromCBS, ToCBS, Amount)SELECT 'ABC', 10, 50, 100 UNIONSELECT 'ABC', 51, 1000, 200 UNIONSELECT 'XYZ', 10, 50, 50 UNIONSELECT 'XYZ', 51, 1000, 100go SELECT TS1.Party ,TS1.Qty ,MIN(TI1.FromCBS) AS FromCBS ,MIN(TI1.ToCBS) AS ToCBS ,MIN(TI1.Amount) AS Amount FROM (SELECT TS01.Party ,SUM(TS01.Qty) AS Qty FROM tblSales AS TS01 GROUP BY TS01.Party) AS TS1 INNER JOIN tblIncentive AS TI1 ON TS1.Party = TI1.PartyGROUP BY TS1.Party ,TS1.QtyParty Qty FromCBS ToCBS AmountABC 30 10 50 100XYZ 37 10 50 50[/code] |
 |
|
|
ranganaath
Starting Member
18 Posts |
Posted - 2011-02-14 : 13:17:29
|
| thanks for reply here if i insert one more row in tblSalesINSERT INTO tblSales(Party, Qty, InvoiceNo)SELECT 'ABC', 90, 1then my out put should beParty====Qty====FromCBS====ToCBS====AmountABC======120====51=========1000=====200XYX======37=====10=========50=======50but in your query it is showingfromcbs ===ToCBS=Amount10=========50====100i want the tblIncentive.Amount where sum(tblsales.qty) between tblIncentive.FromCBS and tblIncentive.ToCBSranganath |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2011-02-14 : 13:32:36
|
[code]DECLARE @tblSales TABLE(Party NVARCHAR(3),Qty INTEGER,InvoiceNo INTEGER)DECLARE @tblIncentive TABLE (Party NVARCHAR(3),FromCBS INTEGER,ToCBS INTEGER,Amount INTEGER)INSERT INTO @tblSales(Party, Qty, InvoiceNo)SELECT 'ABC', 10, 1 UNIONSELECT 'ABC', 20, 2 UNIONSELECT 'XYZ', 15, 101 UNIONSELECT 'XYZ', 10, 123 UNIONSELECT 'XYZ', 12, 134 UNIONSELECT 'ABC', 90, 1INSERT INTO @tblIncentive(Party, FromCBS, ToCBS, Amount)SELECT 'ABC', 10, 50, 100 UNIONSELECT 'ABC', 51, 1000, 200 UNIONSELECT 'XYZ', 10, 50, 50 UNIONSELECT 'XYZ', 51, 1000, 100 SELECT ti.Party,SUM(Qty), ti.ToCBS , ti.FromCBS ,ti.AmountFROM @tblincentive AS ti join @tblSales AS ts on ti.Party = ts.PartyGROUP BY ti.Party,ti.ToCBS, ti.FromCBS,ti.AmountHAVING SUM(Qty)>=ti.FromCBS and SUM(Qty) <= ti.ToCBS[/code]/*resultsParty (No column name) ToCBS FromCBS AmountABC 120 1000 51 200XYZ 37 50 10 50*/ Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
ranganaath
Starting Member
18 Posts |
Posted - 2011-02-14 : 13:43:14
|
| thank You Siri got the result"Happy valentine's Day"ranganath |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2011-02-14 : 13:54:23
|
You are welcome. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
|
|
|