Solved

SQL pass thru query for current month

Posted on 2006-06-29
25
649 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
 
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 44

Accepted Solution

by:
Leigh Purvis earned 250 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 250 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now