创造一个'休息''工会之间

时间:2015-02-21 01:28:37

标签: sql sql-server

我仍然是微软新手,所以我只想弄清楚一些事情。 如果我做错了,请道歉;在错误的区域发布,而不是正确发布,等等。

查询:

SELECT TOP 10 YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))) 'Year', MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))) 'Index', 'January 2014' 'Month', U.LastName + ', ' + U.FirstName 'Full Name', A.EffectedUserName 'Username', US.RoleName 'Role Name', COUNT(*) 'Amount'
FROM Audits A, Users U, UserSlot US
WHERE A.EffectedUserName = U.UserName
AND U.UserName = US.UserName
AND A.EventName = 'UserLogin'
AND A.EffectedUserName not in ('Admin')
AND A.DateTime between '2014/01/01' and '2014/02/01'
GROUP BY YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), U.LastName + ', ' + U.FirstName, US.RoleName, A.EffectedUserName

UNION ALL

SELECT TOP 10 YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))) 'Year', MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))) 'Index', 'February 2014' 'Month', U.LastName + ', ' + U.FirstName 'Full Name', A.EffectedUserName 'Username', US.RoleName 'Role Name', COUNT(*) 'Amount'
FROM Audits A, Users U, UserSlot US
WHERE A.EffectedUserName = U.UserName
AND U.UserName = US.UserName
AND A.EventName = 'UserLogin'
AND A.EffectedUserName not in ('Admin')
AND A.DateTime between '2014/02/01' and '2014/03/01'
GROUP BY YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), U.LastName + ', ' + U.FirstName, US.RoleName, A.EffectedUserName

ORDER BY YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), [Amount] Desc

这大约是整个查询的1/6。我想在每个月之间都有一个' Break '。因此,每隔一个月就会有一个间隙或其他东西。 像:

- January 2014
- January 2014
- January 2014

- February 2014
- February 2014
- February 2014

- March 2014
- March 2014
- March 2014

间隙是UNION ALL中的' Break '。

我觉得我没有很好地提出这个问题,但我不太确定如何说出来。谢谢! :)

2 个答案:

答案 0 :(得分:0)

你也可以做点什么

SELECT TOP 10 YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))) 
'Year', MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))) 
'Index', 'January 2014' 'Month', U.LastName + ', ' + U.FirstName 'Full 
 Name', A.EffectedUserName 'Username', US.RoleName 'Role Name', COUNT(*) 'Amount',
'January' Month
    FROM Audits A, Users U, UserSlot US
    WHERE A.EffectedUserName = U.UserName
    AND U.UserName = US.UserName
    AND A.EventName = 'UserLogin'
    AND A.EffectedUserName not in ('Admin')
    AND A.DateTime between '2014/01/01' and '2014/02/01'
    GROUP BY YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), 
    MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))),
    U.LastName + ', ' + U.FirstName, US.RoleName, A.EffectedUserName

    UNION ALL

SELECT TOP 10 YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))) 
'Year', MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))) 
'Index', 'January 2014' 'Month', U.LastName + ', ' + U.FirstName 'Full 
 Name', A.EffectedUserName 'Username', US.RoleName 'Role Name', COUNT(*) 'Amount',
'February' Month
    FROM Audits A, Users U, UserSlot US
    WHERE A.EffectedUserName = U.UserName
    AND U.UserName = US.UserName
    AND A.EventName = 'UserLogin'
    AND A.EffectedUserName not in ('Admin')
    AND A.DateTime between '2014/02/01' and '2014/03/01'
    GROUP BY YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), 
    MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))),
    U.LastName + ', ' + U.FirstName, US.RoleName, A.EffectedUserName

答案 1 :(得分:-1)

我不确定这是你在看什么。您只需添加空行即可创建差距。在前端而不是后端做得更好。

SELECT TOP 10 YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))) 'Year', MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))) 'Index', 'January 2014' 'Month', U.LastName + ', ' + U.FirstName 'Full Name', A.EffectedUserName 'Username', US.RoleName 'Role Name', COUNT(*) 'Amount'
    FROM Audits A, Users U, UserSlot US
    WHERE A.EffectedUserName = U.UserName
    AND U.UserName = US.UserName
    AND A.EventName = 'UserLogin'
    AND A.EffectedUserName not in ('Admin')
    AND A.DateTime between '2014/01/01' and '2014/02/01'
    GROUP BY YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), U.LastName + ', ' + U.FirstName, US.RoleName, A.EffectedUserName

    UNION ALL

    SELECT null AS Year , null AS Index, null AS  Month, null AS Full Name, null AS Username,    null AS [Role Name], null AS Amount 

    UNION ALL

    SELECT TOP 10 YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))) 'Year', MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))) 'Index', 'February 2014' 'Month', U.LastName + ', ' + U.FirstName 'Full Name', A.EffectedUserName 'Username', US.RoleName 'Role Name', COUNT(*) 'Amount'
    FROM Audits A, Users U, UserSlot US
    WHERE A.EffectedUserName = U.UserName
    AND U.UserName = US.UserName
    AND A.EventName = 'UserLogin'
    AND A.EffectedUserName not in ('Admin')
    AND A.DateTime between '2014/02/01' and '2014/03/01'
    GROUP BY YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), U.LastName + ', ' + U.FirstName, US.RoleName, A.EffectedUserName

    ORDER BY YEAR(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), MONTH(CONVERT(Date, FORMAT(CONVERT(Date, A.DateTime), 'Y'))), [Amount] Desc