[英]How to get the count of a join table in sql server?
I am working with an existing sql server statement: 我正在使用现有的SQL Server语句:
select
rs.record_StepID, rs.recordID, rs.stepTypeID,
rs.label, rs.assignedToUserID, rs.stepActivatedDate,
si.scheduledInspectionID,
l.streetNo, l.streetName, l.unit, l.city, l.state,
l.postalCode, l.country, rt.categoryName
from
Record_Steps as rs
left join
Users as u on rs.assignedToUserID = u.userID
left join
ScheduledInspections as si on rs.record_StepID = si.record_StepID
left join
Records as r on r.recordID = rs.recordID
left join
Locations as l on l.locationID = r.locationID
left join
recordTypes as rt on r.recordTypeID = rt.recordTypeID
where
(rs.stepTypeID = 1 and rs.status = 1 and rs.assignedToUserID = '3200')
or
(rs.stepTypeID = 6 and rs.status = 1 and rs.assignedToUserID = '3200')
or
(rs.stepTypeID = 4 and rs.status = 1 and si.inspectorUserID = '3200'
and si.inspectionDate IS NOT NULL
AND CAST(si.inspectionDate AS DATE) <= CONVERT(DATE,'2014-9-8'))
order by
stepActivatedDate asc
And all I need to do right now is to get the count of the records retrieved by this statement. 我现在要做的就是获取此语句检索的记录数。 I will need to set up a conditional to decide when to get a count and when to get the actual data but right now I need to get the count first, then after confirmation on the client side, get the data. 我将需要设置一个条件来决定何时获取计数以及何时获取实际数据,但是现在我需要首先获取计数,然后在客户端确认后获取数据。
I am more familiar with mysql so I am looking to get something like select count(*) from (select * from users)
, but any alteration that will allow me to get only the count as the return value from this statement would help me greatly. 我对mysql较为熟悉,因此我希望select count(*) from (select * from users)
获取类似select count(*) from (select * from users)
,但是任何允许我仅获取count的更改,因为该语句的返回值将对我有很大帮助。
I tried doing 我试着做
select count(*) from ( <copy paste statement>)
to no avail. 无济于事。
This should work. 这应该工作。 I removed the order by
from your derived table as it would cause an error and also gave the derived table an alias. 我从您的派生表中删除了order by
,因为这将导致错误,并且还为派生表提供了别名。
select count(1) as Cnt
from
(
select rs.record_StepID, rs.recordID, rs.stepTypeID, rs.label, rs.assignedToUserID, rs.stepActivatedDate,si.scheduledInspectionID, l.streetNo,l.streetName, l.unit, l.city, l.state, l.postalCode, l.country, rt.categoryName
from Record_Steps as rs
left join Users as u on rs.assignedToUserID = u.userID
left join ScheduledInspections as si on rs.record_StepID=si.record_StepID
left join Records as r on r.recordID = rs.recordID
left join Locations as l on l.locationID = r.locationID
left join recordTypes as rt on r.recordTypeID = rt.recordTypeID
where (rs.stepTypeID = 1 and rs.status=1 and rs.assignedToUserID = '3200')
or (rs.stepTypeID = 6 and rs.status=1 and rs.assignedToUserID = '3200')
or (rs.stepTypeID = 4 and rs.status = 1 and si.inspectorUserID = '3200' and si.inspectionDate is not NULL and CAST(si.inspectionDate AS DATE) <= CONVERT(DATE,'2014-9-8'))
) as aTable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.