vb.net 将数据库中表1的数据(商品编码,名称,规格,数量,日期等),在选择的日期范围内合并(商品编码,名称,规格)相同的数据,且数量大于1,与有相同商品编码的表2中库存 以及 有相同商品编码的表2中销量 显示在datagridviewx 控件上 ,可以使用inner join ,group by ,rollup详细代码
时间: 2024-03-21 08:44:28 浏览: 124
首先,需要连接数据库并查询表1中的数据:
```
Dim connString As String = "Data Source=YourDatabaseServerName;Initial Catalog=YourDatabaseName;Integrated Security=True"
Dim conn As New SqlConnection(connString)
Dim cmd As New SqlCommand()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT 商品编码, 名称, 规格, SUM(数量) AS 数量, CONVERT(date, 日期) AS 日期 FROM 表1 WHERE 日期 BETWEEN @FromDate AND @ToDate GROUP BY 商品编码, 名称, 规格, CONVERT(date, 日期) HAVING SUM(数量) > 1"
cmd.Parameters.AddWithValue("@FromDate", fromDate)
cmd.Parameters.AddWithValue("@ToDate", toDate)
Dim adapter As New SqlDataAdapter(cmd)
Dim table1 As New DataTable()
adapter.Fill(table1)
```
然后,需要查询表2中与表1中商品编码相同的数据,包括库存和销量:
```
cmd.CommandText = "SELECT 商品编码, SUM(库存) AS 库存, SUM(销量) AS 销量 FROM 表2 WHERE 商品编码 IN (SELECT DISTINCT 商品编码 FROM 表1 WHERE 日期 BETWEEN @FromDate AND @ToDate AND SUM(数量) > 1) GROUP BY 商品编码"
Dim table2 As New DataTable()
adapter.Fill(table2)
```
接下来,使用INNER JOIN将表1和表2中的数据合并:
```
cmd.CommandText = "SELECT t1.商品编码, t1.名称, t1.规格, t1.数量, t1.日期, t2.库存, t2.销量 FROM (SELECT 商品编码, 名称, 规格, SUM(数量) AS 数量, CONVERT(date, 日期) AS 日期 FROM 表1 WHERE 日期 BETWEEN @FromDate AND @ToDate GROUP BY 商品编码, 名称, 规格, CONVERT(date, 日期) HAVING SUM(数量) > 1) AS t1 INNER JOIN (SELECT 商品编码, SUM(库存) AS 库存, SUM(销量) AS 销量 FROM 表2 WHERE 商品编码 IN (SELECT DISTINCT 商品编码 FROM 表1 WHERE 日期 BETWEEN @FromDate AND @ToDate AND SUM(数量) > 1) GROUP BY 商品编码) AS t2 ON t1.商品编码 = t2.商品编码"
Dim table3 As New DataTable()
adapter.Fill(table3)
```
最后,使用ROLLUP将结果按照商品编码、名称、规格、日期、总计进行汇总,并绑定到DataGridView控件上:
```
Dim result = From row In table3.AsEnumerable()
Group row By 商品编码 = row.Field(Of String)("商品编码"), 名称 = row.Field(Of String)("名称"), 规格 = row.Field(Of String)("规格"), 日期 = row.Field(Of Date)("日期") Into g = Group
Select New With {
.商品编码 = 商品编码,
.名称 = 名称,
.规格 = 规格,
.日期 = If(日期 = DateTime.MinValue, "总计", 日期.ToString("yyyy-MM-dd")),
.数量 = g.Sum(Function(row) row.Field(Of Integer)("数量")),
.库存 = g.Sum(Function(row) row.Field(Of Integer)("库存")),
.销量 = g.Sum(Function(row) row.Field(Of Integer)("销量"))
} Order By 商品编码, 名称, 规格, 日期
DataGridView1.DataSource = result.ToList()
```
完整的代码如下:
```
Dim connString As String = "Data Source=YourDatabaseServerName;Initial Catalog=YourDatabaseName;Integrated Security=True"
Dim conn As New SqlConnection(connString)
Dim cmd As New SqlCommand()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT 商品编码, 名称, 规格, SUM(数量) AS 数量, CONVERT(date, 日期) AS 日期 FROM 表1 WHERE 日期 BETWEEN @FromDate AND @ToDate GROUP BY 商品编码, 名称, 规格, CONVERT(date, 日期) HAVING SUM(数量) > 1"
cmd.Parameters.AddWithValue("@FromDate", fromDate)
cmd.Parameters.AddWithValue("@ToDate", toDate)
Dim adapter As New SqlDataAdapter(cmd)
Dim table1 As New DataTable()
adapter.Fill(table1)
cmd.CommandText = "SELECT 商品编码, SUM(库存) AS 库存, SUM(销量) AS 销量 FROM 表2 WHERE 商品编码 IN (SELECT DISTINCT 商品编码 FROM 表1 WHERE 日期 BETWEEN @FromDate AND @ToDate AND SUM(数量) > 1) GROUP BY 商品编码"
Dim table2 As New DataTable()
adapter.Fill(table2)
cmd.CommandText = "SELECT t1.商品编码, t1.名称, t1.规格, t1.数量, t1.日期, t2.库存, t2.销量 FROM (SELECT 商品编码, 名称, 规格, SUM(数量) AS 数量, CONVERT(date, 日期) AS 日期 FROM 表1 WHERE 日期 BETWEEN @FromDate AND @ToDate GROUP BY 商品编码, 名称, 规格, CONVERT(date, 日期) HAVING SUM(数量) > 1) AS t1 INNER JOIN (SELECT 商品编码, SUM(库存) AS 库存, SUM(销量) AS 销量 FROM 表2 WHERE 商品编码 IN (SELECT DISTINCT 商品编码 FROM 表1 WHERE 日期 BETWEEN @FromDate AND @ToDate AND SUM(数量) > 1) GROUP BY 商品编码) AS t2 ON t1.商品编码 = t2.商品编码"
Dim table3 As New DataTable()
adapter.Fill(table3)
Dim result = From row In table3.AsEnumerable()
Group row By 商品编码 = row.Field(Of String)("商品编码"), 名称 = row.Field(Of String)("名称"), 规格 = row.Field(Of String)("规格"), 日期 = row.Field(Of Date)("日期") Into g = Group
Select New With {
.商品编码 = 商品编码,
.名称 = 名称,
.规格 = 规格,
.日期 = If(日期 = DateTime.MinValue, "总计", 日期.ToString("yyyy-MM-dd")),
.数量 = g.Sum(Function(row) row.Field(Of Integer)("数量")),
.库存 = g.Sum(Function(row) row.Field(Of Integer)("库存")),
.销量 = g.Sum(Function(row) row.Field(Of Integer)("销量"))
} Order By 商品编码, 名称, 规格, 日期
DataGridView1.DataSource = result.ToList()
```
阅读全文