如何写复杂的SQL

这里需要使用到case when语句,这个就是sql中的if else语句:         select                  a.ins_id,                 b.product_id,                 b.product_name,                 c.cust_id,                 c.cust_name,                 c.cust_sex,                 c.cust_age,                 c.family_num,                 -- 男                 -- 这个地方根据数据库字段的不同,处理方式也不同                 -- 如果数据库中cust_sex的数据类型本身就是0和1,那么就不需要转换                 -- 只列出来即可                 (case when c.cust_sex='男' then 1 else 0 end) as f,                 -- 女                 (case when c.cust_sex='女' then 1 else 0 end) as as m,                 -- 其他的依次类推                 -- 家庭成员数                 (case when c.family_num=1 then 1 else 0 end) as p_1,                 (case when c.family_num=2 then 1 else 0 end) as P_2,                 (case when c.family_num=3 then 1 else 0 end) as p_3,                 (case when c.family_num>3 then 1 else 0 end) as gt3,                 -- 客户年龄                 (case when c.cust_age<=25 then 1 else 0 end) as lt25,                 (case when c.cust_age>25 and c.cust_age<=35 then 1 else 0 end) as gt25lt35,                 (case when c.cust_age>35 and c.cust_age<=45 then 1 else 0 end) as gt35lt45,                 (case when c.cust_age>45 and c.cust_age<=55 then 1 else 0 end) as gt45lt55,                 (case when c.cust_age>55 then 1 else 0 end) as gt55         from                 insurance a,                 product b,                 customer c         where                  a.product_id=b.product_id                 and a.cust_id=c.cust_id                  最后分组count一下即可:         select                  a.product_id,                 a.product_name,                 count(a.ins_id) as ins_num,                 -- 性别                 count(a.f) as f_num,                 count(a.m) as m_num,                 -- 成员数                 count(a.p_1) as p_1_num,                 count(a.p_2) as p_1_num,                 count(a.p_3) as p_1_num,                 count(a.gt3) as gt3_num,                 -- 年龄                 count(lt25) as lt25_num,                 count(gt25lt35) as gt25lt35_num,                 count(gt35lt45) as gt25lt35_num,                 count(gt45lt55) as gt25lt35_num,                 count(gt55) as gt55_num         from(                 select                          a.ins_id,                         b.product_id,                         b.product_name,                         c.cust_id,                         c.cust_name,                         c.cust_sex,                         c.cust_age,                         c.family_num,                         -- 男                         -- 这个地方根据数据库字段的不同,处理方式也不同                         -- 如果数据库中cust_sex的数据类型本身就是0和1,那么就不需要转换                         -- 只列出来即可                         (case when c.cust_sex='男' then 1 else 0 end) as f,                         -- 女                         (case when c.cust_sex='女' then 1 else 0 end) as as m,                         -- 其他的依次类推                         -- 家庭成员数                         (case when c.family_num=1 then 1 else 0 end) as p_1,                         (case when c.family_num=2 then 1 else 0 end) as P_2,                         (case when c.family_num=3 then 1 else 0 end) as p_3,                         (case when c.family_num>3 then 1 else 0 end) as gt3,                         -- 客户年龄                         (case when c.cust_age<=25 then 1 else 0 end) as lt25,                         (case when c.cust_age>25 and c.cust_age<=35 then 1 else 0 end) as gt25lt35,                         (case when c.cust_age>35 and c.cust_age<=45 then 1 else 0 end) as gt35lt45,                         (case when c.cust_age>45 and c.cust_age<=55 then 1 else 0 end) as gt45lt55,                         (case when c.cust_age>55 then 1 else 0 end) as gt55                 from                         insurance a,                         product b,                         customer c                 where                          a.product_id=b.product_id                         and a.cust_id=c.cust_id         ) a         group by b.product_id, b.product_name

        到现在未知,我们所要的结果就完全出来了。整个过程是一个化繁为简,再由简单堆砌为复杂的过程。         对于初学者,培养出这样的思维方式似乎还很难,但是只要经过一两个这样的需求的练习,这中思维         方式就很容易形成了,到最后,当你看到一个报表结构的时候,这样的思维过程仅仅是一瞬间的事,         你的脑海里是n张的数据表格,经过相应关联之后,你的脑海里得到是报表结构的前一张结果集的结构,         然后再往前推前一张结果集的结构,直到推到主表,然后再正向推一编,最后推到完整的报表结构,         这个思维过程非常快,很可能再你的脑子只推一两步的时候,你就已经知道怎么写了。

        谨以此文送给初学sql的朋友们。

你可能感兴趣的:(如何写复杂的SQL)