在访问数据库设计器中使用带有计数的多列

时间:2016-05-03 19:49:08

标签: sql ms-access union

我试图在微软访问查询中显示具有不同计数的多个列。它不允许我做某些事情,普通的查询可以b / c它有sql设计视图。 我想要展示 多个单个等等列及其计数。

注意:表名和属性已更改。

select (select  count(*)as multiple from (select  userId from  dbo.Purchases
 where userId is not null GRoup by userId having count(*)>1) x), (
select  count(*)as single from (select  userId from  dbo.Purchases where
userId is not null GRoup by userId having count(*)=1) x );

如果我单独执行这些操作,我可以显示它,但我想将它们组合成一个查询和一行。这可能吗?

select  count(*)as multiple from (select  userId from  dbo.Purchases
 where userId is not null GRoup by userId having count(*)>1) x)

2 个答案:

答案 0 :(得分:1)

使用2个查询非常简单:

第一个,保存为“购买摘要”

Select UserID, count(UserID) as Count from Purchases Group By UserID

建立第二个:

SELECT Sum(IIf([count]=1,1)) AS [Single], Sum(IIf([count]>1,1)) AS Multiple FROM [Purchases Summary]

我无法找到一种巧妙的方法将其合并为一个查询。

我不知道昨晚我的问题是什么,但单个查询是

SELECT Sum(IIf([count]=1,1)) AS [Single], Sum(IIf([count]>1,1)) AS Multiple
FROM (Select UserID, count(UserID) as Count from Purchases Group By UserID)

答案 1 :(得分:0)

唐·乔治解决方案也可行。 我最终为每列使用了表单和VBA。我遇到的一个问题是,我需要对唯一ID使用不同的调用,并且当有一个sql设计视图并不真正支持时。支持 distinctrow ,但它不适用于我的查询。我最后像以前一样写它,所以它不需要明显。 这是我用来覆盖访问表单内的每个输入的VBA。 currentDb需要在数据库正常工作之前正确连接到数据库。

selectStatement = "SELECT Count(* ) FROM (SELECT userID FROM dbo_Purchases WHERE userID is not null  GROUP BY userID HAVING count(*)>1)  AS x;"
rs = CurrentDb.OpenRecordset(selectStatement).Fields(0).Value
[Text30].Value = rs