计算枚举类型列上的唯一行数以在 mysql 中再输出两列

发布于 2024-12-15 05:47:08 字数 597 浏览 2 评论 0原文

我有以下 mysql 表:

Item Name   Listing Fee Listing Type
watch       $0.20       LISTED
watch       $0.20       LISTED
watch       $0.30       SOLD
glasses     $0.50       LISTED
glasses     $0.50       LISTED
glasses     $0.50       LISTED
glasses     $1.00       SOLD

我需要的是下面由 SQL 分组的输出:

Item Name   Total Fee   Total Listed    Total Sold
watch       $0.70       2       1
glasses     $2.50       3       1

规则是每个“项目名称”将具有多个列表记录,其中定义了费用和列表类型。可能有两种“列表类型”[LISTED & LISTED]。卖]。

我想在表上运行查询并生成类似于提到的输出的摘要。

谢谢 维基百科

I have following mysql table:

Item Name   Listing Fee Listing Type
watch       $0.20       LISTED
watch       $0.20       LISTED
watch       $0.30       SOLD
glasses     $0.50       LISTED
glasses     $0.50       LISTED
glasses     $0.50       LISTED
glasses     $1.00       SOLD

What I require is below output by a Group by SQL:

Item Name   Total Fee   Total Listed    Total Sold
watch       $0.70       2       1
glasses     $2.50       3       1

The rule is that each "Item name" will having multiple records of listing, where fee and listing type is defined. There could be two "listing type" [LISTED & SOLD].

I want to run a query on the table and generate the summary like output mentioned.

thanks
wikki

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

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

发布评论

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

评论(2

吹泡泡o 2024-12-22 05:47:08
SELECT
    `Item Name`, 
    SUM(`Listing Fee`) AS `Total Fee`,
    SUM(CASE `Listing Type` WHEN 'LISTED' THEN 1 ELSE 0 END) AS `Total Listed`,
    SUM(CASE `Listing Type` WHEN 'SOLD' THEN 1 ELSE 0 END) AS `Total Sold`
FROM `Table Name`
GROUP BY `Item Name`

这将使用静态“列表类型”总结列表。如果需要动态列表,则必须在存储过程中构建 SQL 并执行它。

输出:

Item Name   Total Fee   Total Listed   Total Sold
watch       $0.70       2              1
glasses     $2.50       3              1

您可以更进一步:

SELECT
    `Item Name`,
    SUM(
        CASE `Listing Type`
            WHEN 'LISTED' THEN `Listing Fee`
            ELSE 0
        END
    ) AS `Total Fee Listing`,
    SUM(
        CASE `Listing Type`
            WHEN 'SOLD' THEN `Listing Fee`
            ELSE 0
        END
    ) AS `Total Fee Sold`
FROM `Table Name`
GROUP BY `Item Name`

输出:

Item Name   Total Fee Listing   Total Fee Sold
watch       $0.40               $0.30
glasses     $1.50               $1.00
SELECT
    `Item Name`, 
    SUM(`Listing Fee`) AS `Total Fee`,
    SUM(CASE `Listing Type` WHEN 'LISTED' THEN 1 ELSE 0 END) AS `Total Listed`,
    SUM(CASE `Listing Type` WHEN 'SOLD' THEN 1 ELSE 0 END) AS `Total Sold`
FROM `Table Name`
GROUP BY `Item Name`

This will summarize the list using static "Listing Types". If you want a dynamic list, you would have to build the SQL in a stored procedure, and execute it.

Output:

Item Name   Total Fee   Total Listed   Total Sold
watch       $0.70       2              1
glasses     $2.50       3              1

You could take this a step further:

SELECT
    `Item Name`,
    SUM(
        CASE `Listing Type`
            WHEN 'LISTED' THEN `Listing Fee`
            ELSE 0
        END
    ) AS `Total Fee Listing`,
    SUM(
        CASE `Listing Type`
            WHEN 'SOLD' THEN `Listing Fee`
            ELSE 0
        END
    ) AS `Total Fee Sold`
FROM `Table Name`
GROUP BY `Item Name`

Output:

Item Name   Total Fee Listing   Total Fee Sold
watch       $0.40               $0.30
glasses     $1.50               $1.00
看春风乍起 2024-12-22 05:47:08

首先 - 您应该按项目对这些记录进行分组,然后要计算总数,您可以使用一个小技巧 -

SELECT
  `Item Name`,
  SUM(`Listing Fee`) `Total Fee`,
  COUNT(IF(`Listing Type` = 'LISTED', 1, NULL)) `Total Listed`,
  COUNT(IF(`Listing Type` = 'SOLD', 1, NULL)) `Total Sold`
FROM
  mytable
GROUP BY
  `Item Name`;

Firstly - you should group these records by items, then to count totals you can use a small trick -

SELECT
  `Item Name`,
  SUM(`Listing Fee`) `Total Fee`,
  COUNT(IF(`Listing Type` = 'LISTED', 1, NULL)) `Total Listed`,
  COUNT(IF(`Listing Type` = 'SOLD', 1, NULL)) `Total Sold`
FROM
  mytable
GROUP BY
  `Item Name`;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文