大家好我正在尝试构建一个SQL查询来在仪表板上显示一些信息。
我有一张表存储我的销售数据。我的查询需要查找过去7天的总销售额,平均值并根据区域(存在于用户表中)对其进行分组。我创建了一个临时表获取项目sales.DashBoard_Items表包含需要在仪表板上显示的特定项目。现在我的问题是我需要获取每个地区的商店数量来查找平均销售额。可以请一些人帮忙
Declare @TableTest
table(itemid int,itemname varchar(100),itemdescription varchar(100),id int,itemidd int,userid int,orderdate varchar(40),qty int)
insert into @TableTest
select * from DashBoard_Items join
SalesQTY on SalesQTY.OrderDate>= CONVERT(varchar(10) , DATEADD(DAY,-7,GETDATE()),126)
and OOS_DashBoard_CoreItems.itemid=SalesQTY.itemid
select distinct t.userid,u.region from @TableTest t join users u on t.userid=u.userid and region is not null
以上选择查询返回
如何从上面的选择查询中获取区域数
region count
5 - SUN WEST 2
2 - LONG ISLAND 3
答案 0 :(得分:1)
您需要按region
分组,然后使用cont(*)
SELECT region, count(*)
FROM @TableTest
GROUP BY region;
答案 1 :(得分:1)
请尝试:
SELECT
Region,
COUNT(*) AS [Count]
FROM YourTable
GROUP BY Region
OR
SELECT
DISTINCT Region,
COUNT(*) OVER (PARTITION BY Region) AS [Count]
FROM YourTable
答案 2 :(得分:1)
SELECT u.region, COUNT(*)
FROM @TableTest t JOIN users u ON t.userid=u.userid AND u.region IS NOT NULL
GROUP BY u.region
答案 3 :(得分:1)
尝试
SELECT region, count( * )
FROM mytable
GROUP BY region