如何计算SQL Server中具有不同范围的行来计算范围内的记录

时间:2014-01-21 02:35:20

标签: sql-server-2008-r2

SELECT Distinct 
    m_KioskInformationHdr.kioskID,m_KioskInformationHdr.agency,
    m_KioskInformationHdr.kioskIP,
    COUNT(distinct m_KioskInformationHdr.KioskID) as NoDT
from
    m_KioskInformationHdr
inner join 
    t_KioskStatus ON t_kioskStatus.kioskID = m_KioskInformationHdr.kioskID
where
    t_KioskStatus.IsOpen = 1 
    and DatePost between '2014-01-20 00:00:00' and '2014-01-21 23:59:59'
group by 
    t_KioskStatus.DatePost, m_KioskInformationHdr.kioskID,
    m_KioskInformationHdr.kioskIP, m_KioskInformationHdr.agency

我想要这个输出:

01254878 | PHilippines,Pasig | 192.168.2.8 | 2

这是我目前的输出:

01254878 | PHilippines,Pasig | 192.168.2.8 | 1
01254878 | PHilippines,Pasig | 192.168.2.8 | 1

我该如何进行此查询?

1 个答案:

答案 0 :(得分:0)

Select 
mk.kioskID, mk.agency, mk.kioskIP, count(mk.kioskID) as NoDT 
from   
( 
    SELECT
    m_KioskInformationHdr.kioskID,m_KioskInformationHdr.agency,m_KioskInformationHdr.kioskIP

    from  
        m_KioskInformationHdr
    inner join
        t_KioskStatus ON t_kioskStatus.kioskID = m_KioskInformationHdr.kioskID
    where
        t_KioskStatus.IsOpen = 1 
        and DatePost between '2014-01-20 00:00:00' and '2014-01-21 23:59:59') mk
    group by 
        mk.DatePost, mk.kioskID,
        mk.kioskIP, mk.agency