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 |
|
abcd
Yak Posting Veteran
92 Posts |
Posted - 2009-04-08 : 04:45:23
|
| HI all this is my query...DECLARE @FromMonthId int DECLARE @FromYearId int DECLARE @ToMonthId int DECLARE @ToYearId intSET @FromMonthId =3SET @FromYearId= 2008 SET @ToMonthId= 3SET @ToYearId= 2009DECLARE @fromdate datetime, @todate datetime select @fromdate=convert(varchar,@FromYearID)+'-'+convert(varchar,@FromMonthID)+'-1' select @todate=convert(varchar,@ToYearID)+'-'+convert(varchar,@ToMonthID)+'-1' --SELECT--MonthAtten.USERIDDECLARE @a intDECLARE @b int set @a=(SELECT sum(MonthAtten.DaysInMonth) from Staff_MonthAttendence as MonthAtten where convert(datetime,convert(varchar,MonthAtten.YearID)+'-'+convert(varchar,MonthAtten.MonthID)+'-1') between @fromdate and @todate ) set @b=(SELECT Count(HolidayDate) from SurbhiCalender WHERE HolidayDate between @fromdate and @todate)SELECTMonthAtten.UserId,sum(Present) as Present,sum(Absent) AS Absent,@a AS TotalDays,@b AS Holidays,(@a-@b) as workingdaysFROM Staff_MonthAttendence AS MonthAttenWHERE convert(datetime,convert(varchar,MonthAtten.YearID)+'-'+convert(varchar,MonthAtten.MonthID)+'-1') between @fromdate and @todate group byMonthAtten.USERID--,MonthAtten.present--,MonthAtten.absentAnd the small portion of the total result is..userid,present,absent,totaldays,holidays,workingdays25 317.5 16.5 20700 59 2064126 347.5 17.5 20700 59 2064127 346 19 20700 59 20641As i am passing the value of date between march 2008 to april 2009 as declared in the starting of the query..But we can never have so many days in a year...pls do help thw column of total days have some problem... |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-04-08 : 04:52:25
|
| You should be doing COUNT hereset @a=(SELECT sum(MonthAtten.DaysInMonth) from Staff_MonthAttendence as MonthAtten where convert(datetime,convert(varchar,MonthAtten.YearID)+'-'+convert(varchar,MonthAtten.MonthID)+'-1') between @fromdate and @todate )Mangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-04-08 : 04:54:25
|
| even select datediff(dd,@fromdate,@todate)will give you the number of days between 2 datesMangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-08 : 04:55:58
|
[code]DECLARE @FromMonthId int, @FromYearId int, @ToMonthId int, @ToYearId intSELECT @FromMonthId = 3, @FromYearId = 2008, @ToMonthId = 3, @ToYearId = 2009DECLARE @fromdate datetime, @todate datetime, @b intSELECT @fromdate = convert(varchar(4), @FromYearID) + '-' + convert(varchar(2), @FromMonthID) + '-01', @todate = convert(varchar(4), @ToYearID) + '-' + convert(varchar(2), @ToMonthID) + '-01'SELECT @b = COUNT(HolidayDate)FROM SurbhiCalenderWHERE HolidayDate BETWEEN @fromdate AND @todateSELECT UserId, sum(Present) as Present, sum(Absent) AS Absent, SUM(DaysInMonth) AS TotalDays, @b AS Holidays, SUM(DaysInMonth) - @b as workingdaysFROM Staff_MonthAttendenceWHERE 100 * YearID + MonthID BETWEEN 100 * @FromYearId + @FromMonthId AND 100 * @ToYearId + @ToMonthIdGROUP BY USERID[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-08 : 04:58:17
|
Your REAL problem is that you do not account for UserID when SUMming DaysInMonth... E 12°55'05.63"N 56°04'39.26" |
 |
|
|
abcd
Yak Posting Veteran
92 Posts |
Posted - 2009-04-08 : 05:00:38
|
| Peso these are the results of your solution....and total days cant be such a huge number...UserId Present Absent TotalDays Holidays workingdays----------- ---------------------- ---------------------- ----------- ----------- -----------25 317.5 16.5 20700 113 2058726 347.5 17.5 20700 113 2058727 346 19 20700 113 2058728 353 12 20700 113 2058729 363 2 20700 113 2058730 305.5 28.5 20700 113 2058731 332.5 32.5 20700 113 20587 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-08 : 05:04:23
|
Try again (see post made 04/08/2009 : 04:55:58 ), after reading my explanation above posted 04/08/2009 : 05:00:38. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
abcd
Yak Posting Veteran
92 Posts |
Posted - 2009-04-08 : 05:08:37
|
| Peso Sir i did this after your suggestion but stillllll....:(:(DECLARE @FromMonthId int DECLARE @FromYearId int DECLARE @ToMonthId int DECLARE @ToYearId intSET @FromMonthId =1SET @FromYearId= 2008 SET @ToMonthId= 12SET @ToYearId= 2008DECLARE @fromdate datetime, @todate datetime select @fromdate=convert(varchar,@FromYearID)+'-'+convert(varchar,@FromMonthID)+'-1' select @todate=convert(varchar,@ToYearID)+'-'+convert(varchar,@ToMonthID)+'-1' --SELECT--MonthAtten.USERIDDECLARE @a intDECLARE @b int set @a=(datediff(dd,@fromdate,@todate) ) set @b=(SELECT Count(HolidayDate) from SurbhiCalender WHERE HolidayDate between @fromdate and @todate)SELECTMonthAtten.UserId,sum(Present) as Present,sum(Absent) AS Absent,@a AS TotalDays,@b AS Holidays,(@a-@b) as workingdaysFROM Staff_MonthAttendence AS MonthAttenWHERE convert(datetime,convert(varchar,MonthAtten.YearID)+'-'+convert(varchar,MonthAtten.MonthID)+'-1') between @fromdate and @todate group byMonthAtten.USERID--,MonthAtten.present--,MonthAtten.absentResults are..UserId Present Absent TotalDays Holidays workingdays----------- ---------------------- ---------------------- ----------- ----------- -----------25 258.5 16.5 335 47 28826 260.5 14.5 335 47 28827 264 11 335 47 28828 264 11 335 47 28829 274 1 335 47 28830 253.5 21.5 335 47 28831 257.5 17.5 335 47 28832 242 33 335 47 28833 268.5 6.5 335 47 288 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-08 : 05:10:49
|
That is NOT my suggestion.THIS is my suggestionDECLARE @FromMonthId int, @FromYearId int, @ToMonthId int, @ToYearId intSELECT @FromMonthId = 3, @FromYearId = 2008, @ToMonthId = 3, @ToYearId = 2009DECLARE @fromdate datetime, @todate datetime, @b intSELECT @fromdate = convert(varchar(4), @FromYearID) + '-' + convert(varchar(2), @FromMonthID) + '-01', @todate = convert(varchar(4), @ToYearID) + '-' + convert(varchar(2), @ToMonthID) + '-01'SELECT @b = COUNT(HolidayDate)FROM SurbhiCalenderWHERE HolidayDate BETWEEN @fromdate AND @todateSELECT UserId, sum(Present) as Present, sum(Absent) AS Absent, SUM(DaysInMonth) AS TotalDays, @b AS Holidays, SUM(DaysInMonth) - @b as workingdaysFROM Staff_MonthAttendenceWHERE 100 * YearID + MonthID BETWEEN 100 * @FromYearId + @FromMonthId AND 100 * @ToYearId + @ToMonthIdGROUP BY USERID E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-08 : 05:13:41
|
The datediff approach is wrong because you miss all days in ending month except the 1st of ending month. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-04-08 : 05:34:30
|
| datediff is on right path,just need complete information what abcd is trying - just doSELECT @A=DATEDIFF(dd,@fromdate,dateadd(mm,1,@todate))-1SUM(DaysInMonth) is the reason for getting huge number 20700.Better way you should provide the table structure.what is in the column DaysInMonth?Mangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-04-08 : 05:44:10
|
| again if you want to include the complete month for the @ToMonth then this will give you wrong resultSELECT @b = COUNT(HolidayDate)FROM SurbhiCalenderWHERE HolidayDate BETWEEN @fromdate AND @todateMangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
abcd
Yak Posting Veteran
92 Posts |
Posted - 2009-04-08 : 06:02:31
|
| Thank you mangal and peso...finally i got it right,,,DECLARE @FromMonthId int DECLARE @FromYearId int DECLARE @ToMonthId int DECLARE @ToYearId int SET @FromMonthId =1SET @FromYearId= 2008 SET @ToMonthId= 12SET @ToYearId= 2008DECLARE @fromdate datetime, @todate datetime select @fromdate=convert(varchar,@FromYearID)+'-'+convert(varchar,@FromMonthID)+'-1' select @todate=convert(varchar,@ToYearID)+'-'+convert(varchar,@ToMonthID)+'-1' --SELECT --MonthAtten.USERIDDECLARE @a intDECLARE @b int set @A=DATEDIFF(dd,@fromdate,dateadd(mm,1,@todate))-1 set @b=(SELECT Count(HolidayDate) from SurbhiCalender WHERE HolidayDate between @fromdate and @todate) SELECT MonthAtten.UserId,sum(Present) as Present,sum(Absent) AS Absent,@a AS TotalDays,@b AS Holidays,(@a-@b) as workingdaysFROM Staff_MonthAttendence AS MonthAttenWHERE convert(datetime,convert(varchar,MonthAtten.YearID)+'-'+convert(varchar,MonthAtten.MonthID)+'-1') between @fromdate and @todate group byMonthAtten.USERID--,MonthAtten.present--,MonthAtten.absent |
 |
|
|
|
|
|
|
|