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 |
sross81
Posting Yak Master
228 Posts |
Posted - 2012-12-19 : 11:33:55
|
Hello, I have a table with records like this:Enc#, AbxAntiboticName,AbxAntibioticAdminDate, AbxAntiboticAdminTimecp555, name1, 12/1/2012, 8:00cp555, name2, 12/4/2012, 14:25cp555, name3, 12/5/2012, 3:25The enc # is the field I want to group on. It can be any random assigned number. I want to group by encounter and then show the 1st, 2nd, 3rd, etc antibiotic that was received based on the date and time. There could be alot more than just 3 and multiple on the same day, etc.So add a field called AbxSequence and have it be 1, 2, 3 etc. Enc#, AbxAntiboticName,AbxAntibioticAdminDate, AbxAntiboticAdminTime, abxsequencecp555, name1, 12/1/2012, 8:00,1cp555, name2, 12/4/2012, 14:25,2cp555, name3, 12/5/2012, 3:25,3I have been trying to find an example online, but everything I found seems to be related to having specific predefined groups and in my example the encounter number isn't predefined.Thanks in Advance!Sherri |
|
sross81
Posting Yak Master
228 Posts |
Posted - 2012-12-19 : 11:42:08
|
I have tried using this, but for one encounter number with 5 antibiotics in 2 days it only gives a sequence number of 1, 2select AbxEncNbr,AbxAntibioticName, ROW_NUMBER() OVER(ORDER BY MAX(AbxAntibioticAdministrationDate), MAX(AbxAntibioticAdministrationTime)) as 'ABXSeqNum' from Temp_ABX_PDEgroup by AbxEncNbr, AbxAntibioticNameI've also tried this and it just basically numbers the entire list so one encounter number with 9 antibitoics goes 1-9 and then it switches to 10 on the next encounter number. I need it to start over on the new encounter number:select AbxEncNbr,AbxAntibioticName, ROW_NUMBER() OVER(ORDER BY AbxEncNbr,AbxAntibioticName,AbxAntibioticAdministrationDate,AbxAntibioticAdministrationTime) as 'ABXSeqNum' from Temp_ABX_PDEgroup by AbxEncNbr, AbxAntibioticName, AbxAntibioticAdministrationDate,AbxAntibioticAdministrationTimeorder by AbxEncNbr, AbxAntibioticName, AbxAntibioticAdministrationDate,AbxAntibioticAdministrationTimeThanks in Advance!Sherri |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-12-19 : 12:17:40
|
Would this work for you?SELECT AbxEncNbr, AbxAntibioticName, ROW_NUMBER() OVER (PARTITION BY Enc# ORDER BY AbxAntibioticAdminDate,AbxAntiboticAdminTime) AS 'ABXSeqNum'FROM Temp_ABX_PDE |
|
|
sross81
Posting Yak Master
228 Posts |
Posted - 2012-12-19 : 13:51:10
|
Thank you for your reply. It is still a little off. Here is my code:SELECT AbxEncNbr, AbxAntibioticName, ROW_NUMBER() OVER (PARTITION BY AbxEncNbr ORDER BY AbxAntibioticAdministrationDate,AbxAntibioticAdministrationTime) AS 'ABXSeqNum'FROM Temp_ABX_PDERaw dataAbxEncNbr bxAntibioticName AbxAntibioticAdministrationDate AbxAntibioticAdministrationTimeCP123455555 Rocephin 12/29/2011 18:25CP123455555 Azithromycin 12/29/2011 18:58CP123455555 Aztreonam 1/1/2012 11:38CP123455555 Rocephin 1/1/2012 14:31CP123455555 Zithromax 1/1/2012 15:50Queried Data. I think that it should be Rocephin, AZithromycin based on the date and times.....the one's are 1/1/2012 seem backwards too.AbxEncNbr AbxAntibioticName ABXSeqNumCP123455555 Aztreonam 1CP123455555 Rocephin 2CP123455555 Zithromax 3CP123455555 Rocephin 4CP123455555 Azithromycin 5Any other ideas?quote: Originally posted by sunitabeck Would this work for you?SELECT AbxEncNbr, AbxAntibioticName, ROW_NUMBER() OVER (PARTITION BY Enc# ORDER BY AbxAntibioticAdminDate,AbxAntiboticAdminTime) AS 'ABXSeqNum'FROM Temp_ABX_PDE
Thanks in Advance!Sherri |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-12-19 : 14:03:03
|
What is the data type of the AbxAntibioticAdministrationDate and AbxAntibioticAdministrationTime columns? You can find out usingSELECT COLUMN_NAME,DATA_TYPEFROM INFORMATION_SCHEMA.COLUMNSWHERE TABLE_NAME = 'Temp_ABX_PDE' AND COLUMN_NAME IN ('AbxAntibioticAdministrationDate','AbxAntibioticAdministrationTime') If they are not DATETIME and TIME (or one of its variants, change the code to the following)SELECT AbxEncNbr,AbxAntibioticName,ROW_NUMBER() OVER (PARTITION BY AbxEncNbr ORDER BY CAST(AbxAntibioticAdministrationDate AS DATE), CAST(AbxAntibioticAdministrationTime AS TIME)) AS 'ABXSeqNum'FROM Temp_ABX_PDE |
|
|
sross81
Posting Yak Master
228 Posts |
Posted - 2012-12-19 : 14:05:26
|
It was the data types! I didn't even think of that. I am just getting back into using SQL again. Thank you so much for pointing that out :).quote: Originally posted by sunitabeck What is the data type of the AbxAntibioticAdministrationDate and AbxAntibioticAdministrationTime columns? You can find out usingSELECT COLUMN_NAME,DATA_TYPEFROM INFORMATION_SCHEMA.COLUMNSWHERE TABLE_NAME = 'Temp_ABX_PDE' AND COLUMN_NAME IN ('AbxAntibioticAdministrationDate','AbxAntibioticAdministrationTime') If they are not DATETIME and TIME (or one of its variants, change the code to the following)SELECT AbxEncNbr,AbxAntibioticName,ROW_NUMBER() OVER (PARTITION BY AbxEncNbr ORDER BY CAST(AbxAntibioticAdministrationDate AS DATE), CAST(AbxAntibioticAdministrationTime AS TIME)) AS 'ABXSeqNum'FROM Temp_ABX_PDE
Thanks in Advance!Sherri |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-12-19 : 15:10:28
|
You are quite welcome!Many of the experts on this forum would suggest that if at all possible, data that represents DATE, DATETIME, TIME etc. should be stored in columns of appropriate data types (such as DATE, DATETIME, TIME etc.) Same goes for other types of data as well - to avoid this type of problem. Also, if you use varchar for the data type of a column that stores date/time, someone could (inadvertently perhaps) store something that is not a date (for example the text "Joe's bar") into that column. That would cause the above query to fail!! |
|
|
|
|
|
|
|