SQL pass thru query for current month

I'm having difficulty with this pass-thru query:

SELECT TOP 10 Count(Jobs.JobId) AS CountOfJobId, [Forename]+' '+ [Surname] AS Name
FROM Jobs
INNER JOIN Users ON Jobs.SoldBy = Users.UserId
WHERE((Users.PartTime)=False)
GROUP BY [Forename] & " " & [Surname]
HAVING (((Jobs.DateReceived) Between DateSerial(Year(GetDate()),Month(GetDate()),1) And DateSerial(Year(GetDate()),Month(GetDate()),31))
ORDER BY Count(Jobs.JobId) DESC;

Seems that GatDate and dateSerial do not like SQL pass through!

How can I get the data for the current month?

Thanks
lapchien


LapchienDirectorAsked:
Who is Participating?
 
Leigh PurvisConnect With a Mentor Database DeveloperCommented:
In fact - to further simplify

SELECT Count(Jobs.JobId) AS CountOfJobId, [Forename] + ' ' + [Surname] AS MyName
FROM Jobs INNER JOIN Users ON Jobs.SoldBy = Users.UserId
WHERE Users.PartTime = 0
AND MONTH(DateReceived) =  MONTH(GetDate())
AND YEAR(DateReceived) = YEAR(GetDate())
GROUP BY [Forename] + ' ' + [Surname]
ORDER BY Count(Jobs.JobId) DESC;

0
 
rockiroadsCommented:
SQL Server perhaps?

this may be what your after


http://www.webbedwonder.com/SQL-Server/Access-queries-to-TSQL.asp


0
 
rockiroadsCommented:
Remember Pass Thru queries, u are running sql native to the backend DB, not Access!
therefore DateSerial will not work

I dont know what your GetDate does, but I guess u need to convert that also

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
LapchienDirectorAuthor Commented:
Ok, a bit more straightforward - using the format function from Access - is there an equivalent SQL one?

SELECT Count(Jobs.JobId) AS CountOfJobId, [Forename] +' '+ [Surname] AS Name
FROM Jobs INNER JOIN dbo_Users ON dbo_Jobs.SoldBy = dbo_Users.UserId
WHERE((Users.PartTime)=False)
GROUP BY [Forename] & " " & [Surname]
HAVING Format(DateReceived,'mm')=Format(GetDate(),'mm')
ORDER BY Count(Jobs.JobId) DESC;

This should return all results for current month...

0
 
Leigh PurvisDatabase DeveloperCommented:
No - you've gotta work a bit harder for it.
Well - probably something like

SELECT TOP 10 Count(Jobs.JobId) AS CountOfJobId, [Forename] + ' ' + [Surname] AS MyName
FROM Jobs
INNER JOIN Users ON Jobs.SoldBy = Users.UserId
WHERE Users.PartTime=0
    AND Jobs.DateReceived Between
      CONVERT(DateTime, CONVERT(varChar,YEAR(GetDate()),4) + '-' + CONVERT(varChar, Month(GetDate()),2) + '-01')
    AND
      CONVERT(DateTime, CONVERT(varChar,YEAR(GetDate()),4) + '-' + CONVERT(varChar, Month(GetDate()),2) + '-31')
GROUP BY [Forename] + ' ' + [Surname]
ORDER BY Count(Jobs.JobId) DESC;

(There were other errors in there - Access creep overs :-)
0
 
rockiroadsCommented:
lets see, something like this perhaps. I guess u dont need to have in HAVING, stick it as part of your WHERE/AND clause

HAVING Format(DateReceived,'mm')=Format(GetDate(),'mm')

AND CONVERT(nvarchar, DATEPART(mm, DateReceived)) = CONVERT(nvarchar, DATEPART(mm, GetDate()))

Do u remember the datatype for integer? will be quicker than comparing nvarchar
still it should work hopefully

e.g.

SELECT Count(Jobs.JobId) AS CountOfJobId, [Forename] +' '+ [Surname] AS Name
FROM Jobs INNER JOIN dbo_Users ON dbo_Jobs.SoldBy = dbo_Users.UserId
WHERE((Users.PartTime)=False)
AND CONVERT(nvarchar, DATEPART(mm, DateReceived)) = CONVERT(nvarchar, DATEPART(mm, GetDate()))
GROUP BY [Forename] & " " & [Surname]
ORDER BY Count(Jobs.JobId) DESC;

0
 
rockiroadsCommented:
Its been a while since I last did T-SQL so bear with me
But now LPurvis is on the case, its gonna get sorted quicker :)

Lapchien, was that link any help to you?

0
 
rockiroadsCommented:
Leigh, tele is boring what with no footy on :(
Im getting a neckache watching the tennis, all Im doing is moving my head left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right, left, right,

well only when there is a good volley

with henman it was

left, left, left,  GAME!

0
 
LapchienDirectorAuthor Commented:
!

Invalid column name rom:

SELECT Count(Jobs.JobId) AS CountOfJobId, [Forename] +' '+ [Surname] AS Name
FROM Jobs INNER JOIN Users ON Jobs.SoldBy = Users.UserId
WHERE((Users.PartTime)=0)
AND CONVERT(nvarchar, DATEPART(mm, DateReceived)) = CONVERT(nvarchar, DATEPART(mm, GetDate()))
GROUP BY [Forename] & " " & [Surname]
ORDER BY Count(Jobs.JobId) DESC;

0
 
Leigh PurvisDatabase DeveloperCommented:
lol I didn't bother watching the Henman match Rocki - the Brazil Japan game was less of a foregone conclusion than *that* match :-)

My reworking of the SQL was effectively a direct translation of the Access code.
Rocki's take isn't a bad one at all... (i.e. I like it).
Just needs a little tweak ;-)

SELECT Count(Jobs.JobId) AS CountOfJobId, [Forename] + ' ' + [Surname] AS MyName
FROM Jobs INNER JOIN Users ON Jobs.SoldBy = Users.UserId
WHERE Users.PartTime = 0
AND CONVERT(int, DATEPART(mm, DateReceived)) = CONVERT(int, DATEPART(mm, GetDate()))
AND CONVERT(int, DATEPART(yy, DateReceived)) = CONVERT(int, DATEPART(yy, GetDate()))
GROUP BY [Forename] + ' ' + [Surname]
ORDER BY Count(Jobs.JobId) DESC;

If that runs OK it'll probably be quicker.
0
 
rockiroadsCommented:
comparing with this

SELECT Count(Jobs.JobId) AS CountOfJobId, [Forename] +' '+ [Surname] AS Name
FROM Jobs INNER JOIN dbo_Users ON dbo_Jobs.SoldBy = dbo_Users.UserId
WHERE((Users.PartTime)=False)
GROUP BY [Forename] & " " & [Surname]
HAVING Format(DateReceived,'mm')=Format(GetDate(),'mm')
ORDER BY Count(Jobs.JobId) DESC;


Have I a type with a fieldname somewhere? Can u double check the fields are correct?

Perhaps Leigh can shed some light


0
 
rockiroadsCommented:
Yea, I thought about adding the years but I thought I had better get the month one working first!
Cheers Leigh

0
 
rockiroadsCommented:
True about Henman, still tomorrow is the day Argentina v Germany. Tough one to call
Pissed off about Saturday though, I have to go to Melton Mowbray, have a meeting (not work) at 4pm. But its at some Polish club so hopefully there is a tele there.

Sorry about the off topic char Lapchien



0
 
Leigh PurvisDatabase DeveloperCommented:
(Not wishing to sound nob-headish here - but do any of the posts I've suggested actually fail? Just don't see that they've been tried yet? :-S )
0
 
rockiroadsCommented:
Ive gotta go. eSpeak later, unless Leigh's solved it!
0
 
LapchienDirectorAuthor Commented:
You guys are the best!  Just getting to grips with sql pass through queries here - they run soooo much quicker than access ones don't they?!

Will try the above tomorrow and dish out the points!

Thanks
Lap
0
 
LapchienDirectorAuthor Commented:
All looks good.  Just one thing - how would I amend them to return data for either the current week, or for the last 7 days?
0
 
rockiroadsConnect With a Mentor Commented:
Are u familiar with DateDiff? I believe that still works with T-SQL

DateDiff(DAY,YourDatefield,GetDate())

then I guess u can check if its greater than 7

Can't remember, if u try

select YourDatefield, DateDiff(DAY,YourDatefield,GetDate()) from table

then u know what the value will be and u can check accordingly
0
 
Leigh PurvisDatabase DeveloperCommented:
Current week:
SELECT Count(Jobs.JobId) AS CountOfJobId, [Forename] + ' ' + [Surname] AS MyName
FROM Jobs INNER JOIN Users ON Jobs.SoldBy = Users.UserId
WHERE Users.PartTime = 0
AND DATEPART(ww,DateReceived) =  DATEPART(ww,GetDate())
AND YEAR(DateReceived) = YEAR(GetDate())
GROUP BY [Forename] + ' ' + [Surname]
ORDER BY Count(Jobs.JobId) DESC;

Last 7 days:
SELECT TOP 10 Count(Jobs.JobId) AS CountOfJobId, [Forename] + ' ' + [Surname] AS MyName
FROM Jobs
INNER JOIN Users ON Jobs.SoldBy = Users.UserId
WHERE Users.PartTime=0
    AND Jobs.DateReceived Between
      DATEADD(dd, -7, GetDate())
    AND
      GetDate()
GROUP BY [Forename] + ' ' + [Surname]
ORDER BY Count(Jobs.JobId) DESC;

As always of course - beware date fields with time components.
0
 
LapchienDirectorAuthor Commented:
Thanks that's great - does the current week start on Sunday or Monday though?  I guess this is a convention?
0
 
rockiroadsCommented:
Sunday, I believe is the default but with Access and using Format (and Im sure its the same with SQL Server), u can use your NLS settings so it may be different start.
0
 
Leigh PurvisDatabase DeveloperCommented:
The Default is indeed Sunday (7)

You can check the current setting using
SELECT @@DateFirst

If you want it to be something other than Sunday (e.g. Monday) you can use
SET DATEFIRST 1
At the start of your SQL statements.


As for passthroughs running so much quicker...
Well, compared to queries based on Linked tables to server data - oh yes big time.  But the results are, of course, read only.
If you have a powerful server then the SQL executed there will be benefitting from that.
Well designed Acccess queries based on Jet data aren't too bad though.  They get a bum rep.
(But ultimately can't always compete with the big boy's raw power).
0
 
LapchienDirectorAuthor Commented:
Thanks for the insight guys!

Hope you enjoy the tennis/footy/sun this weekend!

Lap


0
 
rockiroadsCommented:
Will do, cheers

forget the tennis, Ive even forgot to watch the cricket!
Mind u England are doing their consistent performance again :(
0
 
Leigh PurvisDatabase DeveloperCommented:
There's something other than football on this weekend? :-)

(Don't have Sky - I'd never get any work done if I did... especially not with Cricket on ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.