久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將另一個組添加到遞歸 CTE 中

Adding a further group into a recursive CTE(將另一個組添加到遞歸 CTE 中)
本文介紹了將另一個組添加到遞歸 CTE 中的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

參考這個 .新的Sql Fiddle.

Referring back to this SO post

If there is a Grouping category "Category" which, for simplicity's sake, can be either X or Y - is it a trivial matter amending this script so that it will add in the missing dates for each of the categories ?

I assume the category will need adding into the CTE?

In other words if I have the following initial table:

...how do I get to the following:

Will upload my attempt shortly

I've called the initial table #x. I'm hoping to adapt a recursive CTE query like the following to include the field Category:

DECLARE @MinDate DATETIME; 
SET @MinDate =  (SELECT Min(DATE) FROM #x)

DECLARE @MaxDate DATETIME; 
SET @MaxDate =  (SELECT Max(DATE) FROM #x)

;WITH times AS 
    ( 
    SELECT @MinDate dt , 1 depth 
    UNION ALL 
    SELECT 
            DATEADD(d, depth, @MinDate) dt 
            , 1 + depth as depth 
    FROM times 
    WHERE DATEADD(d, depth, @MinDate) <= @MaxDate
    ) 
SELECT 
    * 
FROM 
    TIMES t
    LEFT OUTER JOIN #X x
        ON 
        t.dt = x.Date

Ok - I've tied including a CROSS JOIN but it expands things incorrectly:

SELECT DISTINCT Category INTO #Cat FROM #x

DECLARE @MinDate DATETIME; 
SET @MinDate =  (SELECT Min(DATE) FROM #x)

DECLARE @MaxDate DATETIME; 
SET @MaxDate =  (SELECT Max(DATE) FROM #x)


;WITH times AS 
    ( 
    SELECT 
            Category
            , @MinDate dt 
            , 1 depth 
    FROM #Cat
    UNION ALL 
    SELECT 
            c.Category
            , DATEADD(d, depth, @MinDate) dt 
            , 1 + depth as depth 
    FROM 
            times t
            CROSS JOIN #Cat c
                --ON c.Category IS NOT NULL
    WHERE DATEADD(d, depth, @MinDate) <= @MaxDate
    ) 
SELECT 
    * 
FROM 
    TIMES

This seems to have worked ok:

SELECT DISTINCT Category INTO #Cat FROM #x

DECLARE @MinDate DATETIME; 
SET @MinDate =  (SELECT Min(DATE) FROM #x)

DECLARE @MaxDate DATETIME; 
SET @MaxDate =  (SELECT Max(DATE) FROM #x)


;WITH times AS 
    ( 
    SELECT 
            Category
            , @MinDate dt 
            , 1 depth 
    FROM #Cat
    UNION ALL 
    SELECT 
            Category
            , DATEADD(d, depth, @MinDate) dt 
            , 1 + depth as depth 
    FROM 
            times t
    WHERE DATEADD(d, depth, @MinDate) <= @MaxDate
    ) 
SELECT 
    * 
FROM 
    TIMES

解決方案

Here is a solution without a calendar table (which is a must in production). You might have date range in variables, or you might go for min() and max() from the_table.

EDIT: shorter version incorporating categories into date range generation

declare @startdate datetime = '2012-1-1'
declare @enddate datetime = '2012-1-5'

; with dates([date], category) as (
  select distinct @startdate, category
    from the_table
   union all
  select dateadd (day, 1, [date]), category
    from dates
   where [date] < @enddate
)
select dates.date, 
       dates.category, 
       isnull(the_table.amount, 0) Amount
  from dates
  left join the_table
    on dates.date = the_table.date
   and dates.category = the_table.category
 order by dates.category, dates.date
option (maxrecursion 0)

There is live test @ Sql Fiddle. New Sql Fiddle.

這篇關于將另一個組添加到遞歸 CTE 中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 中文字幕欧美一区 | 涩涩导航 | 99热这里| 精品国产一二三区 | 日韩在线中文字幕 | 91免费观看在线 | 国产羞羞视频在线观看 | 超碰男人天堂 | 国产一区三区视频 | 成人黄色a | 欧美成人精品一区二区三区 | 久久精品一级 | 久久久久久久综合 | 日本久久精 | 四虎免费视频 | 国产99久久久国产精品 | 精品一区二区在线观看 | 久草免费在线视频 | 自拍偷拍亚洲视频 | 天天搞天天操 | 国产第一区二区 | 91精品国产欧美一区二区 | 色婷婷精品久久二区二区蜜臂av | 91精品久久久久久久久 | 99re6在线视频 | 国产欧美视频一区 | 中文字幕在线视频免费视频 | 久久免费小视频 | 久久久91| 99pao成人国产永久免费视频 | 国产99视频精品免视看9 | 亚洲伊人精品酒店 | 亚洲欧美国产精品久久 | 日韩久久综合 | 日韩免费一区二区 | 中文av电影 | 久久亚洲国产精品日日av夜夜 | 美女福利视频一区 | 色婷婷综合在线观看 | 免费精品在线视频 | 久草视频网站 |