如何在sql查询中分隔行组

时间:2014-09-01 13:16:20

标签: sql vb.net ms-access ms-access-2007

我一直致力于查询,如下所示。 (我确实使用VB.NET 2010而DB是MsAccess 2007)

SELECT 
    sale_details.invoiceno AS sale_details_invoiceno, 
    sale_details.product_code,
    sale_details.qty, sale_details.totalkg, sale_details.Rate, sale_details.subtotal,     
    sale_head.invoiceno AS sale_head_invoiceno, sale_head.suppliername, sale_head.invoicedate
FROM 
    sale_head 
INNER JOIN 
    sale_details ON sale_head.[invoiceno] = sale_details.[invoiceno]
WHERE 
    (((sale_head.suppliername)='Ramkrishna Creation'));

显示结果如下:

sale_details_invoice|product_code|qty |totalkg|Rate |Subtotal|Sale_head_invoice|suppliername          |invoicedate

5026                | M 1010     | 10 | 2.5   | 270 |   675  |5026             |Ramkrishna Creation   | 18/07/2014
5026                | CHIKU      | 100| 25    | 500 |  12500 |5026             |Ramkrishna Creation   | 18/07/2014
5026                | F PINK     | 50 | 12.5  | 500 |  6250  |5026             |Ramkrishna Creation   | 18/07/2014
4002                | LJ 16      | 80 | 12    | 350 |  4200  |4002             |Ramkrishna Creation   | 10/08/2014
4002                | BCH 950    | 50 | 12.5  | 150 |  1875  |4002             |Ramkrishna Creation   | 10/08/2014
3598                | L COPPER   | 150| 37.5  | 500 |  18750 |3598             |Ramkrishna Creation   | 10/08/2014
3598                | BCH 950    | 50 | 12.5  | 150 |  1875  |3598             |Ramkrishna Creation   | 10/08/2014
3598                | CHIKU      | 100| 25    | 500 |  12500 |3598             |Ramkrishna Creation   | 18/07/2014

我想打破并显示发票号码的总计。在SQL或VB.NET DataGrid Control中有没有办法显示如下的结果?

sale_details_invoice|product_code|qty |totalkg|Rate |Subtotal|Sale_head_invoice|suppliername          |invoicedate

5026                | M 1010     | 10 | 2.5   | 270 |   675  |5026             |Ramkrishna Creation   | 18/07/2014
5026                | CHIKU      | 100| 25    | 500 |  12500 |5026             |Ramkrishna Creation   | 18/07/2014
5026                | F PINK     | 50 | 12.5  | 500 |  6250  |5026             |Ramkrishna Creation   | 18/07/2014
TOTAL                                                  19425

4002                | LJ 16      | 80 | 12    | 350 |  4200  |4002             |Ramkrishna Creation   | 10/08/2014
4002                | BCH 950    | 50 | 12.5  | 150 |  1875  |4002             |Ramkrishna Creation   | 10/08/2014
TOTAL                                                  6075

3598                | L COPPER   | 150| 37.5  | 500 |  18750 |3598             |Ramkrishna Creation   | 10/08/2014
3598                | BCH 950    | 50 | 12.5  | 150 |  1875  |3598             |Ramkrishna Creation   | 10/08/2014
3598                | CHIKU      | 100| 25    | 500 |  12500 |3598             |Ramkrishna Creation   | 18/07/2014
TOTAL                                                  33125

有没有办法通过SQL或VB.NET DataGrid控件来解决这个问题?我一直在寻找解决方案,但我还没有得到它,请大家,如果你知道,请告诉我

1 个答案:

答案 0 :(得分:0)

您可以在VB中循环计算数据,或者让SQL选择总计。对于后者,您将再次查询相同的数据:

SELECT 
  sd.invoiceno AS sale_details_invoiceno, 
  sd.product_code,
  sd.qty, 
  sd.totalkg, 
  sd.Rate, 
  sd.subtotal,     
  sh.invoiceno AS sale_head_invoiceno, 
  sh.suppliername, 
  sh.invoicedate
FROM sale_head sh
INNER JOIN sale_details sd ON sh.invoiceno = sd.invoiceno
WHERE sh.suppliername = 'Ramkrishna Creation'
UNION ALL
SELECT 
  sd.invoiceno,
  'TOTAL',
  null, 
  null, 
  null, 
  SUM(sd.subtotal),     
  null, 
  null, 
  null
FROM sale_head sh
INNER JOIN sale_details sd ON sh.invoiceno = sd.invoiceno
WHERE sh.suppliername = 'Ramkrishna Creation'
GROUP BY sd.invoiceno 
ORDER BY sale_details_invoiceno, IIF(product_code = 'TOTAL', 1, 0);