如何将MySQL语句整合在一起?

时间:2018-05-12 14:45:51

标签: mysql sql

我有一张有股票及其价格的表格。此外,我在这张表中有所有价格。现在,我希望所有股票的 USD(=专栏MarketCap * FXRATE)获得所有MArketCAP。原始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;          

如何在所有股票的FXRATE中将所有MarketCap乘以(考虑LocalCurrencyCountry = country)?

我这样做了:(我想可以用临时表完成,对吗?)

enter image description here

    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;

2 个答案:

答案 0 :(得分:1)

您需要执行选择和子选择。子选择将选择由密钥分组的密钥和MAX(TradeDate)。外部选择将从交易日期的表格加入到交易日期的表格中,以便您获得每个的最新价格。

主键为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

答案 1 :(得分:1)

这是我尝试解决此问题,它包含3个(!)子查询,所以它不漂亮,但也许有人可以优化它。

第一个子查询是为每个国家选择“fx-rate”行,第二个是从最终结果中排除相同的“fx-rate”行,第三个是仅包含最新的市值上限

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)