正在查找如何编写CASE和COUNT个查询,该查询将显示日期为2017年1月2日至2017年4月19日期间的项目总销售天数。我正在接受“期待”这个词之间的预期。 ' BETWEEN'关键字。
表1:
ITEM SALE DATES
Apple Yes 4/19/2017
Apple Yes 2/18/2017
Apple Yes 1/17/2017
Apple no 3/16/2017
Apple no 4/17/2017
Banana Yes 4/19/2017
Banana Yes 2/18/2017
Banana Yes 1/17/2017
Banana Yes 3/16/2017
Banana no 4/17/2017
我在寻找:
ITEM Total days on sale
Apple 2
Banana 3
当前查询:
SELECT
ITEM,
CASE WHEN SALE='YES' THEN COUNT(DATES BETWEEN '2/1/2017' AND '4/19/2017') END AS TOTAL_DAYS_ON_SALE_DAYS
FROM TABLE 1
GROUP BY
ITEM
ORDER BY
ITEM ASC
答案 0 :(得分:0)
我认为你想要条件聚合:
SELECT ITEM,
SUM(CASE WHEN SALE = 'YES' AND DATES BETWEEN DATE '2017-02-01' AND DATE '2017-04-19'
THEN 1 ELSE 0
END) AS TOTAL_DAYS_ON_SALE_DAYS
FROM TABLE 1
GROUP BY ITEM
ORDER BY ITEM ASC;
答案 1 :(得分:0)
您可以将DATES上的检查移至WHERE子句 并总结YES
<ul class="nav navbar-nav navbar-right">
@{
AccountContactVM customer = (AccountContactVM)ViewBag.AccountData;
}
@if (customer.User == customer.User && customer.User != null )
{
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<i class="fa fa-user-circle fa-fw fa-lg" aria-hidden="true"</i>
@customer.User.FirstName @customer.User.LastName
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li class="disabled">
<div class="row">
<div class="col-md-3">
<i class="fa fa-user-circle" style="font-size:40px;padding:5px 20px;" aria-hidden="true"></i>
</div>
<div class="col-md-9">
<p style="padding-top:5px;">
@customer.User.FirstName @customer.User.LastName
<br />
@customer.Company.AccountName
</p>
</div>
</div>
</li>
<li class="divider"></li>
<li><a asp-controller="Account" asp-action="Index">Customer Portal</a></li>
<li class="divider"></li>
<li><a a asp-controller="Account" asp-action="LogOut">Logout</a></li>
</ul>
</li>
}
else
{
<li><a asp-controller="Account" asp-action="SignUp">Register</a></li>
<li><a asp-controller="Account" asp-action="SignIn"><i class="fa fa-sign-in fa-fw" aria-hidden="true"></i>Login</a></li>
}
<li ng-cloak><a href="@Url.Action("Index","Cart")"><span class="glyphicon glyphicon-shopping-cart"></span></i>Cart(<strong>{{CartCount}}</strong>)</a></li>
</ul>
您的查询的问题是,COUNT(DATES)之类的东西可以使用,但不能使用BETWEEN或=等标准。