SQL练习和Java作业

1、

有一个员工表dept_emp简况如下:

emp_nodept_nofrom_dateto_date
10001d0011986-06-269999-01-01
10002d0011996-08-039999-01-01
10003d0021995-12-039999-01-01

第一行表示为员工编号为10001的部门是d001部门。

有一个部门经理表dept_manager简况如下:

dept_noemp_nofrom_dateto_date
d001100021996-08-039999-01-01
d002100031990-08-059999-01-01

第一行表示为d001部门的经理是编号为10002的员工。

获取所有的员工和员工对应的经理,如果员工本身是经理的话则不显示,以上例子如下:

emp_nomanager
1000110002

SELECT t1.emp_no,t2.emp_no as manager from dept_emp t1 JOIN dept_manager t2 USING (dept_no) where t1.emp_no <> t2.emp_no;
 

2、

如下有一张商品表(goods),字段依次为:商品id、商品名、商品质量

idnameweight
1A1100
2A220
3B329
4T160
5G233
6C055

还有一张交易表(trans),字段依次为:交易id、商品id、这个商品购买个数

idgoods_idcount
1310
2144
369
412
5265
6523
7320
8216
945
1013

查找购买个数超过20,质量小于50的商品,按照商品id升序排序,如:

idnameweighttotal
2A22081
3B32930
5G23323

select goods_id as id,sum(count) as total from trans GROUP BY goods_id;
select * from goods t1 join(select goods_id as id,sum(count) as total from trans GROUP BY goods_id) t2 using (id) where t2.total > 20 and t1.weight < 50 ORDER BY id;

Java作业

读取文件,统计每个班级的人数,并将结果写入到另一个文件中

package com.shujia.za.homework9;

import java.io.*;
import java.util.HashMap;
import java.util.Set;

public class Test11 {
    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;
        HashMap<String, Integer> map = new HashMap<>();
        try {
            br = new BufferedReader(new FileReader("E:\\IDEAprojects\\bigdata\\students.txt"));
            bw = new BufferedWriter(new FileWriter("E:\\IDEAprojects\\bigdata\\count.txt"));
            String s = null;
            //按行读取文件
            while ((s = br.readLine()) != null) {
                //将读取的一行数据以","分开
                String[] split = s.split(",");
                //判断map集合是否有这个班级,没有就添加,有就人数加一
                if (!map.containsKey(split[4])) {
                    map.put(split[4], 1);
                } else {
                    map.put(split[4], map.get(split[4]) + 1);
                }
            }
            //遍历集合,并写入
            Set<String> set = map.keySet();
            for (String s2 : set) {
                Integer i2 = map.get(s2);
                bw.write(s2 + ":" + i2);
                bw.newLine();
                bw.flush;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

}

2022-05-20作业

一、SQL题

select '80后' as '年龄段',SUM(CREDIT) as '信用卡余额' from TableA join TableB USING (NO) where birth like "198%"
union
select '90后' as '年龄段',SUM(CREDIT) as '信用卡余额' from TableA join TableB USING (NO) where birth like "199%"; 

union是将两个select语句查询出的结果拼接起来,union不包含重复行,union all包含重复行。

使用union必须select结果具有相同数量的列,且union按照select字段的顺序进行拼接,不会按照列名进行匹配。

二、Java程序题

使用java程序模拟hadoop切分文件,统计students文件中,姓名包含'白'汉字的人数。

package com.shujia.za.homework9;
import java.io.*;
import java.util.ArrayList;

public class Test12 {
    public static void main(String[] args) {
        //将文件切分,写入blocks
        try {
            int index = 0;
            BufferedReader br = new BufferedReader(new FileReader("E:\\IDEAprojects\\bigdata\\students.txt"));
            BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\IDEAprojects\\bigdata\\blocks\\block--" + index));
            ArrayList<String> list = new ArrayList<>();
            int offset = 0;
            int num = 0;
            String line = null;
            while ((line = br.readLine()) != null) {
                list.add(line);
                offset++;
                num = 128 * index;
                if (offset == 140) {
                    for (int i = num; i <= num + 127; i++) {
                        String s = list.get(i);
                        bw.write(s);
                        bw.newLine();
                        bw.flush();
                    }
                    offset = 12;
                    index++;
                    bw = new BufferedWriter(new FileWriter("E:\\IDEAprojects\\bigdata\\blocks\\block--" + index));
                }
            }
            for (int i = list.size() - offset; i < list.size(); i++) {
                String s = list.get(i);
                bw.write(s);
                bw.newLine();
                bw.flush();
            }
            bw.close();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

===========================================================================
package com.shujia.za.homework9;
import java.io.*;

public class Test13 {
    public static void main(String[] args) {
        //获取名字中有“白”的学生人数
        File file = new File("E:\\IDEAprojects\\bigdata\\blocks");
        File[] files = file.listFiles();
        int index = 0;
        for (File s : files) {
            try {
                BufferedReader br = new BufferedReader(new FileReader(s));
                String line = null;
                while ((line = br.readLine()) != null) {
                    String[] split = line.split(",");
                    if (split[1].contains("白")) {
                        index++;
                    }
                }
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("名字中含有“白”的有:"+index);
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值