(If query1>query2 then query1 Else query2)

问题描述 投票:0回答:1
I'm just not sure how to evaluate multiple rows against each other. Here is a top 5 of each table. Which ever count is higher, that's the one that needs to be resulted in my query.

SELECT
    COUNT(n.Caption) AS Interfaces
FROM NodesData n
LEFT JOIN [Interfaces] i ON i.NodeID = n.NodeID
GROUP BY n.Caption

SELECT
    COUNT(n.Caption) AS Volumes
FROM NodesData n
LEFT JOIN [Volumes] v ON v.NodeID = n.NodeID
GROUP BY n.Caption

I've got a couple loosely related tables. I need to evaluate a count against another count. Query 1 is SELECT COUNT(n.Caption) AS Interfaces FROM NodesData n LEFT JOIN [Interfaces] i ON i.NodeID ...

See whether this works;

Here is

You can use outer apply:
sql sql-server
1个回答
1
投票

Select Caption, 
    Case When Interfaces > Volumes then Interfaces Else Volumes End as VorI
From
(
    SELECT
        ni.Caption, COUNT(ni.Caption) AS Interfaces, COUNT(nv.Caption) AS Volumes
    FROM NodesData ni
    inner join NodesData nv on ni.NodeID = nv.NodeID
    LEFT JOIN [Interfaces] i ON i.NodeID = ni.NodeID
    LEFT JOIN [Volumes] v ON v.NodeID = nv.NodeID
    GROUP BY ni.Caption
) Q
我有几张松散相关的表。我需要将一个计数与另一个计数进行评估。

1
投票

查询2是

SELECT n.Caption, SUM(i.cnt) AS Interfaces, SUM(v.cnt) as Volumes
FROM NodesData n
OUTER APPLY
(
    SELECT COUNT(*) as cnt
    FROM [Interfaces] i 
    WHERE i.NodeID = n.NodeID
) i
OUTER APPLY
(
    SELECT COUNT(*) as cnt
    FROM [Volumes] v 
    WHERE v.NodeID = n.NodeID
) v
GROUP BY n.Caption;

理想情况下,我需要输出一个有2列的表。第二列是具有挑战性的一列。CASEn.Caption

© www.soinside.com 2019 - 2024. All rights reserved.