Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 按Sql查询嵌套的组_Sql Server - Fatal编程技术网

Sql server 按Sql查询嵌套的组

Sql server 按Sql查询嵌套的组,sql-server,Sql Server,我的sql server数据库中有以下三个表 1->GroupList GroupID || GroupName 2->机械师 MachineID || GroupID || MachineName 3->捕获日志 LogID || MachineID || VoilationWord 我需要一个查询来获取以下报告 GroupName || MachineCountInGroup || TotalLogCountByMachinesInGroup 伙计们,请帮帮我

我的sql server数据库中有以下三个表

1->GroupList

GroupID || GroupName 
2->机械师

MachineID   || GroupID  || MachineName
3->捕获日志

LogID   || MachineID    || VoilationWord
我需要一个查询来获取以下报告

GroupName   || MachineCountInGroup  || TotalLogCountByMachinesInGroup
伙计们,请帮帮我。

你们可以

  select GroupName ,
    (select count(1) from  MachineList where GroupID = grp.GroupID ) as    
     MachineCountInGroup   ,
   (select count(1) from  MachineList 
   inner join  CaptureLogs 
   on  CaptureLogs.MachineID=MachineList .MachineID   
   where GroupID  = grp.GroupID ) as TotalLogCountByMachinesInGroup
   from  GroupList grp
我确信有更好的方法,但这会起作用。

这会起作用:

挑选 g、 组名, 将(不同的l.MachineID)计数为MachineCountingGroup, 计数(*)为TotalogCountByMachineSingGroup 从CaptureL 在m.MachineID=l.MachineID上连接MachineList m 在g.GroupID=m.GroupID上加入GroupList g 按g.GroupName分组

另外,我希望计算l.MachineID比计算m.MachineID为引擎提供更好的优化访问的机会。因为存在内部联接,所以这两个变量是相同的


在上测试,因为我自己没有安装SQL Server。

如果没有数据,很难确定,但我猜这样的东西可能就是你想要的

选择
不同的gl.groupid,
gl.groupname,
将(按gl.groupid划分的)上的(ml.MachineId)计数为MachineCountingGroup,
将(按cl.MachineId分区)上的(cl.LogId)计数为TotalogCountByMachineSingGroup
从dbo.GroupList作为gl
将dbo.MachineList作为ml.groupid=gl.groupid上的ml连接
将dbo.CaptureLogs作为cl.machineid=ml.machineid上的cl加入

这将提供一些数据,如下所示:

groupname、MachineCountingGroup、TotalogCount。。。
A 3 1
A 3 2
B 2 2

如果一行只需要一行,则需要调整

COUNT(cl.LogId)OVER(按cl.MachineId分区)作为totalogcountbymachinesinggroup

这句话的意思是

将(按gl.groupid划分的)上的(cl.logid)计数为TotalogCountByMachineSingGroup

这将为A和B分别提供一行。

A 3 3
B 2 2

因此,鉴于我不知道您到底想要什么,这是我能做的最好的了(同样-测试数据会很好)

我已经准备好了完整的脚本,然后是满足需求的最佳解决方案

Select gl.GroupName, count(distinct ml.MachineId) MachineCountInGroup, 
        count(LogId) TotalLogCountByMachinesInGroup from GroupList gl
        inner join MachineList ml on gl.GroupId = ml.GroupId
        inner join CaptureLogs cl on ml.MachineId = cl.MachineId
        group by gl.GroupName;
创建表的脚本

create table GroupList
(GroupId int primary key, GroupName varchar(50) not null);

create table MachineList
(MachineId int primary key,
 GroupId int not null,
 MachineName varchar(50) not null
);

Alter table MachineList
Add foreign key(GroupId)
references GroupList(GroupId)

Create table CaptureLogs
(LogId int identity(1,1) primary key,
 MachineId int not null,
 VoilationWord varchar(50) not null
);

Alter table CaptureLogs
Add foreign key(MachineId)
references MachineList(MachineId);
现在,在表中插入一些示例数据的脚本:

Insert into GroupList Values(1,'Group 1'),(2,'Group 2');

Insert into MachineList 
values(1,1,'Machine 1'),(2,1,'Machine 2 of 1'),(3,2,'Machine 3 of 2'),(4,2,'Machine 4 of 2'),(5,2,'Machine 5 of 2');

Insert into CaptureLogs
    (MachineId, VoilationWord)
select MachineId, 'First Word from ' + MachineName from MachineList
union all
select MachineId, 'Second Word from ' + MachineName from MachineList
    where MachineId > 1
union all
select MachineId, 'Third Word from ' + MachineName from MachineList
    where MachineId > 2
union all
select MachineId, 'Fourth Word from ' + MachineName from MachineList
    where MachineId > 3
union all
select MachineId, 'Fifth Word from ' + MachineName from MachineList
    where MachineId > 4;
现在要获得所需的数据,需要运行简单的代码。我认为这是最好的要求

Select gl.GroupName, count(distinct ml.MachineId) MachineCountInGroup, 
        count(LogId) TotalLogCountByMachinesInGroup from GroupList gl
        inner join MachineList ml on gl.GroupId = ml.GroupId
        inner join CaptureLogs cl on ml.MachineId = cl.MachineId
        group by gl.GroupName;

我希望这能有所帮助。

如果你想让我们给你一个正确的答案,你真的应该提供一些数据和预期的结果。你要求的第三栏不是很清楚。这是该组所有计算机的所有日志计数吗?@user3567971:看起来您希望每个组都有一行,然后对该组中的计算机进行计数。到目前为止还可以。但是你需要一个“TotalogCountByMachineSingGroup”。我能理解“TotalogCountingGroup”,但用机器计数?您是指组中每台机器的平均日志数吗?或者别的什么?我同意《愤怒的公牛》和《弗拉兹》;你可以表现出一些努力,而不是让我们做所有的工作,包括猜测。你试过执行这样的查询吗?我是说没有分组?这将不会执行。我通过添加GROUP by修复了它。还有什么问题吗?我现在已经测试并修复了它。抱歉,如果我看起来很固执,但我宁愿修复而不是删除。这个解决方案在结构上与你的不同,我认为它应该存在。谢谢你的意见。老兄,你太棒了!!。非常感谢,这正是我想要的。上帝保佑你。@kabs不客气。正如我所说,我确信有更好的解决方案,但sql并不是我最好的部分:)