我有一张这样的桌子:
id name date
-------------------
1 Adam 2018-10-01
1 Adam 2018-08-01
2 Eve 2018-07-01
2 Eve 2018-05-01
我希望它变成这样:
id name firstdate lastdate
--------------------------------
1 Adam 2018-08-01 2018-10-01
2 Eve 2018-05-01 2018-07-01
我尝试使用此查询,但失败了:
SELECT * FROM View_MySource
PIVOT (
MIN(mydate)
FOR id IN ([firstdate], [lastdate])
) piv
我是新手,有人可以帮助我吗?
答案 0 :(得分:5)
在这种情况下,我什至可以避免使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="swapper-first" class="" style="display:block; border:2px dashed red; padding:25px;">
<p style="margin:0; color:red;">
This div displayed when the web page first loaded.
</p>
</div>
<div id="swapper-other" style="display:block; border:2px dotted blue; padding:25px;">
<p style="margin:0; color:blue;">
This div displayed when the link was clicked.
</p>
</div>
<p style="text-align:center; font-weight:bold; font-style:italic;">
<a href="javascript:SwapDivsWithClick()">(Swap Divs)</a>
</p>
:
PIVOT
答案 1 :(得分:4)
这实际上与数据透视无关。您要做的就是获取每个ID的最小和最大日期;一个简单的聚合:
select id, name, min(date) as firstdate, max(date) as lastdate
from view_mysource
group by id, name
order by id;