我有下面的代码,我试图找出上个月使用的前10个服务器,但有问题,不是很好的SQL。如果可能的话,需要一些帮助或建议。
我在前10个应用程序上工作,但无法弄清楚如何让这10个服务器实现这一点。
SELECT TOP 10 dbo_LU_SERVERNAME.SERVERNAME, Count(*) AS SessionNumbers
FROM dbo_LU_SERVERNAME INNER JOIN dbo_SDB_SESSION ON dbo_LU_SERVERNAME.PK_SERVERNAMEID = dbo_SDB_SESSION.FK_SERVERNAMEID
WHERE (((dbo_SDB_SESSION.SESSIONSTART) Between Now() And DateAdd("d",-30,Now())))
GROUP BY dbo_LU_SERVERNAME.SERVERNAME
ORDER BY Count(*) DESC;
UNION ALL SELECT "Other" AS SERVERNAME, Count(*) AS SessionNumbers
FROM (dbo_LU_SERVERNAME
INNER JOIN dbo_SDB_SESSION
ON dbo_LU_SERVER.PK_SERVERID = dbo_SDB_SESSION.FK_SERVERID)
LEFT JOIN (SELECT TOP 10 dbo_LU_SERVERNAME.SERVERNAME, Count(*) AS SessionNos
FROM dbo_LU_SERVERNAME
INNER JOIN dbo_SDB_SESSION
ON dbo_LU_SERVER.PK_SERVERID = dbo_SDB_SESSION.FK_SERVERID
WHERE (((dbo_SDB_SESSION.SESSIONSTART) Between Now() And DateAdd("d",-31,Now())))
GROUP BY dbo_LU_SERVERNAME.SERVERNAME
ORDER BY Count(*) DESC) AS s ON dbo_LU_SERVERNAME.SERVERNAME = s.SERVERNAME
WHERE s.SERVERNAME Is Null
GROUP BY "Other";
这是适用于前10名APPS的SQL。
SELECT TOP 10 dbo_LU_APPNAME.APPNAME, Count(*) AS SessionNos
FROM dbo_LU_APPNAME INNER JOIN dbo_SDB_SESSION ON dbo_LU_APPNAME.PK_APPNAMEID = dbo_SDB_SESSION.FK_APPNAMEID
WHERE (((dbo_SDB_SESSION.SESSIONSTART) Between Now() And DateAdd("d",-30,Now())))
GROUP BY dbo_LU_APPNAME.APPNAME
ORDER BY Count(*) DESC;
UNION ALL SELECT "Other" AS APPNAME, Count(*) AS SessionNos
FROM (dbo_LU_APPNAME
INNER JOIN dbo_SDB_SESSION
ON dbo_LU_APPNAME.PK_APPNAMEID = dbo_SDB_SESSION.FK_APPNAMEID)
LEFT JOIN (SELECT TOP 10 dbo_LU_APPNAME.APPNAME, Count(*) AS SessionNos
FROM dbo_LU_APPNAME
INNER JOIN dbo_SDB_SESSION
ON dbo_LU_APPNAME.PK_APPNAMEID = dbo_SDB_SESSION.FK_APPNAMEID
WHERE (((dbo_SDB_SESSION.SESSIONSTART) Between Now() And DateAdd("d",-31,Now())))
GROUP BY dbo_LU_APPNAME.APPNAME
ORDER BY Count(*) DESC) AS s ON dbo_LU_APPNAME.APPNAME = s.APPNAME
WHERE s.APPNAME Is Null
GROUP BY "Other";
请注意,这些表格相关: dbo_LU_SERVER ---> FK_SERVERNAMEID,PK_SERVERID dbo_LU_SERVERNAME ---> PK_SERVERNAMEID dbo.SDB.SESSION ---> FK_SERVERID
我不确定我做错了什么。
请帮助。
谢谢
答案 0 :(得分:1)
我设法回答了我的问题。
SELECT TOP 10 dbo_LU_SERVERNAME.SERVERNAME, Count(*) AS SessionNumbers
FROM dbo_LU_SERVERNAME INNER JOIN dbo_SDB_SESSION ON dbo_LU_SERVERNAME.PK_SERVERNAMEID = dbo_SDB_SESSION.FK_SERVERID
WHERE (((dbo_SDB_SESSION.SESSIONSTART) Between Now() And DateAdd("d",-30,Now())))
GROUP BY dbo_LU_SERVERNAME.SERVERNAME
ORDER BY Count(*) DESC;
UNION ALL SELECT "Other" AS SERVERNAME, Count(*) AS SessionNumbers
FROM (dbo_LU_SERVERNAME INNER JOIN dbo_SDB_SESSION ON dbo_LU_SERVERNAME.PK_SERVERNAMEID = dbo_SDB_SESSION.FK_SERVERID)
LEFT JOIN (SELECT TOP 10 dbo_LU_SERVERNAME.SERVERNAME, Count(*) AS SessionNos
FROM dbo_LU_SERVERNAME INNER JOIN dbo_SDB_SESSION ON dbo_LU_SERVERNAME.PK_SERVERNAMEID = dbo_SDB_SESSION.FK_SERVERID
WHERE (((dbo_SDB_SESSION.SESSIONSTART) Between Now() And DateAdd("d",-31,Now())))
GROUP BY dbo_LU_SERVERNAME.SERVERNAME
ORDER BY Count(*) DESC) AS a ON dbo_LU_SERVERNAME.SERVERNAME = a.SERVERNAME
WHERE a.SERVERNAME Is Null
GROUP BY "Other";
感谢您的评论,我确实在几乎与此相同的时间之前提出了另一个问题,但另一个问题涉及两个相关实体,这是三个相关实体。
再次感谢
干杯:)