查看: 1412| 回复: 9
跳转到指定楼层
上一主题 下一主题
收起左侧

[其他] 请教一道SQL的题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
最近看了一些公司的面经,里面有些SQL 的题我都不太会,想请教大佬。

给了table, 让输出交易间隔10秒的用户。谢谢了
Id customer_nametransaction-time
1AAA 2017-01-01 10:10:15
2 BBB 2017-01-01 11:11:11
3CCC 2017-01-01 12:12:12
4 AAA 2017-01-01 10:10:20
5 AAA 2017-01-01 10:10:30
6 CCC 2017-01-01 12:12:22


上一篇:indices of second larger number on the right
下一篇:刷题准备碰到了点疑难题目,求高手指点下
推荐
xiaobeixin 2019-11-15 14:01:46 | 只看该作者
全局:
张望堃 发表于 2019-11-15 13:38
请问1=1是什么意思呀

没有意义,因为1=1 always true,只是写sql的一个best practice或者说trick,如果注释掉某个filter, 不用考虑落单的and。

比如注释掉第一个filter,直接在前面加 -- 就好了
select distinct a.customer_name
from customers a inner join customers b
where 1=1
-- and a.customer_name = b.customer_name
and a.transaction_time = b.transaction_time - 10

如果不加1=1,注释掉第一个filter后,第二个filter前面有个and,就会有syntax error,还得手动删掉and。等加回来这个filter,还得把and 加回来
select distinct a.customer_name
from customers a inner join customers b
where
-- a.customer_name = b.customer_name
and a.transaction_time = b.transaction_time - 10
回复

使用道具 举报

推荐
xiaobeixin 2019-11-15 13:26:55 | 只看该作者
全局:
本帖最后由 xiaobeixin 于 2019-11-15 13:28 编辑

select distinct a.customer_name --, a.transaction_time as first_transaction, b.transaction_time as second_transaction
from customers a inner join customers b
where 1=1
and a.customer_name = b.customer_name
and a.transaction_time = b.transaction_time - 10

第一个column就是customer_name, 后面两个column debug用,所以comment out了

回复

使用道具 举报

🔗
亚麻农民 2019-11-15 13:38:41 | 只看该作者
全局:
xiaobeixin 发表于 2019-11-15 13:26
select distinct a.customer_name --, a.transaction_time as first_transaction, b.transaction_time as s ...

请问1=1是什么意思呀
回复

使用道具 举报

🔗
 楼主| wawsst1988 2019-11-15 20:42:02 | 只看该作者
全局:
xiaobeixin 发表于 2019-11-15 13:26
select distinct a.customer_name --, a.transaction_time as first_transaction, b.transaction_time as s ...

大神能直接po一下mysql怎么写的吗?还是有点不清楚,谢谢
回复

使用道具 举报

全局:
学习了……………
回复

使用道具 举报

🔗
Shawnh19 2019-11-20 01:12:33 | 只看该作者
全局:
I run this code :select distinct a.customer_name --, a.transaction_time as first_transaction, b.transaction_time as second_transaction
from customers a inner join customers b
where 1=1
and a.customer_name = b.customer_name
and a.transaction_time = b.transaction_time - 10

But look like not show any Customer_Nmae, why?
回复

使用道具 举报

🔗
Shawnh19 2019-11-20 01:51:35 | 只看该作者
全局:
I modify code like below , and test run seem to work:
Select distinct a.CustomerName,
        a.transaction_time as first_transaction,
         b.transaction_time as second_transaction,
         a.transaction_time -b.transaction_time  as diff
from dbo.Customers a inner join dbo.Customers b
On 1=1
and a.customerName = b.CustomerName
where
a.transaction_time != b.transaction_time
and a.transaction_time -b.transaction_time ='1900-01-01 00:00:10.000'

Show 2 records:
CustomerName        first_transaction        second_transaction        diff
AAA        2017-01-01 10:10:30.000        2017-01-01 10:10:20.000        1900-01-01 00:00:10.000
CCC        2017-01-01 12:12:22.000        2017-01-01 12:12:12.000        1900-01-01 00:00:10.000
回复

使用道具 举报

全局:
可以多刷刷leetcode上面的题,好多这种self join的题
回复

使用道具 举报

🔗
Shawnh19 2019-11-20 07:04:59 | 只看该作者
全局:
本帖最后由 Shawnh19 于 2019-11-20 07:19 编辑

I rewrite code, not use self join use LEAD window function, I think much better?:
Declare @TimeDiff int
Set @TimeDiff = 10;

With CTE_NextTime
As(
Select Id, CustomerName,transaction_time,
Lead(Transaction_time,1,0) over (partition by customerName order by transaction_time)
as NextTransTime
From dbo.Customers)
Select CustomerName, transaction_time,NextTransTime,
Datepart(second,NextTransTime - transaction_time) as Diff
From CTE_NextTime
Where Datepart(second,NextTransTime - transaction_time) = @TimeDiff
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表