在不使用存储过程的情况下,在sql中多次运行查询以获取不同的日期范围(从单独的表中获取)?

时间:2017-05-02 15:26:29

标签: sql-server date

我只选择访问数据库。此查询获取整年的结果(1行)。

SELECT 
    COUNT(U.ssnnoncumhrs) as [Non Cums Hrs] 
FROM
    (SELECT TOP 100 PERCENT
         (CASE 
             WHEN (([Social Security Number (SSN)] = LEAD([Social Security Number (SSN)], 1, 0) OVER (ORDER BY [Social Security Number (SSN)], sequencedate DESC, [YTD Productive Hours] DESC, [YTD Uncapped gross FUTA Wages] DESC)) 
                  AND ([YTD Productive Hours] < LEAD([YTD Productive Hours], 1, 0) OVER (ORDER BY [Social Security Number (SSN)], sequencedate DESC, [YTD Productive Hours] DESC, [YTD Uncapped gross FUTA Wages] DESC)))
                THEN [Social Security Number (SSN)] 
          END) as ssnnoncumhrs
      FROM
          dbo.staging P, dbo.tblBatch T 
      WHERE
          P.BatchId = T.BatchId 
          AND P.FileId = T.FileId 
          AND sequencedate >= '1/1/2016'
          AND sequencedate <='12/31/2016' 
      ORDER BY
          [Social Security Number (SSN)], sequencedate DESC,
          [YTD Productive Hours] DESC, [YTD Uncapped gross FUTA Wages] DESC)  as U

相反,我希望对不同的日期范围执行此查询。

dbo.tblbatch有一个日期列表。此查询需要在第1和第2个日期之间运行,然后在第2个和第3个日期之间运行,依此类推,直到它涵盖列表中的所有日期,并提供所有结果,就好像我每次手动更改了from和to日期一样。

1 个答案:

答案 0 :(得分:0)

这样的事情会起作用

<table>
  <tbody>
    <tr>
      <th>Goal Name</th>
      <th>Category</th>
      <th>Rewards</th>
    </tr>
  </tbody>
  <!-- Let's wrap each object in a ng-container -->
  <ng-container *ngFor="let goal of goals">
    <tbody>
      <!-- Iterate the rewards of each goal -->
      <tr *ngFor="let reward of goal.rewards">
        <td>{{goal.gname}}</td>
        <td>{{goal.goalCategory}}</td>
        <td>{{reward.rewardName}}</td>
      </tr>
    </tbody>
  </ng-container>
</table>