两个 SQL 交叉表的相除值

发布于 2024-12-26 09:41:55 字数 1149 浏览 1 评论 0原文

我想创建两个交叉表,然后取两者的比率以获得百分比。第一个看起来像这样:

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 技术交流群。

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

发布评论

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

评论(1

|煩躁 2025-01-02 09:41:55

您可以基于按交易类型连接的两个交叉表创建第三个查询。

在查询设计窗口中,创建查询 1:

TRANSFORM Count([Deal Data ECM].[Fees (Y/N)]) AS [CountOfFees1]
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];

接下来,创建查询 2:

TRANSFORM Count([Deal Data ECM].[Fees (Y/N)]) AS [CountOfFees2]
SELECT [Deal Data ECM].[Deal Type]
FROM [Deal Data ECM]
WHERE ((([Deal Data ECM].[Fees (Y/N)])="N"))
GROUP BY [Deal Data ECM].[Deal Type]
PIVOT [Deal Data ECM].[Deal Region];

最后,创建查询 3:

SELECT Query1.[DealRegion1], Query2.[DealRegion1], 
       Query2.[DealRegion2], Query1.[DealRegion2]
FROM Query1 
INNER JOIN Query2 
ON Query1.[Deal Type] = Query2.[Deal Type];

您可以将一个表或查询中的字段拖到下一个表或查询中以创建联接。 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:

TRANSFORM Count([Deal Data ECM].[Fees (Y/N)]) AS [CountOfFees1]
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];

Next, create Query2:

TRANSFORM Count([Deal Data ECM].[Fees (Y/N)]) AS [CountOfFees2]
SELECT [Deal Data ECM].[Deal Type]
FROM [Deal Data ECM]
WHERE ((([Deal Data ECM].[Fees (Y/N)])="N"))
GROUP BY [Deal Data ECM].[Deal Type]
PIVOT [Deal Data ECM].[Deal Region];

Finally, create Query3:

SELECT Query1.[DealRegion1], Query2.[DealRegion1], 
       Query2.[DealRegion2], Query1.[DealRegion2]
FROM Query1 
INNER JOIN Query2 
ON Query1.[Deal Type] = Query2.[Deal Type];

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文