博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何写复杂的SQL
阅读量:5268 次
发布时间:2019-06-14

本文共 2759 字,大约阅读时间需要 9 分钟。

这里需要使用到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的朋友们。

转载于:https://www.cnblogs.com/chengyihardworking/p/3810132.html

你可能感兴趣的文章
【洛谷P1816 忠诚】线段树
查看>>
电子眼抓拍大解密
查看>>
tomcat7的数据库连接池tomcatjdbc的25个优势
查看>>
Html 小插件5 百度搜索代码2
查看>>
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
查看>>
java 常用命令
查看>>
51nod1076 (边双连通)
查看>>
Linux pipe函数
查看>>
java equals 小记
查看>>
2019春 软件工程实践 助教总结
查看>>
Zerver是一个C#开发的Nginx+PHP+Mysql+memcached+redis绿色集成开发环境
查看>>
java实现哈弗曼树
查看>>
程序的静态链接,动态链接和装载 (补充)
查看>>
关于本博客说明
查看>>
HDU 2548 A strange lift
查看>>
Linux服务器在外地,如何用eclipse连接hdfs
查看>>
react双组件传值和传参
查看>>
[Kaggle] Sentiment Analysis on Movie Reviews
查看>>
价值观
查看>>
mongodb命令----批量更改文档字段名
查看>>