Teradata 默认列表

发布于 2024-12-06 23:22:40 字数 529 浏览 0 评论 0原文

有一张表,其中包含我需要的金额和说明。但是,该表包含年份信息,但我想要月份信息。例如,表中显示了 2011 年肯塔基州的信息……仅此而已。对于加利福尼亚州,它显示大约 5 个不同的年份。但我需要每月重复一次。

因此,如果 2011 年肯塔基州总共有 12 个,那么我需要一个查询来显示一月、二月、五月的 12 个......

现在我用一个愚蠢的查询得到这个输出:

Kentucky  12   January
California 800 January

这可以通过按州分组轻松完成,数量和月份

我想确保无论数量是多少,每个州都有所有月份

Kentucky  12   January
Kentucky  12   February
Kentucky  12   May
California 800 January
California 800 February
California 800 May

您知道如何使用 Teradata SQL 执行此操作吗?

There is this one table which contains the amounts and states that I need. However, this table contains a year information but I want month. For example, in the table it shows information for Kentucky for 2011..and thats it. For California it shows about 5 different years. But I need it to repeat by month.

So if in 2011 Kentucky had 12 total, then I need a query that shows 12 for January, February, May....repeatedly

Right now I get this output with a dumb query I have:

Kentucky  12   January
California 800 January

This is done easily by grouping by State, Quantity and Month

I want to make sure that no matter what the Quantity is, each State has ALL months

Kentucky  12   January
Kentucky  12   February
Kentucky  12   May
California 800 January
California 800 February
California 800 May

Any idea on how to do this with Teradata SQL?

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

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

发布评论

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

评论(1

メ斷腸人バ 2024-12-13 23:22:40

整个查询看起来像这样:

SELECT
  state_quantities.state,
  state_quantities.quantity,
  all_months.month_name
FROM state_quantities
CROSS JOIN (
  ...
) all_months

all_months 括号之间的内容取决于“所有月份”的含义。

如果您的意思是出现在 state_quantities 中的所有月份,无论哪个州(因此,如果肯塔基州为 1 月,加利福尼亚州为 2 月,佛罗里达州为 5 月,那么您只能得到这三个月),您可以使用如下内容:

SELECT
  month_name
FROM state_quantities
GROUP BY month_name

如果您想要所有 12 个月份月,您将连接到包含所有 12 个月的表。如果没有,您可以使用 sys_calendar.calendar (下面的语法可能会关闭):

SELECT
  CAST(calendar_date AS DATE FORMAT 'MMM') AS month_name
FROM sys_calendar.calendar
GROUP BY month_name

The overall query would look something like this:

SELECT
  state_quantities.state,
  state_quantities.quantity,
  all_months.month_name
FROM state_quantities
CROSS JOIN (
  ...
) all_months

What goes between the brackets for all_months depends on what you mean by "all months".

If you mean all months that appear in state_quantities irrespective of state (so if you have Kentucky with January, California with February and Florida with May, you'd only get those three months) you could use something like this:

SELECT
  month_name
FROM state_quantities
GROUP BY month_name

If you want all 12 months, you would join to a table containing all 12 months. In the absence of that, you could use sys_calendar.calendar (syntax below might be off):

SELECT
  CAST(calendar_date AS DATE FORMAT 'MMM') AS month_name
FROM sys_calendar.calendar
GROUP BY month_name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文