數據挖掘鄙視題-數據庫(查詢)

1、如何寫sql查詢語句查找11位手機號碼所有後四位尾數符合AABB或者ABAB或者AAAA形式的電話號碼?
設表PhoneNum

select phone from PhoneNum 
where (SUBSTRING(phone, 11)=SUBSTRING(phone, 10, 1)
       and 
       SUBSTRING(phone, 9, 1)=SUBSTRING(phone, 8, 1)
       )
or (SUBSTRING(phone, 11)=SUBSTRING(phone, 9, 1)
    and 
    SUBSTRING(phone, 10, 1)=SUBSTRING(phone, 8, 1)
    )

2、 用一條SQL 語句 查詢出每門課都大於80 分的學生姓名

name kecheng fenshu
張三 語文 81
張三 數學 75
李四 語文 76
李四 數學 90
王五 語文 81
王五 數學 100
王五 英語 90
--- 解法1 ------
select name from table
group by name having min(fenshu)>80

--- 解法2 ------
select distinct name from table 
where name not in (select distinct name from table where fenshu<=80)

3、刪除除了自動編號不同, 其他都相同的學生冗餘信息

自動編號 學號 姓名 課程編號 課程名稱 分數
1 2005001 張三 0001 數學 69
2 2005002 李四 0001 數學 89
3 2005001 張三 0001 數學 69
delete tablename 
where 自動編號 not in(select min( 自動編號) 
                     from tablename 
                     group by 學號, 姓名, 課程編號, 課程名稱, 分數)

4、從TestDB 數據表中查詢出所有月份的發生額都比101 科目相應月份的發生額高的科目。請注意:TestDB 中有很多科目,都有1 -12 月份的發生額。
AccID :科目代碼,Occmonth :發生額月份,DebitOccur :發生額。
數據庫名:JcyAudit ,數據集:Select * from TestDB

select *
from TestDB as a 
,(select Occmonth,max(DebitOccur) Debit101ccur 
  from TestDB 
  where AccID='101' 
  group by Occmonth) b
where a.Occmonth=b.Occmonth 
and a.DebitOccur>b.Debit101ccur

5、選出6、7、8月份電話某月消費在51到100之間,且在9、10月份消費均爲0的用戶。

SELECT DISTINCT ID
FROM A 
WHERE (COST BETWEEN 50 AND 100
       AND ID NOT IN (SELECT distinct ID FROM a
                     WHERE (MONTH = 9 AND COST != 0)
                     OR (MONTH = 10 AND COST != 0)
                     )
       ) 

6、選出6、7、8月份電話消費均在51到100之間,且在9、10月份消費均爲0的用戶。(目前個人想到的是窮舉)

7、 將下表B進行dcast操作

year month amount
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4

查成這樣一個結果

year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
select year, 
(select amount from B m where month=1 and m.year=B.year) as m1,
(select amount from B m where month=2 and m.year=B.year) as m2,
(select amount from B m where month=3 and m.year=B.year) as m3,
(select amount from B m where month=4 and m.year=B.year) as m4
from B group by year

8、表A結構如下:
Member_ID (用戶的ID,字符型)
Log_time (用戶訪問頁面時間,日期型(只有一天的數據))
URL (訪問的頁面地址,字符型)
要求:提取出每個用戶訪問的第一個URL(按時間最早),形成一個新表(新表名爲B,表結構和表A一致)

create table B as 
select Member_ID, min(Log_time), URL 
from A 
group by Member_ID ;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章