简体   繁体   English

SQL最后一天的选择

[英]SQL last day in moth select

I have the following table which is basically the summary of daily transactions and is loaded nightly. 我有下表,该表基本上是每日交易的摘要,并且每晚加载一次。

+++++++++++++++++++++++
+ DateCreated + Sale  +
+++++++++++++++++++++++
+ 20100101    + 1000  +
+ 20100131    + 2000  +
+ 20100210    + 2000  +
+ 20100331    + 4000  +
+++++++++++++++++++++++

I need to display the sale by month, but only for the last day in each month. 我需要按月显示销售情况,但仅显示每个月的最后一天。

eg 例如

JAN    2000
FEB       0
MAR    4000

I could probably accomplish this with CASE in my select, but is this efficient? 我可以选择CASE来完成此操作,但这有效吗? This is SQL Server 2000. 这是SQL Server 2000。

我对此的Q&D解决方案是构造一个日期,该日期是下个月的第一天,然后减去一天。

You could do something like this, but it will not show 0 for missing data: 您可以执行以下操作,但丢失数据不会显示0:

select YEAR(DateCreated) as Year, MONTH(DateCreated) as Month, Sale
from MyTable
where DAY(DateCreated + 1) = 1

I had a calendar table, just added a "IsLastDayOfMonth" column, set the last day of each month. 我有一个日历表,只是添加了一个“ IsLastDayOfMonth”列,设置了每个月的最后一天。 Then joined that to my query, filtering by "IsLastDayOfMonth" only and that worked. 然后将其加入我的查询中,仅通过“ IsLastDayOfMonth”进行过滤,并且可以正常工作。

select a.datecreated,a.sale
         from salesummary a
 inner join calendar c
on a.datecreated = c.fulldatekey
 and c.IsLastDayOfMonth = 1
group by a.datecreated,a.sale

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM