Access SQL - 是否可以按开关值分组?
Access SQL - Is it possible to group by switch values?
我正在使用 Access 2010 尝试按开关值分组,但目前在编写代码时遇到问题。基本上,在我的数据库的处理字段中,我有 D1、D2、D3、E1、E2、E3 等值。第一个字母表示特定的治疗类型,例如D = 治疗 1,E = 治疗 2。例如,我希望按 'Treatment1' 和 'Treatment2' 对数据进行分组。
SELECT switch(LEFT(t1.[treatment],1)='D',"Treatment1",LEFT(t1.[treatment],1)='E',"Treatment2"), count(t1.[UniqueID]) AS Total_Population, count(t2.[date]) AS DateSent, format(count(t2.[date]) / count(t1.[UniqueID]),"0.00%") AS Percentage_Sent, COUNT(IIF(t1.[requirements]='1',1,)) AS requirements_population
FROM Main_table AS t1 LEFT JOIN (SELECT t2.[ID], t2.[date] FROM Main_table AS t2 WHERE t2.date Not Like ('NA')) AS t2 ON t1.[ID] = t2.[ID]
GROUP BY [treatment]
例如,预期输出是 Treatment1 - x 量,Treatment2 - y 量。但是目前我得到了 Treatment1 和 Treatment2 的多行,因为这些表达式背后的数据不是唯一的(即 D1、D2、D3、E1、E2、E3)。
更改为:
GROUP BY LEFT(t1.[treatment], 1)
所以你按 t1.[treatment]
的第一个字母分组而不是 t1.[treatment]
我正在使用 Access 2010 尝试按开关值分组,但目前在编写代码时遇到问题。基本上,在我的数据库的处理字段中,我有 D1、D2、D3、E1、E2、E3 等值。第一个字母表示特定的治疗类型,例如D = 治疗 1,E = 治疗 2。例如,我希望按 'Treatment1' 和 'Treatment2' 对数据进行分组。
SELECT switch(LEFT(t1.[treatment],1)='D',"Treatment1",LEFT(t1.[treatment],1)='E',"Treatment2"), count(t1.[UniqueID]) AS Total_Population, count(t2.[date]) AS DateSent, format(count(t2.[date]) / count(t1.[UniqueID]),"0.00%") AS Percentage_Sent, COUNT(IIF(t1.[requirements]='1',1,)) AS requirements_population
FROM Main_table AS t1 LEFT JOIN (SELECT t2.[ID], t2.[date] FROM Main_table AS t2 WHERE t2.date Not Like ('NA')) AS t2 ON t1.[ID] = t2.[ID]
GROUP BY [treatment]
例如,预期输出是 Treatment1 - x 量,Treatment2 - y 量。但是目前我得到了 Treatment1 和 Treatment2 的多行,因为这些表达式背后的数据不是唯一的(即 D1、D2、D3、E1、E2、E3)。
更改为:
GROUP BY LEFT(t1.[treatment], 1)
所以你按 t1.[treatment]
的第一个字母分组而不是 t1.[treatment]