zoukankan      html  css  js  c++  java
  • sql查询总结

    这周的任务有一项是做数据统计

    统计内容如下:

    一张表中的会议类型有党小组会议、党员大会、党委会议、区划字段

    要求:统计出每种区划下的每种会议类型的正常开展、逾期开展、逾期未开展

    我设想的思路:先把每个区划查询出来并去重复  

           然后把每个区划下的各种会议总数统计出来

           然后在查询每种会议的正常开展、会议逾期开展的在用每种会议的总数减去正常开展、会议逾期的个数

           这样构建一条数据传到前台显示就相当于下面图文中的数据样式

    实现这种样式我用的是mvc+mybatis,下面我们上代码前台的就不显示都是差不多一样的你们改下变量就差不多了

    controller

    @RequestMapping("statquery")
        public ModelAndView statquery(Page page) throws Exception {
            ModelAndView mv = new ModelAndView();
            PageData pd = this.getPageData();
            page.setPd(pd);
            List<PageData> list  =    meetingService.stat(page);
            mv.addObject("list", list);
            
            List<PageData> map = new ArrayList<PageData>();
            
            List<PageData> total  =    meetingService.total(page);
            List<PageData> finsk = meetingService.finsk(page);
            List<PageData> total2 = meetingService.total2(page);
            List<PageData> total3 = meetingService.total3(page);
            List<PageData> total4 = meetingService.total4(page);
            for (PageData qh : finsk) {
                PageData gj = new PageData();
                gj.put("total",qh.getInt("ta"));
                int num = 0;
                int dy = 0;
                int dw = 0;
                int dx = 0;
                for (PageData disk4 : total4) {
                    num=disk4.getInt("total");
                                for (PageData pageData : total) {
                                    if(qh.getString("QHDM").equals(pageData.getString("QHDM"))){
                                        gj.put("QHMC", pageData.getString("QHMC"));
                                        String type = pageData.getString("TYPE");
                                        System.out.println(type.equals("党员大会"));
                                        if(type.equals("党员大会")){
                                            gj.put("dydh", pageData.getInt("total"));
                                            dy=num-pageData.getInt("total");
                                        }
                                        if(type.equals("党委会议")){
                                            gj.put("dwhy", pageData.getInt("total"));
                                            dw=num-pageData.getInt("total");
                                        }
                                        if(type.equals("党小组会议")){
                                            gj.put("dxzhy", pageData.getInt("total"));
                                            dx=num-pageData.getInt("total");
                                        }
                                    }
                                }
                                for (PageData pageData : total2) {
                                    if(qh.getString("QHDM").equals(pageData.getString("QHDM"))){
                                        String type = pageData.getString("TYPE");
                                        if(type.equals("党员大会")){
                                            gj.put("dydh1", pageData.getInt("total"));
                                            dy=dy-pageData.getInt("total");
                                        }
                                        if(type.equals("党委会议")){
                                            gj.put("dwhy1", pageData.getInt("total"));
                                            dw=dw-pageData.getInt("total");
                                        }
                                        if(type.equals("党小组会议")){
                                            gj.put("dxzhy1", pageData.getInt("total"));
                                            dx=dx-pageData.getInt("total");
                                        }
                                    }
                                }
                }
                gj.put("dydh2",dy);
                gj.put("dwhy2",dw);
                gj.put("dxzhy2",dx);
                map.add(gj);
            }
            mv.addObject("map",map);
            List<PageData> tqhList = tqhService.listAll(pd);// 列出所有区划
            mv.addObject("tqhList", tqhList);
            User user= (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
            //会议类型
            List<Dictionaries> mTypeList = dictionariesService.listDicByBanma(Const.DIC_MEET_TYPE);
            mv.addObject("mTypeList", mTypeList);
            //用户的党组织
            PageData mpartyPd = new PageData();
            //mpartyPd.put("USER_ID", user.getUSER_ID());
            List<PageData> partyList = mpartyService.listAll(mpartyPd);
            mv.addObject("partyList", partyList);
    
            //会议任务
            List<PageData> mTaskList = mtaskService.listAll(new PageData());
            mv.addObject("mtaskList", mTaskList);
            mv.setViewName("meeting/mstat/meeting_stat");
    
            return mv;
        }

    service

    @Override
        public List<PageData> total2(Page page) throws Exception {
            // TODO Auto-generated method stub
            return (List<PageData>) dao.findForList("MeetingMapper.total2",  page);
        }
    
        @Override
        public List<PageData> total3(Page page) throws Exception {
            // TODO Auto-generated method stub
            return (List<PageData>) dao.findForList("MeetingMapper.total3",  page);
        }
    
        @Override
        public List<PageData> total4(Page page) throws Exception {
            // TODO Auto-generated method stub
            return (List<PageData>) dao.findForList("MeetingMapper.total4",  page);
        }
    
        @Override
        public List<PageData> finsk(Page page) throws Exception {
            // TODO Auto-generated method stub
            return (List<PageData>) dao.findForList("MeetingMapper.finsk",  page);
        }
        
        @Override
        public List<PageData> total(Page page) throws Exception {
            // TODO Auto-generated method stub
            return (List<PageData>) dao.findForList("MeetingMapper.total",  page);
        }
        
        @Override
        public List<PageData> disk(Page page) throws Exception {
            // TODO Auto-generated method stub
            return (List<PageData>) dao.findForList("MeetingMapper.disk",  page);
        }

    mapper.xml

    <select id="total"  parameterType="page" resultType="pd">
            select
            count( * )as total,
            m.QHDM,
            tqh.QHMC,
            m.MPARTY_ID,
            m_mparty.PARTY_NAME,
            m.TYPE 
            from
            <include refid="tableName"></include> m
            LEFT JOIN sys_tqh tqh ON m.QHDM = tqh.QHDM
            LEFT JOIN m_mparty ON m_mparty.MPARTY_ID = m.MPARTY_ID 
            where 1=1
                AND m.state = '1' 
            group by 
                m.QHDM,
                m.type,
                MPARTY_ID
        </select>
        
        <select id="total2"  parameterType="page" resultType="pd">
            select
            count( * )as total,
            m.QHDM,
            tqh.QHMC,
            m.MPARTY_ID,
            m_mparty.PARTY_NAME,
            m.TYPE 
            from
            <include refid="tableName"></include> m
            LEFT JOIN sys_tqh tqh ON m.QHDM = tqh.QHDM
            LEFT JOIN m_mparty ON m_mparty.MPARTY_ID = m.MPARTY_ID 
            where 1=1
                AND m.state = '2' 
            group by 
                m.QHDM,
                m.type,
                MPARTY_ID
        </select>
        
        <select id="total3"  parameterType="page" resultType="pd">
            select
            count( * )as total,
            m.QHDM,
            tqh.QHMC,
            m.MPARTY_ID,
            m_mparty.PARTY_NAME,
            m.TYPE 
            from
            <include refid="tableName"></include> m
            LEFT JOIN sys_tqh tqh ON m.QHDM = tqh.QHDM
            LEFT JOIN m_mparty ON m_mparty.MPARTY_ID = m.MPARTY_ID 
            where 1=1
                AND m.state = '0' 
            group by 
                m.QHDM,
                m.type,
                MPARTY_ID
        </select>
        
        <select id="total4"  parameterType="page" resultType="pd">
        select 
        count(*) as total ,QHDM,TYPE 
        from 
        <include refid="tableName"></include>
        GROUP BY TYPE,QHDM;
        
        </select>
        
        <!-- 去除重复数据 -->
        <select id="disk" parameterType="page" resultType="pd">
            select 
            DISTINCT QHDM,count(*) as ta
            from 
            <include refid="tableName"></include>
            GROUP BY QHDM
        </select>
        
        <!-- 去除重复数据 -->
        <select id="finsk" parameterType="pd" resultType="pd">
            select 
            DISTINCT QHDM,count(*) as ta
            from 
            <include refid="tableName"></include>
            <if test="pd.tqh!= null and pd.tqh != ''"><!-- 关键词检索 -->
                where 
                    QHDM = #{pd.tqh}
            </if>
            GROUP BY QHDM
        </select>

    接口就不上了都差不多

    开始做的时候有点懵,后来慢慢理了下思路找到这么个笨方法如何有更好的办法。各位大佬请指出来我也借鉴下。

  • 相关阅读:
    python中os模块中文帮助
    TypeError: string indices must be integers, not str
    ValueError: multi-byte encodings are not supported
    Codeforces Round #620 (Div. 2)E(LCA求树上两点最短距离)
    Codeforces Round #620 (Div. 2)D(LIS,构造)
    Codeforces Round #619 (Div. 2)D(模拟)
    Codeforces Round #619 (Div. 2)C(构造,容斥)
    Educational Codeforces Round 82 (Rated for Div. 2)E(DP,序列自动机)
    Educational Codeforces Round 82 (Rated for Div. 2)D(模拟)
    【PAT甲级】1114 Family Property (25分)(并查集)
  • 原文地址:https://www.cnblogs.com/wolf-shuai/p/13067566.html
Copyright © 2011-2022 走看看