sum result is being multiplied by number of rows in oracle - sql

I have a view named b_balance which returns the following records:
SECURITIES_CODE BUY_SELL_FLAG C_BALANCE P_BALANCE
--------------- ------------- --------- ---------
10042 BUY 200 0
10042 BUY 500 0
10042 SELL 200 0
10042 BUY 0 5000
10042 SELL 0 2500
10043 BUY 300 0
10043 SELL 0 2500
and another view named as t_balance which returns the following records:
SECURITIES_CODE BUY_SELL_FLAG C_BALANCE P_BALANCE
--------------- ------------- --------- ---------
10042 BUY 0 5000
10043 BUY 300 0
10042 SELL 200 0
10042 SELL 0 2500
10043 SELL 0 2500
10042 BUY 200 0
10042 BUY 500 0
Now the problem occurs, when I execute my SQL
SELECT TO_CHAR(to_date('20170801','yyyyMMdd'), 'MM/dd/yyyy') AS TRADE_DATE,
b.securities_code AS SECURITIES_CODE,
b.buy_sell_flag AS SIDE,
SUM(NVL(t.c_balance,0)) AS C_t_balance,
SUM(NVL(b.c_balance,0)) AS C_b_balance,
SUM(NVL(t.c_balance,0)) - SUM(NVL(b.c_balance,0)) AS C_DIFFERENCE,
SUM(NVL(t.p_balance,0)) AS P_t_balance,
SUM(NVL(b.p_balance,0)) AS P_b_balance,
SUM(NVL(t.p_balance,0)) - SUM(NVL(b.p_balance,0)) AS P_DIFFERENCE
FROM b_balance b
FULL OUTER JOIN t_balance t
ON b.securities_code = t.securities_code
AND b.buy_sell_flag = t.buy_sell_flag
GROUP BY b.securities_code,
b.buy_sell_flag
ORDER BY SECURITIES_CODE,
SIDE ;
this returns the following records:
TRADE_DATE SECURITIES_CODE SIDE C_T_BALANCE C_B_BALANCE C_DIFFERENCE P_T_BALANCE P_B_BALANCE P_DIFFERENCE
---------- --------------- ---- ----------- ----------- ------------ ----------- ------------ ------------
08/01/2017 10042 BUY 2100 2100 0 15000 15000 0
08/01/2017 10042 SELL 400 400 0 5000 5000 0
08/01/2017 10043 BUY 300 300 0 0 0 0
08/01/2017 10043 SELL 0 0 0 2500 2500 0
that means the result is being multiplied by number of rows.
I checked on Stack overflow and did't find anything wrong according to this answer.
So what is the wrong in my SQL?

you should join the aggregated result (not aggregate the joined values)
SELECT TO_CHAR(to_date('20170801','yyyyMMdd'), 'MM/dd/yyyy') AS TRADE_DATE,
t1.SECURITIES_CODE,
t1.SIDE,
t1.C_b_balance,
t1.P_b_balance,
t2.C_t_balance,
t2.P_t_balance,
from (
SELECT
b.securities_code AS SECURITIES_CODE,
b.buy_sell_flag AS SIDE,
SUM(NVL(b.c_balance,0)) AS C_b_balance,
SUM(NVL(b.p_balance,0)) AS P_b_balance,
FROM b_balance b
GROUP BY b.securities_code, b.buy_sell_flag ) t1
left join (
SELECT
t.securities_code AS SECURITIES_CODE,
t.buy_sell_flag AS SIDE,
SUM(NVL(t.c_balance,0)) AS C_t_balance,
SSUM(NVL(t.p_balance,0)) AS P_t_balance,
FROM t_balance t
GROUP BY t.securities_code, t.buy_sell_flag
) on t1.securities_code = t2.securities_code and t1.buy_sell_flag = t2.buy_sell_flag

When working with aggregates from different tables, aggregate before joining:
select
date '2017-08-01' as trade_date,
securities_code as securities_code,
buy_sell_flag as side,
nvl(t.sum_c_balance, 0) as c_t_balance,
nvl(b.sum_c_balance, 0) as c_b_balance,
nvl(t.sum_c_balance, 0) - nvl(b.sum_c_balance, 0) as c_difference,
nvl(t.sum_p_balance, 0) as p_t_balance,
nvl(b.sum_p_balance, 0) as p_b_balance,
nvl(t.sum_p_balance, 0) - nvl(b.sum_c_balance, 0) as p_difference
from
(
select
securities_code,
buy_sell_flag,
sum(c_balance) as sum_c_balance,
sum(p_balance) as sum_p_balance
from b_balance
group by securities_code, buy_sell_flag
) b
full outer join
(
select
securities_code,
buy_sell_flag,
sum(c_balance) as sum_c_balance,
sum(p_balance) as sum_p_balance
from t_balance
group by securities_code, buy_sell_flag
) t using (securities_code, buy_sell_flag)
order by securities_code, buy_sell_flag;

Related

How to change query result without using pivot functions?

I have a query which shows number of cheap and expensive orders for a date:
SELECT od1.date,
(SELECT COUNT( od2.id) FROM myorders od2
INNER JOIN myproducts ON od2.product_id = myproducts.id
WHERE od2.date = od1.date
AND myproducts.price BETWEEN 500 AND 2000
) cheap_orders,
(SELECT SUM(myproducts.price)
FROM myorders od2
INNER JOIN myproducts ON od2.product_id = myproducts.id
WHERE od2.date = od1.date
AND myproducts.price BETWEEN 500 AND 2000
) sum_ch_o,
(SELECT COUNT( od2.id) FROM myorders od2
INNER JOIN myproducts ON od2.product_id = myproducts.id
WHERE od2.date = od1.date
AND myproducts.price > 2000
) expensive_orders,
(SELECT SUM(myproducts.price)
FROM myorders od2
INNER JOIN myproducts ON od2.product_id = myproducts.id
WHERE od2.date = od1.date
AND myproducts.price > 2000
) sum_exp_o
FROM myorders od1
GROUP BY od1.date
It returns me a table:
date cheap_orders sum_ch_o expensive_orders sum_exp_o
2018-06-06 0 0 1 4000
2018-06-04 2 3100 0 0
2018-06-07 1 780 3 28000
2018-06-05 1 560 4 15500
How can I modify my query to get another view of this table:
date order_cat quantity sum
2018-06-06 cheap 0 0
2018-06-06 expensive 1 4000
2018-06-04 cheap 2 3100
2018-06-04 expensive 0 0
2018-06-07 cheap 1 780
2018-06-07 expensive 3 28000

Getting value repeated for next year if value is recurring

I have a record for project and release number and I need to repeat the row in next year if value is recurring.
First image is showing the data that I have it
My expected output is:
Explanation of output: In year 2017 value_type ITA has frequency as Recurring so, This value should be repeated in all next year(i.e 2018, 2019 and 2020). like that in year 2018 OC and PA is recurring so it also need to repeated in 2019 and 2020.
For that I created a new view for only recurring value and tried to join that view with base table. But it is not giving me proper result.
Can anyone please help me with this?
Thanks in advance..
DECLARE #EndYear INT =2020 --Also you can get from data by MAX(Year)
;WITH tb(PROJECT_ID,RELEASE_NO,[YEAR],VALUE_TYPE,VAL_DES,COST,RUN_TATE,FREQUENCY)
AS(
SELECT 111,1,2016,'IT','EXPENSE',0,NULL,NULL UNION
SELECT 111,1,2016,'IR','INCOME',10000,NULL,NULL UNION
SELECT 111,1,2016,'OC','EXPENSE',-200000,NULL,NULL UNION
SELECT 111,1,2016,'Vendor','EXPENSE',-5000,NULL,NULL UNION
SELECT 111,1,2017,'BC','INCOME',200000,NULL,NULL UNION
SELECT 111,1,2017,'ITA','INCOME',5000,5000,'Recurring' UNION
SELECT 111,1,2017,'OC','EXPENSE',-200000,NULL,NULL UNION
SELECT 111,1,2018,'OC','EXPENSE',-10000,-10000,'Recurring' UNION
SELECT 111,1,2018,'PA','INCOME',100000,100000,'Recurring' UNION
SELECT 111,1,2019,'icc','INCOME',500,NULL,NULL UNION
SELECT 111,1,2020,NULL,NULL,NULL,NULL,NULL
),Recurring AS (
SELECT tb.PROJECT_ID,tb.RELEASE_NO,tb.VALUE_TYPE,tb.VAL_DES,MIN([YEAR]) AS StartYear,MAX(COST) AS COST,MAX(tb.RUN_TATE) AS RUN_TATE
FROM tb WHERE FREQUENCY='Recurring'
GROUP BY tb.PROJECT_ID,tb.RELEASE_NO,tb.VALUE_TYPE,tb.VAL_DES
)
SELECT * FROM tb union
SELECT r.PROJECT_ID,r.RELEASE_NO,n.number AS [YEAR],r.VALUE_TYPE,r.VAL_DES,r.COST,r.RUN_TATE,NULL AS FREQUENCY FROM
Recurring AS r
OUTER APPLY (
SELECT sv.number FROM master.dbo.spt_values AS sv WHERE sv.type='P' AND sv.number BETWEEN r.StartYear+1 AND #EndYear
)n
PROJECT_ID RELEASE_NO YEAR VALUE_TYPE VAL_DES COST RUN_TATE FREQUENCY
----------- ----------- ----------- ---------- ------- ----------- ----------- ---------
111 1 2016 IR INCOME 10000 NULL NULL
111 1 2016 IT EXPENSE 0 NULL NULL
111 1 2016 OC EXPENSE -200000 NULL NULL
111 1 2016 Vendor EXPENSE -5000 NULL NULL
111 1 2017 BC INCOME 200000 NULL NULL
111 1 2017 ITA INCOME 5000 5000 Recurring
111 1 2017 OC EXPENSE -200000 NULL NULL
111 1 2018 ITA INCOME 5000 5000 NULL
111 1 2018 OC EXPENSE -10000 -10000 Recurring
111 1 2018 PA INCOME 100000 100000 Recurring
111 1 2019 icc INCOME 500 NULL NULL
111 1 2019 ITA INCOME 5000 5000 NULL
111 1 2019 OC EXPENSE -10000 -10000 NULL
111 1 2019 PA INCOME 100000 100000 NULL
111 1 2020 NULL NULL NULL NULL NULL
111 1 2020 ITA INCOME 5000 5000 NULL
111 1 2020 OC EXPENSE -10000 -10000 NULL
111 1 2020 PA INCOME 100000 100000 NULL

SQL Group by range and total column

With the following function and stored procedure i get a resultset with 2 columns.
What i need additional is a third column total for all open invoices inner score range
It would great for any idea.
ALTER FUNCTION dbo.OpenOrders
(
#MandantId int
)
RETURNS TABLE
AS
RETURN
SELECT SUM(Invoices.Amount - ISNULL(Payment.Amount, 0)) AS Op
FROM Invoices INNER JOIN
Orders ON Invoices.OrderId = Orders.OrderId LEFT OUTER JOIN
Payment ON Invoices.InvoiceId = Payment.InvoiceId
WHERE (Orders.MandantId = #MandantId)
GROUP BY Invoice.InvoiceId, Invoices.Amount
HAVING (SUM(Invoices.Amount - ISNULL(Payment.Amount, 0)) <> 0)
ALTER PROCEDURE dbo.GetOpRanges
#MandantId int
AS
BEGIN
SELECT * INTO #tmp_ranges
FROM
//wrong in first post -> OPDebitorByMandant(#MandantId)
OpenOrders(#MandantId)
SELECT op AS [score range], COUNT(*) AS [number of occurences]
FROM
(SELECT CASE
WHEN op BETWEEN 0 AND 50 THEN ' 0- 50'
WHEN op BETWEEN 50 AND 100 THEN ' 50-100'
WHEN op BETWEEN 100 AND 500 THEN '100-500'
ELSE '500 and >' END AS op
FROM [#tmp_ranges]) AS t
GROUP BY op
RETURN
Result:
score range number of occurences range
------------------+-------------
0- 50 23
50-100 4
100-500 4
500 and > 21
What i need additional is a third column total for all open invoices inner score range.
Target result:
score range number of occurences Total
-----------+--------------------+------
0- 50 23 1.150
50-100 4 400
100-500 4 2.000
500 and > 21 22.000
Tables:
Invoices
InvoiceId CustomerId OrderId DateOfInvoice Amount
----------+----------+-------+-------------+------
1 1 20 20160301 1000.00
2 2 22 20160501 2000.00
3 1 102 20160601 3000.00
...
Orders
OrderId MandantId CustomerId DateOfOrder Amount
-------+---------+----------+-----------+-----------
20 1 1 20160101 1000.00
22 1 2 20160101 2000.00
102 1 1 20160101 3000.00
...
Payment
PaymentId MandantId CustomerId InvoiceId OrderId DateOfPayment Amount
---------+---------+----------+---------+-------+-------------+-------------
1 1 1 1 20 20160310 1000.00
2 1 2 2 22 20160505 2000.00
3 1 1 3 102 20160610 3000.00
...
hope it's helpfull and thanks again in advance for any solution

get multiple column from both table by joining a table on the basis of other max table column

1) vendor table
--------------------------------------------
VENDid VENDname
--- -----
1 ABC
2 XYZ
3 WXY
2)purchase table
---------------------------------------------
VENDid Purchasedate
------ ------------
1 12-01-2012
1 10-11-2013
2 22-02-2014
2 11-04-2014
3 10-05-2014
3 11-06-2014
1 14-06-2014
output(list all rows of vendor table and only max(purchasedate) from purchase table)
---------------------------------------------
VENDid VENDname PurchaseDate
------- -------- -------------
1 ABC 14-06-2014
2 XYZ 11-04-2014
3 WXY 11-06-2014
i got some queries like to solve previous problem-
SELECT v.VendID, VendName, Max(PurchaseDate)
FROM vendor v
INNER JOIN purchase p
ON v.VendID = p.VendID
Group By v.VendID, VendName
select VENDid, VENDname,
(select top 1 purchaseDate from purchase p
where p.VENDid=v.VENDid order by purchaseDate desc) as 'Purchase date'
from Vendor v
Que. If i will add some more column in purchase table like -
2)purchase table
------------------------------------------
VENDid Purchasedate amount_paid
------ ------------ ------------
1 12-01-2012 10000
1 10-11-2013 20000
2 22-02-2014 15000
2 11-04-2014 30000
3 10-05-2014 80000
3 11-06-2014 17000
1 14-06-2014 28000
and i want amount_paid along with previous output like-
---------------------------------------------
VENDid VENDname PurchaseDate amount_paid
------- -------- ------------- -------------
1 ABC 14-06-2014 28000
2 XYZ 11-04-2014 30000
3 WXY 11-06-2014 17000
then what will be query..
You appear to be using SQL Server. If so, you can use cross apply:
select v.VENDid, v.VENDname, p.PurchaseDate, p.Amount_Paid
from Vendor v cross apply
(select top 1 p.*
from purchase p
where p.VENDid = v.VENDid
order by p.purchaseDate desc
) p ;

Sql query Solution

I have a query as follows.
select strftime('%Y-%m',A.traDate) as Month,sum(A.TraAmt) as Total,C.GroupType from
TransactionTbl A left join TransactionCategory B on A.CategoryID = B.CategoryID left join
CategoryGroup C on B.CatGRoupID=C.CatGRoupID where A.UserID=1 and A.ProfileID=1 and
date(A.TraDate) between date('2009-12-01') and date('2010-11-01') group by C.GroupType,
strftime('%m',A.traDate) order by Month
Above query gives result as follows,
Month Total C.GroupType
----- ----- -----------
2009-12 4100 Expense
2009-12 8000 Income
2010-01 200 Expense
2010-01 2000 Income
2010-02 3500 Expense
2010-02 7500 Income
I want solution like this.
Month Income Expense
----- ----- -----------
2009-12 8000 4100
2010-01 8000 200
2010-02 7500 3500
I am trying hard to get output like this, but I could find any kind of solution.
This is one way of achieving what you want:
SELECT
T1.Month,
SUM(CASE T1.GROUPTYPE WHEN 'Income' THEN T1.TOTAL ELSE 0 END) AS Income,
SUM(CASE T1.GROUPTYPE WHEN 'Expense' THEN T1.TOTAL ELSE 0 END) AS Expense
FROM (/* Your query here */) AS T1
GROUP BY T1.Month