日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務,提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了如何每周獲取前十大數(shù)據(jù)(本周、前一周、上個月、2個月前、3個月前)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想檢索排名前十的產(chǎn)品-每周

Rank     This week product   Previous week rank   Last month rank   2 month ago rank     3 month ago rank
 1             Watch                  2                    3                    1               4
 2             Radio                  3                    2                    4               5
 3             Pen                    4                    5                    6               7
 4
 5
 6
 7
 8
 9
 10

請幫助我了解如何從SQL查詢中獲取此類數(shù)據(jù)。謝謝

推薦答案

假設您有一個表:

CREATE TABLE your_table (
  product          VARCHAR2(20),
  some_date_column DATE
)

然后您可以在RANK分析函數(shù)中使用條件聚合:

SELECT product,
       RANK() OVER (
         ORDER BY COUNT(
           CASE
           WHEN some_date_column >= TRUNC(SYSDATE, 'IW')
           AND  some_date_column <  TRUNC(SYSDATE, 'IW') + INTERVAL '7' DAY
           THEN 1
           END
         ) DESC
       ) AS rank_this_week,
       RANK() OVER (
         ORDER BY COUNT(
           CASE
           WHEN some_date_column >= TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY
           AND  some_date_column <  TRUNC(SYSDATE, 'IW')
           THEN 1
           END
         ) DESC
       ) AS rank_last_week,
       RANK() OVER (
         ORDER BY COUNT(
           CASE
           WHEN some_date_column >= ADD_MONTHS(TRUNC(SYSDATE, 'IW'), -1)
           AND  some_date_column <  TRUNC(SYSDATE, 'MM')
           THEN 1
           END
         ) DESC
       ) AS rank_last_month,
       RANK() OVER (
         ORDER BY COUNT(
           CASE
           WHEN some_date_column >= ADD_MONTHS(TRUNC(SYSDATE, 'IW'), -2)
           AND  some_date_column <  ADD_MONTHS(TRUNC(SYSDATE, 'IW'), -1)
           THEN 1
           END
         ) DESC
       ) AS rank_two_month,
       RANK() OVER (
         ORDER BY COUNT(
           CASE
           WHEN some_date_column >= ADD_MONTHS(TRUNC(SYSDATE, 'IW'), -3)
           AND  some_date_column <  ADD_MONTHS(TRUNC(SYSDATE, 'IW'), -2)
           THEN 1
           END
         ) DESC
       ) AS rank_three_month
FROM   your_table
WHERE  some_date_column < TRUNC(SYSDATE, 'IW') + INTERVAL '7' DAY
AND    some_date_column >= ADD_MONTHS(TRUNC(SYSDATE, 'IW'), -3)
GROUP BY product
ORDER BY rank_this_week
FETCH FIRST 10 ROWS WITH TIES;

對于樣本數(shù)據(jù):

INSERT INTO your_table (product, some_date_column)
SELECT 'A', TRUNC(SYSDATE, 'IW') FROM DUAL CONNECT BY LEVEL <=  1 UNION ALL
SELECT 'B', TRUNC(SYSDATE, 'IW') FROM DUAL CONNECT BY LEVEL <=  2 UNION ALL
SELECT 'C', TRUNC(SYSDATE, 'IW') FROM DUAL CONNECT BY LEVEL <=  3 UNION ALL
SELECT 'D', TRUNC(SYSDATE, 'IW') FROM DUAL CONNECT BY LEVEL <=  4 UNION ALL
SELECT 'E', TRUNC(SYSDATE, 'IW') FROM DUAL CONNECT BY LEVEL <=  5 UNION ALL
SELECT 'F', TRUNC(SYSDATE, 'IW') FROM DUAL CONNECT BY LEVEL <=  6 UNION ALL
SELECT 'G', TRUNC(SYSDATE, 'IW') FROM DUAL CONNECT BY LEVEL <=  7 UNION ALL
SELECT 'H', TRUNC(SYSDATE, 'IW') FROM DUAL CONNECT BY LEVEL <=  8 UNION ALL
SELECT 'I', TRUNC(SYSDATE, 'IW') FROM DUAL CONNECT BY LEVEL <=  9 UNION ALL
SELECT 'J', TRUNC(SYSDATE, 'IW') FROM DUAL CONNECT BY LEVEL <= 10 UNION ALL
SELECT 'K', TRUNC(SYSDATE, 'IW') FROM DUAL CONNECT BY LEVEL <= 11 UNION ALL
SELECT 'A', TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY FROM DUAL CONNECT BY LEVEL <=  3 UNION ALL
SELECT 'B', TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY FROM DUAL CONNECT BY LEVEL <=  3 UNION ALL
SELECT 'C', TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY FROM DUAL CONNECT BY LEVEL <=  1 UNION ALL
SELECT 'D', TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY FROM DUAL CONNECT BY LEVEL <=  2 UNION ALL
SELECT 'E', TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY FROM DUAL CONNECT BY LEVEL <=  5 UNION ALL
SELECT 'F', TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY FROM DUAL CONNECT BY LEVEL <=  7 UNION ALL
SELECT 'G', TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY FROM DUAL CONNECT BY LEVEL <=  6 UNION ALL
SELECT 'H', TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY FROM DUAL CONNECT BY LEVEL <=  4 UNION ALL
SELECT 'I', TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY FROM DUAL CONNECT BY LEVEL <=  5 UNION ALL
SELECT 'J', TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY FROM DUAL CONNECT BY LEVEL <=  3 UNION ALL
SELECT 'K', TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY FROM DUAL CONNECT BY LEVEL <=  2
-- Etc.

(注意:這只是最近兩周的數(shù)據(jù),因此所有其他排名將并列。)

輸出:

這篇關于如何每周獲取前十大數(shù)據(jù)(本周、前一周、上個月、2個月前、3個月前)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:2monthsago 3monthago) errorHowtogettop10dataweekly(Thisweek excepti
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定