Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL pass thru query for current month

Posted on 2006-06-29
25
Medium Priority
?
658 Views
Last Modified: 2008-02-01
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


0
Comment
Question by:Lapchien
  • 12
  • 7
  • 6
25 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 17010997
SQL Server perhaps?

this may be what your after


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


0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17011011
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
 

Author Comment

by:Lapchien
ID: 17011352
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17011402
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17011405
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17011413
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17011432
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
 

Author Comment

by:Lapchien
ID: 17011450
!

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
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17011472
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17011477
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17011491
Yea, I thought about adding the years but I thought I had better get the month one working first!
Cheers Leigh

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17011508
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
 
LVL 44

Accepted Solution

by:
Leigh Purvis earned 1000 total points
ID: 17011511
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
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17011529
(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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17011728
Ive gotta go. eSpeak later, unless Leigh's solved it!
0
 

Author Comment

by:Lapchien
ID: 17012722
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
 

Author Comment

by:Lapchien
ID: 17013205
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
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 1000 total points
ID: 17013331
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
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17013789
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
 

Author Comment

by:Lapchien
ID: 17015899
Thanks that's great - does the current week start on Sunday or Monday though?  I guess this is a convention?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17015909
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
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17015956
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
 

Author Comment

by:Lapchien
ID: 17015987
Thanks for the insight guys!

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

Lap


0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17016029
Will do, cheers

forget the tennis, Ive even forgot to watch the cricket!
Mind u England are doing their consistent performance again :(
0
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17016175
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

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question