按名称访问 SQL 组,然后在水平方向设置从month1到month12的值
我有一个表,其中有(姓名、金钱、日期),我想获取(姓名、第 1 个月的金钱、第 2 个月的金钱,依此类推到第 12 个月)
该怎么做?
我知道如何从日期中提取月份;
I have a Table that has (names, money, date) And I would like to get the (name, money of month 1, money of month 2 and so on to month12)
How to do it?
I know how to extract the month from the date;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个查询:
name, iif(month(date) = 1, Money, 0) AS m1,依此类推,直到 m12
第二个查询:
name, sum(m1) AS mo1,依此类推,直到 mo12
按名称分组
限制:每月只能插入一次,并且查询必须具有仅选择 1 年的年份过滤器。
First query:
name, iif(month(date) = 1, money, 0) AS m1, and so on up to m12
Second query:
name, sum(m1) AS mo1, and so on up to mo12
group by name
Limitations: only one insert per month and the query must have an year filter that selects ONLY 1 YEAR.
您可能想要构建一个数据透视表,使用月份函数生成每个日期的月份值。然后,您将能够使用本月值作为数据透视表中的一列。
请注意:除非您明确过滤特定年份的数据,否则不同年份相同月份的值将被聚合
you might want to build a pivot table, using the month function to generate the month value for each of your dates. You'll then be able to use this month value as a column in your pivot table.
Be careful: values for same months in different years will be aggregated, unless you expressely filter your data for a specific year
您想要的在 msaccess 中称为交叉表查询(在更大的系统中称为 PIVOT)。
这是 Allen Brown 的精彩文章,非常注重细节。
What you want is called crosstab query in msaccess parlance (and PIVOT in bigger systems).
Here's Allen Brown's nice write up with a lot of attention to detail.