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 |
|
SaraDob
Starting Member
3 Posts |
Posted - 2009-01-07 : 15:29:41
|
| Hi all,I'm a first time forum user and a newbie to SQL queries.I want to write a Sql Query to Display Sales dataHere is brief description:I have Three tables1)SalesUser table:SalesUserID FirstName lastName=====================================1 John Smith2 rob brownYearly Forecast table:SalesUSerId Financial Year Target==========================================1 2008-2009 2500002 2008-2009 450000Monthlybooking tableSalesUSerId Month Year ActualBooking=============================================1 1 2009 12001 10 2008 25001 11 2008 30001 12 2008 50001 2 2007 4002 10 2008 10002 11 2008 20002 12 2008 500The compnay i write this application for ,follows a financial year from Oct 1 -Sept 30 th next year (Eg: Oct 1 st 2008 to Sept 30th 2009)I have written this query which has to get the Total(Actualbooking) as YTD ,till date I want the output like this:SalesUSerid Lastname FirstName YTD FinancialYear==========================================================1 John Smith 11700 2008-20092 rob brown 3500 2008-2009But i get a query for each month in MonthlybookingHere is the query:select SalesUserId,LastName,FirstName,YTD,TargetAmountfrom(SELECT SalesUsers.SalesUserId,SalesUsers.LastName,SalesUsers.FirstName,SUM(MonthlyBooking.ActualBooking) AS YTD, YearlyForecast.TargetAmountFROM SalesUsers INNER JOINMonthlyBooking ON SalesUsers.SalesUserId = MonthlyBooking.SalesUserId INNER JOINYearlyForecast ON SalesUsers.SalesUserId = YearlyForecast.SalesUserId Where MonthlyBooking.Month In ('10','11','12') AND MonthlyBooking.Year=convert(varchar(4),year(dateadd(yy,-1,getdate()))) AND YearlyForecast.TargetYear='2008-2009' Group BY SalesUsers.SalesUserId, SalesUsers.LastName,SalesUsers.FirstName,MonthlyBooking.ActualBooking,YearlyForecast.TargetAmountunion allSELECT SalesUsers.SalesUserId,SalesUsers.LastName,SalesUsers.FirstName,SUM(MonthlyBooking.ActualBooking) AS YTD, YearlyForecast.TargetAmountFROM SalesUsers INNER JOINMonthlyBooking ON SalesUsers.SalesUserId = MonthlyBooking.SalesUserId INNER JOINYearlyForecast ON SalesUsers.SalesUserId = YearlyForecast.SalesUserId Where MonthlyBooking.Month Not In ('10','11','12') AND MonthlyBooking.Year=year(getdate()) AND YearlyForecast.TargetYear='2008-2009' Group BY SalesUsers.SalesUserId, SalesUsers.LastName,SalesUsers.FirstName,MonthlyBooking.ActualBooking,YearlyForecast.TargetAmount)as salesdataPlease help!!!Please be patient , as i'm very new to SQL query |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2009-01-07 : 23:54:12
|
check this-- Prepare sample dataDECLARE @SalesUsers TABLE(SalesUserID INT, FirstName VARCHAR(100), LastName VARCHAR(100))DECLARE @YearlyForecast TABLE(SalesUSerId INT, TargetYear VARCHAR(100), TargetAmount INT)DECLARE @Monthlybooking TABLE (SalesUSerId INT, [Month] INT, [Year] INT, ActualBooking INT)INSERT INTO @SalesUsers SELECT 1, 'John', 'Smith'UNION ALL SELECT 2, 'rob', 'brown'INSERT INTO @YearlyForecast SELECT 1, '2008-2009', 250000UNION ALL SELECT 2, '2008-2009', 450000INSERT INTO @Monthlybooking SELECT 1, 1, 2009, 1200UNION ALL SELECT 1, 10, 2008, 2500UNION ALL SELECT 1, 11, 2008, 3000UNION ALL SELECT 1, 12, 2008, 5000UNION ALL SELECT 1, 2, 2007, 400UNION ALL SELECT 2, 10, 2008, 1000UNION ALL SELECT 2, 11, 2008, 2000UNION ALL SELECT 2, 12, 2008, 500-- Prepare Fiscl Year tableDECLARE @FiscalYear TABLE (YearId INT IDENTITY(1, 1), FromDate DATETIME, ToDate DATETIME)INSERT INTO @FiscalYearSELECT DATEADD(YEAR, Number, '10/01/1990'), DATEADD(YEAR, Number + 1, '9/30/1990')FROM master..spt_valuesWHERE type = 'p' AND number <= 100-- Show desired outputSELECT SU.SalesUSerId , SU.FirstName , SU.LastName , SUM(MB.ActualBooking) AS 'YTD' , YF.TargetYear , YF.TargetAmountFROM @SalesUsers SUINNER JOIN @YearlyForecast YF ON YF.SalesUSerId = SU.SalesUSerIdINNER JOIN @Monthlybooking MB ON MB.SalesUSerId = SU.SalesUSerIdINNER JOIN @FiscalYear FY ON CONVERT(VARCHAR(10), MB.[Month]) + '/01/' + CONVERT(VARCHAR(10), MB.[Year]) BETWEEN FY.FromDate AND FY.ToDateGROUP BY SU.SalesUSerId, SU.FirstName, SU.LastName, YF.TargetYear, YF.TargetAmount, FY.YearId "There is only one difference between a dream and an aim.A dream requires soundless sleep to see,whereas an aim requires sleepless efforts to achieve..!!" |
 |
|
|
SaraDob
Starting Member
3 Posts |
Posted - 2009-01-09 : 10:03:06
|
| Thanks PeterNeo for replying and willing assistance.Your query might be right.But i dont have Fiscal Year table.I have to manage to write this query with only three tables.Any further thoughts.? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-09 : 10:11:12
|
quote: Originally posted by SaraDob Thanks PeterNeo for replying and willing assistance.Your query might be right.But i dont have Fiscal Year table.I have to manage to write this query with only three tables.Any further thoughts.?
fiscal table is actually a temporary table peterneo used to get fiscal dates of year. it uses master..spt_values which is an internal count table used by sql server so you can use it without any problem. |
 |
|
|
SaraDob
Starting Member
3 Posts |
Posted - 2009-01-14 : 16:08:06
|
| Thanks PeterNeo for the perfect query.and thanks visakh16 for helping understand better |
 |
|
|
|
|
|
|
|