2016-10-12 102 views
0

所以我有一些查询基本上是该向SQL查询添加一列,表示查询中的行数?

select * 
from tiket 
where storno = 'yes' 
    and time_storno > '2016-07-25 11:48:48.062' 
order by time_storno asc limit 100 

我可以修改此查询以添加在每行的行从表中的头号多个列?此表可以具有更少的行大于100

+0

基本上要存储的行数在表中每个记录在表中? –

回答

2

使用a window function

select *, count(*) over() as row_count 
from tiket 
where storno = 'yes' 
    and time_storno > '2016-07-25 11:48:48.062' 
order by time_storno asc 

这将包含满足标准的行数(使得row_count由查询返回的行的数目相匹配)。

如果你想获得的行数(不where子句)使用标量子查询

select *, (select count(*) from tiket) as row_count 
from tiket 
where storno = 'yes' 
    and time_storno > '2016-07-25 11:48:48.062' 
order by time_storno asc 
+0

是否使用'over'否定分组的需要? – JohnHC

+1

@JohnHC:是的,从我的回答 –

+0

阅读窗口函数教程的链接对不起,我忘记添加一些东西在我的查询中,如果你能再次看看它,请稍作修改(限制)。 –