久久久久久久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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 亚洲国产精品va在线看黑人 | a免费视频| 青青草在线视频免费观看 | 日韩欧美综合在线视频 | aaa天堂 | 9久久婷婷国产综合精品性色 | 久久网一区二区三区 | 久久久久成人精品 | 亚洲精品美女视频 | 在线观看中文字幕视频 | 成人在线播放网站 | 日本电影一区二区 | 欧美久久视频 | 国产精品自产拍 | 亚洲高清一区二区三区 | 欧美精品乱码99久久影院 | 涩涩视频网站在线观看 | 黄色一级大片在线免费看产 | 精品九九 | 在线欧美亚洲 | 国产成人在线视频播放 | 久热m3u8| 国产精品视频中文字幕 | 蜜桃久久 | 亚洲aⅴ | 麻豆一区二区三区精品视频 | 国产一区二 | 91免费在线视频 | 国产亚洲第一页 | 99久久精品免费看国产四区 | 久久aⅴ乱码一区二区三区 亚洲国产成人精品久久久国产成人一区 | 亚洲网站在线观看 | 欧美美女爱爱视频 | 欧美日韩中文字幕 | 久久亚洲综合 | 一区二区久久精品 | 久久成人一区 | 日韩第一页 | 欧美电影免费观看 | 久久99精品久久久97夜夜嗨 | 色视频网站 |