编写查询以从表中获取最近七个月的记录

时间:2014-06-20 06:01:26

标签: sql-server-2008

我有两个表TableA和TableB。

TableA是主表。

表A:

+-------+---------+
|   Id  |   Asset |
+-------+---------+
|   1   |  Asset1 |
|   2   |  Asset2 |
|   3   |  Asset3 |
|   4   |  Asset4 |
|   5   |  Asset5 |
+-------+---------+

表B:

+-------+---------+-------------+
|   Id  |   Asset |   ScanDate  |
+-------+---------+-------------+
|   1   |  Asset1 | 12/Jun/2014 |
|   2   |  Asset2 | 12/Jun/2014 |
|   3   |  Asset3 | 12/Jun/2014 |
|   4   |  Asset4 | 12/Jun/2014 |
|   5   |  Asset5 | 12/Jun/2014 |
|   6   |  Asset1 | 13/Jun/2014 |
|   7   |  Asset2 | 13/Jun/2014 |
|   8   |  Asset3 | 13/Jun/2014 |
|   9   |  Asset4 | 13/Jun/2014 |
|  10   |  Asset5 | 13/Jun/2014 |
|  11   |  Asset1 | 14/Jun/2014 |
|  12   |  Asset2 | 14/Jun/2014 |
|  13   |  Asset3 | 14/Jun/2014 |
|  14   |  Asset4 | 14/Jun/2014 |
|  15   |  Asset5 | 14/Jun/2014 |
|  16   |  Asset1 | 15/Jun/2014 |
|  17   |  Asset2 | 15/Jun/2014 |
|  18   |  Asset5 | 15/Jun/2014 |
|  19   |  Asset1 | 16/Jun/2014 |
|  20   |  Asset2 | 16/Jun/2014 |
|  21   |  Asset3 | 16/Jun/2014 |
|  22   |  Asset1 | 18/Jun/2014 |
|  23   |  Asset2 | 18/Jun/2014 |
|  24   |  Asset3 | 18/Jun/2014 |
|  25   |  Asset4 | 18/Jun/2014 |
|  26   |  Asset5 | 18/Jun/2014 |
|  27   |  Asset3 | 19/Jun/2014 |
|  28   |  Asset4 | 19/Jun/2014 |
|  29   |  Asset5 | 19/Jun/2014 |
|  30   |  Asset4 | 20/Jun/2014 |
|  31   |  Asset5 | 20/Jun/2014 |
+-------+---------+-------------+

我需要检查过去七天扫描的资产。

我需要输出如下:

+--------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
| Asset  |13/Jun/2014|14/Jun/2014|15/Jun/2014|16/Jun/2014|17/Jun/2014|18/Jun/2014|19/Jun/2014|20/Jun/2014|
+--------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
| Asset1 |  Yes      |  Yes      |  Yes      |  Yes      |  No       |  Yes      |  No       |  No       |
| Asset2 |  Yes      |  Yes      |  Yes      |  Yes      |  No       |  Yes      |  No       |  No       |
| Asset3 |  Yes      |  Yes      |  No       |  Yes      |  No       |  Yes      |  Yes      |  No       |
| Asset4 |  Yes      |  Yes      |  No       |  No       |  No       |  Yes      |  Yes      |  Yes      |
| Asset5 |  Yes      |  Yes      |  Yes      |  No       |  No       |  Yes      |  Yes      |  Yes      |
+--------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+

如何编写查询以获取上述输出。 请帮帮我。

我尝试了以下查询:

DECLARE @Now DATETIME = GETDATE();
DECLARE @7DaysAgo DATETIME = DATEADD(day,-10,@Now);

SELECT LastScanDate 
FROM   TableB
WHERE  ScanDate BETWEEN @7DaysAgo AND @Now 
group  BY ScanDate 

1 个答案:

答案 0 :(得分:0)

  

我想出了M Khalid Junaid Idea

试试这个

Select T1.Asset,
Sum(Case When Cast(GETDATE() As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) current_day,
Sum(Case When Cast(DATEADD(Day, -1, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) last_Day,
Sum(Case When Cast(DATEADD(Day, -2, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) Second_last_Day,
Sum(Case When Cast(DATEADD(Day, -3, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) Third_last_Day,
Sum(Case When Cast(DATEADD(Day, -4, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) fourth_last_Day,
Sum(Case When Cast(DATEADD(Day, -5, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) fifth_last_Day,
Sum(Case When Cast(DATEADD(Day, -6, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) sixth_last_Day,
Sum(Case When Cast(DATEADD(Day, -7, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) Seventh_last_Day
From Table1 T1 Left Join Table2 T2 On(T1.Asset = T2.Asset)
Group By T1.Asset;

<强> Fiddle Demo

<强>输出:

ASSET   CURRENT_DAY LAST_DAY    SECOND_LAST_DAY THIRD_LAST_DAY  FOURTH_LAST_DAY FIFTH_LAST_DAY  SIXTH_LAST_DAY  SEVENTH_LAST_DAY
Asset1  0           0           1           0                   1               1               1               1
Asset2  0           0           1           0                   1               1               1               1
Asset3  0           1           1           0                   1               0               1               1
Asset4  1           1           1           0                   0               0               1               1
Asset5  1           1           1           0                   0               1               1               1

<强>更新

如果您希望列值为YesNo

试试这个

Select T1.Asset,
Case When Sum(Case When Cast(GETDATE() As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) = 1 Then 'Yes' else 'No' End Today,
Case When Sum(Case When Cast(DATEADD(Day, -1, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) = 1 Then 'Yes' else 'No' End Last_Day,
Case When Sum(Case When Cast(DATEADD(Day, -2, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) = 1 Then 'Yes' else 'No' End Second_last_Day,
Case When Sum(Case When Cast(DATEADD(Day, -3, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) = 1 Then 'Yes' else 'No' End Third_last_Day,
Case When Sum(Case When Cast(DATEADD(Day, -4, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) = 1 Then 'Yes' else 'No' End fourth_last_Day,
Case When Sum(Case When Cast(DATEADD(Day, -5, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) = 1 Then 'Yes' else 'No' End fifth_last_Day,
Case When Sum(Case When Cast(DATEADD(Day, -6, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) = 1 Then 'Yes' else 'No' End sixth_last_Day,
Case When Sum(Case When Cast(DATEADD(Day, -7, GETDATE()) As Date) = Cast(T1.ScanDate AS Date) Then 1 else 0 end) = 1 Then 'Yes' else 'No' End Seventh_last_Day
From Table1 T1 Left Join Table2 T2 On(T1.Asset = T2.Asset)
Group By T1.Asset;

<强> Live Demo

<强>输出:

ASSET   TODAY   LAST_DAY    SECOND_LAST_DAY THIRD_LAST_DAY  FOURTH_LAST_DAY FIFTH_LAST_DAY  SIXTH_LAST_DAY  SEVENTH_LAST_DAY
Asset1  No      No          Yes             No              Yes             Yes             Yes             Yes
Asset2  No      No          Yes             No              Yes             Yes             Yes             Yes
Asset3  No      Yes         Yes             No              Yes             No              Yes             Yes
Asset4  Yes     Yes         Yes             No              No              No              Yes             Yes
Asset5  Yes     Yes         Yes             No              No              Yes             Yes             Yes