SQL 查询以确定表的数量
SQL Query to determine number of tables
我有这个 table,我正在尝试确定(一家餐厅)有多少张桌子在等餐。
table(在数据库中)有以下列:
TableNumber、ObjectType、ObjectDescription 和 Quantity(以及其他)。
我进行此查询是为了查看订购的内容,但想知道有多少独特的 table 正在等待食物......
SELECT TableNumber AS TAFEL
, COUNT (*) AS AANTAL
, ObjectDescription AS PRODUCT
FROM TableMemory
WHERE (ObjectType = 1 OR ObjectType = 17)
GROUP BY TableNumber, ObjectType, ObjectDescription
ORDER BY TableNumber
这给了我以下输出...
TAFEL AANTAL PRODUCT ### OF TABLES
----------- ----------- --------------------------------------------------
1 1 Black Jacks Hotdog 5
1 5 Friet Groot
1 2 Friet Klein
1 1 Friet Middel
1 1 Knoflooksaus
1 1 Uitsmijter Ham/kaas
1 1 Uitsmijter Lou
3 1 Knoflooksaus
3 1 New York Hotdog
7 1 Broodje Gezond
7 1 Knoflooksaus
40 1 Friet Groot
40 1 Met Uitjes
60 1 Friet Middel
60 1 Meenemen
我尝试在查询中使用 count 和 distinct,但我尝试的任何操作似乎都没有为 tables(restaurant) 的唯一数量提供额外的输出,在上面的结果中应该是 5。
谁能帮帮我?提前致谢!
假设“tafel”是table,那么只需使用count(distinct)
:
SELECT COUNT(DISTINCT TableNumber)
FROM TableMemory
WHERE ObjectType IN (1, 17);
如果您希望原始数据具有不同的 table 个数,则只需添加 dense_rank()
两次:
SELECT TableNumber AS TAFEL, COUNT (*) AS AANTAL,
ObjectDescription AS PRODUCT,
(-1 +
DENSE_RANK() OVER (ORDER BY TableNumber ASC) +
DENSE_RANK() OVER (ORDER BY TableNumber DESC)
) as num_tables
FROM TableMemory
WHERE (ObjectType = 1 OR ObjectType = 17)
GROUP BY TableNumber, ObjectType, ObjectDescription
ORDER BY TableNumber;
首先,如果您使用的是 SQL 服务器的老式版本,请务必告诉我们。自 2000 年以来发生了很多变化!
其次,您实际上是在以错误的方式获取数据。您正在尝试合并 2 组不相关的数据。处理此问题的正确方法是提取 2 个数据集,1 个包含 table 的数量(Gordon 已经提供了这个),另一个包含每个 table 的聚合值(您已经拥有)。如果出于某种原因您确实无法执行此操作,请使用简单的子查询查看下面的选项。
SQL 服务器 - Window 不支持的功能
使用简单的子查询,注意重复 where
子句。
SELECT TableNumber AS TAFEL
, COUNT (*) AS AANTAL
, ObjectDescription AS PRODUCT
, (
SELECT COUNT(DISTINCT TableNumber)
FROM TableMemory
WHERE ObjectType IN (1, 17)
) TableCount
FROM TableMemory
WHERE ObjectType IN (1, 17)
GROUP BY TableNumber, ObjectType, ObjectDescription
ORDER BY TableNumber;
SQL 服务器 - Window 支持的功能
参考 Gordon 对类似问题的 existing answer(我承认这可能很难翻译,因此我没有标记为重复),您可以按如下方式进行。
- 计算按
TableNumber
分区的行号。
- 计算行号 = 1 的行数。因为行号 1 每个
TableNumber
只存在一次。请注意,我在 CROSS APPLY
中执行此操作以避免重复逻辑两次,一次在 SELECT
中,一次在 GROUP BY
. 中
WITH cte AS (
SELECT TableNumber AS TAFEL
, ObjectDescription AS PRODUCT
, ROW_NUMBER() OVER (PARTITION BY TableNumber ORDER BY ObjectDescription) rn
FROM TableMemory T
WHERE ObjectType IN (1, 17)
)
SELECT TAFEL, PRODUCT
, COUNT (*) AS AANTAL
, SUM(RowToCount) OVER () TableCount
FROM cte
CROSS APPLY (VALUES (CASE WHEN rn = 1 THEN 1 ELSE 0 END)) AS X (RowToCount)
GROUP BY TAFEL, PRODUCT, RowToCount
ORDER BY TAFEL;
要添加包含(重复的)[### OF TABLES] 的列,您可以使用 CROSS JOIN
/* This will repeat the [### OF TABLES] for each row of the results */
;with
prod_cte as (
SELECT TableNumber AS TAFEL
, COUNT (*) AS AANTAL
, ObjectDescription AS PRODUCT
FROM TableMemory
WHERE (ObjectType = 1 OR ObjectType = 17)
GROUP BY TableNumber, ObjectType, ObjectDescription),
total_cte as (
SELECT COUNT(DISTINCT TableNumber) [### OF TABLES]
FROM TableMemory
WHERE ObjectType IN (1, 17))
select p.*, t.*
from prod_cte p
cross join total_cte t;
[编辑] 没有 CTE 在 SQL 2000
工作
select p.*, t.*
from (
SELECT TableNumber AS TAFEL
, COUNT (*) AS AANTAL
, ObjectDescription AS PRODUCT
FROM TableMemory
WHERE (ObjectType = 1 OR ObjectType = 17)
GROUP BY TableNumber, ObjectType, ObjectDescription) p,
(
SELECT COUNT(DISTINCT TableNumber) [### OF TABLES]
FROM TableMemory
WHERE ObjectType IN (1, 17)) t;
我有这个 table,我正在尝试确定(一家餐厅)有多少张桌子在等餐。 table(在数据库中)有以下列: TableNumber、ObjectType、ObjectDescription 和 Quantity(以及其他)。
我进行此查询是为了查看订购的内容,但想知道有多少独特的 table 正在等待食物......
SELECT TableNumber AS TAFEL
, COUNT (*) AS AANTAL
, ObjectDescription AS PRODUCT
FROM TableMemory
WHERE (ObjectType = 1 OR ObjectType = 17)
GROUP BY TableNumber, ObjectType, ObjectDescription
ORDER BY TableNumber
这给了我以下输出...
TAFEL AANTAL PRODUCT ### OF TABLES
----------- ----------- --------------------------------------------------
1 1 Black Jacks Hotdog 5
1 5 Friet Groot
1 2 Friet Klein
1 1 Friet Middel
1 1 Knoflooksaus
1 1 Uitsmijter Ham/kaas
1 1 Uitsmijter Lou
3 1 Knoflooksaus
3 1 New York Hotdog
7 1 Broodje Gezond
7 1 Knoflooksaus
40 1 Friet Groot
40 1 Met Uitjes
60 1 Friet Middel
60 1 Meenemen
我尝试在查询中使用 count 和 distinct,但我尝试的任何操作似乎都没有为 tables(restaurant) 的唯一数量提供额外的输出,在上面的结果中应该是 5。
谁能帮帮我?提前致谢!
假设“tafel”是table,那么只需使用count(distinct)
:
SELECT COUNT(DISTINCT TableNumber)
FROM TableMemory
WHERE ObjectType IN (1, 17);
如果您希望原始数据具有不同的 table 个数,则只需添加 dense_rank()
两次:
SELECT TableNumber AS TAFEL, COUNT (*) AS AANTAL,
ObjectDescription AS PRODUCT,
(-1 +
DENSE_RANK() OVER (ORDER BY TableNumber ASC) +
DENSE_RANK() OVER (ORDER BY TableNumber DESC)
) as num_tables
FROM TableMemory
WHERE (ObjectType = 1 OR ObjectType = 17)
GROUP BY TableNumber, ObjectType, ObjectDescription
ORDER BY TableNumber;
首先,如果您使用的是 SQL 服务器的老式版本,请务必告诉我们。自 2000 年以来发生了很多变化!
其次,您实际上是在以错误的方式获取数据。您正在尝试合并 2 组不相关的数据。处理此问题的正确方法是提取 2 个数据集,1 个包含 table 的数量(Gordon 已经提供了这个),另一个包含每个 table 的聚合值(您已经拥有)。如果出于某种原因您确实无法执行此操作,请使用简单的子查询查看下面的选项。
SQL 服务器 - Window 不支持的功能
使用简单的子查询,注意重复 where
子句。
SELECT TableNumber AS TAFEL
, COUNT (*) AS AANTAL
, ObjectDescription AS PRODUCT
, (
SELECT COUNT(DISTINCT TableNumber)
FROM TableMemory
WHERE ObjectType IN (1, 17)
) TableCount
FROM TableMemory
WHERE ObjectType IN (1, 17)
GROUP BY TableNumber, ObjectType, ObjectDescription
ORDER BY TableNumber;
SQL 服务器 - Window 支持的功能
参考 Gordon 对类似问题的 existing answer(我承认这可能很难翻译,因此我没有标记为重复),您可以按如下方式进行。
- 计算按
TableNumber
分区的行号。 - 计算行号 = 1 的行数。因为行号 1 每个
TableNumber
只存在一次。请注意,我在CROSS APPLY
中执行此操作以避免重复逻辑两次,一次在SELECT
中,一次在GROUP BY
. 中
WITH cte AS (
SELECT TableNumber AS TAFEL
, ObjectDescription AS PRODUCT
, ROW_NUMBER() OVER (PARTITION BY TableNumber ORDER BY ObjectDescription) rn
FROM TableMemory T
WHERE ObjectType IN (1, 17)
)
SELECT TAFEL, PRODUCT
, COUNT (*) AS AANTAL
, SUM(RowToCount) OVER () TableCount
FROM cte
CROSS APPLY (VALUES (CASE WHEN rn = 1 THEN 1 ELSE 0 END)) AS X (RowToCount)
GROUP BY TAFEL, PRODUCT, RowToCount
ORDER BY TAFEL;
要添加包含(重复的)[### OF TABLES] 的列,您可以使用 CROSS JOIN
/* This will repeat the [### OF TABLES] for each row of the results */
;with
prod_cte as (
SELECT TableNumber AS TAFEL
, COUNT (*) AS AANTAL
, ObjectDescription AS PRODUCT
FROM TableMemory
WHERE (ObjectType = 1 OR ObjectType = 17)
GROUP BY TableNumber, ObjectType, ObjectDescription),
total_cte as (
SELECT COUNT(DISTINCT TableNumber) [### OF TABLES]
FROM TableMemory
WHERE ObjectType IN (1, 17))
select p.*, t.*
from prod_cte p
cross join total_cte t;
[编辑] 没有 CTE 在 SQL 2000
工作select p.*, t.*
from (
SELECT TableNumber AS TAFEL
, COUNT (*) AS AANTAL
, ObjectDescription AS PRODUCT
FROM TableMemory
WHERE (ObjectType = 1 OR ObjectType = 17)
GROUP BY TableNumber, ObjectType, ObjectDescription) p,
(
SELECT COUNT(DISTINCT TableNumber) [### OF TABLES]
FROM TableMemory
WHERE ObjectType IN (1, 17)) t;