Lapchien
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)=Fal se)
GROUP BY [Forename] & " " & [Surname]
HAVING (((Jobs.DateReceived) Between DateSerial(Year(GetDate()) ,Month(Get Date()),1) And DateSerial(Year(GetDate()) ,Month(Get Date()),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
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)=Fal
GROUP BY [Forename] & " " & [Surname]
HAVING (((Jobs.DateReceived) Between DateSerial(Year(GetDate())
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
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
therefore DateSerial will not work
I dont know what your GetDate does, but I guess u need to convert that also
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)=Fal se)
GROUP BY [Forename] & " " & [Surname]
HAVING Format(DateReceived,'mm')= Format(Get Date(),'mm ')
ORDER BY Count(Jobs.JobId) DESC;
This should return all results for current month...
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)=Fal
GROUP BY [Forename] & " " & [Surname]
HAVING Format(DateReceived,'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(GetDa te()),4) + '-' + CONVERT(varChar, Month(GetDate()),2) + '-01')
AND
CONVERT(DateTime, CONVERT(varChar,YEAR(GetDa te()),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 :-)
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(GetDa
AND
CONVERT(DateTime, CONVERT(varChar,YEAR(GetDa
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(Get Date(),'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)=Fal se)
AND CONVERT(nvarchar, DATEPART(mm, DateReceived)) = CONVERT(nvarchar, DATEPART(mm, GetDate()))
GROUP BY [Forename] & " " & [Surname]
ORDER BY Count(Jobs.JobId) DESC;
HAVING Format(DateReceived,'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)=Fal
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?
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!
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!
ASKER
!
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;
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.
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)=Fal se)
GROUP BY [Forename] & " " & [Surname]
HAVING Format(DateReceived,'mm')= Format(Get Date(),'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
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)=Fal
GROUP BY [Forename] & " " & [Surname]
HAVING Format(DateReceived,'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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(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!
ASKER
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
Will try the above tomorrow and dish out the points!
Thanks
Lap
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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).
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).
ASKER
Thanks for the insight guys!
Hope you enjoy the tennis/footy/sun this weekend!
Lap
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 :(
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 ;-)
(Don't have Sky - I'd never get any work done if I did... especially not with Cricket on ;-)
this may be what your after
http://www.webbedwonder.com/SQL-Server/Access-queries-to-TSQL.asp