简体   繁体   English

如何在日期范围内执行SQL查询

[英]How to perform sql query with date range

I have this SQL Server tables: 我有此SQL Server表:

在此处输入图片说明

I need to group together each category of streams and show the user an overview of the last weeks, as evidenced in this image: 我需要将流的每个类别组合在一起,并向用户显示最近几周的概述,如以下图片所示:

在此处输入图片说明

This datatable interrogates a web service that, in turn, queries the database. 该数据表查询一个Web服务,该Web服务又查询数据库。

The color of checkmarks depends on the value CodState : if there is also one state equal 4 ('Denied') then the checkmark is red. 复选标记的颜色取决于值CodState :如果还存在一个等于4的状态(“拒绝”),则复选标记为红色。 Otherwise, is green. 否则为绿色。

Please note that the Date field DetailsStream corresponds to a certain day. 请注意,日期字段DetailsS​​tream对应于某一天。

The problem is the formulation of the query with different ranges of dates (in the picture: the latest five weeks, from Monday to Friday). 问题是使用不同的日期范围 (如图所示:最近的五个星期,从星期一到星期五)来制定查询

EDIT 编辑

As suggested, my tables schemas : 按照建议,我的表架构

-- Category:
CREATE TABLE [dbo].[StreamCategory](
    [Cod] [int] NOT NULL,
    [Name] [varchar](50) NOT NULL,
 CONSTRAINT [PK_StreamCategory] PRIMARY KEY CLUSTERED 
(
    [Cod] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]



-- Stream
CREATE TABLE [dbo].[Stream](
    [Id] [int] IDENTITY (1, 1) NOT NULL,
    [Name] [varchar](100) NOT NULL,
    [TypeStream] [varchar](15) NOT NULL,
    [CodCategory] [int] NOT NULL,
 CONSTRAINT [PK_Stream] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


ALTER TABLE [dbo].[Stream]  WITH CHECK ADD  CONSTRAINT [FK_Stream_StreamCategory] FOREIGN KEY([CodCategoria])
REFERENCES [dbo].[StreamCategory] ([Cod])
GO

ALTER TABLE [dbo].[Stream] CHECK CONSTRAINT [FK_Stream_StreamCategory]
GO



-- State Details stream
CREATE TABLE [dbo].[StateDetailsStream](
    [Cod] [int] NOT NULL,
    [Description] [varchar](50) NOT NULL,
 CONSTRAINT [PK_DetailsStream] PRIMARY KEY CLUSTERED 
(
    [Cod] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]



--  Details stream
CREATE TABLE [dbo].[DetailsStream](
    [Id] [int] IDENTITY (1, 1) NOT NULL,
    [DateStream] [datetime] NOT NULL,
    [CodStateDetailsStream] [int] NOT NULL,
    [IdStream] [int] NOT NULL,
 CONSTRAINT [PK_DetailsStream] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


ALTER TABLE [dbo].[DetailsStream]  WITH CHECK ADD  CONSTRAINT [FK_DetailsStream_StateDetailsStream] FOREIGN KEY([CodStateDetailsStream])
REFERENCES [dbo].[StateDetailsStream] ([Cod])
GO

ALTER TABLE [dbo].[DetailsStream] CHECK CONSTRAINT [FK_DetailsStream_StateDetailsStream]
GO

ALTER TABLE [dbo].[DetailsStream]  WITH CHECK ADD  CONSTRAINT [FK_DetailsStream_Stream] FOREIGN KEY([IdStream])
REFERENCES [dbo].[Stream] ([Id])
GO

ALTER TABLE [dbo].[DetailsStream] CHECK CONSTRAINT [FK_DetailsStream_Stream]
GO

Inserts : 插入物

-- StreamCategory

INSERT INTO [myDb].[dbo].[StreamCategory]
           ([Cod]
           ,[Name])
     VALUES
       (1,'Category A'),
       (2,'Category B'),
       (3,'Category C')
GO



-- Stream

INSERT INTO [myDb].[dbo].[Stream]
           ( [CodCategory]
            ,[Name]
           )
     VALUES
        -- Category A:
        (   1       ,'Stream001'    ), -- IdStream: 1
        (   1       ,'Stream002'    ), -- IdStream: 2
        (   1       ,'Stream003'    ), -- IdStream: 3
        (   1       ,'Stream004'    ),
        -- Category B:
        (   2       ,'Stream005'    ), -- IdStream: 5
        (   2       ,'Stream006'    ),
        (   2       ,'Stream007'    ),
        (   2       ,'Stream008'    ),
        -- Category C:
        (   3       ,'Stream009'    ), -- IdStream: 9
        (   3       ,'Stream010'    ), -- IdStream: 10
        (   3       ,'Stream011'    ),
        (   3       ,'Stream012'    )
GO



-- StateDetailsStream

INSERT INTO [myDB].[dbo].[StateDetailsStream]
           ([Cod]
           ,[Description])
     VALUES
       (1, 'InProgress'),
       (2, 'Received'),
       (3, 'Ko'),
       (4, 'Declined')
GO



-- DetailsStream

DECLARE
@mon    as datetime = '20/06/2016'
DECLARE
@tue    as datetime = @mon+1,
@wed    as datetime = @mon+2,
@thu    as datetime = @mon+3,
@fry    as datetime = @mon+4


INSERT INTO [myDB].[dbo].[DetailsStream]
           ([IdStream] -- fk
           ,[DateStream]
           ,[CodStateDetailsStream]
           )
    VALUES
        -- Category A
        (1  ,@mon           ,2  ), -- Stream001
        (1  ,@mon           ,2  ),
        (1  ,@tue           ,2  ),
        (1  ,@tue           ,2  ),
        (1  ,@wed           ,3  ),
        (1  ,@wed           ,2  ),
        (1  ,@thu           ,2  ),
        (1  ,@thu           ,2  ),
        (1  ,@fry           ,2  ),
        (1  ,@fry           ,1  ),
        (1  ,@mon+7         ,1  ),
        (1  ,@mon+7         ,1  ),
        (1  ,@tue+7         ,3  ),
        (1  ,@tue+7         ,4  ),
        (1  ,@wed+7         ,2  ),
        (1  ,@wed+7         ,1  ),
        (1  ,@thu+7         ,2  ),
        (1  ,@thu+7         ,2  ),
        (1  ,@fry+7         ,4  ),
        (1  ,@fry+7         ,2  ),
        (2  ,@mon           ,2  ), -- Stream002
        (2  ,@mon           ,4  ),
        (2  ,@tue           ,4  ),
        (2  ,@tue           ,2  ),
        (2  ,@wed           ,3  ),
        (2  ,@wed           ,2  ),
        (2  ,@thu           ,2  ),
        (2  ,@thu           ,2  ),
        (2  ,@fry           ,2  ),
        (2  ,@fry           ,1  ),

        -- Category B
        (5  ,@mon           ,2  ), -- Stream005
        (5  ,@tue           ,2  ),
        (5  ,@wed           ,2  ),
        (5  ,@thu           ,2  ),
        (5  ,@fry           ,2  ),

        -- Category C
        (10 ,@mon           ,1  ), -- Stream010
        (10 ,@mon           ,2  ),
        (10 ,@tue           ,2  ),
        (10 ,@tue           ,3  ),
        (10 ,@fry           ,2  ),
        (10 ,@fry           ,4  ),

        (11 ,@wed           ,4  ), -- Stream011
        (11 ,@wed           ,1  ),
        (11 ,@thu           ,2  ),
        (11 ,@thu           ,3  )

(NB: I don't know if it might be interested, but usually I use this methodology: Web Form code-behind -> WebService -> Core -> Repository, with entities and DTOs to communicate between the different levels) (注:我不知道它是否可能感兴趣,但是通常我使用这种方法:Web表单代码隐藏-> WebService->核心->存储库,实体和DTO在不同级别之间进行通信)

You could look into using a Calendar table (which is useful for a number of date related calculations... 您可以考虑使用日历表(对于许多与日期相关的计算非常有用...

Here is a reference for one: https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/ 这是一个参考: https : //www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/

Using this table you can join on the date key to your relevant date, then group by the other fields in the Calendar table; 使用此表,您可以将日期键加入到您的相关日期,然后按日历表中的其他字段分组; by the sounds of it the Year and Week. 通过年和周的声音。 This should allow you to easily report in weekly periods 这应该使您可以轻松地每周报告一次

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

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