2016-12-01 137 views
0

我想創建一個視圖,顯示數據庫這樣的統計:伯爵錶行

NumberOfProduct | NumberOfUsers | NumberOfOnlineUsers

45453 54665 5654645

數據源

NumberOfProduct - 是產品表總計行數

NumberOfUsers - 行數在用戶表

NumberOfOnlineUsers - 正所謂位於表內的 「數量」 列一列的值稱爲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 

但我很努力地寫入語句寫入句法。

+0

您是否嘗試刪除主要選擇的FROM子句?你會得到什麼錯誤? – Snowlockk

回答

1

select SQL Sever中的語句不需要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 
+0

哦,上帝......爲什麼我不試試?大聲笑...謝謝戴夫! –