简体   繁体   English

SQL-从交叉表查询获取总计

[英]SQL - getting totals from a crosstab query

TRANSFORM Count(Research.Patient_ID) AS CountOfPatient_ID
SELECT DateDiff("yyyy",[DOB],Date()) AS [Age Group 22 - 24]
FROM (Research INNER JOIN Demographics ON Research.Patient_ID = Demographics.ID) INNER JOIN [Letter Status] ON Research.Patient_ID = [Letter Status].Patient_ID
WHERE (((DateDiff("yyyy",[DOB],Date())) Between 22 And 24))
GROUP BY DateDiff("yyyy",[DOB],Date())
PIVOT [Letter Status].Letter_Status;

This code lists individual ages, but I want to calculate the Total of all ages that fall within that range. 该代码列出了各个年龄段,但我想计算该范围内的所有年龄段的总和。 So if there were two 22 year olds, and one 23 year old, the Total would be 3, as opposed to seeing all of their individual ages in the column... 因此,如果有两个22岁的孩子和一个23岁的孩子,那么总计将是3,而不是在栏中查看他们所有的个人年龄...

Please help. 请帮忙。

Please be sure you understand what you're getting when you use this method to determine a person's age. 使用此方法确定一个人的年龄时,请确保您了解所得到的东西。

DateDiff("yyyy",[DOB],Date())

If DOB = #1987-12-31#, on today's date (#2011-6-16#), DateDiff will give you this as the age: 如果DOB =#1987-12-31#,则在今天的日期(#2011-6-16#),DateDiff将为您指定年龄:

? DateDiff("yyyy", #1987-12-31#, #2011-6-16#)
 24 

(And actually for any DOB during 1987, DateDiff with today's date would give you 24 as the "age". Furthermore, for any DOB during 1987, using that method on any day in 2011, you would get 24.) (实际上,对于1987年期间的任何DOB,带有当前日期的DateDiff都会给您24作为“年龄”。此外,对于1987年期间的任何DOB,在2011年的任何一天使用该方法,您都会得到24。)

OTOH, as age is commonly understood, one might say "Nonsense! His birthday is still more than 6 months away. He's only 23 today." 众所周知,OTOH可能会说:“胡说!他的生日还有6个月了。今天他才23岁。”

The reason for this discrepancy is that DateDiff("yyyy" only evaluates the year component of two dates. Consider these two dates which are one day apart: 这种差异的原因是DateDiff("yyyy"仅计算两个日期的年份组成部分。请考虑相隔一天的两个日期:

? DateDiff("yyyy",#2010-12-31#,#2011-1-1#)
 1

That same thing happens when you use that expression to calculate "age". 当您使用该表达式计算“年龄”时,也会发生同样的事情。

To return the age as commonly understood, you can use an expression like this: 要返回通常理解的年龄,可以使用如下表达式:

? DateDiff("yyyy", #1987-12-31#, Date())+ _
            Int( Format(Date(), "mmdd") < Format( #1987-12-31#, "mmdd"))

I copied that expression from the Access Web: Calculate Age of a person . 我从Access Web复制了该表达式: 计算一个人的年龄 That page includes other approaches for determining age. 该页面包括确定年龄的其他方法。

None of this matters if, apparently like the OP, you want all DOB in 1987 to be counted as age 24 today, or on any day in 2011. 如果您显然希望像OP一样,希望将1987年的所有DOB都计入今天的24岁或2011年的任何一天,那么这些都不重要。

You need to define the age ranges in a switch statement. 您需要在switch语句中定义年龄范围。 To make it easier I've created a inline query to calcuate the age (I'm assuming its on the research table). 为了简化起见,我创建了一个内联查询来计算年龄(我假设它在研究表上)。

TRANSFORM Count(Research.Patient_ID) AS CountOfPatient_ID
SELECT  SWITCH (Age <= 22, "Under 22",
        Age > 22 and AGE <= 24 , "Between 22 And 24",
                Age > 24 and AGE <= 26,  "Between 24 And 26",
                Age > 26 , "Over 26") as Age_Range
FROM 
   (Research
    INNER JOIN (SELECT ID,  DateDiff("yyyy",DOB,Date()) as AGE
        FROM Demographics) Demographics
    ON Research.Patient_ID = Demographics.ID) 
    INNER JOIN [Letter Status] 
    ON Research.Patient_ID = [Letter Status].Patient_ID

GROUP BY  AGE


PIVOT [Letter Status].Letter_Status;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM