string time1 = dateTimePicker1.Value.Date.ToShortDateString();
string time2 = dateTimePicker2.Value.Date.ToShortDateString();string sql1 = "select  distinct  收入来源,count(收入来源),sum(收入金额)  from  总收入表  where 收   入时间  between  '" + time1 + "' and  '" + time2 + "' group  by 收入来源"; string sql2 = "select distinct 支出项目,count(支出项目),sum(支出金额) from  总支出表  where 支出时间  between  '" + time1 + "' and  '" + time2 + "' group  by  支出项目"; 
我想实现通过一段时间内的查询得出统计表:
我想了好久,都没做出来,我还是个学生,想多学习一些,如果真的不能做出来了,请给我一些更好的方法!!谢谢大家了!!!

解决方案 »

  1.   

    select xxxxxx
    union all
    selec xxxx
      

  2.   

    select * from 
    (select  distinct  收入来源,count(收入来源),sum(收入金额)  from  总收入表  where 收   入时间  between  '" + time1 + "' and  '" + time2 + "' group  by 收入来源) a,
    (select distinct 支出项目,count(支出项目),sum(支出金额) from  总支出表  where 支出时间  between  '" + time1 + "' and  '" + time2 + "' group  by  支出项目) b你每个都查询只有一条数据,用笛卡尔就可以了
      

  3.   


    好吧,无视3楼这一段吧
    你这种与其纠结于如何合成1条sql,还不如在程序中通过展示的方式将两张表并成一张表收入项目和支出项目肯定不等的啊
      

  4.   

    合并两个Table        protected DataTable MergeDataTable(DataTable dt1, DataTable dt2, string KeyColName,bool isMergeAll)
            {
                //合并列
                DataTable ndt1 = dt1.Copy();
                DataTable ndt2 = dt2.Copy();
                ndt2.PrimaryKey = new DataColumn[] { ndt2.Columns[KeyColName] };
                for (int i = 0; i < ndt2.Columns.Count; i++)
                {
                    if (ndt2.Columns[i].ColumnName.ToLower().Trim() != KeyColName.ToLower().Trim())
                    {
                        ndt1.Columns.Add(ndt2.Columns[i].ColumnName);
                    }
                }            //合并相同主键的数据
                for (int i = 0; i < ndt1.Rows.Count; i++)
                {
                    DataRow inrow = ndt2.Rows.Find(ndt1.Rows[i][KeyColName]);
                    if (inrow != null)
                    {
                        for (int j = 0; j < ndt2.Columns.Count; j++)
                        {
                            if (ndt2.Columns[j].ColumnName.ToLower().Trim() != KeyColName.ToLower().Trim())
                            {
                                ndt1.Rows[i][ndt2.Columns[j].ColumnName] = inrow[ndt2.Columns[j].ColumnName];
                            }
                        }
                        if (isMergeAll)
                        {
                            ndt2.Rows.Remove(inrow);
                        }
                    }
                }
                if (isMergeAll)
                {
                    //合并不同主键的数据
                    for (int i = 0; i < ndt2.Rows.Count; i++)
                    {
                        DataRow row = ndt1.NewRow();
                        for (int j = 0; j < ndt2.Columns.Count; j++)
                        {
                            row[ndt2.Columns[j].ColumnName] = ndt2.Rows[i][ndt2.Columns[j].ColumnName];
                        }
                        ndt1.Rows.Add(row);
                    }
                }
                return ndt1;
            }
      

  5.   


    好吧,无视3楼这一段吧
    你这种与其纠结于如何合成1条sql,还不如在程序中通过展示的方式将两张表并成一张表收入项目和支出项目肯定不等的啊
    那该怎么弄呢?我现在茫然的很,完全就不知道咋个弄了??
      

  6.   

    List<收入实体>  list1;
    List<支出实体>  list2;
    int maxCount = Math.Max(list1.Count,list2.Count);
    for(int i=list1.Count;i<maxCount;i++)
    {
        list1.Add(new 收入实体());
    }
    for(int i=list2.Count;i<maxCount;i++)
    {
        list2.Add(new 支出实体());
    }List<绑定用的实体> list3 = new List<绑定用的实体>();
    for(int i=0;i<maxCount ;i++)
    {
       list3.Add(new 绑定用的实体(){收入=list1[i],支出=list2[i]});
    }
      

  7.   

    给你建议:1、select xxxxxx
    union all
    select xxxx 
    2、9楼和13楼得方法都可以,13楼得估计你还得学一阵子,9楼得可以前提是你的数据库列数字段名必须相同
      

  8.   

    比较推荐数据出来之后,对数据集进行操作,太复杂的SQL语句不太适合学生吧。
      

  9.   

    不一样的用别名,两个数据表的栏位一样,用Union纵向合并
      

  10.   

    是在要一句sqlSELECT * FROM 
    (
    select   ROW_NUMBER() OVER (ORDER BY 收入来源) AS RowNum, 收入来源,count(1),sum(收入金额)  from  总收入表  where 收入时间  between  '" + time1 + "' and  '" + time2 + "' group  by 收入来源) t
    FULL JOIN
    (select   ROW_NUMBER() OVER (ORDER BY 支出项目) AS RowNum,支出项目 ,count(1),sum(支出金额) from  总支出表  where 支出时间  between  '" + time1 + "' and  '" + time2 + "' group  by  支出项目) t2
    ON t.RowNum = t2.RowNum