我有两张表Table1
和Table2
。
这些表中的每一个都有Column1, Column2
列。
我需要在Column2中找到Table1组中新Column1的百分比。
1)Column1中新记录的计数=(不同列1)的计数 见表1,而不是表2)。
2)TableOne中不同Column1的计数
百分比=(1/2)* 100.
我尝试的是
select count(distinct column1)
from TableOne left outer join TableTwo on
(tableone.column1=tabletwo.column2)
where TableTwo.column1 is null.
而且:
select count(distinct column1) from tableone.
现在如何在一个查询中合并两者并按列2进行分组。
答案 0 :(得分:0)
快速做到这一点是:
select ((countA/countB)*100) as Percentage (
select count(distinct column1) as countA,
(select count(distinct column1) from tableone) as countB
from TableOne left outer join TableTwo on
(tableone.column1=tabletwo.column2)
where TableTwo.column1 is null) as X
from X
对于按column2进行分组,您只需将其添加到查询中即可。但需要知道要将哪个column2分组。
如果您需要在表格之间进行任何交叉引用(1& 2),那将会有点复杂。
答案 1 :(得分:0)
DECLARE @COUNT1 INT
DECLARE @COUNT2 INT
SELECT @COUNT1 = COUNT(*) FROM comparetable
SELECT @COUNT2 = COUNT(*) FROM comparetable WHERE Description IS NOT NULL
SELECT CAST(CAST(@COUNT2 AS DECIMAL(10,2) )/CAST(@COUNT1 AS DECIMAL(10,2)) AS DECIMAL(10,2)) AS PERCENTAGE