-3

please help me , I want to create table C with using table A and B ,I am new in oracle .what is the idea? how can I write this code ...... enter image description here

MT0
  • 143,790
  • 11
  • 59
  • 117
  • 1
    Please do **NOT** post images of data. Instead provide it as text we can copy/paste (preferably as `CREATE TABLE` and `INSERT` statements that can also be executed). – MT0 Jan 25 '23 at 11:17
  • Does the question suppose that we need to guess your requirement by looking at the data and column names? – astentx Jan 25 '23 at 12:02
  • https://stackoverflow.com/questions/4841718/oracle-sql-pivot-query – astentx Jan 25 '23 at 12:04

2 Answers2

1

One option is to conditionally aggregate values (but it also means that table B won't help much) (also, date is most probably not a column name as it is reserved word, reserved for the date datatype so I renamed it to datum):

select a.datum,
  sum(case when a.code = 1 then rate else 0 end) usd,
  sum(case when a.code = 2 then rate else 0 end) eur,
  sum(case when a.code = 3 then rate else 0 end) obp
from a
group by a.datum
order by a.datum;

DATUM              USD        EUR        OBP
----------- ---------- ---------- ----------
28-oct-2018       1000       2000       4000
29-oct-2018       2000       3000       5000
30-oct-2018       3000       4000       6000
Littlefoot
  • 131,892
  • 15
  • 35
  • 57
1

You can use a PIVOT:

SELECT *
FROM   (
  SELECT a."DATE",
         b.name,
         a.rate
  FROM   A
         INNER JOIN B
         ON (a.code = b.id)
)
PIVOT (
  SUM(rate)
  FOR name IN (
    'USD' AS usd,
    'EUR' AS eur,
    'OBP' AS obp
  )
)

or conditional aggregation:

SELECT a."DATE",
       SUM(CASE b.name WHEN 'USD' THEN a.rate END) AS usd,
       SUM(CASE b.name WHEN 'EUR' THEN a.rate END) AS eur,
       SUM(CASE b.name WHEN 'OBP' THEN a.rate END) AS obp
FROM   A
       INNER JOIN B
       ON (a.code = b.id)
GROUP BY a."DATE"

Which, for the sample data:

CREATE TABLE a (code, rate, "DATE") AS
  SELECT 1, 1000, DATE '2018-10-28' FROM DUAL UNION ALL
  SELECT 1, 2000, DATE '2018-10-29' FROM DUAL UNION ALL
  SELECT 1, 3000, DATE '2018-10-30' FROM DUAL UNION ALL
  SELECT 2, 2000, DATE '2018-10-28' FROM DUAL UNION ALL
  SELECT 2, 3000, DATE '2018-10-29' FROM DUAL UNION ALL
  SELECT 2, 4000, DATE '2018-10-30' FROM DUAL UNION ALL
  SELECT 3, 4000, DATE '2018-10-28' FROM DUAL UNION ALL
  SELECT 3, 5000, DATE '2018-10-29' FROM DUAL UNION ALL
  SELECT 3, 6000, DATE '2018-10-30' FROM DUAL

CREATE TABLE b (name, id) AS
  SELECT 'USD', 1 FROM DUAL UNION ALL
  SELECT 'EUR', 2 FROM DUAL UNION ALL
  SELECT 'OBP', 3 FROM DUAL;

Both output:

DATE USD EUR OBP
2018-10-28 00:00:00 1000 2000 4000
2018-10-29 00:00:00 2000 3000 5000
2018-10-30 00:00:00 3000 4000 6000

If you actually want to create a table then use CREATE TABLE ... AS SELECT ... with one of the above statements (or consider creating a view instead of a physical table).

fiddle

MT0
  • 143,790
  • 11
  • 59
  • 117
  • @MTO This question should be solved with the assumption that the number of rows in each of the tables (specifically B) is very large – Big Dream American Jan 25 '23 at 11:27
  • @fazi Then when you use the code provided in the answer, you can list all the currencies in your code. In SQL (not just Oracle) it is impossible to generate a static query with a dynamic number of output columns; you **must** generate a known, fixed, number of columns which means that `PIVOT` **must** have a known, fixed number of things it is pivoting by and you need to list them all. The same is true for queries with conditional aggregation; you need to list all the values you are conditionally aggregating with. – MT0 Jan 25 '23 at 11:43