Click here to Skip to main content
15,169,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have a table as below.


SQL
Id  FromPlace ToPlace TotalSend      Date             TotalPrice

1     A       B         3           1/Mar/2015               2000
2     A       C         4           1/Mar/2015               3000
3     B       A         3           3/Mar/2015               1000
4     B       C         6           3/Mar/2015               1500
5     A       B         2           3/Mar/2015                600
6     C       B         6           8/Mar/2015               3000
7     C       A         2           10/Mar/2015              500
8     B       A         1           11/Mar/2015              600
9     B       C         2           14/Mar/2014              700           


Output Needed as below
SQL
Place TotalSendTo TotalSendFrom  TotToFrom  TotPriceTo TotPriceFrom TotalPriceFrom

A         9           6              15         5600       1100           6700
B         12          11             23         3800       5600           9400
C         8           12             20         3500       5200           8700  



I need to sum Totalsend and TotalPrice as per Place (A,B,C)
Posted

1 solution

Try:
SQL
SELECT f.FromPlace AS Place,
       f.TotalSendFrom,
       t.TotalSendTo,
       f.TotalSendFrom + t.TotalSendTo AS TotToFrom,
       f.TotPriceFrom,
       t.TotPriceTo,
       t.TotPriceTo + f.TotPriceFrom As TotalPrice
FROM (SELECT FromPlace, SUM(TotalSend) AS TotalSendFrom, SUM(TotalPrice) AS TotPriceFrom
      FROM MyTable GROUP BY FromPlace) f
JOIN (SELECT ToPlace, SUM(TotalSend) AS TotalSendTo, SUM(TotalPrice) AS TotPriceTo
      FROM MyTable GROUP BY ToPlace) t
ON t.ToPlace = f.FromPlace
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900