从每周数据中选择“从MSSQL组查询”

时间:2014-01-28 12:33:15

标签: sql-server-2008 tsql

我使用sql server 2008的以下查询来按记录选择分组。

     Sum(ISNULL(ljoin,0)) AS ljoin,
     Sum(ISNULL(rjoin,0)) AS rjoin, DATEPART(wk, Date) AS WeekNumber,
     CASE 
     WHEN YEAR(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min([date])), Min([date]))) < YEAR(Min([date]))
     THEN CAST(DATEADD(YEAR, DATEDIFF(YEAR, 0,DATEADD(YEAR, 0 ,GETDATE())), 0) AS      Varchar(50))   + ' TO ' + Cast(DATEADD(dd, 7-(DATEPART(dw, Min([date]))), Min([date]))  AS Varchar(50))
    ELSE
    Cast(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min([date])), Min([date])) AS Varchar(50)) + ' TO ' +       Cast(DATEADD(dd, 7-(DATEPART(dw, Min([date]))), Min([date]))  AS Varchar(50))
    END AS  DateRange
    FROM Pairs_Details  where userid='jitu'
    Group By userid,DATEPART(wk, Date)

它返回以下结果。 for perticular userid ='jitu'

   userid   ljoin   rjoin   WeekNumber  DateRange                             
   jitu 1   2   1   Jan  1 2014 12:00AM TO Jan  6 2013 12:00AM
   jitu 1   1   2   Jan  6 2014 12:00AM TO Jan 12 2014 12:00AM
   jitu 1   0   3   Jan 13 2014 12:00AM TO Jan 19 2014 12:00AM
   jitu 2   0   5   Jan 27 2014 10:00PM TO Feb  2 2014 10:00PM

从2012-08-06开始,因为周期应该从星期一到星期日开始(2014-01-01)。我已经尝试了谷歌搜索大约一个小时左右,似乎没有结果符合我的问题,我希望有人可以帮助我有我的下面的表,数据和欲望输出,我想要

Pairs_Details表定义:

CREATE TABLE [dbo].[Pairs_Details](
[sno] [int] IDENTITY(1,1) NOT NULL,
[userid] [nvarchar](50) NULL,
[date] [datetime] NULL,
[ljoin] [int] NULL,
[rjoin] [int] NULL
   ) ON [PRIMARY]

示例数据:

   sno  userid              date                            ljoin rjoin
   1    jitu                2013-01-01 00:00:00.000         1   NULL
   2    jitu                2014-01-02 00:00:00.000             NULL    1
   3    tetu1234            2014-01-03 00:00:00.000     1   NULL
   4    jitu                2014-01-04 00:00:00.000             NULL    1
   5    saurbh123           2014-01-05 00:00:00.000     1   NULL
   6    jitu                2014-01-06 00:00:00.000         1   NULL
   9    saurbh123           2014-01-12 00:00:00.000         NULL    1
  10    jitu                2014-01-13 00:00:00.000         1   NULL
  11    rajeev123           2014-01-29 22:00:13.000         1   NULL
  12    saurbh123           2014-01-29 22:00:13.000         1   NULL
  13    jitu                2014-01-29 22:00:13.000         1   NULL
  14    rajeev123           2014-01-29 22:01:03.000         NULL    1
  15    saurbh123           2014-01-29 22:01:03.000         1   NULL
  16    jitu                2014-01-29 22:01:03.000         1   NULL
  7         tetu1234        2014-01-07 00:00:00.000         NULL    1
  8         jitu                2014-01-08 00:00:00.000         NULL    1

我想要以下结果

 userid ljoin   rjoin   WeekNumber  DateRange                               pair        
   jitu 1   2   1      Jan  1 2014 12:00AM TO Jan  6 2013 12:00AM          1
   jitu 1   1   2      Jan  6 2014 12:00AM TO Jan 12 2014 12:00AM          1
   jitu 1   0   3      Jan 13 2014 12:00AM TO Jan 19 2014 12:00AM          0
   jitu 2   0   5      Jan 27 2014 10:00PM TO Feb  2 2014 10:00PM          0

这可以使用以下查询来完成。

Select Case When ljoin <= rjoin  Then ljoin Else rjoin End As pair from Pairs_Details 

但是这个查询如何与上述查询一起使用,任何人都可以告诉我们

请任何人都可以帮助我。这样我们就可以检索出所需的结果

感谢。

1 个答案:

答案 0 :(得分:1)

- 更新了查询我试图获得所需的输出,但您仍然需要按照您的要求玩游戏;祝一切顺利!!!

SET DATEFIRST 1;
SELECT  userid,
Sum(ISNULL(ljoin,0)) AS lJoin,
Sum(ISNULL(rjoin,0)) AS rjoin, DATEPART(wk, Date) AS WeekNumber,
CASE 
WHEN YEAR(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min([date])), Min([date]))) < YEAR(Min([date]))
THEN CAST(DATEADD(YEAR, DATEDIFF(YEAR, 0,DATEADD(YEAR, 0 ,GETDATE())), 0) AS Varchar(50))   + ' TO ' + Cast(DATEADD(dd, 7-(DATEPART(dw, Min([date]))), Min([date]))  AS Varchar(50))
ELSE
Cast(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min([date])), Min([date])) AS Varchar(50)) + ' TO ' + Cast(DATEADD(dd, 7-(DATEPART(dw, Min([date]))), Min([date]))  AS Varchar(50))
END AS  DateRange
FROM Pairs_Details
Group By userid,DATEPART(wk, Date)