最近转行到数据库,对于mysql不甚了解,刚换了新工作、经理让做的活儿,大虾们帮帮忙吧。谢谢!订单表(OrderTB)中有订单ID(主键),订单名称(OrderName),退换次数(BackCount),下单时间(DownTime),用户ID(UserID),注册时间(regtime)。1,从注册到首次购物的时间段用户的分布情况;2,首次购物与第二次购物的时间段用户分布情况;3,购物次数的用户分布;4,首次购物到停止购物之间时段的用户分布;5退换货次数的用户分布情况。

解决方案 »

  1.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  2.   


    1,从注册到首次购物的时间段用户的分布情况;
    select userid,min(downtime),regtime from ordertb group by userid;2,首次购物与第二次购物的时间段用户分布情况;
    select userid,count(*),downtime from ordertb
    group by userid
    having count(*)=2;3,购物次数的用户分布;
    select userid,count(*) from ordertb group by userid;
      

  3.   

    2,首次购物与第二次购物的时间段用户分布情况;127.0.0.1~root@localhost~test>select userid,count(*),min(downtime),max(downtime) from ordertb
        -> group by userid
        -> having count(*)=2;
    +--------+----------+---------------------+---------------------+
    | userid | count(*) | min(downtime)       | max(downtime)       |
    +--------+----------+---------------------+---------------------+
    |      1 |        2 | 2010-09-14 00:00:00 | 2010-09-15 00:00:00 |
    |      2 |        2 | 2010-09-10 00:00:00 | 2010-09-14 00:00:00 |
    +--------+----------+---------------------+---------------------+
    2 rows in set (0.05 sec)
      

  4.   

    ok,mssql也没问题的。mssql2005,我自己的机子上面装的是sql2005。
    1,GO/****** Object:  Table [dbo].[OrderTB]    Script Date: 09/14/2010 22:55:23 ******/
    SET ANSI_NULLS ON
    GOSET QUOTED_IDENTIFIER ON
    GOSET ANSI_PADDING ON
    GOCREATE TABLE [dbo].[OrderTB](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [OrderName] [varchar](50) NULL,
    [BackCount] [int] NULL,
    [DownTime] [datetime] NULL,
    [userID] [int] NULL,
    [RegTime] [datetime] NULL,
     CONSTRAINT [PK_B] 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]GOSET ANSI_PADDING OFF
    GO
    2.INSERT INTO [NVCSH].[dbo].[AA]
               ([OrderName]
               ,[BackCount]
               ,[DownTime]
               ,[userID]
               ,[RegTime])
         VALUES
               (<OrderName, varchar(50),>
               ,<BackCount, int,>
               ,<DownTime, datetime,>
               ,<userID, int,>
               ,<RegTime, datetime,>)
    GO
    3.第一问的结果是 第一步:最后结果;
    第二问和第一问类似的;
    第三问应该是先算出所有用户的购物次数,然后在每个购物次数上面有哪些用户这样子的;
    第四问忽略
    第五问和第三问类似。
    谢谢啦
      

  5.   

    3,购物次数的用户分布;127.0.0.1~root@localhost~test>select userid,count(*) from ordertb group by userid;
    +--------+----------+
    | userid | count(*) |
    +--------+----------+
    |      1 |        2 |
    |      2 |        2 |
    |      3 |        1 |
    +--------+----------+
    3 rows in set (0.00 sec)1:从注册到首次购物的时间段用户的分布情况;127.0.0.1~root@localhost~test>select userid,min(downtime),regtime from ordertb group by userid;
    +--------+---------------------+---------------------+
    | userid | min(downtime)       | regtime             |
    +--------+---------------------+---------------------+
    |      1 | 2010-09-14 00:00:00 | 2010-08-01 00:00:00 |
    |      2 | 2010-09-10 00:00:00 | 2010-08-02 00:00:00 |
    |      3 | 2010-09-09 00:00:00 | 2010-08-03 00:00:00 |
    +--------+---------------------+---------------------+
    3 rows in set (0.02 sec)
      

  6.   

    select DATEDIFF(DAY,RegTime,DownTime),USERID from OrderTB
      

  7.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  8.   

    没有必要让别人按照你的图自己手工敲一遍INSERT, CREATE代码吧。
      

  9.   

    第一个。127.0.0.1~root@localhost~test>select DATEDIFF(downtime,regtime) as t,group_concat(userid) as userid from ordertb group by t;
    +------+--------+
    | t    | userid |
    +------+--------+
    |   37 | 3      |
    |   39 | 2      |
    |   44 | 1,2    |
    |   45 | 1      |
    +------+--------+
    4 rows in set (0.00 sec)
      

  10.   

    第二个127.0.0.1~root@localhost~test>select DATEDIFF(max(downtime),min(downtime)) as t,group_concat(userid) as userid from ordertb g
    roup by userid;
    +------+--------+
    | t    | userid |
    +------+--------+
    |    1 | 1,1    |
    |    4 | 2,2    |
    |    0 | 3      |
    +------+--------+
    3 rows in set, 1 warning (0.00 sec)
      

  11.   

    第三个127.0.0.1~root@localhost~test>select t,group_concat(userid) from (select count(*) as t,userid as userid from ordertb group by
     userid) a group by t;
    +---+----------------------+
    | t | group_concat(userid) |
    +---+----------------------+
    | 1 | 3                    |
    | 2 | 1,2                  |
    +---+----------------------+
    2 rows in set (0.00 sec)
      

  12.   

    第5个127.0.0.1~root@localhost~test>select t,group_concat(userid) from (select sum(backcount) as t,userid as userid from ordertb gr
    oup by userid) a group by t;
    +------+----------------------+
    | t    | group_concat(userid) |
    +------+----------------------+
    |    0 | 1,3                  |
    |    1 | 2                    |
    +------+----------------------+
    2 rows in set (0.00 sec)
      

  13.   

    在此基础上面统计用户的 数量怎么算?
    +------+----------------------+
    | t    | group_concat(userid) |用户数量
    +------+----------------------+
    |    0 | 1,3                  |2
    |    1 | 2                    |1
    +------+----------------------+
      

  14.   

    上面都贴了那么多了,你举一反三也能得出你要的啊。把你的SHOW CREATE TABLE和INSERT语句都贴出来,别人也好给你做测试。