我有 2 张桌子,我正在尝试加入并计算有多少人回答了每个问题。我正在尝试找到一种只有两列的更好方法。第一个是回答“只是我,我的配偶/伴侣”的人,另一列是回答儿童 0-5 岁、6-12 岁、13-17 岁的人。我正在尝试找出一种更好的方法来拥有一张看起来像这样的桌子
Question Answer Children Justme/partner
do you buy milk weekly 400 20
do you consume cheese daily 15 300
how many time you drive daily 400 220
这是 2 个表格 这是提供的称为“家庭”的表格:
User JustMe Children 0-5 Children 6-12 Myspouse/partner
5 N N N N
5 N N N N
4 N N N N
4 N N N N
12 N N N N
12 Just Me N N Myspousepartner
46 Just Me N Children 6-12 years Mypousepartner
46 Just Me N Children 6-12 years Mypousepartner
调查表
User questionid answerid question answer
4 2 5 how often you buy gas weekly
46 3 4 how often you buy milk monthly
75 3 4 how often you buy milk weekly
13 4 2 how often you use coupon monthly
我用
SELECT id,question, Children_0_5_years, Children_6_12_years,
Children_13_17_years, count(*) AS NEW_COUNT
FROM table1 inner join table2 on table1.user=table2.user group by...
不幸的是,这个查询返回了我不需要的东西。
SELECT Question, SUM( CASE --Case for each column WHEN h."Children 0-5" = 'Children 0-5' THEN 1 Else 0 END) as "Children 0-5", SUM( CASE
WHEN h."Children 6-12" = 'Children 0-5' THEN 1 Else 0 END as "Children 0-5"* from household h INNER JOIN survey s ON h.user = s.user GROUP BY s.question, answer
好的,从我的大脑中做这件事而不是测试,但这是我的想法。使用 case 语句将您的字符串数据转换为可求和的数字。然后按问题分组以汇总答案。
如果我是您(并且您可以更改数据),我将摆脱冗长的字符串字段,并制作诸如年龄组 Y、N 甚至更好的 1、0 之类的东西。