I know there must be some question like me. Seems I cannot find the question, I sorry to ask this question.
Below is my table (SalesTransaction)
ID |SalesID |Amount |AmountReceived |OutStanding |Paid
2041 1000 600000 600000 0 1
2042 1000 1500000 2000000 -500000 1
2043 1000 900000 0 900000 0
2047 1002 300000 0 300000 0
Using SQL Query below:
SELECT ID,
SalesID,
Amount,
AmountReceived,
OutStanding,
Paid,
(CASE
WHEN Paid = 0 THEN (SELECT SUM(OutStanding)
FROM SalesTransaction
WHERE Paid = 1 )
ELSE 0 END) AS BalanceLastSchedule
FROM dbo.SalesTransaction
GROUP BY ID, SalesID, Amount, AmountReceived, OutStanding, Paid
I get result like below:
ID |SalesID |Amount |AmountReceived |OutStanding |Paid |BalanceLastSch
2041 1000 600000 600000 0 1 0.00
2042 1000 1500000 2000000 -500000 1 0.00
2043 1000 900000 0 900000 0 -500000
2047 1002 300000 0 300000 0 -500000
What I want to get like below:
ID |SalesID |Amount |AmountReceived |OutStanding |Paid |BalanceLastSch
2041 1000 600000 600000 0 1 0.00
2042 1000 1500000 2000000 -500000 1 0.00
2043 1000 900000 0 900000 0 -500000
2047 1002 300000 0 300000 0 0.00
Appreciate I get help on this. Thanks.
You are getting the wrong answer because you have not joined the Inner Query to find the sum with the SalesId. try this below script
SELECT
SeqNo,
SalesID,
Amount,
AmountReceived,
OutStanding,
Paid,
ISNULL((CASE
WHEN Paid = 0 THEN (SELECT SUM(OutStanding)
FROM SalesTransaction
WHERE Paid = 1
AND SalesId = T.SalesId)
ELSE 0 END),0.00) AS BalanceLastSchedule
FROM SalesTransaction T;
i just added the below setion inside tour inner query and removed the group By Clause
AND SalesId = T.SalesId
Related
Have a problem to solve in the SQL Server to generate a Inventory aging report using FIFO Based on SKU & Warehouse. I have attached the schema here.
SKU
TransactionType
WarehouseCode
TransactionDate
Qty
100
IN
WH1
2021-04-30
100
100
IN
WH2
2021-04-30
50
101
IN
WH1
2021-04-30
30
101
IN
WH2
2021-05-01
25
100
OUT
WH2
2021-05-02
30
100
OUT
WH1
2021-05-02
20
100
OUT
WH1
2021-05-04
50
100
OUT
WH2
2021-05-04
20
100
OUT
WH1
2021-05-05
25
100
IN
WH2
2021-05-10
30
100
IN
WH1
2021-05-11
30
101
OUT
WH2
2021-05-12
20
100
OUT
WH1
2021-05-15
30
Based on the above schema structure, i need to develop a inventory aging report based on first in first out (FIFO) and show the remaining qty of each SKU and Warehouse combination and make the previous incoming records remaining quantities as zero.
Expected report format Assuming the report is run on (2021-05-20)
SKU
TransactionType
WarehouseCode
TransactionDate
Qty
Remaining
Aging
100
IN
WH1
2021-04-30
100
0
21
100
IN
WH2
2021-04-30
50
0
21
101
IN
WH1
2021-04-30
30
30
21
101
IN
WH2
2021-05-01
25
5
20
100
IN
WH2
2021-05-10
30
30
11
100
IN
WH1
2021-05-11
50
5
10
Thanks in advance.
You can use window functions to sum up the IN records over time (a cumulative sum), and find out the final total OUT.
Then, if the in_so_far is less than the final_out, you know you have use everything for that line.
The aging just appears to be a datediff() against a constant value?
WITH
cumulative AS
(
SELECT
*,
SUM(CASE WHEN TransactionType = 'IN' THEN Qty ELSE 0 END)
OVER (
PARTITION BY SKU, WarehouseCode
ORDER BY TransactionDate
)
AS qty_in_so_far,
SUM(CASE WHEN TransactionType = 'OUT' THEN Qty ELSE 0 END)
OVER (
PARTITION BY SKU, WarehouseCode
)
AS qty_out_final
FROM
inventory
)
SELECT
*,
CASE
WHEN qty_out_final > qty_in_so_far
THEN 0
WHEN qty_in_so_far - qty_out_final > qty
THEN qty
ELSE qty_in_so_far - qty_out_final
END
AS qty_final,
DATEDIFF(day, TransactionDate, '2021-05-20') + 1 AS aging
FROM
cumulative
WHERE
TransactionType = 'IN'
ORDER BY
TransactionDate,
SKU,
WarehouseCode
Demo... https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=b6725edd625001cd874bdb8f37283763
I have a record of users' purchasing behavior. However, it is long and includes a lot of redundant data. I want to delete orders that purchased and deleted within 5 min
My query so far:
--TABLE 3 COD
select z.user_id,
z.date,
z.actions,
x.name,
x.email,
x.address
sum(z.debit) over (partition by z.seller_id order by z.created_at) -
sum(z.credit) over (partition by z.seller_id order by z.created_at)
as balance
from table_1 z
left join
table_2 x
on z.seller_id = x.uid
order by seller_id, created_at
For simplicity, i got this result
user actions credit debit balance date
1 do_action_A 5000 0 5000 2020-01-01 1:00:00 #no need these 2
1 cancel_A 0 5000 0 2020-01-01 1:03:00 #in result
1 do_action_A 5000 0 5000 2020-01-01 1:10:00
1 do_action_b 3000 0 8000 2020-01-01 1:20:00
1 do_action_c 0 7000 1000 2020-01-01 1:30:00
2 do_action_A 5000 0 5000 2020-01-01 1:00:00
2 do_action_B 3000 0 8000 2020-01-01 1:10:00
We know that users can only cancel their orders within 5 minutes, unfortunately, there is a lot of cancels. I need to make this data table simple and short so as to track and visualize it easily.
Here is my expectataion:
user actions credit debit balance date
1 do_action_A 5000 0 5000 2020-01-01 1:10:00
1 do_action_b 3000 0 8000 2020-01-01 1:20:00
1 do_action_c 0 7000 1000 2020-01-01 1:30:00
2 do_action_A 5000 0 5000 2020-01-01 1:00:00
2 do_action_B 3000 0 8000 2020-01-01 1:10:00
You can try using lead()
select * from
(
select z.user_id,z.date,z.actions,x.name,
x.email,x.address,debtit, credit, balance,
lead(z.actions) over(parition by z.user_id order by z.created_at) as next_action
from table_1 z left join table_2 x
on z.seller_id = x.uid
)A where next_action not like '%cancel%' and actions not like '%cancel%'
I have a table that looks like this
TIMECODE UNIT_CODE Department Account AMOUNT
20194 10 1000 1000 100
20194 10 2354 1100 150
20194 10 1000 1000 200
20194 10 2354 1000 100
20194 20 500 1000 250
20194 20 500 1100 200
How I need the results to be is like this
TIMECODE UNIT_CODE Department 1000 1100
20194 10 1000 300 NULL
20194 10 2354 100 150
20194 20 500 250 200
hopefully that gives you a better image, but basically I would need to do a SUM depending on the distinct value of the other columns. The accounts that were previously in rows would be changed into columns.
any ideas or help with this would be greatly appreciated
Try the following, here is the demo.
select
TIMECODE,
UNIT_CODE,
Department,
sum(case when Account = 1000 then AMOUNT end) as "1000",
sum(case when Account = 1100 then AMOUNT end) as "1100"
from myTable
group by
TIMECODE,
UNIT_CODE,
Department
Output:
---------------------------------------------------
| TIMECODE UNIT_CODE DEPARTMENT 1000 1100 |
---------------------------------------------------
| 20194 20 500 250 200 |
| 20194 10 1000 300 null|
| 20194 10 2354 100 150 |
---------------------------------------------------
Given the following code:
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=consignacion.mdb;")
SQLStr = "Select buyer,balance,
(SELECT SUM(salesdb.total)
From salesdb
Where salesdb.buyer = buyerdb.buyer and
buydate=#buydate and
salesdb.total is not Null and
salesdb.total<>#sales ) As [paid]
From buyerdb where balance>#balance"
cmd = New OleDbCommand(SQLStr, connection)
cmd.Parameters.AddWithValue("#buydate", Form1.Lbldate2.Text)
cmd.Parameters.AddWithValue("#balance", 0)
cmd.Parameters.AddWithValue("#sales", 0)
And below the ouput:
name balance sales
BANAL 1000
BAYA 500
RICKY 350
GEN 0
CASH 0
BAGON 0
LOREY 0
TANIS 0 2250
DARWIN 0 345
GLEN 1000 4334
LITO 0 505
LIZA 0 460
CESS 350 984
LOUIE 0 280
YOLLY 0 832
GLENDA 0 170
JOSE 1000 2240
I want to get the followed output:
name balance sales
BANAL 1000
BAYA 500
RICKY 350
TANIS 0 2250
DARWIN 0 345
GLEN 1000 4334
LITO 0 505
LIZA 0 460
CESS 350 984
LOUIE 0 280
YOLLY 0 832
GLENDA 0 170
JOSE 1000 2240
i wanted to display all buyers with balance > 0
i wanted to display all buyer with sales/total >0 at a given date. (sometimes buyers dont have record of sales on a given date.
i don't want to display buyers with balance and sales/total is zero at the same time.
First, use an LEFT JOIN between buyerdb b and salesdb s on the buyer key, and perform a SUM() on these records according a GROUP BY command. It will display buyerdb rows (the left table) even if there are no corresponding salesdb rows:
SELECT b.buyer, b.balance, SUM(s.total) as total
FROM buyerdb b
LEFT JOIN salesdb s on (b.buyer = s.buyer AND s.buydate = #buydate)
GROUP BY buyer
Then, use this request as a nested one, and filtered to get only the rows you want:
SELECT t.buyer, t.balance, t.total
FROM
(
SELECT b.buyer, b.balance, SUM(s.total) as total
FROM buyerdb b
LEFT JOIN salesdb s on (b.buyer = s.buyer AND s.buydate = #buydate)
GROUP BY buyer
) t
-- Filter the rows you don't want (both zero balance and total)
WHERE balance > 0 or total > 0
Working SQL fiddle (should work with Access).
Say I have the following data in my table;
tran_date withdraw deposit
25/11/2010 0 500
2/12/2010 100 0
15/12/2010 0 300
18/12/2010 0 200
25/12/2010 200 0
Suppose I want to get the following for date range between 1/12/2010 and 31/12/2010.
tran_date withdraw deposit balance days_since_last_tran
1/12/2010 0 0 500 0
2/12/2010 100 0 400 1
15/12/2010 0 300 700 13
18/12/2010 0 200 900 3
25/12/2010 200 0 700 7
31/12/2010 0 0 700 6
Is this doable in PostgreSQL 8.4?
Use:
SELECT t.tran_date,
t.withdraw,
t.deposit,
(SELECT SUM(y.deposit) - SUM(y.withdrawl)
FROM YOUR_TABLE y
WHERE y.tran_date <= t.tran_date) AS balance,
t.tran_date - COALESCE(LAG(t.tran_date) OVER(ORDER BY t.tran_date),
t.tran_date) AS days_since_last
FROM YOUR_TABLE t
8.4+ is nice, providing access to analytic/windowing functions like LAG.