我正在尝试找出SQL来汇总我的Vehicles
表中的数据:
Id | Name | Make
---+-------+------
1 Car Ford
1 Car Volvo
1 Car BMW
2 Bike Honda
3 Truck Tata
3 Truck abc
4 Train bullet
以返回以下内容:
Id | Name | Result
---+-------+------
1 Car 3 items selected
2 Bike Honda
3 Truck 2 items selected
4 Train bullet
因此,如果Id,Name
组合具有多个制造商,则输出“ X个选定项目”,其中X是该组合的制造商数量。否则,按原样输出Make。请注意,对于每个Id,Name组合而言,make都是唯一的。
这可以使用GROUP BY表达式实现吗?如果是这样,我需要什么聚合函数来代替????下面?还是可以通过其他方式实现?
select Id,
Name,
case when count(Make) > 1 then convert(varchar(10), count(Make)) + ' items selected'
else ??? end Result
from Vehicles
group by Id, Name
我正在使用SQL Server 2008。
答案 0 :(得分:2)
使用<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<label id="ratestructureLabel">Required Rate Structure:</label>
<br/>
<div class="form-check">
<input class="form-check-input" type="radio" name="ratestructure" id="allin" value="all-in">
<label class="form-check-label" for="yesebiz">All-In</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="ratestructure" id="allindest" value="all-inD">
<label class="form-check-label" for="allindest">All-In, subject to Destinations</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="ratestructure" id="allinori" value="all-inO">
<label class="form-check-label" for="allinori">All-In, subject to Origins</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="ratestructure" id="boforindest" value="bofOD">
<label class="form-check-label" for="boforindest">BOF, subject to Origins & Destination</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="ratestructure" id="pernote2" value="pernote2">
<label class="form-check-label" for="pernote2">Per Note 2</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="ratestructure" id="surcharges" value="surcharges">
<label class="form-check-label" for="surcharges">Surcharges per tariff</label>
</div>
<div class="form-check form-check-inline otherrate">
<input class="form-check-input" type="radio" name="ratestructure" id="otherrate" value="Other Rate">
<label class="form-check-label" for="otherrate">Other:</label>
<input type="text" class="form-control ml-2" name="otherstructure" id="otherrateIn" placeholder="">
</div>
</div>
</div>
</div>
<button id="nextsales3Btn">Check</button>
或MIN()
:
MAX()
如果只有一行,则select Id, Name,
(case when count(Make) > 1
then convert(varchar(10), count(Make)) + ' items selected'
else min(Make)
end) as Result
from Vehicles
group by Id, Name;
和MIN()
是该行的值。
答案 1 :(得分:0)
使用CTE(公用表表达式),因为这样可以提高查询性能,尤其是在查询中。检查查询
WITH Vehicles_CTE (Id, Name, Result,Make)
AS (
SELECT Id, Name, COUNT(1) Result, MAX(Make) Make FROM Vehicles
Group By Id, Name
)
SELECT Id, Name, CASE WHEN Result>1
THEN Cast(Result AS varchar(10)) + ' items selected'
ELSE Make End Result FROM Vehicles_CTE