两个 SQL 交叉表的相除值
我想创建两个交叉表,然后取两者的比率以获得百分比。第一个看起来像这样:
TRANSFORM Count([Deal Data ECM].[Fees (Y/N)]) AS [CountOfFees (Y/N)]
SELECT [Deal Data ECM].[Deal Type]
FROM [Deal Data ECM]
WHERE ((([Deal Data ECM].[Fees (Y/N)])="Y"))
GROUP BY [Deal Data ECM].[Deal Type]
PIVOT [Deal Data ECM].[Deal Region];
第二个应该具有相同的一切,除了费用(Y/N)将设置为“N”。然后我想划分每个表中对应的值。有没有一种方法可以在一个 SQL 表达式中做到这一点?
编辑:
让我们想象一下 query1 和 2 的结果如下:
Query1
Blue Red
Cat 1 2
Dog 3 4
Query2
Blue Red
Cat 5 10
Dog 15 20
I want Query 3 to result in:
Query3
Blue Red
Cat .5 .5
Dog .5 .5
编辑2:这就是数据的样子。更具体地说,每个查询的结果如下所示:
Region1 Region2 Region3
Dealtype1 some numbers in each of these cells
Dealtype2
Dealtype3
我不确定为什么需要实际数据,上面的示例将具有与此相同的解决方案(我只是想知道语法,使用什么 SQL 语句,等)
解决方案:
TRANSFORM Sum(IIf([Disclosed Fees (Y/N)]="Y",1,0))/Count([Disclosed Fees (Y/N)]) AS percent_fees_disclosed
SELECT [Deal Data ECM].[Deal Type]
FROM [Deal Data ECM]
GROUP BY [Deal Data ECM].[Deal Type]
PIVOT [Deal Data ECM].[Deal Region];
I want to create two crosstab tables, and then take the ratios of the two to get a percentage. The first one looks like this:
TRANSFORM Count([Deal Data ECM].[Fees (Y/N)]) AS [CountOfFees (Y/N)]
SELECT [Deal Data ECM].[Deal Type]
FROM [Deal Data ECM]
WHERE ((([Deal Data ECM].[Fees (Y/N)])="Y"))
GROUP BY [Deal Data ECM].[Deal Type]
PIVOT [Deal Data ECM].[Deal Region];
and the second one should have everything the same, except Fees (Y/N) will be set ="N". Then I want to divide the corresponding values in each table. Is there a way to do this in one SQL expression?
EDIT:
let's imagine the result of query1 and 2 are as follows:
Query1
Blue Red
Cat 1 2
Dog 3 4
Query2
Blue Red
Cat 5 10
Dog 15 20
I want Query 3 to result in:
Query3
Blue Red
Cat .5 .5
Dog .5 .5
EDIT 2: That is what the data looks like. More specifically, the results of each query would look like this:
Region1 Region2 Region3
Dealtype1 some numbers in each of these cells
Dealtype2
Dealtype3
I'm not sure why you need actual data, the example above would have the same solution sa this one (I just want to know the syntax, what SQL statements to use, etc)
SOLUTION:
TRANSFORM Sum(IIf([Disclosed Fees (Y/N)]="Y",1,0))/Count([Disclosed Fees (Y/N)]) AS percent_fees_disclosed
SELECT [Deal Data ECM].[Deal Type]
FROM [Deal Data ECM]
GROUP BY [Deal Data ECM].[Deal Type]
PIVOT [Deal Data ECM].[Deal Region];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以基于按交易类型连接的两个交叉表创建第三个查询。
在查询设计窗口中,创建查询 1:
接下来,创建查询 2:
最后,创建查询 3:
您可以将一个表或查询中的字段拖到下一个表或查询中以创建联接。 INNER JOIN 仅包含两个字段都匹配的记录。
更多信息:
关系数据库设计基础,Paul Litwin,2003
基本 Microsoft Jet Access 2000 的 SQL
Access 2000 中级 Microsoft Jet SQL
Access 2000 的高级 Microsoft Jet SQL
You can create a third query based on the two crosstabs joined on deal type.
In the query design window, create Query1:
Next, create Query2:
Finally, create Query3:
You can drag a field from one table or query to the next to create a join. INNER JOIN only includes records where both fields match.
Further information:
Fundamentals of Relational Database Design, Paul Litwin, 2003
Fundamental Microsoft Jet SQL for Access 2000
Intermediate Microsoft Jet SQL for Access 2000
Advanced Microsoft Jet SQL for Access 2000