SQL Server : sum and multiplies on 2 tables - sql

I need help to SUM and MULTIPLIES on join 2 tables:
tb1
tb2
In tb1, I need to sum QTY and multiples SKU with PRICE without repeating same SKU (21135208, 21035621):
Current query:
SELECT
tb1.DOC_NO,
CAST(SUM(tb2.QTY) AS FLOAT) AS QTY_TOTAL,
ROUND(CAST(SUM(tb2.QTY * tb2.PRICE) AS FLOAT), 2) AS PRICE_TOTAL,
tb1.DATE,
tb1.STATUS_A,
tb2.STATUS_B
FROM
tb1
INNER JOIN
tb2 ON tb1.DOC_NO = tb2.DOC_NO
WHERE
tb1.STATUS_B = '0'
GROUP BY
tb1.DOC_NO, tb1.DATE,
tb1.STATUS_A, tb1.STATUS_B
ORDER BY
COH.DOC_NO_REQ_TO_ULI DESC
My result is:
Expected result is:

I believe that you could filter out duplicates by using a subquery like SELECT DISTINCT ... FROM tb1, while leaving the rest of the query untouched:
SELECT
tb0.DOC_NO,
CAST ( SUM ( tb2.QTY ) AS FLOAT ) AS QTY_TOTAL,
ROUND( CAST ( SUM ( tb2.QTY * tb2.PRICE ) AS FLOAT ), 2) AS PRICE_TOTAL,
tb0.DATE,
tb2.STATUS_A,
tb2.STATUS_B
FROM
(SELECT DISTINCT DOC_NO, CM, SKU, PRICE, QTY, DATE FROM tb1) AS tb0
INNER JOIN tb2 ON tb0.DOC_NO = tb2.DOC_NO
WHERE
tb2.STATUS_B = '0'
GROUP BY
tb0.DOC_NO,
tb0.DATE,
tb2.STATUS_A,
tb2.STATUS_B
ORDER BY
COH.DOC_NO_REQ_TO_ULI DESC
NB: there are some issues with the table aliases in your query:
columns STATUS_A or STATUS_B should be prefixed tb2, not tb1 (I fixed that)
alias COH which is being used in the ORDER BY clause is not declared anywhere in the query (that's a syntax error)

Related

Convert rows to columns and sum with group by

I have a table with columns ID, WorkingDate and AmountReceived where data is show as
ID WorkingDate AmountReceived
-------------------------------------
1 13/April/2021 201999.01
2 14/Arpil/2021 1099.02
Now, I am trying to show it like
13/April/2021 14/April/2021 15/April/2021
--------------------------------------------------
201999.01 1099.02 102.09
I tried this
select AmountReceived, WorkingDate
from Orders o
select distinct o.WorkingDate, sum(AmountReceived) over (partition by WorkingDate) as total
from Orders o
group by WorkingDate, AmountReceived
But it throws an aggregate error.
Using PIVOT operator, you can achieve what you want
select *
from
(
select o.WorkingDate, o.AmountReceived
from Orders o
) o
pivot
(
sum(AmountReceived)
for WorkingDate in ([2021-04-13], [2021-04-14], [2021-04-15])
) p

SAP HANA: Create a join with a max on date

I am busy with a SAP HANA development but ran into an issue with currency conversion.
On the left of a join, I have a projection with the Sales Order Number, Customer Requested Delivery Date and order value in Document Currency (from VBAK/VBAP). On the right of the join, I have a projection containing the TCURR table (from SAP), filtered on MAER (monthly average exchange rate) and the "from currency" joined to document currency from the Sales Order. I need to convert the value in document currency say to EUR but must select the latest exchange rate available in TCURR. How do I do the join? So effectively, I need to join the date from the Sales Order to max( Exchange Rate Date) but must be less than or equal to the Sales Order date.
Could you please check following HANA db SQLScript
I used multiple SQL CTE expressions on HANA SQLScript to get the most recent entry for each currency conversion to EUR
And then join this CTE tables (last one CTE3) to VBAK table
I actually did not do the amount convertion using the currency rate, I think you can handle it using multiplication or division, etc on the SELECT list
with cte as (
select
to_date( to_nvarchar(99999999 - gdatu) ) gdate,
*
from "SAPS4S".TCURR
where tcurr = 'EUR'
), cte2 as (
select
row_number() over (partition by fcurr, YEAR(gdate), MONTH(gdate) order by gdate desc) as rn,
YEAR(gdate) as gdate_year,
MONTH(gdate) as gdate_month,
*
from cte
), cte3 as (
select * from cte2 where rn = 1
)
select
vbeln,
erdat,
netwr,
waerk,
cte3.*
from "SAPS4S".VBAK as vbak
left join cte3
on
vbak.waerk = cte3.fcurr and
YEAR(vbak.erdat) = cte3.gdate_year and
MONTH(vbak.erdat) = cte3.gdate_month;
Hello Ernie,
According to your second comment, I changed the SQLScript query a bit as follows
with cte as (
select
to_date( to_nvarchar(99999999 - gdatu) ) gdate,
*
from "SAPABAP1".TCURR
where tcurr = 'EUR'
), cte2 as (
select
vbeln,
erdat,
netwr,
waerk,
sum(1) over (partition by vbeln order by gdate desc rows unbounded preceding) as rownum,
cte.*
from "SAPABAP1".VBAK as vbak
left join cte
on
vbak.waerk = cte.fcurr and
vbak.erdat >= cte.gdate
)
select *
from cte2
where ifnull(rownum,1) = 1
I'll be happy if it works on your database and get your feedback
There are NULL records coming from TCURR table because there is no currency rate entry or the document currency is already defined as EUR (actually the rates should be then equal to 1)

Using query result as subquery syntax

I have a table that I need to identify duplicate entries to delete them. I can find the duplicates using the following query
select s.*, t.*
from [tableXYZ] s
join (
select [date], [product], count(*) as qty
from [tableXYZ]
group by [date], [product]
having count(*) > 1
) t on s.[date] = t.[date] and s.[product] = t.[product]
ORDER BY s.[date], s.[product], s.[id]
and then need to use the result from this table to show where [fieldZ] IS NULL
I've tried the following but get error The column 'date' was specified multiple times for 'subquery'.
select * from
(
select s.*, t.*
from [tableXYZ] s
join (
select [date], [product], count(*) as qty
from [tableXYZ]
group by [date], [product]
having count(*) > 1
) t on s.[date] = t.[date] and s.[product] = t.[product]
) as subquery
where [fieldZ] is null
You have column date in your subquery twice because you are selecting s.* and t.*, this will return s.Date and t.date. If you need both columns, alias one of the columns.
You will also run into this problem with the product column. Your subquery cannot return multiple columns with the same name. Only select the columns you need in your subquery instead of selecting all columns. This is a good practice in general and will solve this issue.

SQL Server correlated query want to take sum

select
SUM(Convert(int, (Select SUM(Convert(int, amount))
from ChargesType
where ChargesType.ID = Charges.ChargesTypeID))) as Amount
from Charges
Where Charges.RollNo = 1
This is my query; I want to get sum of amount column.
Try this instead:
select
SUM(t.Amount) as Amount
from Charges AS c
INNER JOIN
(
SELECT Convert(int, amount) AS Amount, ID
FROM ChargesType
) AS t ON t.ID = c.ChargesTypeID
Where c.RollNo = 1

aggregate value from one table updated in other table

i want to insert an aggregated value from one table to other using Update clause.Here is what i did.
SELECT Sum(1.0 * volume * speed) / Sum(volume) AS aggregated_speed
FROM traffic_data_replica
GROUP BY date,
id
ORDER BY date;
I want update this values in other table. This what i have tried.
UPDATE traffic_data_aggregated_lanes
SET traffic_data_aggregated_lanes.aggregated_speed = (SELECT
Sum(1.0 * volume *
speed) / Sum(volume) AS aggregated_speed
FROM
traffic_data_replica
GROUP BY date,
id
ORDER BY date);
Please Help
relation is not clear.
Somewhat
Merge traffic_data_aggregated_lanes a
using (select id, sum(1.0*volume*speed)/sum(volume) as aggregated_speed from
traffic_data_replica group by date,id) b
on a.id=b.id
when matched then
update set a.aggregated_speed= b.aggregated_speed
we can go by using CTE also i am not sure of data.
So i have given you assumption result
WITH CTE AS (
select t.Id,sum(1.0*volume*speed)/sum(volume) as aggregated_speed from traffic_data_replica t
group by date,id order by date
)
Select * FROM CTE
update traffic_data_aggregated_lanes set traffic_data_aggregated_lanes.aggregated_sp= t.aggregated_speed From traffic_data_replica t
INNER JOIN CTE c ON C.Id = t.Id