MyException - 我的异常网
当前位置:我的异常网» Java Web开发 » 关于日期连续显示有关问题

关于日期连续显示有关问题(2)

www.MyException.Cn  网友分享于:2013-12-11  浏览:225次

------解决方案--------------------
可以做个报表的封装类

Java code
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计报表数据容器
 * 
 * @param <R>       行名类型
 * @param <C>       列名类型
 *
 * @since 2009-4-28 上午10:41:48
 */
public class Report<R, C> {

    /**
     * 报表容器
     */
    private Map<R, Map<C, Long>> data = new LinkedHashMap<R, Map<C, Long>>();
    
    /**
     * 总计
     */
    private Map<C, Long> total;
    
    /**
     * 列名
     */
    private List<C> columnNames;
    
    /**
     * 构造方法
     * @param rowNames     行名集
     * @param columnNames  列名集
     */
    public Report(R[] rowNames, C[] columnNames) {
        this(Arrays.asList(rowNames), Arrays.asList(columnNames));
    }
    
    public Report(R[] rowNames, Collection<C> columnNames) {
        this(Arrays.asList(rowNames), columnNames);
    }
    
    public Report(Collection<R> rowNames, C[] columnNames) {
        this(rowNames, Arrays.asList(columnNames));
    }
    
    public Report(Collection<R> rowNames, Collection<C> columnNames) {
        this.columnNames = new ArrayList<C>(columnNames);
        init(rowNames, columnNames);        
    }
    
    /**
     * 根据行名、列名存放数据
     * @param rowName
     * @param columnName
     * @param count
     *
     * @since 2009-4-28 上午10:42:15
     */
    public void addData(R rowName, C columnName, Long count) {
        Map<C, Long> row = data.get(rowName);
        row.put(columnName, row.get(columnName) + count);
        
        total.put(columnName, total.get(columnName) + count);
    }
    
    public void addData(R rowName, C columnName, Integer count) {
        addData(rowName, columnName, count.longValue());
    }

    public List<C> getColumnNames() {
        return columnNames;
    }

    public Map<C, Long> getTotal() {
        return total;
    }
    
    public Map<R, Map<C, Long>> getData() {
        return data;
    }
    
    /**
     * 初始化数据报表
     * @param rowNames
     * @param columnNames
     *
     * @since 2009-4-28 上午10:43:53
     */
    private void init(Collection<R> rowNames, Collection<C> columnNames) {
        for(Iterator<R> i = rowNames.iterator(); i.hasNext(); ) {
            data.put(i.next(), generateRow(columnNames));
        }
        total = generateRow(columnNames);
    }
    
    /**
     * 初始化报表中的一行
     * @param columnNames
     * @return
     *
     * @since 2009-4-28 上午10:43:32
     */
    private Map<C, Long> generateRow(Collection<C> columnNames) {
        Map<C, Long> row = new LinkedHashMap<C, Long>();
        for(Iterator<C> i = columnNames.iterator(); i.hasNext(); ) {
            row.put(i.next(), 0L);
        }
        return row;
    }
}

------解决方案--------------------

create table DAYPRODUCT(id int identity,date smalldatetime,quantity int)

insert DAYPRODUCT
select '2008-3-10',1000
union all
select '2008-3-11',1001
union all
select '2008-3-12',1002
union all
select '2008-3-13',1003
union all
select '2008-3-14',1004
union all
select '2008-3-15',1005

declare @reportDate smalldatetime
set @reportDate='2008-3-1'

;with product as(select date,quantity from DAYPRODUCT where date between @reportDate and dateadd(mm,1,@reportDate)-1)

select * from product
union all 
select date,0 quantity from (select top 31 @reportDate+ row_number() over(order by colid)-1 date from syscolumns) tmp
where date<dateadd(mm,1,@reportDate) and not exists(select 1 from product where date=tmp.date)
order by date

drop table DAYPRODUCT
------解决方案--------------------

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有