SQL full outer join + group by + count column [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two tables:
currency_table_1
ID - currency_1
------------------
01 - EUR
02 - EUR
03 - EUR
04 - USD
05 - USD
06 - USD
currency_table_2
ID - currency_2
------------------
01 - EUR
02 - EUR
04 - JPY
05 - JPY
06 - JPY
07 - JPY
I want to FULL OUTER JOIN the two tables on the ID. In the result table I like to add a COUNT column which sums the ocurrences of unique currency combinations of the two tables. If there is no ID/currency in the other table, respectively, the currency value in that combination will show as [null]. For above example the result table would look like:
Count - currency_1 - currency_2
----------------------------------
3 - USD - JPY
2 - EUR - EUR
1 - EUR - [null]
1 - [null] - JPY
How does the sql look like? I am familiar with group by and joins, but didn't have success on that one so far.
Thanks for your input!

I think this is what you want:
select ct1.currency_1, ct2.currency_2, count(*)
from currency_table1 ct1 full join
currency_table2 ct2
on ct1.id = ct2.id
group by ct1.currency_1, ct2.currency_2;

Related

How to loop over different values in SQL and create a new row for it?

I have the following table of data:
Item
BasePrice
Price
PriceList
A
1
1
-
B
1
1
-
C
1.5
1.5
-
D
1.5
1.5
-
A
1
1.5
01
B
1
1.5
01
A
1
1.25
02
C
1.5
1.25
02
D
1.5
1.25
03
When you sort this data on item, you see:
Item
BasePrice
Price
PriceList
A
1
1
-
A
1
1.5
01
A
1
1.25
02
B
1
1
-
B
1
1.5
01
C
1.5
1.5
-
C
1.5
1.25
02
D
1.5
1.5
-
D
1.5
1.25
03
It give a good overview on price per pricelist.
Now I want to query the items per pricelist. The thing is, the items without a pricelist need to be in every result if there is not specific pricelist entry, with the value of the price that is the baseprice.
I would like the resulting data to look like this:
Item
BasePrice
Price
PriceList
A
1
1
-
A
1
1.5
01
A
1
1.25
02
A
1
1
03
B
1
1
-
B
1
1.5
01
B
1
1
02
B
1
1
03
C
1.5
1.5
-
C
1.5
1
01
C
1.5
1.25
02
C
1.5
1
03
D
1.5
1
-
D
1.5
1
01
D
1.5
1
02
D
1.5
1.5
03
So essentially, if I query for a pricelist, I get ALL items that exist returned, filled with baseprices if they were not in the queried pricelist.
I cannot get my head around the query. I tried to work with Joins but I end up with multiple columns. I think there is an easier way.
PS the number of available pricelists is dynamic.
Your database design is not good. You are storing the base price in the price list although there is only one base price per item. This redundancy violates database normalization, and you should change it before running into any issues.
Here are three normalized tables (primary keys italic):
items (item_no, name, base_price, ...)
price_lists (price_list_no, purpose, ...)
prices (item_no, price_list_no, price, ...)
Then to the task: You want one result row per item and price list. You get this by cross joining all items with all price lists. Then outer join the price table.
select
i.item_no,
i.base_price,
coalesce(p.price, i.base_price) as price
pl.price_list_no,
from items i
cross join price_lists pl
left outer join prices p on p.item_no = i.item_no
and p.price_list_no = pl.price_list_no
order by i.item_no, i.base_price;

Sum of all equal entries in sqlite [duplicate]

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;

sql: afficher Les articles sans aucune vente en 2010 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
My tables:
sinvoiced
num_facture
ITEM_REF
date
1
a
2010-01-31 00:00:00.000
2
b
2011-01-31 00:00:00.000
3
c
2012-01-31 00:00:00.000
4
d
2013-01-31 00:00:00.000
itmsales
ITEM_REF
a
b
c
d
e
f
sql: display Items without any sales in 2010
I want to display items without any sale in 2010
I translated as following.
sql: display Items without any sales in 2010
I want to display item without any sale in 2010
select ITEM_REF
from itmsales
where ITEM_REF not in
(select ITEM_REF
from sinvoiced
where date between '2010-01-01 00:00:00.000' and '2010-12-31 23:59:59.999');

SQL - Toad for Oracle v11.6. - trying to select the top row of for each unique value in a particular field data after using Distinct

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.

SQL join under a double condition

Imagine we have 2 Tables, A and B, having the same structure, as follows:
Currency, Spot, Exposure, Fixing.
We want to join these two tables in order to have a query on, for example, Fixing = '2013-01-03' and having the major list of currencies (there is not always correspondence between currencies in A and currencies in B).
1 condition: A.Fixing = B.Fixing
2 condition: A.Currency = B.Currency (returning also Currency values not matched)
This is an example: We have the USD currency in table A for that day requested but we don't have it on table B. What we want for that day is a join table having one row with USD followed by the Exposure value in Table A and followed by zero (because ARS was not in table B and therefore it has no Exposure on USD)
How can we write a query?
Below Tables and results:
Table A:
Currency Spot Exposure Fixing
-------- ------- ------------ ----------
AUD 1.3023 -504,561.00 30/01/2013
CHF 1.2378 268,243.00 30/01/2013
GBP 0.8597 2,204.00 30/01/2013
JPY 123.635 -552.00 30/01/2013
USD 1.3572 5,242.00 30/01/2013
AUD 1.300 -574,561.00 29/01/2013
CHF 1.235 545,152.00 29/01/2013
GBP 0.858 1,155,212.00 29/01/2013
JPY 123.388 -45,115.00 29/01/2013
USD 1.354 22,468.00 29/01/2013
Table B:
Currency Spot Exposure Fixing
-------- ------- ------------- ----------
AUD 1.3023 256,442.00 30/01/2013
CHF 1.2378 -4,456,421.00 30/01/2013
GBP 0.8597 15,246.00 30/01/2013
JPY 123.635 1,243,146.00 30/01/2013
AUD 1.300 41,246.00 29/01/2013
CHF 1.235 243.00 29/01/2013
GBP 0.858 54,564.00 29/01/2013
JPY 123.388 140.00 29/01/2013
Results:
Currency A.Exposure B.Exposure Fixing
-------- ----------- ------------- ----------
AUD -504,561.00 256,442.00 30/01/2013
CHF 268,243.00 -4,456,421.00 30/01/2013
GBP 2,204.00 15,246.00 30/01/2013
JPY -552.00 1,243,146.00 30/01/2013
USD 5,242.00 - 30/01/2013
EDITED: The left outer join allows you to grab everything in table a and matching values in table b and having NULLs where there isn't data in table b. Use the ISNULL() function to give you cleaner results by checking for NULL values and if it finds one, replacing it with a different value of your choosing.
select
a.Currency
, a.Spot
, a.Exposure as a_Exposure
, ISNULL(b.Exposure, 0) as b_Exposure
, a.Fixing
from a
left outer join b
on b.Currency = a.Currency
and b.Fixing = a.Fixing
where a.Fixing = '2013-01-30';
I have changed the query to reflect the suggestion from Clockwork-Muse. You will need to add any columns you want data returned for.