Link to home
Start Free TrialLog in
Avatar of Lapchien
LapchienFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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


Avatar of rockiroads
rockiroads
Flag of United States of America image

SQL Server perhaps?

this may be what your after


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


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

Avatar of Lapchien

ASKER

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...

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 :-)
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;

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?

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!

!

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;

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.
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


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

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



ASKER CERTIFIED SOLUTION
Avatar of Leigh Purvis
Leigh Purvis
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(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 )
Ive gotta go. eSpeak later, unless Leigh's solved it!
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
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Thanks that's great - does the current week start on Sunday or Monday though?  I guess this is a convention?
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.
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).
Thanks for the insight guys!

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

Lap


Will do, cheers

forget the tennis, Ive even forgot to watch the cricket!
Mind u England are doing their consistent performance again :(
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 ;-)