在 SQL 中按年份并排比较数据

发布于 2024-09-05 06:19:57 字数 429 浏览 5 评论 0原文

我有类似于以下的表:

Year | Product |  Value
2006   A          10
2006   B          20
2006   C          30
2007   A          40
2007   B          50
2007   C          60

我想要一个将返回以下比较的查询

Product | 2006 Value | 2007 Value
A         10           40
B         20           50
C         30           60

有哪些选项可以执行此操作?可以不用join就完成吗?

我正在使用 DB2,但所有 SQL 类型的答案都会有所帮助。

I have table similar to the following:

Year | Product |  Value
2006   A          10
2006   B          20
2006   C          30
2007   A          40
2007   B          50
2007   C          60

I would like a query that would return the following comparison

Product | 2006 Value | 2007 Value
A         10           40
B         20           50
C         30           60

What are the options to do so? Can it be done without joins?

I'm working with DB2, but answers in all SQL types would be helpful.

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

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

发布评论

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

评论(3

梦言归人 2024-09-12 06:19:57
select Product, 
    max(case when Year = 2006 then Value end) as [2006 Value], 
    max(case when Year = 2007 then Value end) as [2007 Value] 
from MyTable
group by Product
order by Product
select Product, 
    max(case when Year = 2006 then Value end) as [2006 Value], 
    max(case when Year = 2007 then Value end) as [2007 Value] 
from MyTable
group by Product
order by Product
软甜啾 2024-09-12 06:19:57

一个简单的交叉表查询应该可以完成

SELECT DISTINCT (year), PRODUCT,
sum (case when year = 2006 then VALUE else 0 end ) as [2006 Value]
sum (case when year = 2007 then value else 0 end ) as [2007 value]
from table
group by year, product

检查语法,但这是基本思想。确实没有必要加入。

A simple cross tab query should do it

SELECT DISTINCT (year), PRODUCT,
sum (case when year = 2006 then VALUE else 0 end ) as [2006 Value]
sum (case when year = 2007 then value else 0 end ) as [2007 value]
from table
group by year, product

Check the syntax, but that's the basic idea. There is really no need to join.

a√萤火虫的光℡ 2024-09-12 06:19:57

您已经得到了一些不使用联接的答案,但为了进行比较,这里有一个使用联接的替代方案:

SELECT
    T1.Product,
    T1.Value AS [2006 Value],
    T2.Value AS [2007 Value]
FROM Table1 T1
JOIN Table1 T2
ON T1.Product = T2.Product
AND T1.Year = 2006
AND T2.Year = 2007

我正在使用 DB2,但所有 SQL 类型的答案都会很有帮助。

以下是使用 SQL Server 支持的 PIVOT 的解决方案:

SELECT
    Product,
    [2006] AS [2006 Value],
    [2007] AS [2007 Value]
FROM Table1
PIVOT(MAX(Value) FOR Year IN ([2006], [2007]))
AS p;

You've already got some answers that don't use a join, but just for comparison here's an alternative that does use a join:

SELECT
    T1.Product,
    T1.Value AS [2006 Value],
    T2.Value AS [2007 Value]
FROM Table1 T1
JOIN Table1 T2
ON T1.Product = T2.Product
AND T1.Year = 2006
AND T2.Year = 2007

I'm working with DB2, but answers in all SQL types would be helpful.

Here's a solution using PIVOT that SQL Server supports:

SELECT
    Product,
    [2006] AS [2006 Value],
    [2007] AS [2007 Value]
FROM Table1
PIVOT(MAX(Value) FOR Year IN ([2006], [2007]))
AS p;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文