我想创建一个显示数据库统计信息的视图,如下所示:
NumberOfProduct | NumberOfUsers | NumberOfOnlineUsers
45453 54665 5654645
数据来源:
NumberOfProduct - 是产品表的总行数
NumberOfUsers - 用户表中的行数
NumberOfOnlineUsers - 列中的行值是" number"位于名为 SiteOverView 的表中,其中stat_name =' NumberOfOnlineUsers'
SiteOverView表:
stat_name | number
NumberOfOnlineUsers 5654645
NumberOfOfflineUsers 54665
NumberOfVisitors 45453
我想要像
这样的东西Create View [dbo].[dashboard_stat]
AS
SELECT
(select count(*) from products) as NumberOfProduct,
(select count(*) from users) as NumberOfUsers,
(select number from SiteOverView WHERE stat_name='NumberOfOnlineUsers') as NumberOfOnlineUsers
FROM (NumberOfOnlineUsers) dashboard_stat
但我正在努力用write syntactic写这个语句。
答案 0 :(得分:1)
select
语句不需要from
子句。因此,如果没有from
:
create view [dbo].[dashboard_stat]
as
select (select count(1) from products) as NumberOfProduct
,(select count(1) from users) as NumberOfUsers
,(select number
from SiteOverView
where stat_name = 'NumberOfOnlineUsers'
) as NumberOfOnlineUsers
go