English 中文(简体)
按照ql服务器第08条所载条款,分两行退回
原标题:returning two rows using a GROUP BY clause in sql server 08

我的表格如下:

acc_name        dr_amt     cr_amt

Cash in hand    10000     0
Share Capital   00      1000
Cash in hand    2000      0
Share Capital   0        2000.00
Vehicles        5000      0
Cash in hand    0         5000

询问如下:

SELECT    a.acc_name, sum(j.dr_amt) AS dr_sum, sum(j.cr_amt) AS cr_sum
FROM      journal_voucher_details_mcg AS j 
INNER JOIN  acc_head_mcg AS a ON a.acc_code = j.acc_code 
INNER JOIN  journal_voucher_mcg AS jv ON jv.jv_no = j.jv_no
WHERE       jv.jv_date =  2011-04-08 
GROUP BY    a.acc_name    

我能够取得以下成果:

acc_name        dr_sum  cr_sum  

Cash in hand    3000    5000
Share Capital   0      3000
Vehicles        5000    0   

但我想有两条手头现金条目(以及任何双干_和 cr子都超过0的条目),结果如下:

acc_name        dr_sum  cr_sum  

Cash in hand    3000    0
Cash in hand    0       5000
Share Capital   0      3000
Vehicles        5000    0

I also need the sum of both dr_sum and cr_sum which in this case should be 8000 to both....My final query is

SELECT *, SUM(dr_sum), SUM(cr_sum)
FROM (SELECT a.acc_name, sum(j.dr_amt) AS dr_sum, sum(j.cr_amt) AS cr_sum 
FROM        journal_voucher_details_mcg AS j 
INNER JOIN  acc_head_mcg AS a ON a.acc_code = j.acc_code 
INNER JOIN  journal_voucher_mcg AS jv ON jv.jv_no = j.jv_no
WHERE       jv.jv_date =  2011-04-08 
GROUP BY    j.acc_code)

但是,这不奏效,真的会赞赏你的努力!

<><>Edit>/strong>

最后成果:

acc_name        dr_sum  cr_sum  

Cash in hand    3000    0
Cash in hand    0       5000
Share Capital   0      3000
Vehicles        5000    0
Total           8000    8000

我无法在最后一个学期展示“Total”(“总数”一词)。

最佳回答

the current SQL you have is fine you should not change this. If you need to split and have both entries separated you can do this:

WITH W_RESULT AS (
   -- your current query goes here....
)
SELECT acc_name, dr_sum, 0 AS cr_sum
FROM W_RESULT
WHERE dr_sum <> 0
UNION
SELECT acc_name, 0 AS dr_sum, cr_sum
FROM W_RESULT
WHERE cr_sum <> 0

<>根据最新解决办法,包括讨论表

WITH W_RESULT AS (
   -- your current query goes here....
)
SELECT acc_name, dr_sum, 0 AS cr_sum
FROM W_RESULT
WHERE dr_sum <> 0
UNION
SELECT acc_name, 0 AS dr_sum, cr_sum
FROM W_RESULT
WHERE cr_sum <> 0
UNION ALL
SELECT  Total  AS acc_name, SUM(dr_sum), SUM(cr_sum)
FROM W_RESULT


previous discussion...

您可在问询表中加上总金额:sum(j.dr_amt + j.cr_amt) 和t_sum;如果你需要,作为单独条目,则你可以利用额外栏和UNION栏扩大上述解决办法。 甚至可能更简单地这样做:

WITH W_RESULT AS (
   -- your current query goes here....
)
SELECT acc_name, dr_sum, 0 AS cr_sum, 0 AS t_sum
FROM W_RESULT
WHERE dr_sum <> 0
UNION
SELECT acc_name, 0 AS dr_sum, cr_sum, 0 AS t_sum
FROM W_RESULT
WHERE cr_sum <> 0
UNION
SELECT acc_name, 0 AS dr_sum, 0 AS cr_sum, dr_sum + cr_sum AS t_sum
FROM W_RESULT

进口<>

the current query from the question I m referring to is the first one.
The "final query" at the end of the question is a bit strange.

它与它一样。

  • sum of "Cash in hand 3000" + "Vehicles 5000" = 8000 as dr_sum
  • sum of "Cash in hand 5000" + "Share Capital 3000" = 8000 as cr_sum

我假设的是,有人要求一笔总款像这样的东西。

  • sum of "Cash in hand 3000" + "Cash in hand 5000" = 8000 as t_sum
  • sum of "Share Capital 3000" = 3000 as t_sum
  • sum of "Vehicles 5000" = 5000 as t_sum

请补充一点意见,即正确。

问题回答
;with cte as
(
  select
    a.acc_name,
    case n.n when 1 then sum(j.dr_amt) else 0 end as dr_sum,
    case n.n when 2 then sum(j.cr_amt) else 0 end as cr_sum
  from journal_voucher_details_mcg as j 
    inner join  acc_head_mcg as a
      on a.acc_code = j.acc_code 
    inner join journal_voucher_mcg as jv
      on jv.jv_no = j.jv_no
    cross join (select 1 union all select 2) as n(n)
  where jv.jv_date =  2011-04-08 
  group by a.acc_name, n.n   
)
select
  acc_name,
  dr_sum,
  cr_sum
from cte
where not (dr_sum = 0 and cr_sum = 0)
union all
select 
   Total ,
  sum(dr_sum),
  sum(cr_sum)
from cte

http://www.ohchr.org。

WITH groups AS (
  SELECT
    a.acc_name,
    dr_sum = sum(j.dr_amt),
    cr_sum = sum(j.cr_amt)
  FROM journal_voucher_details_mcg AS j
    INNER JOIN acc_head_mcg AS a ON a.acc_code = j.acc_code 
    INNER JOIN journal_voucher_mcg AS jv ON jv.jv_no = j.jv_no
  WHERE jv.jv_date =  2011-04-08 
  GROUP BY j.acc_code, CASE WHEN dr_amt > 0 THEN 1 ELSE 2 END
)

SELECT
  acc_name,
  dr_sum,
  cr_sum
FROM groups

UNION ALL

SELECT
   Total ,
  SUM(dr_sum),
  SUM(cr_sum)
FROM groups

在2008年服务器(2008年新特点)中,您可在<条码>上添加。 按

SELECT 
        a.acc_name, sum(j.dr_amt) AS dr_sum, sum(j.cr_amt) AS cr_sum 
FROM        
        journal_voucher_details_mcg AS j 
INNER JOIN  
        acc_head_mcg AS a ON a.acc_code = j.acc_code 
INNER JOIN  
        journal_voucher_mcg AS jv ON jv.jv_no = j.jv_no
WHERE       
        jv.jv_date =  2011-04-08 
GROUP BY    
        j.acc_code WITH ROLLUP

在此情况下,请你增加一行,其中<代码>acc_code为NUL,符合所有账户的总额。

因此,你们的新产出应当看一看:

acc_name        dr_sum  cr_sum  

Cash in hand    3000      0
Cash in hand    0         5000
Share Capital   0         3000
Vehicles        5000      0
NULL            8000      8000   <-- that s the line with the totals




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签