从结果多次计数的sql中选择数据

时间:2017-03-02 06:31:24

标签: sql

我需要帮助。 我想从这样的SQL中选择数据。总计是count(*) * 7500的结果。

| fullname | count(*) | total |
+==========+==========+=======+
| angelis  | 3        | 22500 |
| freed    | 2        | 14000 |
| debora   | 4        | 28500 |

我使用像这样的查询

select fullname, count(*) as jml 
from pms_occupancy 
where month(date)='02' 
group by fullname

并显示

enter image description here

1 个答案:

答案 0 :(得分:0)

使用以下代码在SQL SERVER中查找所需的输出:

样本表结构

=================
ID  |   Country
=================
1   |   C1
2   |   C2
3   |   C3
4   |   C4
=================

必填查询:

declare @result_string varchar(max)=''
;WITH CTE_TABLE 
AS
(
SELECT ID,Country FROM dbo.tblCountry
)
select @result_string=@result_string+'<tr><td>'+(cast(ID as varchar(100))+'</td><td>'+Country+'</td></tr>') from (
SELECT * FROM CTE_TABLE
UNION 
SELECT COUNT(*) ID,'Total' Country FROM CTE_TABLE
)a

select '<table>'+@result_string+'</table>'

查询输出

<table>
<tr><td>1</td><td>C1</td></tr>
<tr><td>2</td><td>C2</td></tr>
<tr><td>3</td><td>C3</td></tr>
<tr><td>4</td><td>C4</td></tr>
<tr><td>4</td><td>Total</td></tr>
</table>