Sql 导致错误的具有Union的Group By
我想合并两个查询的输出-Sql 导致错误的具有Union的Group By,sql,sql-server,tsql,Sql,Sql Server,Tsql,我想合并两个查询的输出- select top(10) hex_code from dbo.colors 输出- +----------+ | hex_code | +----------+ | #2ecc71 | | #3498db | | #9b59b6 | | #f1c40f | | #e67e22 | | #e74c3c | | #2980b9 | | #2c3e50 | | #27ae60 | | #f39c12 | +----------+ +--------
select top(10) hex_code from dbo.colors
输出-
+----------+
| hex_code |
+----------+
| #2ecc71 |
| #3498db |
| #9b59b6 |
| #f1c40f |
| #e67e22 |
| #e74c3c |
| #2980b9 |
| #2c3e50 |
| #27ae60 |
| #f39c12 |
+----------+
+---------+-------+
| Product | Count |
+---------+-------+
| A | 105 |
| B | 99 |
| C | 87 |
| D | 75 |
| E | 56 |
| F | 52 |
| G | 37 |
| I | 18 |
| K | 16 |
| L | 15 |
+---------+-------+
质疑-
SELECT top(10) [Product], count([Product]) as Count
FROM [dbo].[TableA] group by [Product] order by count([Product]) desc
输出-
+----------+
| hex_code |
+----------+
| #2ecc71 |
| #3498db |
| #9b59b6 |
| #f1c40f |
| #e67e22 |
| #e74c3c |
| #2980b9 |
| #2c3e50 |
| #27ae60 |
| #f39c12 |
+----------+
+---------+-------+
| Product | Count |
+---------+-------+
| A | 105 |
| B | 99 |
| C | 87 |
| D | 75 |
| E | 56 |
| F | 52 |
| G | 37 |
| I | 18 |
| K | 16 |
| L | 15 |
+---------+-------+
我尝试使用UNION
合并输出,但GROUPBY子句不允许我这样做。我不知道如何将它与GROUPBY和ORDERBY子句一起使用
我试过了-
SELECT top(10) [Product], count([Product]) as Count
FROM [dbo].[TableA] group by [Product] order by count([Product]) desc
UNION
select top(10) hex_code from dbo.colors
但这会导致错误。还有其他方法合并这两列吗
编辑-预期输出
+---------+-------+----------+
| Product | Count | Hex Code |
+---------+-------+----------+
| A | 105 | #2ecc71 |
| B | 99 | #3498db |
| C | 87 | #9b59b6 |
+---------+-------+----------+
for all 10 rows.
根据斯凯里奇的回答,结果是这样的
A 105 #27ae60
A 105 #2980b9
注意-两列都获取前10条记录。这两个表不相关。(我想没有连接)如果是任意的,您可以这样做
;With cte
as
(
SELECT top(10) [Product], count([Product]) as Count
FROM [dbo].[TableA] group by [Product] order by count desc
)
,cte1
as(
select top 10 hex_code from dbo.colors
)
select * from cte c
cross apply
(select top 1 hex_code from cte1 order by newid()
)b
如果是任意的,你可以这样做
;With cte
as
(
SELECT top(10) [Product], count([Product]) as Count
FROM [dbo].[TableA] group by [Product] order by count desc
)
,cte1
as(
select top 10 hex_code from dbo.colors
)
select * from cte c
cross apply
(select top 1 hex_code from cte1 order by newid()
)b
您需要
连接这两个表/查询。如果您没有要连接的列,并且您只想将每个产品与任意颜色匹配,则可以在行号()上连接,类似于:
select p.Product, p.Count, c.hex_code
from (
SELECT top(10)
[Product], count([Product]) as Count,
row_number() over (order by count([Product])) [rn]
FROM [dbo].[TableA]
group by [Product]
) p
left join (
select top(10)
hex_code,
row_number() over (order by hex_code) [rn]
from dbo.colors
) on p.rn=c.rn
order by p.Count desc
您需要连接这两个表/查询。如果您没有要连接的列,并且您只想将每个产品与任意颜色匹配,则可以在行号()上连接,类似于:
select p.Product, p.Count, c.hex_code
from (
SELECT top(10)
[Product], count([Product]) as Count,
row_number() over (order by count([Product])) [rn]
FROM [dbo].[TableA]
group by [Product]
) p
left join (
select top(10)
hex_code,
row_number() over (order by hex_code) [rn]
from dbo.colors
) on p.rn=c.rn
order by p.Count desc
在执行联合时,每个查询中需要相同数量的列。第一个返回2列,第二个只返回1列。(SQL中的并集只是指叠加结果)当bot表不相关时,为产品a显示十六进制代码(#2ecc71)的逻辑是什么?输出将进一步传递到JS库,该库期望第三列作为颜色值。听起来像是一个完整联接的作业。当进行并集时,每个查询中都需要相同数量的列。第一个返回2列,第二个只返回1列。(SQL中的并集只是指叠加结果)当bot表不相关时,为产品a显示十六进制代码(#2ecc71)的逻辑是什么?输出将进一步传递到JS库,该库期望第三列作为颜色值。听起来像是完全联接的作业。您的查询只分配一种颜色。如果将top 1
更改为top 10
,我会得到与SCAISEDGE相同的输出。这就是我们得到的结果。我假设在有限的值集中,任意地,您可以对bot查询执行一个行数,并将它们连接起来。您的查询只指定一种颜色。如果将top 1
更改为top 10
,我会得到与SCAISEDGE相同的输出。这就是我们得到的结果。我假设在有限的值集中,您可以任意对bot查询执行行数,并将它们两者连接起来