在您阅读之前,请注意,我创建了一个查询来实现以下目的,但只是想仔细检查我是否做错了什么,或者是否有更有效的方法。
我在MySQL表中有一组记录,如下所示(按运行代码段查看该表)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252"/>
<title></title>
<meta name="generator" content="LibreOffice 4.4.5.2 (Windows)"/>
<meta name="created" content="2018-06-21T09:33:19.600000000"/>
<meta name="changed" content="2018-06-21T09:42:20.756000000"/>
<style type="text/css">
body,div,table,thead,tbody,tfoot,tr,th,td,p { font-family:"Calibri"; font-size:x-small }
</style>
</head>
<body>
<table cellspacing="0" border="0">
<colgroup width="85"></colgroup>
<colgroup width="84"></colgroup>
<colgroup width="110"></colgroup>
<colgroup width="127"></colgroup>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="21" align="left"><font color="#000000">Date</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000">PortfolioId</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000">ISIN</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000">AssetOwnsership</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="21" align="right" sdval="43269" sdnum="2057;0;DD/MM/YYYY"><font color="#000000">18/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="1234" sdnum="2057;"><font color="#000000">1234</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000">GB00B069DV22</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="1" sdnum="2057;"><font color="#000000">1</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="21" align="right" sdval="43270" sdnum="2057;0;DD/MM/YYYY"><font color="#000000">19/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="3521" sdnum="2057;"><font color="#000000">3521</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000">GB00B069DV22</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="1.18" sdnum="2057;"><font color="#000000">1.18</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="21" align="right" sdval="43270" sdnum="2057;0;DD/MM/YYYY"><font color="#000000">19/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="3521" sdnum="2057;"><font color="#000000">3521</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000">GB00B6774699</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="0.1298" sdnum="2057;"><font color="#000000">0.1298</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="21" align="right" sdval="43270" sdnum="2057;0;DD/MM/YYYY"><font color="#000000">19/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="3521" sdnum="2057;"><font color="#000000">3521</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000"><br></font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="0" sdnum="2057;"><font color="#000000">0</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="21" align="right" sdval="43270" sdnum="2057;0;DD/MM/YYYY"><font color="#000000">19/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="82294" sdnum="2057;"><font color="#000000">82294</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000"><br></font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="0" sdnum="2057;"><font color="#000000">0</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="21" align="right" sdval="43270" sdnum="2057;0;DD/MM/YYYY"><font color="#000000">19/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="82294" sdnum="2057;"><font color="#000000">82294</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000"><br></font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="0" sdnum="2057;"><font color="#000000">0</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="21" align="right" sdval="43270" sdnum="2057;0;DD/MM/YYYY"><font color="#000000">19/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="82294" sdnum="2057;"><font color="#000000">82294</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000">GB00B6774699</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="0.1378" sdnum="2057;"><font color="#000000">0.1378</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="21" align="right" sdval="43271" sdnum="2057;0;DD/MM/YYYY"><font color="#000000">20/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="3521" sdnum="2057;"><font color="#000000">3521</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000">GB00B069DV22</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="1.25" sdnum="2057;"><font color="#000000">1.25</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="21" align="right" sdval="43271" sdnum="2057;0;DD/MM/YYYY"><font color="#000000">20/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="3521" sdnum="2057;"><font color="#000000">3521</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000">GB00B6774699</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="0.1175" sdnum="2057;"><font color="#000000">0.1175</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="21" align="right" sdval="43271" sdnum="2057;0;DD/MM/YYYY"><font color="#000000">20/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="3521" sdnum="2057;"><font color="#000000">3521</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000">GB00B6774691</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="0.1" sdnum="2057;"><font color="#000000">0.1</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="21" align="right" sdval="43271" sdnum="2057;0;DD/MM/YYYY"><font color="#000000">20/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="1234" sdnum="2057;"><font color="#000000">1234</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font color="#000000">GB00B6774699</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="0.5" sdnum="2057;"><font color="#000000">0.5</font></td>
</tr>
</table>
<!-- ************************************************************************** -->
</body>
</html>
我想做的是找到Assetswnwnsership列在今天和昨天之间的差异,以基于两列(ISIN和Portfolio ID)的匹配来显示当前头寸的增加或减少,并输出任何新记录/今天的位置。
基于上述数据集,输出应为:(按运行代码段查看表)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252"/>
<title></title>
<meta name="generator" content="LibreOffice 4.4.5.2 (Windows)"/>
<meta name="created" content="2018-06-21T10:02:42.397000000"/>
<meta name="changed" content="2018-06-21T10:03:10.865000000"/>
<style type="text/css">
body,div,table,thead,tbody,tfoot,tr,th,td,p { font-family:"Liberation Sans"; font-size:x-small }
</style>
</head>
<body>
<table cellspacing="0" border="0">
<colgroup span="4" width="85"></colgroup>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="32" align="left"><font face="Liberation Serif" color="#000000">Date</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font face="Liberation Serif" color="#000000">PortfolioId</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font face="Liberation Serif" color="#000000">ISIN</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font face="Liberation Serif" color="#000000">AssetOwnsership</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="32" align="right" sdval="43271" sdnum="2057;0;DD/MM/YYYY"><font face="Liberation Serif" color="#000000">20/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="3521" sdnum="2057;"><font face="Liberation Serif" color="#000000">3521</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font face="Liberation Serif" color="#000000">GB00B069DV22</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="0.0700000000000001" sdnum="2057;"><font face="Liberation Serif" color="#000000">0.07</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="32" align="right" sdval="43271" sdnum="2057;0;DD/MM/YYYY"><font face="Liberation Serif" color="#000000">20/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="3521" sdnum="2057;"><font face="Liberation Serif" color="#000000">3521</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font face="Liberation Serif" color="#000000">GB00B6774699</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="-0.0123" sdnum="2057;"><font face="Liberation Serif" color="#000000">-0.0123</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="32" align="right" sdval="43271" sdnum="2057;0;DD/MM/YYYY"><font face="Liberation Serif" color="#000000">20/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="3521" sdnum="2057;"><font face="Liberation Serif" color="#000000">3521</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font face="Liberation Serif" color="#000000">GB00B6774691</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="0.1" sdnum="2057;"><font face="Liberation Serif" color="#000000">0.1</font></td>
</tr>
<tr>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="32" align="right" sdval="43271" sdnum="2057;0;DD/MM/YYYY"><font face="Liberation Serif" color="#000000">20/06/2018</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="1234" sdnum="2057;"><font face="Liberation Serif" color="#000000">1234</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="left"><font face="Liberation Serif" color="#000000">GB00B6774699</font></td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="right" sdval="0.5" sdnum="2057;"><font face="Liberation Serif" color="#000000">0.5</font></td>
</tr>
</table>
<!-- ************************************************************************** -->
</body>
</html>
我创建了下面的SQL查询,该查询为我提供了以下数据:(假设今天是20/06/2018,可以对数据进行运行)
SELECT *, (case WHEN SUM(b.assetownsership-a.assetownsership)=0 then b.assetownsership
else SUM(b.assetownsership-a.assetownsership)
end) Difference, (case WHEN SUM(b.assetownsership-a.assetownsership)<0 then "S"
else "B"
end) BuySell
FROM performance.Assetallocation a
JOIN performance.Assetallocation b on b.isin = a.isin and b.portfolioID=a.portfolioID
where b.date>=DATE_FORMAT(CURDATE(), '%d/%m/%Y')-1
group by a.portfolioID, a.isin
我的问题是:
答案 0 :(得分:0)
我认为您的查询需要进行重大更改,因为您需要根据protfolioID和ISIN查找今天和昨天之间的差异,它应该像
SELECT
b.date,
(CASE WHEN (b.assetownsership-a.assetownsership)<=0 THEN b.assetownsership
ELSE (b.assetownsership-a.assetownsership) END) Difference,
(CASE WHEN (b.assetownsership-a.assetownsership)<0 THEN "S" ELSE "B" END) BuySell
FROM performance.Assetallocation a
JOIN performance.Assetallocation b ON b.isin = a.isin AND b.portfolioID=a.portfolioID
WHERE b.date = DATE_FORMAT(CURDATE(), '%d/%m/%Y')
AND a.date = DATE_FORMAT(CURDATE(), '%d/%m/%Y')-1
答案 1 :(得分:0)
我认为这是查询的一种更好形式:
select a.portfolioID, a.isin,
sum(case when a.date = curdate()
then a.assetownsership
when a.date = curdate() - interval 1 day
then - a.assetownsership
end) as difference,
(case when sum(case when a.date = curdate()
then a.assetownsership
when a.date = curdate() - interval 1 day
then - a.assetownsership
end) < 0
then 'S' else 'B'
end) as BuySell
from performance.Assetallocation a
where a.date >= curdate() - interval 1 day
group by a.portfolioID, a.isin;
注意:
join
。date
实际上是一个日期。如果它具有时间成分,请使用date()
函数或类似的逻辑。select
中唯一未聚合的表达式应该是group by
中的表达式。因此,没有select *
。