This question already has an answer here:
SQL combining GROUP BY and SUM
(1 answer)
Closed last year.
Is it possible to take the sum of all values where another field is the same ?
I have a table which looks like this :
id
symbol
rate
0
USD
0.98
1
USD
1.93
2
EUR
2.08
3
EUR
0.42
4
USD
0.18
So when I like to only get the list of symbols I do this:
select DISTINCT symbol from myTable
Which returns me
id
EUR
USD
What would I need to do to get a sum of all the rates ?
id
rates
EUR
2,5
USD
3,09
Thanks
Use an aggregation query:
SELECT symbol, SUM(rate) AS rates
FROM myTable
GROUP BY symbol;
Related
I am using postgresql, and I have those two tables, Sale and Royalty.
Sale
saleId
ItemId
price
createdAt
1
a
200
2022-08-17
2
b
400
2022-08-19
3
c
500
2022-09-04
Royalty
Id
rate
createdAt
deletedAt
1
0.25
2022-08-10
2022-08-20
2
0.15
2022-08-20
2022-09-01
3
0.20
2022-09-01
null
I want to join sale and royalty to make result like this.
the point is how to match rate with Sale.createdAt comparing to Royalty's rate period.
selected Result
ItemId
rate*price
Sale.createdAt
a
50 (200*0.25)
2022-08-17
b
100 (400*0.25)
2022-08-19
c
100 (500*0.20)
2022-09-04
I don't want to use between on every royalty since more rows could be added.
I'm considering making Sale-Royalty table to get rate*price easily,
but I wonder if there's a way to solve using join with this condition...
One approach is to utilize postgres' daterange type with its <# operator :
select
s.*,
r.rate,
s.price * rate as value
from sale s
join royalty r on s.createdAt <# daterange(r.createdAt, r.deletedAt)
;
caveats :
if royalty date ranges overlap, this will multiply the returned rows (a sale having several valid royalty ranges will appear n times)
replace with an outer (left) join if you need sales even without royalties
dbfiddle
hopefully the following information helps more here. I've run the following code in Toad for oracle:
select distinct cc_orig, cc_base, txn_orig, txn_base
from table a
which has returned the information along the lines of
cc_orig cc_base txn_orig txn_base
GBP CAD 50 35
GBP CAD 75 45
GBP CAD 20 10
EUR CAD 10 8
EUR CAD 13 11
AUD CAD 90 50
AUD CAD 15 5
AUD CAD 80 45
I would like to only pull back one row for each unique value within cc_orig field (it doesn't matter which row it is), so the new results would look something like the following:
cc_orig cc_base txn_orig txn_base
GBP CAD 50 35
EUR CAD 10 8
AUD CAD 90 50
Hopefully this makes sense and somebody can help - I think this is similar to Distinct On in Postgres, but this doesn't appear to work in Toad
You can use window functions:
select cc_orig, cc_base, txn_orig, txn_base
from (select cc_orig, cc_base, txn_orig, txn_base,
row_number() over (partition by cc_orig order by cc_orig) as seqnum
from table a
) a
where seqnum = 1;
Notice that you don't need the select distinct. The row_number() is only choosing one (arbitrary) row anyway.
I have table Price:
WhseKey ItemKey CurrID Sheet1Price Sheet2Price Sheet3Price Sheet4Price
24 452 USD 14.90000 14.90000 13.70000 12.50000 11.03000
24 453 USD 1.15000 1.15000 1.05000 0.95000 0.85000
24 454 USD 12.95000 12.95000 11.90000 10.88000 9.70000
24 459 USD 3.95000 3.95000 3.65000 3.30000 2.92000
I want the result to be like:
CurrID Name ID
USD Sheet1Price Sheet1Price
USD Sheet2Price Sheet2Price
USD Sheet3Price Sheet3Price
USD Sheet4Price Sheet4Price
Current query:
select UPV.CurrID , UPV.Name
from Price
unpivot
(
Name
for Price in (Sheet1Price, Sheet2Price, Sheet3Price, Sheet4Price)
) UPV;
But the result is not correct, it comes as:
CurrID Name
USD 14.90000
USD 13.70000
USD 12.50000
USD 11.03000
USD 1.15000
USD 1.05000
USD 0.95000
USD 0.85000
USD 12.95000
USD 11.90000
What i am missing?
To output filed names you should use UPV.Price, Try this:
select WhseKey, ItemKey, UPV.CurrID , UPV.Name, UPV.Price
from Price
unpivot
(
Price
for Name in (Sheet1Price, Sheet2Price, Sheet3Price, Sheet4Price)
) UPV;
I need to query an SQL database to find all distinct values of one column and I need an arbitrary value from another two columns. For example, consider the following table with three columns: CurrencyCode, BuyRate and SellRate:
CurrencyCode BuyRate SellRate
AUD 1.037 1.97
AUD 1.079 1.99
EUR 0.7288 0.8763
EUR 0.731 0.88
GBP 0.59 0.72
I wish to retrieve one row with distinct CurrencyCode, perhaps getting these three rows:
CurrencyCode BuyRate SellRate
AUD 1.037 1.97
EUR 0.7288 0.8763
GBP 0.59 0.72
I tried my SQL query like:
SELECT distinct CurrencyCode, BuyRate, SellRate FROM Currencies
But I am not getting the desired result as it districts all the columns.
Try with GROUP BY clause and MIN function as below
SELECT CurrencyCode, MIN(BuyRate), MIN(SellRate)
FROM Currencies
GROUP BY CurrencyCode
Till now I found this is the best answer to get Min(SellRate) on the basis of Min(BuyRate)
eg.
CurrencyCode BuyRate SellRate
AUD 1.037 1.97
AUD 1.079 1.89 //Changed from 1.99 to 1.89
AUD 1.038 1.77 //New row added
EUR 0.7288 0.8763
EUR 0.731 0.88
GBP 0.59 0.72
Here I am expecting AUDrows for BuyRate and SaleRate will be 1.037 and 1.97
select CurrencyCode, Min(BuyRate) as BuyRate,
(select top 1 SellRate from Currencies as C
where C.CurrencyCode=Currencies.CurrencyCode
and
C.BuyRate= Min(Currencies.BuyRate) order by SellRate) as SellRate
from Currencies
group
by CurrencyCode
I hv two tables:-
First Table:-
EFF_DATE RATE CURRENCY TYPE
20110101 1.286 USD 1
20110101 1.300 USD 2
20110201 1.275 USD 1
20110201 1.290 USD 2
20110301 1.275 USD 1
20110301 1.285 USD 2
20110401 1.260 USD 1
20110401 1.270 USD 2
20110501 1.225 USD 1
20110501 1.230 USD 2
2nd Table:-
PO_NO TRANS_DATE ACCT_DATE SUPP_NO CURRENCY LOCAL_AMT
1000068 20110114 20110115 S016 USD 16,500.00
1000070 20110214 20110215 S016 USD 7,660.00
1000072 20110317 20110322 S025 USD 1,080.00
1000132 20110314 20110315 S037 USD 3,500.00
1000133 20110414 20110415 S038 USD 14,500.00
Based on ACCT_DATE & TYPE = 1, I wish to add a RATE column & the result should look like this:-
PO_NO TRANS_DATE ACCT_DATE SUPP_NO CURRENCY LOCAL_AMT RATE
1000068 20110114 20110115 S016 USD 16,500.00 1.286
1000070 20110214 20110215 S016 USD 7,660.00 1.275
1000072 20110317 20110322 S025 USD 1,080.00 1.275
1000132 20110314 20110315 S037 USD 3,500.00 1.275
1000133 20110414 20110415 S038 USD 14,500.00 1.26
1000170 20110531 20110531 S016 USD 15,400.00 1.225
I used the below SQL to run but for some reasons, it just keep looping without any output:-
SELECT st.*, ft.rate
FROM second_table st
LEFT JOIN first_table ft
ON (MONTH(acct_date) = MONTH(eff_date) AND YEAR(acct_date) = YEAR(eff_date) )
where ft.TYPE=1
Pls help.
Thanks.
Not very large. If I remove ft.TYPE=1,
it will generate the result almost
immediately.
Based on your comment above you need to put an index on ft.TYPE
You could also set up the query like this which might improve how the optimizer runs the query
SELECT st.*, ft.rate
FROM second_table st
LEFT JOIN first_table ft
ON ft.TYPE=1 AND MONTH(acct_date) = MONTH(eff_date) AND YEAR(acct_date) = YEAR(eff_date)
Also, are the dates stored as strings or as datetime values. If they are stored as strings you should do a string compare instead of coverting to datetimes and then extracting the month and year.
How large are your tables? Your joining on an agrigate function so it will be very slow if the tables are large