序号  烟站   乡镇   村       屯     申请户名字   申请种植亩数   约定商品量
  1  湖南   长沙  长沙村   小二屯     张三          100          200
  2  湖南   长沙  长沙村   吴屯      李四           299           300
  3  湖南   长沙  火星村   王屯      王二           100          200
  4  湖北   武汉  武汉村   张屯      王二           299          99
-------------------------------------------------------------------
这是我想要的结果:
-------------------------------------------------------------------
序号  烟站   乡镇   村       屯     申请户数   申请种植亩数   约定商品量
1    湖南    长沙   2       3       3          499           700
2    湖北    武汉   1       1       1          299            99
==================================================================
能有一条sql语句实现这样的功能吗 ?
谢谢了 
如果不行 要怎么做才能实现 
刚刚学 

解决方案 »

  1.   

    select yanzhan,xiangzhen,count(cun),count(tun),count(name),sum(zhongzhiNum),sum(shopNum) from TableName group by yanzhan,xiangzhenyanzhan :烟站
    xiangzhen:乡镇
    cun:村
    tun:屯
    name:户名
    cun:种植亩数
    shopNum:商品量
    注:你建个表发出来多好,这样写真累
      

  2.   

    select 烟站,乡镇,count (distinct 村),count (屯), count( distinct 申请户名字) 申请户数, sum (申请种植亩数), sum(约定商品量) from tablename group by 烟站 我没试过,你试试
      

  3.   

    这样是可以查询出来 但是在JSP页面里 怎么写呢 ?----------------------------
    <table cellpadding="0" cellspacing="0">
         <tr>
        
         <th>序号</th>
    <th>烟站</th>
    <th>所辖乡镇</th>
    <th>村数</th>
    <th>屯数</th>
    <th>申请户数</th>
                <th>申请种植亩数</th>
                <th>约定商品量(公斤)</th>
         </tr>
            <!--
              List list = managerAppService.findAllCollect(pageNo);
              request.setAttribute("AppCollect", list);
            -->
         <logic:iterate id="ac" name="AppCollect">   
         <tr>
             <td>${ac.id }</td>
             <td>${ac.station }</td>
        <td>${ac.town}</td>
                <td>${ac.village }</td>
                        <td>${ac.team }</td>
                        <td>${ac.name }</td>
        <td>${ac.applyPlantArea }</td>
                        <td>${ac.gmv }</td>
         </tr>
         </logic:iterate>
        
        </table>
    ---------------------------------------------------我这样在jsp里写 出错了
    The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array, but that value cannot be converted to an integer.
      

  4.   

    有谁知道 ?
    我怎么在jsp里显示出来啊 ?谢谢了 急死我了
      

  5.   

    代码没写什么啊
    就在dao实现层 把2楼的查询语句写进去然后再action里调用,转到查询jsp页面层我jsp页面就在3楼 看看是不是那样写的?其它的就没了
      

  6.   

    转什么 ? 转成OBJ的
      

  7.   

    你有没有用javabean?
    你不会就查询放在List里面的吧你先这样: 创建一个javaBean,属性为: 
    序号  烟站  乡镇  村      屯    申请户名字  申请种植亩数  约定商品量
    分别对应 id,station,town,village ,team ,name,applyPlantArea ,gmv ;然后从数据库中查询出结果集的时候,这样写:
    ResultSet rs = pstmt.executeQuery();
    List<Smoke> listSmoke = new ArrayList<Smoke>();
    while(rs.next()){   Smoke smoke = new Smoke(rs.getInt(id),rs.getString(somkestation),rs.getString(xiangzhen),rs.getString(cun),rs.getString(tun),rs.getString(name),rs.getString(mucount),rs.getString(shopcount));
       listSmoke.add(smoke);
    }
    return listSmoke;Servlet中这样写:
    List<Smoke> list = (ArrayList<Smoke>)managerAppService.findAllCollect(pageNo); 
    request.setAttribute("AppCollect", list);
    转发到显示页面在显示页面:
    <logic:iterate id="smoke" name="AppCollect">  
        <tr> 
            <td>${smoke.id } </td> 
            <td>${smoke.station } </td> 
        <td>${smoke.town} </td> 
                <td>${smoke.village } </td> 
                        <td>${smoke.team } </td> 
                        <td>${smoke.name } </td> 
        <td>${smoke.applyPlantArea } </td> 
                        <td>${smoke.gmv } </td> 
        </tr> 
        </logic:iterate> 
    这样试试...
      

  8.   

    dao实现层: 里面的参数是分页 不用管
    public List<TDMonopolizeManagerApp> findAllCollect(int pageNo, int pageSize) {

    if (pageNo < 1)
    {
    return null;
    }
    int offset = (pageNo - 1) * pageSize;
    return findByPage("select station,town,count(distinct village),count(distinct team),count(name),sum(applyPlantArea),sum(gmv) from TDMonopolizeManagerApp group by station,town",offset , pageSize);}
    --------------------------------------------
    然后再Servlet调用了这个方法 后面就一样了JavaBean用到了.
      

  9.   

    TDMonopolizeManagerApp这个BEAN的代码贴出来。
    我觉得是因为你的查询语句是不包含ID的,即索引。所以你这里就有些矛盾了。你在servlet中跟踪一下你返回的list,看看里边ID字段是否有值。
    你在页面显示的时候,序号应该用你迭代的索引去控制。
    <logic:iterate id="ac" name="AppCollect" indexId="index"><td>${index+1}</td>  
      

  10.   

     findByPage("select station,town,count(distinct village),count(distinct team),count(name),sum(applyPlantArea),sum(gmv) from TDMonopolizeManagerApp group by station,town",offset , pageSize);} 
     这个方法的代码片段也可能会出现问题。首先,你查询出来的结果集是不能直接的去使用泛性指定类型,你要获取到查询的结果集,然后迭代,再重新set到一个Bean对象中(和页面显示的BEAN想对应)。