I've table named 'detail_transaksi'. I want to SUM all of the data in "jumlah" column where "debit_kredit" = 'D' which is grouped in the same "kode_akun" data. Also, I want to SUM all of the data in "jumlah" column where "debit_kredit" = 'K' which is grouped in the same "kode_akun" data. Then, I want to SUBSTRACT both of them. How can I do it in SQL or maybe in PHP?
Here's columns in my table:
id
kode_transaksi
kode_akun
debit_kredit
jumlah
You can use conditional aggregation:
select kode_akun,
sum(case when debit_kredit = 'K' then jumlah
when debit_kredit = 'D' then - jumlah
end)
from detail_transaksi
group by kode_akun;
You can use GROUP BY debit_kredit to grouping SUM of records by debit_kredit value
and after that, you can do subtract or whatever you want on your data.
SELECT debit_kredit , SUM(jumlah) AS total_sum FROM detail_transaksi GROUP BY debit_kredit
You need to sum if for both K and D in debit_kredit
The following should achieve that.
SELECT
kode_akun,
SUM(CASE WHEN debit_kredit='K' then jumlah else 0 end) as jumlah_k,
SUM(CASE WHEN debit_kredit='D' then jumlah else 0 end) as jumlah_d,
FROM detail_transaksi GROUP BY kode_akun
As for the difference between jumlah_k and jumlah_d you would have to subtract the sum expressions,this is rather inefficient, I would advice you to do that in php
Related
I'm working with Yii2 and using ActiveRecords. I am trying find ethnic distribution in a department. I have a query that groups the staff into their tribes and further groups them according to gender and returns the total count in each case.
The end result should look similar to this photo:
The ethnic subtotal is given by adding all the totals of all the tribes in the department. So far I have all the totals for all the tribes in each department. How can I add these totals to get the ethnic subtotal?
My code:
$query = StaffEmploymentListView::find()
->select([
'DEPT_NAME',
'TRIBE_NAME',
"COUNT(CASE WHEN GENDER='MALE' THEN 1 END) AS MALE_COUNT",
"COUNT(CASE WHEN GENDER='FEMALE' THEN 1 END) AS FEMALE_COUNT",
"COUNT(TRIBE_NAME) AS TRIBE_COUNT",
])
->groupBy(['DEPT_NAME','TRIBE_NAME']);
Raw SQL answers are also welcome.
I am not familiar with yii, but generally speaking you can just use + to add values, so your selection criteria would be something like count(...) + count(...) will select the sum of those two counts.
select t.DEPT_NAME,
SUM(CASE WHEN GENDER = 'MALE' OR GENDER = 'FEMALE' THEN 1 END) as SUM,
tt.MALE_COUNT,
tt.FEMALE_COUNT,
tt.TRIBE_COUNT,
tt.TRIBE_NAME from 'StaffEmploymentListView' t
left join (
select DEPT_NAME,
TRIBE_NAME,
SUM(CASE WHEN GENDER = 'MALE' THEN 1 END) as MALE_COUNT,
SUM(CASE WHEN GENDER = 'FEMALE' THEN 1 END) as FEMALE_COUNT,
count(TRIBE_NAME) as TRIBE_COUNT
from 'StaffEmploymentListView' t
group by DEPT_NAME, TRIBE_NAME) tt on t.DEPT_NAME = tt.DEPT_NAME group by t.DEPT_NAME, tt.MALE_COUNT, tt.FEMALE_COUNT, tt.TRIBE_COUNT, tt.TRIBE_NAME;
This maybe can help you, i plat with postgresql
I have two tables one is "invoices" and other is "invoice_items".so i want to generate report using these two tables.
This should do the trick...please have a look on CASE WHEN
select i.Date, i.No,sum(CASE WHEN t.VAT<>'no' THEN
amount ELSE 0 END) as Excluding_VAT,
sum(t.amt_vat)as vatamount,
sum(CASE WHEN t.VAT='no' THEN amount ELSE 0 END) as NonVat,
sum(t.amt_vat+t.amount)as totamt
from a i join b t on i.ID=t.ID
where i.Date between '1991-11-18' and '1995-11-19'
group by i.ID,i.No,i.Date
this is for Sale_Value_Excluding_VAT
SELECT SUM(amount) FROM (select amount from invoice_items join invoices on invoices.invoiceid=invoice_items.invoiceid
where includevat=TRUE) AS T
I've been trying to figure out how to count all column values and add them to each other.
What i want to achieve is that my code will calculate all the price values, where check = 1 and add them to eachother as $total.
My goal : So i can divide the total result where check = 1, and check is 0, and subtract them from eachother.
I apologize for my paint skills.
Try this:
SELECT sum(price) as Total from YOUR_TABLE where `check` = 1;
You can try the following:
Step 1: Query all results from the table.
Step 2: Loop through the result.
Step 3: Create two array array1 and array2
Step 4: Inside the loop check condition if (check == 1) stored price value in array1 else if (check == 0) store in array2
Step 5: sum values of array1 and array2 in different variables and finally subtract from each other.
or try the following query:
SELECT SUM(CASE WHEN check='0' THEN price END) as zerossum,
SUM(CASE WHEN check='1' THEN price END) as onessum
FROM tablename
You can get the complete answer using just SQL if you want
SELECT ones - zeros as theTotal
FROM
(
SELECT SUM(CASE WHEN `check`=0 THEN price END) as zeros,
SUM(CASE WHEN `check`=1 THEN price END) as ones
FROM test
) test1;
I have a table with a field called type, where 3 various rows are possible: 1, 2 and 3.
Now, I don't care about 3 at all. I need to count how many rows there are with type = 1 and with type = 2. I am doing this with 2 queries, like this:
Query1: SELECT COUNT(id) as count FROM users WHERE type='1'
Query2: SELECT COUNT(id) as count FROM users WHERE type='2'
Can I do this with only 1 single query? If so, should I, or not? How would the query look?
SELECT type,
COUNT(id) AS count
FROM users
WHERE type IN ('1','2')
GROUP BY type
SELECT type, COUNT(id) AS count
FROM users
GROUP BY type
HAVING type < 3
SELECT sum(case when type = '1' then 1 else 0 end) as count_for_one,
sum(case when type = '2' then 1 else 0 end) as count_for_tow
FROM users
WHERE type IN ('1','2')
If you want separate numbers,
SELECT SUM(IF(type='1', 1, 0)), SUM(IF(type='2', 1, 0))
FROM users WHERE type IN ('1', '2')
I have a table with two columns:
column A column B
1 2
1 2
2 1
I want to return total of ones = 3 total of twos = 3
The best I can come up with is two queries like so:
SELECT sum(CASE WHEN columnA =1 THEN 1 ELSE 0 END )
+ sum(CASE WHEN columnB =1 THEN 1 ELSE 0 END )
SELECT sum(CASE WHEN columnA =2 THEN 1 ELSE 0 END )
+ sum(CASE WHEN columnB =2 THEN 1 ELSE 0 END )
Can this be done in one query?
Thanks
You didn't specify if you want to do this as 2 rows or as 2 values in a row.
Two rows are somewhat obvious (just union together all the values from each columns, and count(1) group by value against the result of the union; so I'll assume you want to do one row.
If you only have 1s or 2s, it's simple:
SELECT SUM(A+B-2) 'twos', SUM(4-A-B) 'ones' FROM myTable
SELECT SUM(IF(columnA=1, 1, 0) + IF(columnB=1, 1, 0)) as ones,
SUM(IF(columnA=2, 1, 0) + IF(columnB=2, 1, 0)) as twos
FROM myTable;
C.
To get everything in one query, I would try something like this.
SELECT Result.Val, COUNT(Result.Val) AS Count
FROM (
SELECT ColumnA AS Val
FROM TableName
UNION
SELECT ColumnB AS Val
FROM TableName
) AS Result
GROUP BY Result.Val
In general, you would count things like so:
SELECT columnA, COUNT(*) FROM myTable
GROUP BY columnA
to get the count of all different values in columnA.
SELECT COUNT(*) FROM table WHERE columnA=1 or columnB=1