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

在單個表中使用 while 循環的多個選擇查詢?是否可

Multiple select queries using while loop in a single table? Is it Possible?(在單個表中使用 while 循環的多個選擇查詢?是否可以?)
本文介紹了在單個表中使用 while 循環的多個選擇查詢?是否可以?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有兩張桌子.表 A 有日期、ISBN(書籍)、需求(該日期的需求).表 B 包含日期、ISBN(用于圖書)和 SalesRank.

I have 2 tables. Table A has Date, ISBN (for Book), Demand(demand for that date). Table B has Date, ISBN (for Book), and SalesRank.

樣本數據如下:DailyBookFile 的每個日期都有 150k 條記錄,從 2010 年開始(即 150k * 365 天 * 8 年)行.每個日期大約有 50 萬條記錄的 SalesRank 表也是如此

The sample data is as follows: The DailyBookFile has 150k records for each date, from year 2010 (i.e. 150k * 365 days * 8 years) rows. Same goes with SalesRank Table having about 500k records for each date

DailyBookFile       
Date        Isbn13         CurrentModifiedDemandTotal
20180122    9780955153075   13
20180122    9780805863567   9
20180122    9781138779396   1
20180122    9780029001516   9
20180122    9780470614150   42

SalesRank       
importdate  ISBN13          SalesRank
20180122    9780029001516   69499
20180122    9780470614150   52879
20180122    9780805863567   832429
20180122    9780955153075   44528
20180122    9781138779396   926435

Required Output     
Date        Avg_Rank    Book_Group
20180122    385154  Elite
20180121    351545  Elite
20180120    201545  Elite

我想獲取每天的 Top 200 CurrentModifiedDemand,并取平均排名.

I want to get the Top 200 CurrentModifiedDemand for each day, and take the average Rank.

我無法找到解決方案,因為我是 SQL 新手.

I am unable to work out a solution as I am new to SQL.

我從昨天獲得了前 200 名 CurrentModifiedDemand 開始,然后獲得了去年的平均排名.

I started with getting the Top 200 CurrentModifiedDemand for yesterday and get the Avg Rank over last year.

SELECT DBF.Filedate AS [Date],
       AVG(AMA.SalesRank) AS Avg_Rank,
       'Elite' AS Book_Group 
FROM [ODS].[wholesale].[DailyBookFile] AS DBF
INNER JOIN [ODS].[MarketplaceMonitor].[SalesRank] AS AMA ON (DBF.Isbn13 = AMA.ISBN13
                                                        AND DBF.FileDate = AMA.importdate)
WHERE DBF.Isbn13 IN (SELECT TOP 200 Isbn13
                     FROM [ODS].[wholesale].[DailyBookFile]
                     WHERE FileDate = 20180122
                       AND CAST(CurrentModifiedDemandTotal AS int) > 200)
  AND DBF.Filedate > 20170101
GROUP BY DBF.Filedate;

但結果不是我想要的.所以,現在我想要每天前 200 名 CurrentModifiedDemand 的 ISBN 及其平均排名.我試過了.

But the result is not what I want. So, now I want the ISBN for the Top 200 CurrentModifiedDemand for each day and their avg rank. I tried with this.

DECLARE @i int;
SET @i = 20180122;
WHILE (SELECT DISTINCT(DBF.Filedate)
       FROM [ODS].[wholesale].[DailyBookFile] AS DBF
       WHERE DBF.Filedate = @i) IS NOT NULL
BEGIN

    SELECT DBF.Filedate AS [Date],
           AVG(AMA.SalesRank) AS Avg_Rank,
           'Elite' AS Book_Group 
    FROM [ODS].[wholesale].[DailyBookFile] AS DBF
    INNER JOIN [ODS].[MarketplaceMonitor].[SalesRank] as AMA ON DBF.Isbn13 = AMA.ISBN13
                                                            AND DBF.FileDate = AMA.importdate
    WHERE DBF.Isbn13 in (SELECT TOP 200 Isbn13
                         FROM [ODS].[wholesale].[DailyBookFile]
                         WHERE FileDate = @i
                           AND CAST (CurrentModifiedDemandTotal AS int) > 500)
      AND DBF.Filedate = @i
    GROUP BY DBF.Filedate;

    SET @i = @i+1;

END

在這里,我在每個窗口中得到一個選擇查詢結果.有沒有辦法把結果放在一個表中?

In this I am getting one select query result in each window. Is there any way to have the result in a single table?

附言每天前 200 本書的列表會根據 CurrentModifiedDemand 變化.我想取他們的平均值.當天的銷售排名.

P.S. The list of top 200 books every day will change according to the CurrentModifiedDemand. I want to take their avg. sales rank for that day.

推薦答案

您可以將行插入臨時表(或表類型變量)并在循環完成后選擇所有內容,而不是在循環的每次迭代中立即選擇:

Instead of immediately selecting in each iteration of the loop, you can insert rows to temp table (or table-type variable) and select everything after the loop finishes:

IF OBJECT_ID('tempdb..#books') IS NOT NULL
BEGIN
    DROP TABLE #books
END

CREATE TABLE #books (
    [Date] INT,
    [Avg_Rank] FLOAT,
    [Book_Group] VARCHAR(512)
);

DECLARE @i int;
SET @i = 20180122;

BEGIN TRY
WHILE (SELECT DISTINCT(DBF.Filedate)
    FROM [ODS].[wholesale].[DailyBookFile] AS DBF
    WHERE DBF.Filedate = @i) IS NOT NULL
BEGIN

    INSERT INTO #books (
        [Date],
        [Avg_Rank],
        [Book_Group]
    )
    SELECT DBF.Filedate AS [Date],
        AVG(AMA.SalesRank) AS Avg_Rank,
        'Elite' AS Book_Group 
    FROM [ODS].[wholesale].[DailyBookFile] AS DBF
    INNER JOIN [ODS].[MarketplaceMonitor].[SalesRank] as AMA ON DBF.Isbn13 = AMA.ISBN13
                                                            AND DBF.FileDate = AMA.importdate
    WHERE DBF.Isbn13 in (SELECT TOP 200 Isbn13
                        FROM [ODS].[wholesale].[DailyBookFile]
                        WHERE FileDate = @i
                        AND CAST (CurrentModifiedDemandTotal AS int) > 500)
    AND DBF.Filedate = @i
    GROUP BY DBF.Filedate;

    SET @i = @i+1;

END
END TRY
BEGIN CATCH
    IF OBJECT_ID('tempdb..#books') IS NOT NULL
    BEGIN
        DROP TABLE #books
    END
END CATCH

SELECT *
FROM #books

DROP TABLE #books

使用表類型變量會產生更簡單的代碼,但是當存儲大量數據時,表類型變量開始失去對臨時表的性能.我不確定有多少行是截止的,但根據我的經驗,我看到在 10000+ 行計數時將 table-type var 更改為 temp table 顯著提高了性能.對于小行數,可能適用相反的情況.

Using table-type variable would yield simpler code, but when storing large amounts of data table-type variables start losing in performance against temp tables. I'm not sure how many rows is a cut-off, but in my experience I've seen significant performance gains from changing table-type var to temp table at 10000+ row counts. For small row counts an opposite might apply.

這篇關于在單個表中使用 while 循環的多個選擇查詢?是否可以?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
Converting the name of a day to its integer representation(將一天的名稱轉換為其整數表示)
How to convert nvarchar m/d/yy to mm/dd/yyyy in SQL Server?(如何在 SQL Server 中將 nvarchar m/d/yy 轉換為 mm/dd/yyyy?)
主站蜘蛛池模板: 精品国产乱码久久久久久牛牛 | 日本精品999 | 欧美日韩精品一区 | 黄色av网站在线观看 | 91精品国产高清久久久久久久久 | 凹凸日日摸日日碰夜夜 | 精品一区在线 | 久久这里只有精品首页 | 久久成人av | 久久综合爱 | 久久一二 | 欧美日韩手机在线观看 | 精品一区二区三区在线播放 | 国产视频一二三区 | 一区二区在线免费观看 | 精品日韩一区二区 | 日本91av视频 | 成人做爰www免费看视频网站 | 欧美成年人视频在线观看 | 久久久片| 中文字幕一区二区三区四区五区 | 国产精品一区二区在线免费观看 | 亚洲一区二区三区免费在线 | 国产在线视频在线观看 | 精品久久久久久亚洲国产800 | 在线视频a | 亚洲综合久久久 | 成人免费xxxxx在线视频 | 中文字幕一区二区三区在线视频 | 国精日本亚洲欧州国产中文久久 | 国产一极毛片 | 国产精品久久久久久吹潮 | 免费在线观看黄网站 | 午夜网| 国产一区 | 亚洲精品久久久久久久久久久久久 | 国产99久久 | 日本午夜一区二区三区 | 日韩在线免费看 | 颜色网站在线观看 | 欧美日韩专区 |