197. Rising Temperature--DATEDIFF()

题目:

Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.

+---------+------------+------------------+
| Id(INT) | Date(DATE) | Temperature(INT) |
+---------+------------+------------------+
|       1 | 2015-01-01 |               10 |
|       2 | 2015-01-02 |               25 |
|       3 | 2015-01-03 |               20 |
|       4 | 2015-01-04 |               30 |
+---------+------------+------------------+
For example, return the following Ids for the above Weather table:
+----+
| Id |
+----+
|  2 |
|  4 |
+----+


MySQL uses DATEDIFF to compare two date type values.

SELECT
    weather.id AS 'Id'
FROM
    weather
        JOIN
    weather w ON DATEDIFF(weather.date, w.date) = 1
        AND weather.Temperature > w.Temperature


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SELECT a.[Sub-Region], a.[SBU], a.[CompanyCtry], a.[Inv_CM], b.[Inv_LM], ROW_NUMBER() OVER ( PARTITION BY a.[Sub-Region], a.[SBU] ORDER BY ABS(a.[Inv_CM] - b.[Inv_LM]) DESC ) as RowNo INTO [AP_Inventory].[dbo].[Temp_vs_CY_LM] FROM ( SELECT [Sub-Region], [SBU_2023] as [SBU], [CompanyCtry], SUM([TotalInv_EUR]) as [Inv_CM] FROM [AP_Inventory].[dbo].[Inv_Details_for_PowerBI] WHERE DATEDIFF(MONTH, DATEFROMPARTS([ReportYear], [ReportMonth], 1), GETDATE()) = 1 and [StockCategory] <> 'Vendor Consignment' and [CompanyCtry] is not null GROUP BY [Sub-Region], [SBU_2023], [CompanyCtry] ) as a, ( SELECT [Sub-Region], [SBU_2023] as [SBU], [CompanyCtry], SUM([TotalInv_EUR]) as [Inv_LM] FROM [AP_Inventory].[dbo].[Inv_Details_for_PowerBI] WHERE DATEDIFF(MONTH, DATEFROMPARTS([ReportYear], [ReportMonth], 1), GETDATE()) = 2 and [StockCategory] <> 'Vendor Consignment' and [CompanyCtry] is not null GROUP BY [Sub-Region], [SBU_2023], [CompanyCtry] ) as b WHERE a.[Sub-Region] = b.[Sub-Region] and a.[SBU] = b.[SBU] and a.[CompanyCtry] = b.[CompanyCtry]; GO UPDATE a SET a.[vs_CY_LM_Content] = b.[CompanyCtry], a.[vs_CY_LM_Content_Qty] = b.[Inv_CM], a.[vs_CY_LM_Delta_Qty] = b.[Inv_CM] - b.[Inv_LM] FROM [AP_Inventory].[dbo].[Calculate_Top_Variation_V4] as a, [AP_Inventory].[dbo].[Temp_vs_CY_LM] as b WHERE a.[Organization] = b.[Sub-Region] and a.[SBU] = b.[SBU] and a.[Dimension] = '[Country]' and a.[vs_CY_LM_Rank] = b.[RowNo] and DATEDIFF(MONTH, DATEFROMPARTS(a.[Report_Year], a.[Report_Month], 1), GETDATE()) = 1; GO DROP TABLE [AP_Inventory].[dbo].[Temp_vs_CY_LM]; GO
最新发布
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值