如何在循环中运行查询并计算每个循环的行数

时间:2019-03-20 17:20:46

标签: sql sql-server datetime

我有一个查询可以为我收集数据,在查询的最后,我要过滤两个日期,并计算行数。

FROM TAB
WHERE
      (tab.transfer_date < '2019-03-11' AND  Real_Updated_date >= '2019-03-11') 
      ORDER BY transfer_date

是否有可能将两个日期都增加“ 1”直到“ 2019-03-20” 并计算并打印我每天有多少行? 谢谢!

完整查询:

WITH TAB AS (
SELECT 

      [vortex_hvc].[vortex_dbo].material_history.updated_datetime
       ,[vortex_hvc].[vortex_dbo].material_history.transfer_date
      ,cast(
       case
            when  [vortex_hvc].[vortex_dbo].material_history.transfer_date = [vortex_hvc].[vortex_dbo].material_history.updated_datetime then getdate()
            else [vortex_hvc].[vortex_dbo].material_history.updated_datetime end as datetime
             ) as Real_Updated_date

  FROM [vortex_hvc].[vortex_dbo].[vw_public_material_location]

  join [vortex_hvc].[vortex_dbo].[vw_public_material_unit]
  on vw_public_material_location.material_name = vw_public_material_unit.unit_number

   JOIN [vortex_hvc].[vortex_dbo].[material_history] 
  ON [vortex_hvc].[vortex_dbo].vw_public_material_location.material_id = [vortex_hvc].[vortex_dbo].material_history.material_id
  where 

    DateDiff(d,[vortex_hvc].[vortex_dbo].material_history.transfer_date, getdate()) < 30
    AND
    [vortex_hvc].[vortex_dbo].vw_public_material_location.quantity = 1

    and
     [vortex_hvc].[vortex_dbo].material_history.location_id  in ('3492','3500','3981','3493','3504','3497','4140',
     '3498', '3496','3627','4378','3512','4376','4542','4379','3802','4517','4410','4182','4758','3499','4897','4239','4820',
     '4133','4377','4342','5042','5113','5358','5100','5550','5548','5549','5359',
     '5594','5601','5614','5696','5701')
     )

select tab.*
FROM TAB
where
      (tab.transfer_date < '2019-03-11' ANd  Real_Updated_date >= '2019-03-11') 
      order by transfer_date

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

private void function autoLogin () {

    // multiple, concurrent requests could be hitting this on different nodes in the cluster

    // if we're already logged in, nothing to do
    if (session.isLoggedIn) {
        return;
    }

    // get the auth token if it exists
    var token = cookie.keyExists("auth") && isValid("uuid", cookie.auth) ? cookie.auth : "";
    if (token == "") {
        // if a token doesn't exists, nothing to do
        return;
    }

    // assertion: user is not logged in and an auth token exists
    // attempt to login using the token, but make sure that only one 
    // request does this at a time - wrap with an exclusive session lock

    // lock the session - how would you do this on a cluster?
    lock scope="session" type="exclusive" timeout="10" throwontimeout=false {
        // check if logged in again - another thread may have succeeded while this
        // thread was waiting for the lock to open
        if (!session.loggedIn) {
            // we can only call this once if user is not logged in!
            application.auth.loginWithToken(authToken=token);
        }
    }

} // autoLogin()

通过使用DATEADD,您还将能够过滤不同的月份/年份。