Use columns as where and has subtraction in it - sql

My problem is I just wanna show all the unpaid rows:
I have this data:
id name commission paid
1 James 15000 2000
2 Curry 15000 15000
3 Durant 15000 0
4 Wade 15000 5000
5 Harden 15000 15000
I wanted to get only the rows that are not gonna be equal to 0, I wanted to use where clause for commission - paid if it is greater than 0
Output that I would expect is:
id name commission paid
1 James 15000 2000
3 Durant 15000 0
4 Wade 15000 5000

SELECT * FROM table_name WHERE (commission - paid) > 0
Or
SELECT * FROM table_name WHERE commission!=paid
This will work too because either subtraction will be greater than zero or 0 (assuming paid amount is always less than commission)

You can use the following solution using a simple calculation on WHERE:
SELECT * FROM table_name WHERE commission - paid > 0
You can also use the following:
SELECT * FROM table_name WHERE commission > paid
Note: In this case someone can overpay the commission. Using != or <> the overpay would be found as not paid.
demo: https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=a391576f2df2c5d7c8616485b2c70099
To get all paid rows you can use the following:
SELECT * FROM table_name WHERE commission - paid <= 0
or ...
SELECT * FROM table_name WHERE commission <= paid

you can use the following query given below using WHERE clause
SELECT * FROM commision_table_name WHERE (commission - paid) > 0;

Related

Monthly cumulative differences calculation in postgres

I am trying to write an sql script in postgres that find cumulative difference in total price and repayment amount. I have two tables as shown below. I have gone through solution provided here but it doesn't address my question.
item table
item_id cost_price date_purchase
1 200 01-06-2019
2 300 10-07-2019
3 250 15-08-2019
4 400 10-09-2019
payment table
item id payment payment date
1 50 01-06-2019
1 40 20-06-2019
2 30 15-07-2019
1 60 17-07-2019
2 100 15-08-2019
3 90 17-08-2019
4 300 20-09-2019
1 50 25-09-2019
Expected result
Month Remaining amount
06_2019 (200 - 90) = 110
07_2019 (200+ 300) - (90 + 30 + 60) = 320
08_2019 (200+ 300 + 250) - (90 + 90 + 100 + 90) = 380
09_2019 (200 + 300 + 250 + 400) - (90 + 90 + 190 + 300 + 50) = 430
You can do that by SUMs with WINDOWING function that's uses ORDER BY month. But give us the DDL of your table to be helped more...
Since your example ignores the item_id in the results, you can combine purchases and payments into a simple ledger and then use a window function to get a running sum:
with ledger as (
select to_char(date_purchase, 'YYYY-MM') as xact_month, cost_price as amount from item
union all
select to_char(payment_date, 'YYYY-MM'), payment * -1 from payment
)
select distinct xact_month as month,
sum(amount) over (order by xact_month) as remaining_amount
from ledger;
Working fiddle.
This is it:
select distinct date_trunc('month',m.date_1),m.num_1 from (select to_date(b."payment date",'DD-MM-YYYY') as date_1,
sum(a."cost_price"+coalesce((select sum("cost_price") from item_table c where
date_trunc('month',to_date(a."date_purchase",'DD-MM-YYYY'))>date_trunc('month',to_date(c."date_purchase",'DD-MM-YYYY'))
),0)-(coalesce((select sum("payment") from payment_table c where
date_trunc('month',to_date(a."date_purchase",'DD-MM-YYYY'))>=date_trunc('month',to_date(c."payment date",'DD-MM-YYYY'))
),0))) as num_1 from item_table a,payment_table b
where date_trunc('month',to_date(a."date_purchase",'DD-MM-YYYY'))
=date_trunc('month',to_date(b."payment date",'DD-MM-YYYY'))
group by 1 order by 1)m;
please check at http://sqlfiddle.com/#!17/428874/37
possibly give it a green tick and an upvote..

Slab based calculation in SQL

I want to calculate the slab based logic.
This is my table where the min_bucket and max_bucket range is mention and also the rate of it.
slabs min_bucket max_bucket rate_per_month
----------------------------------------------
Slab 1 0 300000 20
Slab 2 300000 500000 17
Slab 3 500000 1000000 14
Slab 4 1000000 13
We need to calculate as
If there are 450k subs, the payout will be 300k20 + 150k17
If the Total Count is 1000001, then its output should be as
min_bucket max_bucket rate_per_month Count rate_per_month revenue
-----------------------------------------------------------------------
0 300000 20 300000 20 6000000
300000 500000 17 500000 17 8500000
500000 1000000 14 200001 14 2800014
Where count is calculated as 300000+500000+200001 = 1000001, and revenue is calculated as rate_per_month * Count as per the slab.
Can anyone help me write the SQL query for this, which will handle all the cases?
You can build running totals of the slabs table and work with them:
with given as (select 1000001 as value)
, slabs as
(
select
slab,
min_bucket,
max_bucket,
rate_per_month,
sum(min_bucket) over (order by min_bucket) as sum_min_bucket,
sum(coalesce(max_bucket, 2147483647)) over (order by min_bucket) as sum_max_bucket
from mytable
)
select
slabs.slab,
slabs.min_bucket,
slabs.max_bucket,
slabs.rate_per_month,
case when slabs.sum_max_bucket <= given.value
then slabs.max_bucket
else given.value - sum_min_bucket
end as used,
case when slabs.sum_max_bucket <= given.value
then slabs.max_bucket
else given.value - sum_min_bucket
end * slabs.rate_per_month as revenue
from given
join slabs on slabs.sum_min_bucket < given.value
order by slabs.min_bucket;
I don't know your DBMS, but this is standard SQL and likely to work for you (either right away or with a little tweak).
Demo: https://dbfiddle.uk/?rdbms=postgres_13&fiddle=9c4f5f837b6167c7e4f2f7e571f4b26f

how to add grand total row in bigquery?

I have got the results in the following format
customer ; Sales
A ; 1000
B ; 1500
c ; 2500
I want to add a grand total row in the results like
customer ; Sales
A ; 1000
B ; 1500
c ; 2500
Grand Total ; 5000
how can I do this?
I have tried roll up function
Select customer, sales from `xyz`
group by customer
expecting grand total in the output
To add a new row to the query you had, you can use the WITH statement, and use a query like the following:
WITH grand_total AS (SELECT SUM(sales) as grand_total FROM TABLE_NAME)
SELECT customer, sales FROM TABLE_NAME
UNION DISTINCT
SELECT 'grand_total' as customer, grand_total.grand_total from grand_total;
which will output the following:
Row customer sales
1 B 1500
2 C 2500
3 A 1000
4 grand_total 5000
You can read more information about the WITH statement in the public documentation.

How to Sum the 1st record of one column with the 2nd record of another column?

I am trying the Sum the 2nd record of one column with the 1st record of another column and store the result in a new column
Here is the example SQL Server table
Emp_Code Emp_Name Month Opening_Balance
G101 Sam 1 1000
G102 James 2 -2500
G103 David 3 3000
G104 Paul 4 1800
G105 Tom 5 -1500
I am trying to get the output as below on the new Reserve column
Emp_Code Emp_Name Month Opening_Balance Reserve
G101 Sam 1 1000 1000
G102 James 2 -2500 -1500
G103 David 3 3000 1500
G104 Paul 4 1800 3300
G105 Tom 5 -1500 1800
Actually the rule for calculating the Reserve column is that
For Month-1 it's the same as Opening Balance
For rest of the months its Reserve for Month-2 = Reserve for Month-1 + Opening Balance for Month-2
You seem to want a cumulative sum. In SQL Server 2012+, you would do:
select t.*,
sum(opening_balance) over (order by [Month]) as Reserve
from t;
In earlier versions, you would do this with a correlated subquery or apply:
select t.*,
(select sum(t2.opening_balance) from t t2 where t2.[Month] <= t.[Month]) as reserve
from t;
You can do a self join.
SELECT t.Emp_Code, t.Emp_Name, t.Month, t.Opening_Balance, t.Opening_Balance + n.Reserve
FROM Table1 t
JOIN Table2 n
ON t.Month = n.Month - 1

SQL Summation with more than one Condition

I have a table like this
Link PeriodiD Debit Credit Project
1 49 - 200 1
1 49 200 - 2
1 49 100 0
1 50 50 - 1
2 49 - 600 0
I want a script to sum the debit and credit per link per period disregarding project.
so the answer should look like
Link PeriodiD TotalDebit TotalCredit
1 49 300 200
1 50 50 -
2 49 - 600
i have more than 60 periodID and more than 100 link.
Please assist to make such a script
Use a Group by with aggregate functions.
SELECT Link,
PeriodID,
SUM(TotalDebit) AS TotalDebit,
SUM(TotalCredit) AS TotalCredit
FROM tablename
GROUP BY Link, PeriodId;
This query might not always give the expected result if you can have NULL values, depending on the DBMS that you use. You can modify it like this to account for this situation:
SELECT Link,
PeriodID,
SUM(COALESCE(TotalDebit,0)) AS TotalDebit,
SUM(COALESCE(TotalCredit,0)) AS TotalCredit
FROM tablename
GROUP BY Link, PeriodId;