Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 syntax question

Author  Topic 

sross81
Posting Yak Master

228 Posts

Posted - 2008-06-05 : 13:10:15
What am I doing wrong here?

With DiabetesFiltered(countperson,person_nbr,date_of_birth,age)as
(
(select count(*) as countperson,a.person_nbr,a.date_of_birth,(2008-year(a.date_of_birth)) as age
from person a
join diabetes_flwsheet_ b on a.person_id = b.person_id
where year(b.create_timestamp) = '2008'
group by a.person_nbr,a.date_of_birth)tmp
where tmp.age >=18 and tmp.countperson >=2
)

It says error near tmp.

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2008-06-05 : 13:14:55
Without knowing what you are trying to do, it is hard to say what you are doing wrong other than your syntax is way off.

Additionally, you should rethink your algorithm for calculating age (if you mean that in the traditional sense), unless we all celebrate our birthday on Jan 1 ...

Jay
to here knows when
Go to Top of Page

sross81
Posting Yak Master

228 Posts

Posted - 2008-06-05 : 13:18:01
I realize the syntax is off thats why I was asking the question.

I am doing the age that way because that is what the criteria requires thank you for noticing though

quote:
Originally posted by Page47

Without knowing what you are trying to do, it is hard to say what you are doing wrong other than your syntax is way off.

Additionally, you should rethink your algorithm for calculating age (if you mean that in the traditional sense), unless we all celebrate our birthday on Jan 1 ...

Jay
to here knows when

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-05 : 13:23:00
You are using a derived table incorrectly:


SELECT countperson, person_nbr, date_of_birth, age
FROM
(
select count(*) as countperson,a.person_nbr,a.date_of_birth,(2008-year(a.date_of_birth)) as age
from person a
join diabetes_flwsheet_ b on a.person_id = b.person_id
where year(b.create_timestamp) = '2008'
group by a.person_nbr,a.date_of_birth
) tmp
where age >=18 and countperson >=2


You should be able to do it without a derived table though, using HAVING for the countperson check and adding your age >= 18 into the main where clause.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

sross81
Posting Yak Master

228 Posts

Posted - 2008-06-05 : 13:28:05
I am sorry I should have been more specific that I actually need the derived table for the next step. Here is my full code. I am trying to get a distinct list of persons with at least 2 visits so far in 2008 from the diabetes_ table and then I want to join that list to the diabetes_flwsheet_ table to get their most recent percent readings. I still have my syntax off....??

With Diabetes(person_id) as
(
select distinct person_id from
(select count(*) as countperson,a.person_id,a.date_of_birth,
(2008-year(a.date_of_birth)) as age
from person a
join diabetes_ b on a.person_id = b.person_id
where year(b.create_timestamp) = '2008'
group by a.person_nbr,a.date_of_birth))tmp
where tmp.age >=18 and tmp.countperson >=2

select tmp.person_id, a.hga1cpercent
from Diabetes tmp
join diabetes_flwsheet_ a on tmp.person_id = a.person_id

quote:
Originally posted by tkizer

You are using a derived table incorrectly:


SELECT countperson, person_nbr, date_of_birth, age
FROM
(
select count(*) as countperson,a.person_nbr,a.date_of_birth,(2008-year(a.date_of_birth)) as age
from person a
join diabetes_flwsheet_ b on a.person_id = b.person_id
where year(b.create_timestamp) = '2008'
group by a.person_nbr,a.date_of_birth
) tmp
where age >=18 and countperson >=2


You should be able to do it without a derived table though, using HAVING for the countperson check and adding your age >= 18 into the main where clause.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx

Go to Top of Page

sross81
Posting Yak Master

228 Posts

Posted - 2008-06-05 : 13:30:13
It was a parenthesis problem. This works:

With Diabetes(person_id) as
(
select distinct person_id from
(select count(*) as countperson,a.person_id,a.date_of_birth,
(2008-year(a.date_of_birth)) as age
from person a
join diabetes_ b on a.person_id = b.person_id
where year(b.create_timestamp) = '2008'
group by a.person_id,a.date_of_birth)tmp
where tmp.age >=18 and tmp.countperson >=2
)
select tmp.person_id, a.hga1cpercent
from Diabetes tmp
join diabetes_flwsheet_ a on tmp.person_id = a.person_id

quote:
Originally posted by sross81

I am sorry I should have been more specific that I actually need the derived table for the next step. Here is my full code. I am trying to get a distinct list of persons with at least 2 visits so far in 2008 from the diabetes_ table and then I want to join that list to the diabetes_flwsheet_ table to get their most recent percent readings. I still have my syntax off....??

With Diabetes(person_id) as
(
select distinct person_id from
(select count(*) as countperson,a.person_id,a.date_of_birth,
(2008-year(a.date_of_birth)) as age
from person a
join diabetes_ b on a.person_id = b.person_id
where year(b.create_timestamp) = '2008'
group by a.person_nbr,a.date_of_birth))tmp
where tmp.age >=18 and tmp.countperson >=2

select tmp.person_id, a.hga1cpercent
from Diabetes tmp
join diabetes_flwsheet_ a on tmp.person_id = a.person_id

quote:
Originally posted by tkizer

You are using a derived table incorrectly:


SELECT countperson, person_nbr, date_of_birth, age
FROM
(
select count(*) as countperson,a.person_nbr,a.date_of_birth,(2008-year(a.date_of_birth)) as age
from person a
join diabetes_flwsheet_ b on a.person_id = b.person_id
where year(b.create_timestamp) = '2008'
group by a.person_nbr,a.date_of_birth
) tmp
where age >=18 and countperson >=2


You should be able to do it without a derived table though, using HAVING for the countperson check and adding your age >= 18 into the main where clause.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx



Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2008-06-05 : 13:30:18
I don't know how you inferred your solution from the malformed CTE above, but I commend your powers of clairvoyance with regard to the OP's required "criteria" ...

Nevermind ...
Go to Top of Page

sross81
Posting Yak Master

228 Posts

Posted - 2008-06-05 : 13:32:20

What do you mean by this?? I am new to this and still get the syntax down. I thought that is what this board was for...


quote:
Originally posted by Page47

I don't know how you inferred your solution from the malformed CTE above, but I commend your powers of clairvoyance with regard to the OP's required "criteria" ...

Jay
to here knows when

Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2008-06-05 : 13:48:43
New to what? SQL? Post on forums?

That post was directed toward Tara, not you.

See, in my original post, I alluded to the fact that no one will be able to help you with your syntax because it was completely unclear (to me anyway) what you were trying to do. Tara came along and "inferred" a solution, to which I gave praise since, like I said, I had no idea what you were trying to do. Somehow Tara was able to see through the confusion and provide a nice query.

Well, then by the time I hit submit, I saw that the "inferred" solution was in fact wrong (shame Tara wasted time writing SQL before having sound requirements), so I withdrew my praise.

Jay
to here knows when
Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2008-06-05 : 13:54:59
Look ... the more effort you put into asking a well formed, concise, intelligible question, the higher quality of an answer/suggestion you will get back. As an added bonus, you won't cause people to waste their time responding to you by changing the question or providing the critical details half-way through the thread. Try answering some questions on this forum ... you'll see what I mean.

Honestly, the only thing that interests me here was the age thing. I find it funny how many people can't seem to figure how old a person is given a DOB and a point in time. That's all I really cared to point out to you.

Aside from that, you don't need a CTE for this query. Wrong application. While I'm at it, your naming conventions need some serious work and your handwriting (query formatting) will lead to major confusion once you start writing some real queries.

Jay
to here knows when
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-05 : 13:55:41
I agree with you, Jay. What I posted was a best guess based on the very little information that was posted.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2008-06-05 : 14:20:22
Tara, you've got many many more posts than I, but we've both been around about the same amount of time.

I've completely removed myself from the "best guess" game as I simply don't have time and generally I have little to gain.

When I post and an OP displays frustration over the fact that I didn't just hand out a freebie, it rubs me the wrong way. I surely didn't mean to include you in my (not so) concealed rant. I'll go back into my cave now.

Jay
to here knows when
Go to Top of Page
   

- Advertisement -