一般来说,我真的是sql / stackoverflow的新手,所以请多多包涵。
我有一个独特的选择查询,它返回以下结果
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="full-width">
<iframe class="video-iframe fullsize"></iframe>
<a href="#section2">
<span class="dot">
<i class="arrow-down"></i>
</span>
</a>
</div>
<div class="half-width" id="section2">
<div class="half-width-content">
<h1></h1>
<div class="half-width-text">
<div class="text-content">
<p>
</p>
</div>
</div>
</div>
</div>
<div class="half-width">
<div class="half-width-content">
<div class="instagram-slideshow" id="1">
<img class="slide">
</div>
</div>
</div>
<div class="half-width section3">
<div class="half-width-content">
<div class="instagram-slideshow" id="2">
<img class="slide">
</div>
</div>
</div>
<div class="half-width section3">
<div class="half-width-content">
<div class="middle-text">
<h2></h2>
<p></p>
<div class="list-div">
<ul class="items-list" id="list">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<img class="hand-writing-img">
</div>
</div>
</div>
<div class="full-width section3">
<div class="content">
<div class="third-parent">
<div class="third" id="one">
<img>
</div>
<div class="third" id="two">
<div class="middle-text">
<h1></h1>
<div class="list-div2">
<ul class="items-list2" id="list">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
</div>
<div class="third" id="three">
<img>
</div>
</div>
</div>
</div>
</div>
因此,显然每个查询将具有不同的名称。我想展示的是 如果第一人称和第二人称只有一行,如果它的ID为0,则想显示一条完成的消息;如果它的1->不完整,并且它们有2行(0和1),我想显示工作消息。 / p>
Name Surname Name2 Surname2 Status
---- ------- ----- -------- ------
Peter Parker Mary Jane 0
Peter Parker Felicia Hardy 1
Peter Parker Felicia Hardy 0
Peter Parker Jonah Jameson 1
我该怎么办?
答案 0 :(得分:1)
使用汇总功能时的用例
select Name,Surname,Name2,Surname2,
case when count( distinct Status) = 2 then
'Working' when sum(Status)=0 then 'Finished' else 'Incomplete'
end as status
from t
group by Name, Surname, Name2, Surname2;
NAME SURNAME NAME2 SURNAME2 STATUS
Peter Parker Mary Jane Finished
Peter Parker Felicia Hardy Working
Peter Parker Jonah Jameson Incomplete
答案 1 :(得分:0)
您可以使用此
SELECT name, surname, name2, surname2,
CASE
WHEN COUNT(DISTINCT status) = 2 THEN 'Working'
WHEN MAX(status) = 1 THEN 'Incomplete'
WHEN MAX(status) = 0 THEN 'Finished'
END AS status
FROM table_name
--WHERE status IN (0, 1)
GROUP BY name, surname, name2, surname2
ORDER BY name, surname, name2, surname2;
或者这个:
SELECT name, surname, name2, surname2,
CASE
WHEN MIN(status) = 1 THEN 'Incomplete'
WHEN MAX(status) = 0 THEN 'Finished'
ELSE 'Working'
END AS status
FROM table_name
WHERE status IN (0, 1)
GROUP BY name, surname, name2, surname2
ORDER BY name, surname, name2, surname2;
如果您只有两个状态0
和1
,则省略WHERE
子句
答案 2 :(得分:0)
您可以将group by Name, Surname, Name2, Surname2
与avg(status)
选项一起使用
select Name, Surname, Name2, Surname2,
( case when avg(Status) = 1 then
'Incomplete'
when avg(Status) = 0 then
'Finished'
else
'Working'
end ) as status
from tab
group by Name, Surname, Name2, Surname2;