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 |
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2007-03-26 : 15:02:25
|
| Hi All,I have a table called Table1 with the following fields and types.ZoneName(nvarchar),OutletID(nvarchar),CatID(nvarchar),SaleDate(datetime),TotalUnits(decimal),DollarSales(decimal),Profit(decimal).Using the above Table1 I want to get the Sum(TotalUnits),Sum(DollarSales),Sum(Profit) per zonename, per saledate, and per catid, where;its same ZoneName, same CatID, and same OutletIDThat is in my select Query i want the following fields:ZoneName,CatID,SaleDate,Sum(TotalUnits),Sum(DollarSales),Sum(Profit)And the join should be on the following fields:ZoneNameCatIDOutletIDBased on above criteria how can I create a self join (with a possible sub query) ?How can I acieve this in TSQL?Please let me know.Thanks a million..... |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2007-03-26 : 15:28:04
|
You don't want a join, you want grouping, something like thisSELECT ZoneName, CatID, dateadd(dd, 0, datediff(dd, 0, getdate())) AS SaleDate , Sum(TotalUnits) AS SumTotalUnits, Sum(DollarSales) AS SumDollarSales, Sum(Profit) AS SumProfitFROM Table1GROUP BY ZoneName, CatID, dateadd(dd, 0, datediff(dd, 0, getdate()) |
 |
|
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2007-03-26 : 15:29:54
|
| No, it doesn't give what I am looking for.I want to create a self join on Table1 to return the following fields: ZoneName,CatID,SaleDate,Sum(TotalUnits),Sum(DollarSales),Sum(Profit)Where its same: (that is the join should be on the following):ZoneNameCatIDOutletIDHow can I achieve this in TSQL?Someone please let me know this as soon as possible.Thanks a million..... |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2007-03-26 : 15:44:58
|
quote: Originally posted by zeeshan13 No, it doesn't give what I am looking for.I want to create a self join on Table1 to return the following fields: ZoneName,CatID,SaleDate,Sum(TotalUnits),Sum(DollarSales),Sum(Profit)Where its same: (that is the join should be on the following):ZoneNameCatIDOutletIDHow can I achieve this in TSQL?Someone please let me know this as soon as possible.Thanks a million.....
Doesn't make sense...read the hint link in my sig and post what itt asks forBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
|
|
|
|
|