c mysql使用場景_Mysql 場景

1個SQL題,1個場景題,會有點難度!

SQL題

699e901af112530385dc9137aed9cd8b.png

該SQL題大量涉及到row_number,case when,group by等高級用法,有一定的實用價值,總結出來,供日后參考

Question.1:

分組匯總

給定篩選條件

SELECT

Sales_Month

,Customer_ID

,Amount

FROM

(

SELECT

MONTH(Sales_Date) AS Sales_Month

,Customer_ID

,sum(Amount) AS Amount

FROM

Sales

GROUP BY

Sales_Month, Customer_ID

) AS A

WHERE

A.Amount BETWEEN 2000 AND 10000

Question.2:

全集合保留最大值所在行(針對天做處理)

為月維度下給定序列號(針對月做處理)

Group By + Case When 抽取特定值為一個維度

SELECT

B.Sales_month

,B.Customer_ID

,max( CASE WHEN B.nums = 1 THEN B.item ELSE NULL END ) AS Item1

,max( CASE WHEN B.nums = 2 THEN B.item ELSE NULL END ) AS Item2

,max( CASE WHEN B.nums = 3 THEN B.item ELSE NULL END ) AS Item3

FROM

(

SELECT

A.Sales_month

,A.Customer_ID

,A.item

,row_number() over (PARTITION BY A.Sales_month, A.Customer_ID

ORDER BY A.Sales_Date ) AS nums

FROM

(

SELECT

concat(YEAR (Sales_Date), '-',

MONTH(Sales_Date)) AS Sales_month

,Sales_Date

,Customer_ID

,item

,row_number() over (PARTITION BY Sales_Date, Customer_ID

ORDER BY Amount DESC ) AS nums

FROM

sales

) AS A

WHERE

A.nums = 1

) AS B

GROUP BY

B.Sales_month

,B.Customer_ID

ORDER BY

B.Sales_month

,B.Customer_ID

Question.3:

分別選取兩個月的集合,對item分類匯總

連接集合,并計算銷售額的差值

輸出類別,并根據差值跟定序號

SELECT

row_number() over(ORDER BY A.decrease_num DESC) AS Rank_

,A.Item as Item

,A.decrease_num AS MoM_Decrease

FROM

(

select

L.item

,(L.Amount-R.Amount) as decrease_num

from

(

SELECT

item

,sum(Amount) AS Amount

FROM

sales

WHERE

year(Sales_Date) =2018 and month(Sales_Date)=7

GROUP BY

Item

) AS L

inner join

(

SELECT

item

,sum(Amount) AS Amount

FROM

sales

WHERE

year(Sales_Date) =2018 and month(Sales_Date)=8

GROUP BY

Item

) AS R

ON L.item=R.item

) AS A

ORDER BY

Rank_ ASC

LIMIT 10

Question.4:

連續的表示方式:8月的每一天相對于7月的某一天以+1的方式線性增長,排序也是以+1的方式線性增長,連續情況下兩者之間的差值相等,對該差值計數即可知道不同的連續天數

計算日期排序的序號和日期相對于7月31日的差值

針對差值分類匯總,計算連續天數和起始日期

給出連續天數大于等于3的類別

SELECT

D.Customer_ID

,D.running_days

,D.start_date

,D.end_date

FROM

(

SELECT

C.Customer_ID

,C.diff_value

,min( C.Sales_Date ) AS start_date

,max( C.Sales_Date ) AS end_date

,count( 1 ) AS running_days

FROM

(

select

B.Customer_ID

,B.Sales_Date

,B.day_interval

,CONVERT(B.day_rank, SIGNED) as day_rank

,(B.day_interval-CONVERT(B.day_rank,SIGNED)) as diff_value

from

(

SELECT

A.Customer_ID

,A.Sales_Date

,datediff( A.Sales_Date, '2018-07-31' ) AS day_interval

,row_number( ) over(PARTITION BY A.Customer_ID

ORDER BY A.Sales_Date ) AS day_rank

FROM

(

select

distinct Sales_Date,Customer_ID

from

sales

) as A

where

Sales_Date>='2018-08-01'

and Sales_Date<='2018-08-31'

) AS B

) as C

GROUP BY

C.Customer_ID

,C.diff_value

) as D

where

D.running_days>=3

ORDER BY

D.Customer_ID

,D.start_date

場景題

有一個列的數據格式是1,2,500,4以逗號分隔數字,創建函數計算小于100數字的平均值

drop FUNCTION if EXISTS `AVG_answser_intval`;

delimiter $

CREATE DEFINER = CURRENT_USER FUNCTION `AVG_answser_intval`(Str VARCHAR(255))

RETURNS DECIMAL(8,2)

DETERMINISTIC

BEGIN

DECLARE Str_sum DECIMAL(8,2) DEFAULT 0.00;

DECLARE Str_con int DEFAULT 0;

DECLARE tmp_dot int;

DECLARE tmp_dec DECIMAL(8,2);

DECLARE result DECIMAL(8,2) DEFAULT 0.00;

while Str<>'' DO

set tmp_dot=LOCATE(',',Str);

IF tmp_dot<>0 THEN

set tmp_dec =CAST(SUBSTR(Str,1,tmp_dot-1)AS DECIMAL(8,2));

set Str_sum=Str_sum+if(tmp_dec <100,tmp_dec,0.0);

set Str=SUBSTR(Str,tmp_dot+1,LENGTH(Str)-tmp_dot);

set Str_con = Str_con+if(tmp_dec <100,1,0);

ELSE

set tmp_dec =CAST(Str AS DECIMAL(8,2));

set Str_sum=Str_sum+if(tmp_dec<100,tmp_dec,0.0);

set Str='';

set Str_con = Str_con+if(tmp_dec <100,1,0);

END IF;

END while;

set result = IF(Str_con>0,ROUND(Str_sum/Str_con,2),0);

RETURN result;

END$

delimiter ;

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542727.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542727.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542727.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

以己為壑

2019獨角獸企業重金招聘Python工程師標準>>> 今天把軟件工程里面關于面向對象的設計學完了,使我對面向對象OOA和OOD的思想有了進一步的認識,各科知識千溝萬壑,犬牙交錯,的確是這樣,能蒙住自己眼的永遠是你自己,而不是這個世界,因為美就在那里;裹住自己雙足的的永遠是…

macos安裝vscode_如何使用VSCode進行PostgreSQL開發及調試

Visual Studio Code (VSCode)是一個輕量級但功能強大的源代碼編輯器&#xff0c;可在桌面上運行&#xff0c;適用于Windows&#xff0c;macOS和Linux。 它內置了對JavaScript&#xff0c;TypeScript和Node.js的支持&#xff0c;并具有豐富的其他語言(如C&#xff0c;C&#xff…

最小生成樹 kruskal_使用Kruskal算法求解Java最小生成樹問題

最小生成樹 kruskalIn Electronic Circuit we often required less wiring to connect pins together. We can model this wiring problem with a connected, undirected graph G(V, E), where V is the set of pins, E is the set of possible interconnections between pair …

mysql數據庫面試題 軟件測試_軟件測試數據庫面試題一

前提本次分享只局限于 sql server 和 mysql 這兩種數據庫&#xff0c;其他數據庫暫不總結正文1. 對查詢的字段進行去重(distinct)用法注意&#xff1a;1. distinct【查詢字段】&#xff0c;必須放在要查詢字段的開頭&#xff0c;即放在第一個參數&#xff1b;2. 只能在SELECT 語…

python數碼時鐘代碼_python時鐘的實現

from time importsleepimporttimeimportosclassClock(object):"""數字時鐘""" def __init__(self, hour0, minute0, second0):"""初始化方法 :param hour: 時 :param minute: 分 :param second: 秒"""self._hourh…

PHP頁面跳轉

本文轉載自&#xff1a;http://blog.sina.com.cn/s/blog_9a06890901014ol1.html PHP頁面跳轉一、header()函數 header函數中Location類型的標頭是一種特殊的header調用&#xff0c;常用來實現頁面跳轉 注意&#xff1a;1、location和“:”號間不能有空格&#xff0c;否則不會跳…

如何打印出給定尺寸的方格_打印給定號碼的表格| 8086微處理器

如何打印出給定尺寸的方格Problem statement: 問題陳述&#xff1a; Write an assembly language program in 8086 to print the table of a given integer. 在8086中編寫匯編語言程序以打印給定整數的表。 Assumptions: Suppose the inputted number is at memory location …

python自動更新excel數據_如何更新Excel數據?(刷新所有查詢)

我有一個帶有一些查詢的Excel xlsm文件。目前我每天打開它&#xff0c;點擊“數據”選項卡中的“全部刷新”命令。我希望這件事能自動完成。我用python編寫了一個腳本(我是python新手)。問題是&#xff0c;刷新數據并保存Excel文件后&#xff0c;刷新的數據不可見(我知道刷新工…

mongoDB 使用手冊

2019獨角獸企業重金招聘Python工程師標準>>> 1、基本操作db.AddUser(username,password) 添加用戶db.auth(usrename,password) 設置數據庫連接驗證db.cloneDataBase(fromhost) 從目標服務器克隆一個數據庫db.commandHelp(name) returns the help for the commanddb.…

android搜索框功能實現_巧用 Trie 樹,實現搜索引擎關鍵詞提示功能

來源 | 碼海責編 | Carol封圖 | CSDN 付費下載于視覺中國我們幾乎每天都在用搜索引擎搜索信息&#xff0c;相信大家肯定有注意過這樣一個細節:當輸入某個字符的時候&#xff0c;搜索引框底下會出現多個推薦詞&#xff0c;如下&#xff0c;輸入「python」后&#xff0c;底下會出…

Python | 從用戶輸入數據,保存到文件,讀取并打印

Here, we have to input the data from the user, to read the data from user, we use input() method, and then the input data we have to store in the file by using write() method and then by using read() method we can get the data. 在這里&#xff0c;我們必須從…

python語句print type 1234的輸出結果是_Python語句 print(type(1J))的輸出結果是

【填空題】遍歷輸出文件所有行。 fopen("d:\\r2.txt","r") while True: str print(str,end) if not str: break f.close()【單選題】執行下列 Python語句將產生的結果是( ) i1 if (i): print(True) else: print( False)【單選題】Python語句 print(type(1/…

qt5.9.0調試如何查看變量的值_深入了解 Java 調試

Bug(俗稱"八阿哥") 是軟件開發繞不過的一道坎&#xff0c;因此調試便成了每位程序員一項必備的核心技能。調試不僅有助于理解程序的運行流程&#xff0c;還能改進代碼質量&#xff0c;最終提高開發者解決問題的能力以及交付軟件的品質。本文旨在討論 Java 調試關鍵技…

python字符串轉浮點數_Python | 打印不同的值(整數,浮點數,字符串,布爾值)...

python字符串轉浮點數In the given example, we are printing different values like integer, float, string and Boolean using print() method in python. 在給定的示例中&#xff0c;我們使用python中的print()方法打印不同的值&#xff0c;例如整數&#xff0c;浮點數&…

(6) 如何用Apache POI操作Excel文件-----POI-3.10的一個和注解(comment)相關的另外一個bug...

如果POI-3.10往一個工作表&#xff08;sheet&#xff09;里面插入數據的話&#xff0c;需要注意了&#xff0c;其有一個不太被容易發現的bug。 被插入的工作表&#xff08;sheet&#xff09;里面的單元格沒有包含任何的注解&#xff08;comment&#xff09;的時候&#xff0c;插…

mysql下拉刷新加載數據_下拉刷新、加載數據功能

paging nick加載更多getData();varm0,n2;//m:button點擊次數 n:一次加載幾條數據$(.page-btn-nick).click(getData);functiongetData(){$.ajax(paging.html).then(function(response){//測試url寫本頁面varobj{developer:[{name:nick},{name:ljy},{name:xzl},{name:jeson},{nam…

mcq 隊列_人工智能邏輯才能問答(MCQ)

mcq 隊列1) Why do we want to implement the concept of Logic in an AI system? So that the agent can have decision making capabilitySo that the agent can think and act humanlySo that the agent can apply the logic for finding the solution to any particular p…

第三周作業!

1、列出當前系統上所有已經登錄的用戶的用戶名&#xff0c;注意&#xff1a;同一個用戶登錄多次&#xff0c;則只顯示一次即可。答&#xff1a;本題思路&#xff1a;先用who命令列出當前登陸的用戶信息&#xff0c;然后使用cut命令對字段進行分割&#xff0c;選出我們需要的字段…

python導入模塊以及類_python模塊的導入以及模塊簡介

標簽&#xff1a; 一、模塊的定義及類型 1、定義 模塊就是用一堆的代碼實現了一些功能的代碼的集合&#xff0c;通常一個或者多個函數寫在一個.py文件里&#xff0c;而如果有些功能實現起來很復雜&#xff0c;那么就需要創建n個.py文件&#xff0c;這n個.py文件的集合就是模塊 …

mysql 指定數字排序_Mysql數據排序

排序數據普通字段排序按照單一字段排序按照多個字段排序手動指定排序順序單個字段手動排序多個字段手動排序普通字段排序按照單一字段排序排序采用order by子句&#xff0c;order by后面跟上排序字段&#xff0c;排序字段可以放多個&#xff0c;多個采用逗號間隔&#xff0c;or…