2 Select 个语句,1 个查询 -- Return 2 列
2 Select Statements, 1 Query -- Return 2 Columns
我有 2 个 select 语句,我想在 1 个查询中 return。我目前正在使用 union
,它在 1 个查询中 returning 两个结果,但它 returning 2 行...
我想尝试 return 2 列,而不是 2 行。
下面是我的查询:
SELECT distinct count([number]) AS 'Total' from [myTbl] WHERE [type] = 'online' union
SELECT distinct count([number]) AS 'Success' from [myTbl] WHERE [type] = 'online' and [MyValue] = 'true'
我想要 2 列.. Total
和 Success
。这可能吗?也许不是通过 union
,而是通过其他方法?
这个怎么样?它为每一行求和一个值,设置为 1 表示成功,否则设置为 0,因此计算成功。
SELECT COUNT([number]) AS 'Total',
SUM(CASE WHEN [MyValue] = 'true' THEN 1 ELSE 0 END) AS 'Success'
FROM [myTbl]
WHERE [type] = 'online'
您可以将其包装在两个子查询中,这样您将获得包含两个结果列的一行。
SELECT
(SELECT distinct count([number]) FROM [myTbl] WHERE [type]='online') AS Total,
(SELECT distinct count([number]) FROM [myTbl] WHERE [type]='online' AND [MyValue]='true') AS Success
注意这里DISTINCT的使用存疑
Common Table Expressions 的完美用例。
;WITH CTE_Total AS (
SELECT DISTINCT COUNT([number]) AS 'Total'
FROM [myTbl]
WHERE [type] = 'online'
), CTE_Success AS (
SELECT DISTINCT COUNT([number]) AS 'Success'
FROM [myTbl]
WHERE [type] = 'online' and [MyValue] = 'true'
)
SELECT
[Total] = CTE_Total.Total,
[Success] = CTE_Success.Success;
很简单,这样做:
SELECT Total, Success from
(
SELECT distinct count([number]) AS 'Total' from [myTbl] WHERE [type] = 'online' union
SELECT distinct count([number]) AS 'Success' from [myTbl] WHERE [type] = 'online' and [MyValue] = 'true'
)
试试这个
SELECT
(SELECT count([number]) FROM [myTbl] WHERE [type] = 'online') AS 'Total',
(SELECT count([number]) FROM [myTbl] WHERE [type] = 'online' and [MyValue] = 'true') AS 'Success'
或者更易于维护
;WITH tbl as (
SELECT [number], [MyValue] FROM [myTbl] WHERE [type] = 'online'
)
SELECT
(SELECT count([number]) FROM tbl) AS 'Total',
(SELECT count([number]) FROM tbl WHERE [MyValue] = 'true') AS 'Success'
我有 2 个 select 语句,我想在 1 个查询中 return。我目前正在使用 union
,它在 1 个查询中 returning 两个结果,但它 returning 2 行...
我想尝试 return 2 列,而不是 2 行。
下面是我的查询:
SELECT distinct count([number]) AS 'Total' from [myTbl] WHERE [type] = 'online' union
SELECT distinct count([number]) AS 'Success' from [myTbl] WHERE [type] = 'online' and [MyValue] = 'true'
我想要 2 列.. Total
和 Success
。这可能吗?也许不是通过 union
,而是通过其他方法?
这个怎么样?它为每一行求和一个值,设置为 1 表示成功,否则设置为 0,因此计算成功。
SELECT COUNT([number]) AS 'Total',
SUM(CASE WHEN [MyValue] = 'true' THEN 1 ELSE 0 END) AS 'Success'
FROM [myTbl]
WHERE [type] = 'online'
您可以将其包装在两个子查询中,这样您将获得包含两个结果列的一行。
SELECT
(SELECT distinct count([number]) FROM [myTbl] WHERE [type]='online') AS Total,
(SELECT distinct count([number]) FROM [myTbl] WHERE [type]='online' AND [MyValue]='true') AS Success
注意这里DISTINCT的使用存疑
Common Table Expressions 的完美用例。
;WITH CTE_Total AS (
SELECT DISTINCT COUNT([number]) AS 'Total'
FROM [myTbl]
WHERE [type] = 'online'
), CTE_Success AS (
SELECT DISTINCT COUNT([number]) AS 'Success'
FROM [myTbl]
WHERE [type] = 'online' and [MyValue] = 'true'
)
SELECT
[Total] = CTE_Total.Total,
[Success] = CTE_Success.Success;
很简单,这样做:
SELECT Total, Success from
(
SELECT distinct count([number]) AS 'Total' from [myTbl] WHERE [type] = 'online' union
SELECT distinct count([number]) AS 'Success' from [myTbl] WHERE [type] = 'online' and [MyValue] = 'true'
)
试试这个
SELECT
(SELECT count([number]) FROM [myTbl] WHERE [type] = 'online') AS 'Total',
(SELECT count([number]) FROM [myTbl] WHERE [type] = 'online' and [MyValue] = 'true') AS 'Success'
或者更易于维护
;WITH tbl as (
SELECT [number], [MyValue] FROM [myTbl] WHERE [type] = 'online'
)
SELECT
(SELECT count([number]) FROM tbl) AS 'Total',
(SELECT count([number]) FROM tbl WHERE [MyValue] = 'true') AS 'Success'