我很陌生,在我去的时候教我自己。我正在尝试操纵SQL代码来做我想要的而不完全理解它,所以我遇到了很多问题。在你回答“我不明白为什么你这样做,你应该这样做”之前,请帮助我找出是否有办法以我接近它的方式做到这一点,因为这需要很长时间让我的大脑弄清楚,但如果确实有一个更简单,更好的方法,请向我详细解释,否则它会让我更加困惑。
在我发布下面的所有废话之前,我的主要问题是 - **
有没有办法通过SQL对Union查询进行编程来计算自身内的百分比?
**比如,第一个查询给了我一个总数,下一个查询根据条件给出了新的总计,但需要一个额外的字段来告诉我与第一个查询的比较是什么。
好的,我的废话有希望解释我正在努力做的事情 -
我需要报告看起来像这样(编辑:对不起,它看起来不像我写帖子时那样):
总案例争议美元总百分比
所有持有11 $ 38,812.86
全部批准7
$ 27,131.32%(所有持有人)Appr w / o Resub 5
$ 14,009.27%(所有Appr)Appr w / Resub 2
$ 13,122.05%(所有Appr)全部拒绝4
$ 11,681.54%(All Holds)Decl w / o Resub 3
$ 7,816.14%(全部Decl)Decl w / Resub 1
$ 3,865.40%(全部Decl)
我正在使用Union查询将我需要的所有查询都粘贴到一个报告中。
这是完整的SQL代码(对不起,这么久)。忽略“选择空”部分,它们是我的绑带,以便将报告分开到我需要它的位置。它只是链接到一个空表。
> SELECT "All Holds" As Type, Count([Holds Table].[Date email received])
> AS [CountOfDate email received], Sum([Holds Table].[Disputed Dollar
> Amount]) AS [SumOfDisputed Dollar Amount] FROM [Holds Table] WHERE
> ((([Holds Table].[Date email received]) Between [Enter start date:]
> And [Enter end date:]))
>
> UNION ALL
>
> SELECT null as Type, null as [CountOfDate email received], null as
> [SumOfDisputed Dollar Amount] from [extra]
>
> UNION ALL
>
> SELECT "All Approved" As Type, Count([Holds Table].[Date email
> received]) AS [CountOfDate email received], Sum([Holds
> Table].[Disputed Dollar Amount]) AS [SumOfDisputed Dollar Amount] FROM
> [Holds Table] WHERE ((([Holds Table].[Date email received]) Between
> [Enter start date:] And [Enter end date:])) GROUP BY [Holds
> Table].[SHU Decision] HAVING ((([Holds Table].[SHU Decision])="1"))
>
> UNION ALL
>
> SELECT"Approved without Resubmission" As type, Count([Holds
> Table].[Date email received]) AS [CountOfDate email received],
> Sum([Holds Table].[Disputed Dollar Amount]) AS [SumOfDisputed Dollar
> Amount] FROM [Holds Table] WHERE ((([Holds Table].[Date email
> received]) Between [Enter start date:] And [Enter end date:])) GROUP
> BY [Holds Table].[SHU Decision], [Holds Table].[Resubmitted?] HAVING
> ((([Holds Table].[SHU Decision])="1") AND (([Holds
> Table].[Resubmitted?])=False))
>
> UNION ALL
>
> SELECT"Approved with Resubmission" As type, Count([Holds Table].[Date
> email received]) AS [CountOfDate email received], Sum([Holds
> Table].[Disputed Dollar Amount]) AS [SumOfDisputed Dollar Amount] FROM
> [Holds Table] WHERE ((([Holds Table].[Date email received]) Between
> [Enter start date:] And [Enter end date:])) GROUP BY [Holds
> Table].[SHU Decision], [Holds Table].[Resubmitted?] HAVING ((([Holds
> Table].[SHU Decision])="1") AND (([Holds Table].[Resubmitted?])=True))
>
> UNION ALL
>
> Select null as Type, null as [CountOfDate email received], null as
> [SumOfDisputed Dollar Amount] from [extra]
>
> UNION ALL
>
> SELECT "All Declined" As Type, Count([Holds Table].[Date email
> received]) AS [CountOfDate email received], Sum([Holds
> Table].[Disputed Dollar Amount]) AS [SumOfDisputed Dollar Amount] FROM
> [Holds Table] WHERE ((([Holds Table].[Date email received]) Between
> [Enter start date:] And [Enter end date:])) GROUP BY [Holds
> Table].[SHU Decision] HAVING ((([Holds Table].[SHU Decision])="2"))
>
> UNION ALL
>
> SELECT "Declined without Resubmission" As type, Count([Holds
> Table].[Date email received]) AS [CountOfDate email received],
> Sum([Holds Table].[Disputed Dollar Amount]) AS [SumOfDisputed Dollar
> Amount] FROM [Holds Table] WHERE ((([Holds Table].[Date email
> received]) Between [Enter start date:] And [Enter end date:])) GROUP
> BY [Holds Table].[SHU Decision], [Holds Table].[Resubmitted?] HAVING
> ((([Holds Table].[SHU Decision])="2") AND (([Holds
> Table].[Resubmitted?])=False))
>
> UNION ALL
>
> SELECT "Declined with Resubmit" As type, Count([Holds Table].[Date
> email received]) AS [CountOfDate email received], Sum([Holds
> Table].[Disputed Dollar Amount]) AS [SumOfDisputed Dollar Amount] FROM
> [Holds Table] WHERE ((([Holds Table].[Date email received]) Between
> [Enter start date:] And [Enter end date:])) GROUP BY [Holds
> Table].[SHU Decision], [Holds Table].[Resubmitted?] HAVING ((([Holds
> Table].[SHU Decision])="2") AND (([Holds
> Table].[Resubmitted?])=True));
phew 那么,这甚至是可能还是我只是搞砸了?我确切地知道如何在几秒钟内在Excel中计算所有这些,所以我真的感到非常沮丧,因为我无法在Access中做一些看似合乎逻辑的事情而没有时间在互联网上搜索我不理解的代码。除此之外,我很感激你能给我的任何帮助,甚至是“你被搞砸了,也许就这样试试”。很有责任!
答案 0 :(得分:0)
..有没有办法通过SQL编写一个Union查询来计算 本身的百分比?
简短回答是:否
您应该将各个查询移动到子查询,然后使用主查询从中提取所需的数据。