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 |
khovorka
Starting Member
8 Posts |
Posted - 2013-10-14 : 11:41:15
|
I am submitting a query and getting the following results:Female, 487Male, 96The results should beFemale, 4Male, 1I would appreciate any help in know what is out of sync hereor what I am missing so that the cnt value is expected.Here is the query:SELECT p1.sex, Count(rn) AS cnt FROM (SELECT center, patientid, timestamp, date1, date2, date3, row_number() OVER (partition by patientid ORDER BY timestamp DESC) AS rnFROM dbo.tbl_mfc_goals) AS gINNER JOIN dbo.tbl_mfc_pat1 AS p1 ON p1.patient = g.patientidINNER JOIN dbo.tbl_current_status AS ps ON ps.lm_index = g.patientidWHERE CONVERT(DATETIME, g.date1) > '2013-10-14' OR CONVERT(DATETIME, g.date2) > '2013-10-14' OR CONVERT(DATETIME, g.date3) > '2013-10-14'AND g.rn = 1 AND g.center=51 AND p1.enrolldate <= '2013-10-14'GROUP BY p1.sexThank you for your insight in getting me corrected on this issue.KimKim H. |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-10-14 : 11:54:30
|
quote: Originally posted by khovorka I am submitting a query and getting the following results:Female, 487Male, 96The results should beFemale, 4Male, 1I would appreciate any help in know what is out of sync hereor what I am missing so that the cnt value is expected.Here is the query:SELECT p1.sex, Count(rn) AS cnt FROM (SELECT center, patientid, timestamp, date1, date2, date3, row_number() OVER (partition by patientid ORDER BY timestamp DESC) AS rnFROM dbo.tbl_mfc_goals) AS gINNER JOIN dbo.tbl_mfc_pat1 AS p1 ON p1.patient = g.patientidINNER JOIN dbo.tbl_current_status AS ps ON ps.lm_index = g.patientidWHERE CONVERT(DATETIME, g.date1) > '2013-10-14' OR CONVERT(DATETIME, g.date2) > '2013-10-14' OR CONVERT(DATETIME, g.date3) > '2013-10-14'AND g.rn = 1 AND g.center=51 AND p1.enrolldate <= '2013-10-14'GROUP BY p1.sexThank you for your insight in getting me corrected on this issue.KimKim H.
It really depends on the PARTITION BY clause you are using and the way you calculate the count. Hard to say without understanding the nature of the data. Perhaps one of these? SELECT p1.sex , COUNT(DISTINCT rn) AS cnt....--- and/or.... date3 , ROW_NUMBER() OVER ( PARTITION BY center ORDER BY timestamp DESC ) AS rn..... |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-10-14 : 13:26:11
|
My Guess:SELECT p1.sex, Count(CASE THEN rn = 1 THEN 1 ELSE NULL END) AS cnt FROM... Here are some links that can help you prepare your question so we can help you better:http://www.sqlservercentral.com/articles/Best+Practices/61537/http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
|
|
|
|