SQL每個月份的發生額都比101科目多的科目

2021-06-02 06:20:21 字數 2397 閱讀 3699

請用sql語句實現:從testdb資料表中查詢出所有月份的發生額都比101科目相應月份的發生額高的科目。請注意:testdb中有很多科目,都有1-12月份的發生額。

accid:科目**,occmonth:發生額月份,debitoccur:發生額。

資料庫名:jcyaudit,資料集:select * from testdb

準備資料的sql**:

drop table if exists testdb;

create table testdb(id int primary key auto_increment,accid

varchar(20), occmonth date, debitoccur bigint);

insert into testdb values

(null,'101','1988-1-1',100),

(null,'101','1988-2-1',110),

(null,'101','1988-3-1',120),

(null,'101','1988-4-1',100),

(null,'101','1988-5-1',100),

(null,'101','1988-6-1',100),

(null,'101','1988-7-1',100),

(null,'101','1988-8-1',100);

--複製上面的資料,故意把第乙個月份的發生額數字改小一點

insert into testdb values

(null,'102','1988-1-1',90),

(null,'102','1988-2-1',110),

(null,'102','1988-3-1',120),

(null,'102','1988-4-1',100),

(null,'102','1988-5-1',100),

(null,'102','1988-6-1',100),

(null,'102','1988-7-1',100),

(null,'102','1988-8-1',100);

--複製最上面的資料,故意把所有發生額數字改大一點

insert into testdb values

(null,'103','1988-1-1',150),

(null,'103','1988-2-1',160),

(null,'103','1988-3-1',180),

(null,'103','1988-4-1',120),

(null,'103','1988-5-1',120),

(null,'103','1988-6-1',120),

(null,'103','1988-7-1',120),

(null,'103','1988-8-1',120);

--複製最上面的資料,故意把所有發生額數字改大一點

insert into testdb values

(null,'104','1988-1-1',130),

(null,'104','1988-2-1',130),

(null,'104','1988-3-1',140),

(null,'104','1988-4-1',150),

(null,'104','1988-5-1',160),

(null,'104','1988-6-1',170),

(null,'104','1988-7-1',180),

(null,'104','1988-8-1',140);

--複製最上面的資料,故意把第二個月份的發生額數字改小一點

insert into testdb values

(null,'105','1988-1-1',100),

(null,'105','1988-2-1',80),

(null,'105','1988-3-1',120),

(null,'105','1988-4-1',100),

(null,'105','1988-5-1',100),

(null,'105','1988-6-1',100),

(null,'105','1988-7-1',100),

(null,'105','1988-8-1',100);

答案:select distinct accid from testdb

where accid not in

(select testdb.accidfrom testdb,

(select * from testdb where accid='101') as db101

where testdb.occmonth=db101.occmonth and

testdb.debitoccur<=db101.debitoccur

);

mysql查詢每個月的資料

select create time,from unixtime create time as create date,month from unixtime create time as monthno,year from unixtime create time as myyear,title ...

6 2 輸出每個月的天數

本題要求實現乙個簡單函式,能計算給定的年份和月份的天數。使得可以利用該函式,輸出給定年份中每個月的天數。其中1 3 5 7 8 10 12月有31天,4 6 9 11月有30天,2月平年有28天,閏年有29天。判斷閏年的條件是 能被 4 整除但不能被 100 整除,或者能被 400 整除。函式介面定...

統計每個月兔子的總數

有乙隻兔子,從出生後第3個月起每個月都生乙隻兔子,小兔子長到第三個月後每個月又生乙隻兔子,假如兔子都不死,問每個月的兔子總數為多少?這個問題可能我比較笨,看大多數解釋都是一句話,f n f n 1 f n 2 但是總有點想不明白這個。列了個 才看清楚咋回事。月份1 2345 67兔子總數11 235...