Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi all,Got a problem here, I am not quite familiar yet with the sql group by clause. As an illustration, I have a table with column StoreName and Sales.StoreNameSalesDFA_Main 50DFA_Main 50DFA_Branch 60DFA_Branch 60DFA_OtherBranch 10MMDA_Main 50MMDA_Main 50MMDA_Branch 30MMDA_Branch 30In my understanding if we are going to group this table by StoreName the result will be:StoreNameSalesDFA_Main 100DFA_Branch 120DFA_OtherBranch 10MMDA_Main 100MMDA_Branch 60But what would be the sql statement to produce an output like this:StoreNameSalesDFA 230MMDA 160Is it possible for me to do that? Thanks for the help in advance.===============JSC0624===============
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-05-10 : 04:25:21
Try like this:-
SELECT t.StoreName,SUM(t.Sales) AS SalesFROM(SELECT CASE WHEN CHARINDEX('_',StoreName) > 0 THEN LEFT(StoreName ,CHARINDEX('_',StoreName)-1) ELSE StoreName END AS StoreName, SalesFROM YoutTable)tGROUP BY t.StoreName