Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested SELECT with DatePart join

Status
Not open for further replies.

EndlessNights

Programmer
Feb 11, 2002
6
CA
Hiya,

I'm trying to run a nested query that will extract for each date a count on certain variables. I'm hoping to get something like this:
TheYear, TheMonth, TheDay, count(Variable=whatever1), count(Variable=whatever2), etc...

Below is a much simplified version of what I'm trying to accomplish:

select
'TheDay' = DatePart(d, TB1.DateStamp),
'Variable1' = (
select count(TB2.ID)
from TableName TB2
where Variable1 = 'whatever' and
DatePart(d, TB2.DateStamp) = DatePart(d, TB1.DateStamp)
)
from TableName TB1
group by DatePart(d, TB1.DateStamp)

Unfortunately I keep getting this error:
Server: Msg 8630, Level 17, State 38, Line 1
Internal Query Processor Error: The query processor encountered an unexpected error during execution.


I know the larger select works without the nested selects, and I know the nested select works when not included in the larger statement and I use hard coded numbers (e.g. DatePart(d, TB1.DateStamp) is replaced by 8 for example)...

So what am I doing wrong?!?!

Any suggestions would be great!

cheers,
mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top