2016-06-23 1 views
0

Je possède ce tables SQL Server:Comment effectuer la requête SQL avec plage de dates

enter image description here

j'ai besoin de regrouper chaque catégorie de flux et montrer à l'utilisateur un aperçu des dernières semaines, comme en témoigne cette image:

enter image description here

ce datatable un service interroge Web qui, à son tour, interroge la base de données.

La couleur des coches dépend de la valeur CodState: s'il y a également un état égal à 4 ('Refusé') alors la coche est rouge. Sinon, est vert.

Veuillez noter que le champ Date DetailsStream correspond à un certain jour.

Le problème est la formulation de la requête avec différentes plages de dates (dans l'image: les cinq dernières semaines, du lundi au vendredi).

EDIT

Comme suggéré, mes tables schémas:

-- 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: Je ne sais pas si cela pourrait être intéressé, mais généralement j'utilise cette méthodologie: Web Form code-b errière -> WebService -> base -> Dépôt, avec des entités et DTO de communiquer entre les différents niveaux)

+3

Aimez les graphiques! Pouvez-vous poster le code SQL que vous avez écrit jusqu'à présent pour répondre à vos besoins, afin que nous puissions vous aider avec le problème que vous rencontrez? Merci. – sstan

+1

Voici un bon point de départ. http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ –

+0

J'ai mis à jour ma question – Gioce90

Répondre

0

Vous pouvez regarder dans une table de calendrier (qui est utile pour un certain nombre de calculs liés à la date ...

Voici une référence pour un: https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/

en utilisant ce tableau, vous pouvez vous joindre à la clé de la date à votre date pertinente, puis groupe par les autres champs de la table de calendrier; par les sons de l'année et de la semaine. Cela devrait vous permettre de rapporter facilement en périodes hebdomadaires