简体   繁体   English

sql查询使用类似查询给我一行

[英]the sql query is giving me a single row using like query

Id  Account_name      Date         Debit    Credit    
1   revenue         2014-05-05    808001    555001      
2   expense         2014-05-05     48000     90000      
3   a/p             2014-05-05       800      1000       
3   a/r             2014-05-05     36000     50000        
4   rent_revenue    2014-05-05     40000     50000       

ok this is my table I want to calculate the total balance of revenue, a/r and the sql query is below given 好的,这是我的表格,我想计算收入, a/r和sql查询的总余额,如下所示

SELECT account_name, SUM(debit)+SUM(credit) AS TOTAL BALANCE 
WHERE  account_name LIKE '%revenue%' 

The problem is that its only showing me the result of account_name='revenue' and its total balance, I want the other row as well what should I do ? 问题是它只向我显示account_name='revenue'的结果及其总余额,我还希望另一行怎么办?

I think you are looking for something like this : 我认为您正在寻找这样的东西:

SELECT account_name ,SUM(debit)+SUM(credit) AS `TOTAL BALANCE`
FROM Tablename
WHERE account_name LIKE '%revenue%' 
GROUP BY account_name

Alternatively, try this.. 或者,尝试这个。

SELECT account_name 
,SUM(debit)+SUM(credit) AS `TOTAL BALANCE`
 WHERE
 Right(account_name,7) = 'revenue'
 GROUP BY account_name

尝试这个:

SELECT account_name , debit+credit AS TOTAL_BALANCE WHERE account_name LIKE '%revenue%' 

If you want the sum of debit and credit returned for each account, you may use 如果您想获得每个帐户的借方和贷方总和,则可以使用

-- sum of debit and credit for each group of accounts that share the same name and match the pattern
SELECT account_name, 
    SUM(debit) + SUM(credit) AS TOTAL_BALANCE 
FROM ACCOUNTS 
WHERE account_name LIKE '%revenue%'
GROUP BY account_name 

or simply 或简单地

-- sum of debit and credit for each account that matches the pattern 
SELECT account_name, 
    debit + credit AS TOTAL_BALANCE 
FROM ACCOUNTS 
WHERE account_name LIKE '%revenue%'

, which gets along without the group by. ,没有分组者的相处。

If you want both the sum over all accounts that match the pattern and the pattern to be returned in the result set, you must somehow include your patterns in the group by clause. 如果要使所有与该模式匹配的帐户的总和和该模式都返回到结果集中,则必须以某种方式将模式包括在group by子句中。 For example 例如

-- returns the respective sums over all debits and credits with the same last 7 signs in their names, as far as the match the pattern in the where clause
SELECT RIGHT(Account_Name, 7) acc_name_tr, 
    SUM(debit) + SUM(credit) AS TOTAL_BALANCE 
FROM Accounts23525506 
WHERE Account_Name LIKE '%revenue%'
GROUP BY RIGHT(Account_Name, 7) 

The last statement should yield 最后一条语句应屈服

acc_name_tr TOTAL_BALANCE
revenue 1453002

Yet I changed your pattern to »account_name ends with 'revenue'«, so be careful about whether the group-by-pattern still matches your purpose. 但是我将您的模式更改为»account_name以'revenue'«结尾,因此请注意逐组模式是否仍符合您的目的。

Use Group By on account_name and then put restrictions on output using having clause. 在account_name上使用Group By,然后使用hading子句对输出进行限制。

SELECT   account_name, SUM(debit+credit) AS [TOTAL BALANCE] 
FROM     Tablename 
GROUP BY account_name  
HAVING   account_name LIKE '%revenue%' .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM