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.
| Author |
Topic |
|
cwildeman
Starting Member
40 Posts |
Posted - 2011-11-28 : 11:01:43
|
| I have a query that has ConsultDate, AcctNum, WoundLocation and WoundUnit. The AcctNum field will have duplicate values when a patient has more than one wound. I want to create a calculated field called PtCount which will simply be a 1 for the first instance an account number appears and zero for additional records where AcctNum appears again. I need to keep all fields in the query (ConsultDate, AcctNum, WoundLocation and WoundUnit) and add PtCount so a Group By with Min won't work (I don't think). Can someone help?Thanks,ChuckChuck W |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-28 : 11:07:17
|
just make update likeUPDATE tSET t.PtCount=1FROM Table tINNER JOIN (SELECT AcctNum,MIN(ConsultDate) AS MInDate FROM table GROUP BY AcctNum)t1ON t1.AcctNum = t.AcctNumAND t1.MinDate=t.ConsultDate ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
cwildeman
Starting Member
40 Posts |
Posted - 2011-11-28 : 11:32:08
|
| Thanks for your help. I substituted my table name dbo.HAPU_Input for the word table in your example and came up with the following. I tried to run it but got an error 'Invalid column name 'PtCount'. Any idea what I am doing wrong?UPDATE tSET t.PtCount=1FROM dbo.HAPU_Input tINNER JOIN (SELECT AcctNum,MIN(ConsultDate) AS MinDate FROM dbo.HAPU_Input GROUP BY AcctNum)t1ON t1.AcctNum = t.AcctNumAND t1.MinDate=t.ConsultDateChuck W |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-28 : 11:43:59
|
quote: Originally posted by cwildeman Thanks for your help. I substituted my table name dbo.HAPU_Input for the word table in your example and came up with the following. I tried to run it but got an error 'Invalid column name 'PtCount'. Any idea what I am doing wrong?UPDATE tSET t.PtCount=1FROM dbo.HAPU_Input tINNER JOIN (SELECT AcctNum,MIN(ConsultDate) AS MinDate FROM dbo.HAPU_Input GROUP BY AcctNum)t1ON t1.AcctNum = t.AcctNumAND t1.MinDate=t.ConsultDateChuck W
have you created that column PtCount yet in table HAPU_Input?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
cwildeman
Starting Member
40 Posts |
Posted - 2011-11-28 : 12:24:41
|
| Thanks that worked. HAPU_Input is a table where end users will enter several records in each month. I need to create a query that will perform this update once a month. I take it I can run it each month and it will update the new records and leave the old one's alone? Also, do you recommend that I create a stored procedure to run this whenever I need to create more query for reporting purposes? Thanks, ChuckChuck W |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-28 : 12:41:19
|
quote: Originally posted by cwildeman Thanks that worked. HAPU_Input is a table where end users will enter several records in each month. I need to create a query that will perform this update once a month. I take it I can run it each month and it will update the new records and leave the old one's alone? Also, do you recommend that I create a stored procedure to run this whenever I need to create more query for reporting purposes? Thanks, ChuckChuck W
you can add this inside procedure and call it from sql job. The job can be scheduled to run each month. then using date field you can identify new records and do this update------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|