2016-10-12 101 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

閱讀窗口函數教程的鏈接對不起,我忘記添加一些東西在我的查詢中,如果你能再次看看它,請稍作修改(限制)。 –