请在这种情况下帮助我。
我想要的输出应该显示在下面的表格示例中。
点击here
然后我有一个表名PRE_SCREEN,其中包含DONOR_MST和BAT_TESDET中的匹配列
以下是每张桌子的PK:
我确实尝试使用INNER JOIN,但它没有用。以下是我的代码
SELECT
(CASE c.GENDER WHEN '1' THEN 1 ELSE 0 END) AS MALE,
(CASE c.GENDER WHEN '2' THEN 1 ELSE 0 END) AS FEMALE,
(CASE WHEN (datediff(YEAR, c.DOB, getdate())) < 19 THEN 1 ELSE 0 END )
(CASE WHEN (datediff(YEAR, c.DOB, getdate())) BETWEEN 20 AND 24 THEN 1 ELSE 0 END )
(CASE WHEN (datediff(YEAR, c.DOB, getdate())) BETWEEN 25 AND 29 THEN 1 ELSE 0 END )
(CASE WHEN (datediff(YEAR, c.DOB, getdate())) BETWEEN 30 AND 34 THEN 1 ELSE 0 END )
(CASE WHEN (datediff(YEAR, c.DOB, getdate())) BETWEEN 35 AND 39 THEN 1 ELSE 0 END )
(CASE WHEN (datediff(YEAR, c.DOB, getdate())) BETWEEN 40 AND 44 THEN 1 ELSE 0 END )
(CASE WHEN (datediff(YEAR, c.DOB, getdate())) BETWEEN 45 AND 49 THEN 1 ELSE 0 END )
(CASE WHEN (datediff(YEAR, c.DOB, getdate())) BETWEEN 50 AND 54 THEN 1 ELSE 0 END )
(CASE WHEN (datediff(YEAR, c.DOB, getdate())) > 55 THEN 1 ELSE 0 END )
FROM dbo.DONOR_MST c
INNER JOIN dbo.PRE_SCREEN b
ON c.DONOR_ID LIKE b.DONOR_ID
INNER JOIN BAT_TESDET a
ON b.BARCODE = a.BARCODE
WHERE
b.IS_ELIGIBLE = '1'
AND b.SCR_DATE >= @STARTDATE
AND b.SCR_DATE <= @ENDDATE
我的问题:
我如何链接三个表
答案 0 :(得分:0)
以下是查询的提议,以获取这些信息:
SELECT D.DOB
,SUM(CASE
WHEN D.GENDER = 'MALE' THEN 1
ELSE 0
END) AS [MALE]
,SUM(CASE
WHEN D.GENDER = 'FEMALE' THEN 1
ELSE 0
END) AS [FEMALE]
,COUNT(*) AS [TOTAL]
FROM DONOR_MST D
INNER JOIN PRE_SCREEN P ON P.DONOR_ID = D.DONOR_ID
INNER JOIN BAT_TESDET B ON B.BARCODE = P.BARCODE
GROUP BY D.DOB
希望这会对你有所帮助。