【问题标题】:The pointer will also create the row according to the sum of the counter指针也会根据计数器的和创建行
【发布时间】:2018-10-29 13:04:36
【问题描述】:

我想根据视图中查询的返回值乘以该行

select count(col1)as COUNTS from TABEL_NAME

|   Result  |
|--------------------------------------
|   COUNTS  |
--------------------------------------
|   5       |
--------------------------------------


create view _LISTING as 

(select count(col1) as COUNTS from TABEL_NAME)... as ROWS (?) from .... (?)


select *from _LISTING

|   Result  |
|--------------------------------------
|   ROWS    |
--------------------------------------
|   Result1 |
|   Result2 |
|   Result3 |
|   Result4 |
|   Result5 |
--------------------------------------

我应该如何创建这样的查询?

【问题讨论】:

    标签: sql-server view


    【解决方案1】:

    试试这个

    CREATE VIEW  [dbo].[vw_LISTING] 
    AS
    WITH Cte 
    AS
    (
    SELECT COUNT(1)OVER() AS Cnt ,ROW_NUMBER()OVER(ORDER BY (SELECT 1)) AS Id
    FROM  [dbo].[Customer]
    )
    SELECT 'Result'+ CAST(Id  AS VARCHAR ) AS ReqResult 
    FROM Cte
    
    GO
    
    SELECT COUNT(1) AS [Count]
    FROM  [dbo].[Customer] --Here this customer table contains 4 record only
    
    Result
    ------
    [Count]
    ----------    
    4
    
    SELECT * FROM [dbo].[vw_LISTING] 
    
    ReqResult
    ---------
    Result1
    Result2
    Result3
    Result4
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-14
      • 2019-06-04
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2022-10-20
      相关资源
      最近更新 更多