
[英]How to get sub total and grand total Based on heading and sub heading in Sql Server
[英]How to get a grand total of distinct cells based on T-sql count output?
我正在尝试从T-SQL查询的输出中获取计数。
这是样本表信息...
------------------------------------------------------
| order_no | company | destination | date |
|-------------|-------------|--------------|----------|
| 100 | Burger King | Los Angeles | 20140305 |
|-------------|-------------|--------------|----------|
| 101 | Burger King | Phoenix | 20140312 |
|-------------|-------------|--------------|----------|
| 102 | Burger King | Los Angeles | 20140322 |
|-------------|-------------|--------------|----------|
| 103 | McDonalds | Las Vegas | 20140315 |
|-------------|-------------|--------------|----------|
| 104 | McDonalds | Las Vegas | 20140324 |
|-------------|-------------|--------------|----------|
| 105 | McDonalds | Las Vegas | 20140305 |
|-------------|-------------|--------------|----------|
| 106 | McDonalds | Las Vegas | 20140311 |
|-------------|-------------|--------------|----------|
| 107 | Burger King | San Diego | 20140317 |
|-------------|-------------|--------------|----------|
| 108 | Burger King | Los Angeles | 20140305 |
|-------------|-------------|--------------|----------|
| 109 | Burger King | Phoenix | 20140311 |
|-------------|-------------|--------------|----------|
| 110 | Burger King | San Diego | 20140313 |
|-------------|-------------|--------------|----------|
| 111 | Burger King | Los Angeles | 20140319 |
|-------------|-------------|--------------|----------|
| 112 | Burger King | San Diego | 20140304 |
|-------------|-------------|--------------|----------|
基于此信息,然后运行以下查询。
SELECT company, COUNT(destination) as company_destination, destination
from dbo.burger_orders
WHERE (date >= 20140301 AND date <= 20140331)
group by company, destination
因此结果如下。 这正是我想要的一点信息。 但是,我需要以下结果提供其他统计信息。 我需要对下面记下的每个目的地进行计数。
Company Company Orders Destination
Burger King 4 Los Angeles
Burger King 3 San Diego
Burger King 2 Phoenix
McDonald's 4 Las Vegas
期望的输出
因此,我需要显示的是“目的地”列总计,该数量基于上列中的每个城市为4。
正如您在下面注意到的那样,我对如何解决此问题感到很困惑,因为我以前从未遇到过这种情况,所以我并没有取得部分成功。
我尝试过的
SELECT company, COUNT(destination) as company_destination,
COUNT(DISTINCT destination)
from dbo.burger_orders
WHERE (date >= 20140301 AND date <= 20140331)
group by company, destination
和
SELECT company, COUNT(destination) as company_destination,
SUM(COUNT(DISTINCT destination)) from dbo.burger_orders
WHERE (date >= 20140301 AND date <= 20140331)
group by company, destination
第一个获取将目标列中的每个单元格中的目标列变为1。 我明白了。 但是,我需要总计。 在第二个示例中,我尝试将SUM拍到COUNT前面,并收到以下错误。
无法对包含聚集或子查询的表达式执行聚集功能。
我是否甚至在正确的道路上获得正确的结果?
您正在寻找分析函数
像这样的东西:
SELECT company, company_orders, COUNT(destination) OVER () AS dest_count
FROM (
SELECT company, COUNT(destination) as company_orders, destination
FROM burger_orders
WHERE (date >= 20140301 AND date <= 20140331)
GROUP BY company, destination
) x
GROUP BY company, company_orders, destination;
这对您有帮助吗?
SELECT company,
COUNT(destination) as company_destination,
destination,
count(destination) over() as TotalCities
from dbo.burger_orders
WHERE (date >= 20140301 AND date <= 20140331)
group by company, destination
如果只想添加另一列,即结果集中的总行数,则可以使用窗口函数:
SELECT company, COUNT(destination) as company_destination, destination,
count(*) over () as NumCompanyDestinations
from dbo.burger_orders
WHERE (date >= 20140301 AND date <= 20140331)
group by company, destination;
这会将新列添加到结果集中,每行上有4
列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.