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 |
ppatel112
Starting Member
35 Posts |
Posted - 2013-03-10 : 21:28:48
|
Hi Folks,i am having the sql below:SELECT TOP (100) SAMINC.dbo.OESTATS.YR as YEAR , SUM(SAMINC.dbo.OESTATS.SALESAMTF) AS TotalSalesFROM SAMINC.dbo.OESTATS INNER JOIN dbo.Company ON SAMINC.dbo.OESTATS.LINVCUST = dbo.Company.Comp_IdCust COLLATE Latin1_General_BINGROUP BY SAMINC.dbo.OESTATS.YRthat generated the results below:YEAR TotalSales2019 21932.3602020 46448.040it is find in SQL, the software that i am using automatically adds a field called comp_companyid to it so the sql becomes SELECT TOP (100) SAMINC.dbo.OESTATS.YR AS YEAR, SUM(SAMINC.dbo.OESTATS.SALESAMTF) AS TotalSales, dbo.Company.Comp_CompanyIdFROM SAMINC.dbo.OESTATS INNER JOIN dbo.Company ON SAMINC.dbo.OESTATS.LINVCUST = dbo.Company.Comp_IdCust COLLATE Latin1_General_BINGROUP BY SAMINC.dbo.OESTATS.YR, dbo.Company.Comp_CompanyIdand the results are diffrent because of it as below:YEAR TOTALSales comp_companyid2020 11082.720 12282019 12575.520 12292020 2159.850 12292020 7973.050 12312020 1890.000 12322019 9356.840 12342020 10794.310 1234as it is a requirement to add the comp_companyid it completely distors my results that i am looking for.any way to acheive the first result by tweaking the second tsql?please advise |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-11 : 03:08:46
|
SELECT DISTINCT SAMINC.dbo.OESTATS.YR AS YEAR, SUM(SAMINC.dbo.OESTATS.SALESAMTF) OVER(PARTITION BY SAMINC.dbo.OESTATS.YR, dbo.Company.Comp_CompanyId) AS TotalSales, dbo.Company.Comp_CompanyIdFROM SAMINC.dbo.OESTATS INNER JOINdbo.Company ON SAMINC.dbo.OESTATS.LINVCUST = dbo.Company.Comp_IdCust COLLATE Latin1_General_BIN--Chandu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-11 : 07:17:01
|
quote: Originally posted by bandi SELECT DISTINCT SAMINC.dbo.OESTATS.YR AS YEAR, SUM(SAMINC.dbo.OESTATS.SALESAMTF) OVER(PARTITION BY SAMINC.dbo.OESTATS.YR, dbo.Company.Comp_CompanyId) AS TotalSales, dbo.Company.Comp_CompanyIdFROM SAMINC.dbo.OESTATS INNER JOINdbo.Company ON SAMINC.dbo.OESTATS.LINVCUST = dbo.Company.Comp_IdCust COLLATE Latin1_General_BIN--Chandu
it should beSELECT DISTINCT SAMINC.dbo.OESTATS.YR AS YEAR, SUM(SAMINC.dbo.OESTATS.SALESAMTF) OVER(PARTITION BY SAMINC.dbo.OESTATS.YR) AS TotalSales, dbo.Company.Comp_CompanyIdFROM SAMINC.dbo.OESTATS INNER JOINdbo.Company ON SAMINC.dbo.OESTATS.LINVCUST = dbo.Company.Comp_IdCust COLLATE Latin1_General_BIN ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|