计算互相关注的对数
id1 id2
a b 表示a关注b
select
a.fromID,
b.toID,
if(b.fromID is not null,'Y','N') as is_friend
from table a left join table b on a.fromID = b.toID and a.toID = b.fromID
计算每个视频类型下面播放量前10的视频id
type id timestamp
select
type,
id
from
(
select
type,
id,
row_number()over(partition by type order by counts desc) as rn
from
(
select
id,
type,
count(timestamp) as counts
from
table
group by id,type
) a
)
where a.rn <= 10
;
拉链表
SELECT *
FROM (
SELECT old.数据
,old.开始时间
,CASE WHEN new.id IS NULL THEN old.结束时间
ELSE '20220901'
END AS 结束时间
FROM old_table old
LEFT JOIN new_table new
ON old.id = new.id
UNION ALL
SELECT new.数据
,'20220901' AS 开始时间
,'99999999' AS 结束时间
FROM new_table NEW
)
;
求直播间最大在线人数
room_id user_id start_time end_time
设置一个flag,用户每进入一次,flag设为1,每退出一次,flag设为-1
select
room_id,
max(uv) as max_uv
from
(
select
room_id,
flag_time,
sum(flag)over(partition by room_id order by flag_time) as uv
from
(
select
room_id,
start_time as flag_time,
1 as flag
from
temp
union all
select
room_id,
end_time as flag_time,
-1 as flag
from
temp
) a
) b
group by
room_id
;
行转列
CREATE TABLE [Inpours]
(
[ID] INT IDENTITY(1,1),
[UserName] NVARCHAR(20), --游戏玩家
[CreateTime] DATETIME, --充值时间
[PayType] NVARCHAR(20), --充值类型
[Money] DECIMAL, --充值金额
[IsSuccess] BIT, --是否成功 1表示成功, 0表示失败
CONSTRAINT [PK_Inpours_ID] PRIMARY KEY(ID)
)
INSERT INTO Inpours SELECT '张三', '2010-05-01', '支付宝', 50, 1
INSERT INTO Inpours SELECT '张三', '2010-06-14', '支付宝', 50, 1
INSERT INTO Inpours SELECT '张三', '2010-06-14', '手机短信', 100, 1
INSERT INTO Inpours SELECT '李四', '2010-06-14', '手机短信', 100, 1
INSERT INTO Inpours SELECT '李四', '2010-07-14', '支付宝', 100, 1
INSERT INTO Inpours SELECT '王五', '2010-07-14', '工商银行卡', 100, 1
INSERT INTO Inpours SELECT '赵六', '2010-07-14', '建设银行卡', 100, 1
SELECT CONVERT(VARCHAR(10), CreateTime, 120) AS CreateTime,
CASE PayType WHEN '支付宝' THEN SUM(Money) ELSE 0 END AS '支付宝',
CASE PayType WHEN '手机短信' THEN SUM(Money) ELSE 0 END AS '手机短信',
CASE PayType WHEN '工商银行卡' THEN SUM(Money) ELSE 0 END AS '工商银行卡',
CASE PayType WHEN '建设银行卡' THEN SUM(Money) ELSE 0 END AS '建设银行卡'
FROM Inpours
GROUP BY CreateTime, PayType
列转行
CREATE TABLE ProgrectDetail
(
ProgrectName NVARCHAR(20), --工程名称
OverseaSupply INT, --海外供应商供给数量
NativeSupply INT, --国内供应商供给数量
SouthSupply INT, --南方供应商供给数量
NorthSupply INT --北方供应商供给数量
)
INSERT INTO ProgrectDetail
SELECT 'A', 100, 200, 50, 50
UNION ALL
SELECT 'B', 200, 300, 150, 150
UNION ALL
SELECT 'C', 159, 400, 20, 320
UNION ALL
SELECT 'D', 250, 30, 15, 15
SELECT ProgrectName, 'OverseaSupply' AS Supplier,
MAX(OverseaSupply) AS 'SupplyNum'
FROM ProgrectDetail
GROUP BY ProgrectName
UNION ALL
SELECT ProgrectName, 'NativeSupply' AS Supplier,
MAX(NativeSupply) AS 'SupplyNum'
FROM ProgrectDetail
GROUP BY ProgrectName
UNION ALL
SELECT ProgrectName, 'SouthSupply' AS Supplier,
MAX(SouthSupply) AS 'SupplyNum'
FROM ProgrectDetail
GROUP BY ProgrectName
UNION ALL
SELECT ProgrectName, 'NorthSupply' AS Supplier,
MAX(NorthSupply) AS 'SupplyNum'
FROM ProgrectDetail
GROUP BY ProgrectName
求任意一门课成绩大于均分的学生
id,course,score
select
id
from
(
select
id,
course,
score,
avg()over(partition by course) as avgscore
from
(
select
id,
course,
score
from
table
)
)
where score >= avgscore
group by id
;
求每门课成绩均大于均分的学生
id,course,score
select
id,
sum(flag) as isqualified
from
(
select
id,
course,
score,
avgscore
if(score >= avgscore,0,1) as flag
from
(
select
id,
course,
score,
avg()over(partition by course) as avgscore
from
(
select
id,
course,
score
from
table
)
)
)
group by id
having sum(flag)=0
;
求股票的波峰和波谷(非最大值和最小值)
id,code,time,price
id--自增序列 code--股票代码 time--时间 price--价格
原表stocks:
+----+------+---------------------+-------+
| id | code | time | price |
+----+------+---------------------+-------+
| 1 | 1 | 2017-12-14 12:03:06 | 12 |
| 2 | 1 | 2017-12-14 12:03:07 | 11 |
| 3 | 1 | 2017-12-14 12:03:08 | 13 |
| 4 | 1 | 2017-12-14 12:03:09 | 10 |
| 5 | 1 | 2017-12-14 12:03:10 | 15 |
| 6 | 2 | 2017-12-14 12:03:06 | 12 |
| 7 | 2 | 2017-12-14 12:03:07 | 14 |
| 8 | 2 | 2017-12-14 12:03:08 | 13 |
| 9 | 2 | 2017-12-14 12:03:09 | 10 |
| 10 | 2 | 2017-12-14 12:03:10 | 15 |
+----+------+---------------------+-------+
tmp_stocks:
select
code,
time,
price,
row_number()over(partition by code order by time) as rn
from
stocks
;
+------+---------------------+-------+----+
| code | time | price | rn |
+------+---------------------+-------+----+
| 1 | 2017-12-14 12:03:06 | 12 | 1 |
| 1 | 2017-12-14 12:03:07 | 11 | 2 |
| 1 | 2017-12-14 12:03:08 | 13 | 3 |
| 1 | 2017-12-14 12:03:09 | 10 | 4 |
| 1 | 2017-12-14 12:03:10 | 15 | 5 |
| 2 | 2017-12-14 12:03:06 | 12 | 1 |
| 2 | 2017-12-14 12:03:07 | 14 | 2 |
| 2 | 2017-12-14 12:03:08 | 13 | 3 |
| 2 | 2017-12-14 12:03:09 | 10 | 4 |
| 2 | 2017-12-14 12:03:10 | 15 | 5 |
+------+---------------------+-------+----+
中间表
select
*
from
tmp_stocks a
left join
tmp_stocks b
on b.rn = a.rn-1 and a.code = b.code
left join
tmp_stocks c
on c.rn = a.rn+1 and a.code = c.code
;
code | time | price | rn | code | time | price | rn | code | time | price | rn |
+------+---------------------+-------+----+-------+---------------------+-------+-------+-------+---------------------+-------+-------+
| 1 | 2017-12-14 12:03:06 | 12 | 1 | <nil> | <nil> | <nil> | <nil> | 1 | 2017-12-14 12:03:07 | 11 | 2 |
| 1 | 2017-12-14 12:03:07 | 11 | 2 | 1 | 2017-12-14 12:03:06 | 12 | 1 | 1 | 2017-12-14 12:03:08 | 13 | 3 |
| 1 | 2017-12-14 12:03:08 | 13 | 3 | 1 | 2017-12-14 12:03:07 | 11 | 2 | 1 | 2017-12-14 12:03:09 | 10 | 4 |
| 1 | 2017-12-14 12:03:09 | 10 | 4 | 1 | 2017-12-14 12:03:08 | 13 | 3 | 1 | 2017-12-14 12:03:10 | 15 | 5 |
| 1 | 2017-12-14 12:03:10 | 15 | 5 | 1 | 2017-12-14 12:03:09 | 10 | 4 | <nil> | <nil> | <nil> | <nil> |
| 2 | 2017-12-14 12:03:06 | 12 | 1 | <nil> | <nil> | <nil> | <nil> | 2 | 2017-12-14 12:03:07 | 14 | 2 |
| 2 | 2017-12-14 12:03:07 | 14 | 2 | 2 | 2017-12-14 12:03:06 | 12 | 1 | 2 | 2017-12-14 12:03:08 | 13 | 3 |
| 2 | 2017-12-14 12:03:08 | 13 | 3 | 2 | 2017-12-14 12:03:07 | 14 | 2 | 2 | 2017-12-14 12:03:09 | 10 | 4 |
| 2 | 2017-12-14 12:03:09 | 10 | 4 | 2 | 2017-12-14 12:03:08 | 13 | 3 | 2 | 2017-12-14 12:03:10 | 15 | 5 |
| 2 | 2017-12-14 12:03:10 | 15 | 5 | 2 | 2017-12-14 12:03:09 | 10 | 4 | <nil> | <nil> | <nil> | <nil> |
select a.code,a.time,a.price,
case when b.price is null then "未知"
when c.price is null then "未知"
when a.price>b.price and a.price>c.price then "波峰"
when a.price<b.price and a.price<c.price then "波谷"
else "中间" end as mark,a.rn
from tmp_stocks a
left join
tmp_stocks b on a.code=b.code and b.rn=a.rn-1
left join
tmp_stocks c on a.code=c.code and c.rn=a.rn+1 order by a.code,a.rn;
+------+---------------------+-------+--------+----+
| code | time | price | mark | rn |
+------+---------------------+-------+--------+----+
| 1 | 2017-12-14 12:03:06 | 12 | 未知 | 1 |
| 1 | 2017-12-14 12:03:07 | 11 | 波谷 | 2 |
| 1 | 2017-12-14 12:03:08 | 13 | 波峰 | 3 |
| 1 | 2017-12-14 12:03:09 | 10 | 波谷 | 4 |
| 1 | 2017-12-14 12:03:10 | 15 | 未知 | 5 |
| 2 | 2017-12-14 12:03:06 | 12 | 未知 | 1 |
| 2 | 2017-12-14 12:03:07 | 14 | 波峰 | 2 |
| 2 | 2017-12-14 12:03:08 | 13 | 中间 | 3 |
| 2 | 2017-12-14 12:03:09 | 10 | 波谷 | 4 |
| 2 | 2017-12-14 12:03:10 | 15 | 未知 | 5 |
+------+---------------------+-------+--------+----+