如何在一个mysql语句中实现这一点?

gywdnpxw  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(355)

我有一张有股票及其价格的table。此外,我在这张表中列出了所有的价格。现在我想得到所有股票的美元市值。原版 MarketCAP 列以本地货币表示。所以我必须乘以最新的利率。
为了获得最新的测试率,我做了以下工作:
表格示例:

DROP TABLE IF EXISTS `aktien`;          

  CREATE TABLE `aktien` (          
    `id` int(11) NOT NULL AUTO_INCREMENT,          
    `dummyfield` varchar(255) DEFAULT NULL,          
    `Company` varchar(255) DEFAULT NULL,          
    `Stock_Short` varchar(50) DEFAULT NULL,          
    `country` varchar(10) DEFAULT 'US',          
    `LastPrice` decimal(17,3) DEFAULT NULL,          
    `PrevClose` decimal(17,3) DEFAULT NULL,          
    `PercChange` varchar(50) DEFAULT NULL,          
    `DayLow` decimal(17,3) DEFAULT NULL,          
    `DayHigh` decimal(17,3) DEFAULT NULL,          
    `Dividend` decimal(17,3) DEFAULT '0.000',          
    `ExDATE` datetime DEFAULT NULL,          
    `ImportDATE` datetime DEFAULT NULL,          
    `TradeDate` date DEFAULT NULL,          
    `refTradeDate` datetime DEFAULT NULL,          
    `timed` time DEFAULT NULL,          
    `Open` decimal(17,3) DEFAULT '0.000',          
    `AfterHrsChange` varchar(200) DEFAULT NULL,          
    `Volume` bigint(20) DEFAULT '0',          
    `AskSize` int(17) DEFAULT '0',          
    `BidSize` int(17) DEFAULT '0',          
    `AvgDayVol` bigint(20) DEFAULT '0',          
    `50DMA` decimal(17,3) DEFAULT '0.000',          
    `200DMA` decimal(17,3) DEFAULT '0.000',          
    `52WeekHigh` decimal(17,3) DEFAULT '0.000',          
    `52WeekLow` decimal(17,3) DEFAULT '0.000',          
    `52WeekRange` varchar(200) DEFAULT NULL,          
    `MarketCap` varchar(200) DEFAULT NULL,          
    `EPS` decimal(17,3) DEFAULT '0.000',          
    `BV` decimal(17,3) DEFAULT '0.000',          
    `EBITDA` varchar(200) DEFAULT NULL,          
    `P2S` decimal(17,3) DEFAULT '0.000',          
    `P2B` decimal(17,3) DEFAULT '0.000',          
    `PE` decimal(17,3) DEFAULT '0.000',          
    `Revenue` varchar(200) DEFAULT NULL,          
    `Mean10LastPrice` decimal(17,3) DEFAULT '0.000',          
    `Mean20LastPrice` decimal(17,3) DEFAULT '0.000',          
    `Mean50LastPrice` decimal(17,3) DEFAULT '0.000',          
    `Mean100LastPrice` decimal(17,3) DEFAULT '0.000',          
    `Mean200LastPrice` decimal(17,3) DEFAULT '0.000',          
    `NewHigh` int(1) DEFAULT '0',          
    `NewLow` int(1) DEFAULT '0',          
    `stocktype` varchar(50) DEFAULT 'stock',          
    `NormLastPrice` decimal(17,3) DEFAULT NULL,          
    `trendcode` int(1) DEFAULT '0',          
    `Todayflag` int(1) DEFAULT '0',          
    `KGVC` decimal(17,3) DEFAULT NULL,          
    `KGVN` decimal(17,3) DEFAULT NULL,          
    `ShortRatio` decimal(17,3) DEFAULT NULL,          
    `EPSC` decimal(17,3) DEFAULT NULL,          
    `EPSN` decimal(17,3) DEFAULT NULL,          
    `Target1Y` decimal(17,3) DEFAULT NULL,          
    `AdjClose` decimal(17,3) DEFAULT NULL,          
    `DMANote` varchar(60) DEFAULT '',          
    PRIMARY KEY (`id`),          
    UNIQUE KEY `Stock_Short` (`Stock_Short`,`TradeDate`)          
  ) ENGINE=InnoDB AUTO_INCREMENT=1594308 DEFAULT CHARSET=latin1;          
  /*!40101 SET character_set_client = @saved_cs_client */;          

  --          
  -- Dumping data for table `aktien_stack`          
  --          

  LOCK TABLES `aktien` WRITE;          
  /*!40000 ALTER TABLE `aktien_stack` DISABLE KEYS */;          
  INSERT INTO `aktien` VALUES (1592307,NULL,'LUFTHANSA AG VNA 
  `O.N','LHA.DE','DE',25.110,24.290,'0.033758733',24.150,25.340,0.800,
  '2018-05-09 00:00:00','2018-05-11 20:01:06',
  '2018-05-11',NULL,NULL,24.270,NULL,4681038,1900800,948200,3320935,
   25.694,27.756,31.260,16.095,NULL,'11862591488',0.000,0.000,
  '5404000256',0.333,0.000,4.990,'35650998272',
  24.739,25.517,26.056,27.511,26.004,0,0,'stock',
  NULL,0,0,0.333,NULL,NULL,NULL,NULL,NULL,NULL,''), 
  (1592660,NULL,'EUR/USD','EURUSD=X','US',1.194,1.192,'0.0020306304',
  1.190,1.197,NULL,NULL,'2018-05-11 23:00:03',
  '2018-05-11',NULL,NULL,1.192,NULL,0,0,0,0,1.223,
  1.209,1.256,1.086,NULL,NULL,0.000,0.000,NULL,NULL,
  0.000,NULL,NULL,0.000,0.000,0.000,0.000,0.000,0,0,
  'cur',NULL,0,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'')

  /*!40000 ALTER TABLE `aktien_stack` ENABLE KEYS */;          
  UNLOCK TABLES;

如何在一个语句中将所有市值乘以所有股票的汇率(考虑localcurrencycountry=country)?
我是这样做的:(我想可能是用临时表来做的,对吧?)

drop table if exists rr;
    create TEMPORARY TABLE rr 
    SELECT  s.Stock_Short,
                s.country,
                rate.country as LocalCurrencyCountry,
                s.PrevClose as FXRATE, TradeDate from 
                    (select *,max(id) as maxid  from aktien GROUP BY 
                TradeDate,Stock_short order by id desc) s  
                join rate on s.Stock_Short = rate.`code` where s.id = maxid 
                group by LocalCurrencyCountry order by TradeDate desc;

                SELECT aktien.MarketCap * FXRATE ,rr.fxrate, Marketcap, 
                aktien.stock_short from aktien join  rr on aktien.country = 
                rr.localcurrencyCountry group by aktien.country;
lb3vh1jj

lb3vh1jj1#

这是我试图解决这个问题,它包含3(!)子查询,所以它不好看,但也许有人可以优化这一点。
第一个子查询用于选择每个国家的“外汇汇率”行,第二个子查询用于从最终结果中排除相同的“外汇汇率”行,第三个子查询仅包括最新的市值

SELECT stock_short, marketCap * FX.rate, FX.country
FROM aktien a,
     (SELECT PrevClose as rate, a3.country 
      FROM aktien a3 JOIN rate r ON a3.stock = r.code 
      ORDER BY tradeDate desc LIMIT 1) as FX
WHERE NOT EXISTS (SELECT * FROM rate WHERE code = a.stock)
AND tradeDate = (SELECT MAX(tradeDate) FROM aktien a2 WHERE a2.stock = a.stock)
li9yvcax

li9yvcax2#

你需要做一个选择和一个子选择。subselect将选择键和按键分组的max(tradedate)。外部的select将在交易日从该表返回到表中,这样您就可以获得每个表的最新汇率。
主键为id、date的通用示例。如果您想具体到您的表,请提供更多信息。

SELECT z.id, z.maxDate, t.rate
FROM (
    SELECT id, MAX(date) AS maxDate 
    FROM table
    GROUP BY id
) z
JOIN table t ON z.id=t.id AND z.maxDate=t.date

相关问题