我可以计算名称在第一个查询中出现的次数的子查询
Subquery where I can count number of times a name appears from the first query
这是我目前的代码:
select Name, ECU, Identifier, Value, Max(Filetime) as "Filetime"
from dbo.view_1
where ECU='EBS7' and Identifier='88' and Value='2459579' or identifier ='87' and Value='2431629'
group by Name, ECU, Identifier, Value
ORDER BY
MAX(Name) ASC
这是我得到的结果:
我想要某种子查询或计算名称出现次数的东西。可以看到Agon出现了两次,Apollo Akka只出现了一次
关于我应该写什么的任何提示?
您可以为此使用相关子查询:
Select Name, ECU, Identifier, Value,
Max(Filetime) as "Filetime",
(select count(*)
from dbo.view_1 t2
where t2.Name = t1.Name and
ECU ='EBS7' and
((Identifier='88' and Value='2459579') or
(identifier ='87' and Value='2431629')) as cnt
from dbo.view_1 t1
where ECU ='EBS7' and
((Identifier='88' and Value='2459579') or
(identifier ='87' and Value='2431629'))
group by Name, ECU, Identifier, Value
ORDER BY MAX(Name) ASC
我不知道 MySQL(您实际使用的是哪个产品?),但在 MS-SQL 这个
ORDER BY MAX(Name) ASC
甚至无法编译。您不能按不在 SELECT 列表中的列排序。
如果您想获得原始结果集,名称的频率在集合本身中,并且您使用的是 MS-SQL,您可以使用 CTE:
WITH theset AS
(select Name, ECU, Identifier, Value, Max(Filetime) as "Filetime"
from dbo.view_1
where ECU='EBS7' and Identifier='88' and Value='2459579' or identifier ='87' and Value='2431629'
group by Name, ECU, Identifier, Value)
SELECT set.*,CountNames.NameCount
FROM
theset set
INNER JOIN
(SELECT Name,Count(*) AS NameCount FROM theset GROUP BY Name) CountNames
ON set.Name=CountNames.Name
这是我目前的代码:
select Name, ECU, Identifier, Value, Max(Filetime) as "Filetime"
from dbo.view_1
where ECU='EBS7' and Identifier='88' and Value='2459579' or identifier ='87' and Value='2431629'
group by Name, ECU, Identifier, Value
ORDER BY
MAX(Name) ASC
这是我得到的结果:
我想要某种子查询或计算名称出现次数的东西。可以看到Agon出现了两次,Apollo Akka只出现了一次
关于我应该写什么的任何提示?
您可以为此使用相关子查询:
Select Name, ECU, Identifier, Value,
Max(Filetime) as "Filetime",
(select count(*)
from dbo.view_1 t2
where t2.Name = t1.Name and
ECU ='EBS7' and
((Identifier='88' and Value='2459579') or
(identifier ='87' and Value='2431629')) as cnt
from dbo.view_1 t1
where ECU ='EBS7' and
((Identifier='88' and Value='2459579') or
(identifier ='87' and Value='2431629'))
group by Name, ECU, Identifier, Value
ORDER BY MAX(Name) ASC
我不知道 MySQL(您实际使用的是哪个产品?),但在 MS-SQL 这个
ORDER BY MAX(Name) ASC
甚至无法编译。您不能按不在 SELECT 列表中的列排序。
如果您想获得原始结果集,名称的频率在集合本身中,并且您使用的是 MS-SQL,您可以使用 CTE:
WITH theset AS
(select Name, ECU, Identifier, Value, Max(Filetime) as "Filetime"
from dbo.view_1
where ECU='EBS7' and Identifier='88' and Value='2459579' or identifier ='87' and Value='2431629'
group by Name, ECU, Identifier, Value)
SELECT set.*,CountNames.NameCount
FROM
theset set
INNER JOIN
(SELECT Name,Count(*) AS NameCount FROM theset GROUP BY Name) CountNames
ON set.Name=CountNames.Name