Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#10

AW: Aufbau einer Währungstabelle

  Alt 21. Jan 2015, 13:38
Mein Vorschlag dazu wäre
SQL-Code:
DROP TABLE IF EXISTS "currency_rates";
DROP TABLE IF EXISTS "currencies";

-- Stammdaten Währungen

CREATE TABLE "currencies" (
    "ISOCODE" VARCHAR(3) NOT NULL,
    "ISONUM" int(3) NOT NULL,
    "ISONUMdate" date NOT NULL,
    "Name" text,
    "Format" text,
   PRIMARY KEY("ISOCODE")
);
CREATE INDEX "idx_currencies_ISONUM" ON currencies (ISONUM, ISONUMdate);
CREATE UNIQUE INDEX "idx_currencies_ISONUMCODE" ON currencies (ISONUM, ISONUMdate, ISOCODE);

-- Währungs-Daten

INSERT INTO "currencies" ( "ISOCODE", "ISONUM", "ISONUMdate", "Name", "Format" )
  VALUES ( 'EUR', 978, '1990-01-01', 'Euro', '%f €' );
INSERT INTO "currencies" ( "ISOCODE", "ISONUM", "ISONUMdate", "Name", "Format" )
  VALUES ( 'USD', 840, '1990-01-01', 'Dollar', '$ %f' );

-- Besonderheiten bei der Belegung der ISONUM Werte

INSERT INTO "currencies" ( "ISOCODE", "ISONUM", "ISONUMdate", "Name", "Format" )
  VALUES ( 'BUK', 104, '1952-07-01', 'Burma Kyat', '%f BUK' );
INSERT INTO "currencies" ( "ISOCODE", "ISONUM", "ISONUMdate", "Name", "Format" )
  VALUES ( 'MMK', 104, '1990-02-01', 'Myanmar Kyat', '%f MMK' );

INSERT INTO "currencies" ( "ISOCODE", "ISONUM", "ISONUMdate", "Name", "Format" )
  VALUES ( 'SUR', 810, '1923-01-01', 'Sowjetischer Rubel', '%f SUR' );
INSERT INTO "currencies" ( "ISOCODE", "ISONUM", "ISONUMdate", "Name", "Format" )
  VALUES ( 'RUR', 810, '1991-01-01', 'Russicher Rubel', '%f RUR' );
INSERT INTO "currencies" ( "ISOCODE", "ISONUM", "ISONUMdate", "Name", "Format" )
  VALUES ( 'RUB', 643, '1998-01-01', 'Neuer Russischer Rubel', '%f RUB' );

-- Wechselkurse

CREATE TABLE "currency_rates" (
    "sourceISOCODE" varchar(3) NOT NULL,
    "destinationISOCODE" varchar(3) NOT NULL,
    "ratetype" varchar(3) NOT NULL,
    "ratedate" "date" NOT NULL,
    "rate" real NOT NULL,
   PRIMARY KEY("sourceISOCODE","destinationISOCODE","ratetype","ratedate"),
   CONSTRAINT "fk_source_currency" FOREIGN KEY ("sourceISOCODE")
     REFERENCES "currencies" ("ISOCODE") ON DELETE CASCADE ON UPDATE CASCADE,
   CONSTRAINT "fk_destination_currency" FOREIGN KEY ("destinationISOCODE")
     REFERENCES "currencies" ("ISOCODE") ON DELETE CASCADE ON UPDATE CASCADE
);

-- Daten Wechselkurse

INSERT INTO "currency_rates" ("sourceISOCODE", "destinationISOCODE", "ratetype", "ratedate", "rate")
  VALUES ( "EUR", "USD", "AWK", date('2015-01-01'), 1.11 );
INSERT INTO "currency_rates" ("sourceISOCODE", "destinationISOCODE", "ratetype", "ratedate", "rate")
  VALUES ( "EUR", "USD", "AWK", date('2015-01-02'), 1.12 );
INSERT INTO "currency_rates" ("sourceISOCODE", "destinationISOCODE", "ratetype", "ratedate", "rate")
  VALUES ( "EUR", "USD", "AWK", date('2015-01-03'), 1.13 );
INSERT INTO "currency_rates" ("sourceISOCODE", "destinationISOCODE", "ratetype", "ratedate", "rate")
  VALUES ( "EUR", "USD", "AWK", date('2015-01-04'), 1.14 );
INSERT INTO "currency_rates" ("sourceISOCODE", "destinationISOCODE", "ratetype", "ratedate", "rate")
  VALUES ( "EUR", "USD", "AWK", date('2015-01-05'), 1.15 );
INSERT INTO "currency_rates" ("sourceISOCODE", "destinationISOCODE", "ratetype", "ratedate", "rate")
  VALUES ( "EUR", "USD", "AWK", date('2015-01-06'), 1.16 );
INSERT INTO "currency_rates" ("sourceISOCODE", "destinationISOCODE", "ratetype", "ratedate", "rate")
  VALUES ( "EUR", "USD", "AWK", date('2015-01-07'), 1.17 );

-- Abfrage eines konkreten Wechselkurs

SELECT *
FROM currency_rates
WHERE sourceISOCODE = 'EUR'
AND destinationISOCODE = 'USD'
AND ratetype = 'AWK'
AND ratedate <= date( '2015-01-03' )
ORDER BY ratedate DESC
LIMIT 1;
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)

Geändert von Sir Rufo (21. Jan 2015 um 13:41 Uhr)
  Mit Zitat antworten Zitat