SQL查询在两个列中搜索一个值,并从另一列及其计数获取值

发布于 2025-02-13 21:23:39 字数 659 浏览 1 评论 0原文

表中有两个列: col_left col_right

我需要选择所有行,其中 foo 发生的所有行都发生在这两个列中的任何一个中,并生成两列:

  1. 列,其其他值在同一行上使用 foo IE来自另一列的值( col_left col_right

  2. 列,该列发生了许多其他值

但我不确定如何构建整个查询。

我正在使用SQL Server LocalDB数据库。

这是它应该如何工作的示例:

”在此处输入图像描述

非常感谢。

There are two columns in the table: COL_LEFT and COL_RIGHT.

I need to select all rows where value of FOO occurs in either of these two columns and generate two columns:

  1. Column with the other value that is on the same row with FOO ie value from the other column (COL_LEFT or COL_RIGHT)

  2. Column with a number of occurences of that other value

I am guessing there would be GROUP BY in the query but I am not sure how to construct the whole query.

I am using SQL Server LocalDB database.

Here is the example of how it should work:

enter image description here

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你好,陌生人 2025-02-20 21:23:39

一个简单的解决方案将是以下内容。

SELECT
  v.OPPOSITE,
  COUNT(*) AS COUNT
FROM YourTable t
CROSS APPLY (VALUES(
    CASE WHEN t.COL_LEFT = 'FOO' THEN t.COL_RIGHT ELSE t.COL_LEFT END
)) v(OPPOSITE)
WHERE 'FOO' IN (t.COL_LEFT, t.COL_RIGHT)
GROUP BY
  v.OPPOSITE;

但是我认为,最有效的解决方案将要么以下两个:

SELECT
  t.OPPOSITE,
  COUNT(*) AS COUNT
FROM (
    SELECT
      t.COL_RIGHT AS OPPOSITE
    FROM YourTable t
    WHERE t.COL_LEFT = 'FOO'

    UNION ALL

    SELECT
      t.COL_LEFT
    FROM YourTable t
    WHERE t.COL_RIGHT = 'FOO'
) t
GROUP BY
  t.OPPOSITE;
SELECT
  t.OPPOSITE,
  SUM(t.COUNT) AS COUNT
FROM (
    SELECT
      t.COL_RIGHT AS OPPOSITE,
      COUNT(*) AS COUNT
    FROM YourTable t
    WHERE t.COL_LEFT = 'FOO'
    GROUP BY
      t.COL_RIGHT

    UNION ALL

    SELECT
      t.COL_LEFT,
      COUNT(*)
    FROM YourTable t
    WHERE t.COL_RIGHT = 'FOO'
    GROUP BY
      t.COL_LEFT
) t
GROUP BY
  t.OPPOSITE;

这将需要两个单独的索引,列的顺序相反。

(COL_LEFT, COL_RIGHT)
(COL_RIGHT, COL_LEFT)

如您所见,从

A simple solution would be the following.

SELECT
  v.OPPOSITE,
  COUNT(*) AS COUNT
FROM YourTable t
CROSS APPLY (VALUES(
    CASE WHEN t.COL_LEFT = 'FOO' THEN t.COL_RIGHT ELSE t.COL_LEFT END
)) v(OPPOSITE)
WHERE 'FOO' IN (t.COL_LEFT, t.COL_RIGHT)
GROUP BY
  v.OPPOSITE;

But I think the most efficient solution would either of the following two:

SELECT
  t.OPPOSITE,
  COUNT(*) AS COUNT
FROM (
    SELECT
      t.COL_RIGHT AS OPPOSITE
    FROM YourTable t
    WHERE t.COL_LEFT = 'FOO'

    UNION ALL

    SELECT
      t.COL_LEFT
    FROM YourTable t
    WHERE t.COL_RIGHT = 'FOO'
) t
GROUP BY
  t.OPPOSITE;
SELECT
  t.OPPOSITE,
  SUM(t.COUNT) AS COUNT
FROM (
    SELECT
      t.COL_RIGHT AS OPPOSITE,
      COUNT(*) AS COUNT
    FROM YourTable t
    WHERE t.COL_LEFT = 'FOO'
    GROUP BY
      t.COL_RIGHT

    UNION ALL

    SELECT
      t.COL_LEFT,
      COUNT(*)
    FROM YourTable t
    WHERE t.COL_RIGHT = 'FOO'
    GROUP BY
      t.COL_LEFT
) t
GROUP BY
  t.OPPOSITE;

This would need two separate indexes, with the columns in opposite order, to be performant.

(COL_LEFT, COL_RIGHT)
(COL_RIGHT, COL_LEFT)

As you can see from this fiddle, the second and third execution plans have no sorts or hash matches, whereas the first requires a sort.

温暖的光 2025-02-20 21:23:39

认为您可以使用:

  1. 使用[左][右]是'foo'的过滤记录。
  2. [left]列中放置不是“ foo”的值。
  3. 组由[左]列和计数。
WITH CTX AS (
    SELECT CASE WHEN [LEFT] = 'Foo' THEN [RIGHT] ELSE [LEFT] END AS [LEFT]
    FROM Ori
    WHERE [LEFT] = 'Foo'
    OR [RIGHT] = 'Foo'
)
SELECT [LEFT], COUNT(*) AS [COUNT]
FROM CTX
GROUP BY [LEFT]

示例SQL小提琴

Thinking that you can do with:

  1. Filter records with [LEFT] or [RIGHT] is 'Foo'.
  2. Place the value which is not 'Foo' in [LEFT] column.
  3. Group by [LEFT] column and count.
WITH CTX AS (
    SELECT CASE WHEN [LEFT] = 'Foo' THEN [RIGHT] ELSE [LEFT] END AS [LEFT]
    FROM Ori
    WHERE [LEFT] = 'Foo'
    OR [RIGHT] = 'Foo'
)
SELECT [LEFT], COUNT(*) AS [COUNT]
FROM CTX
GROUP BY [LEFT]

Sample SQL Fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文