4,815
社区成员




--> SQL查询
select T1.年月,T1.单位,产量,事故数量 from
(select convert(varchar(7), 日期, 120) as 年月,单位,sum(产量) as 产量 from t1
group by convert(varchar(7), 日期, 120),单位) T1
left join --这边连接看你t1,t2表的数据情况
(select convert(varchar(7), 日期时间, 120) as 年月,单位,sum(事故数量) as 事故数量 from t2
group by convert(varchar(7), 日期时间, 120),单位) T2
on T1.年月=T2.年月 and T1.单位=T2.单位
Dim ds1 As New DataSet1
Dim hostName As String = System.Net.Dns.GetHostName()
Dim ConnString As String = "Data Source=" + hostName + ";Initial Catalog=系统;" + "Integrated Security=True;Pooling=False"
Dim conn As New SqlConnection(ConnString)
Dim strSQL As String = _
"select T1.月份,一部产量,一部事故数量,二部产量,二部事故数量,三部产量,三部事故数量" + _
"from (" + _
"select Month(日期) as 月份," + _
"sum(case 单位 when '一部' then 产量 else 0 end) 一部产量," + _
"sum(case 单位 when '二部' then 产量 else 0 end) 二部产量," + _
"sum(case 单位 when '三部' then 产量 else 0 end) 三部产量 " + _
"from t1 group by Month(日期)) T1,(" + _
"select Month(日期时间) as 月份," + _
"sum(case 单位 when '一部' then 事故数量 else 0 end) 一部事故数量," + _
"sum(case 单位 when '二部' then 事故数量 else 0 end) 二部事故数量," + _
"sum(case 单位 when '三部' then 事故数量 else 0 end) 三部事故数量 " + _
"from t2 group by Month(日期时间)) T2 where T1.月份=T2.月份" 'SQL查询
Dim da As New SqlDataAdapter(strSQL, conn)
da.Fill(ds1.DataTable1) '填充到数据集DataSet1,也就是水晶报表“骨架”
Dim myReport As New CrystalReport1
myReport.SetDataSource(ds1)
CrystalReportViewer1.ReportSource = myReport