首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

关于时间函数、聚合函数查询的复杂有关问题,请高手帮忙

2012-02-28 
关于时间函数、聚合函数查询的复杂问题,请高手帮忙!表结构:ID INT  产品ID号    INTIME DATETIME  产品入库

关于时间函数、聚合函数查询的复杂问题,请高手帮忙!
表结构:ID INT  产品ID号
    INTIME DATETIME  产品入库时间
ID,INIME
1,2005-1-2 17:22:01
2,2005-1-2 17:22:02
3,2005-1-2 17:22:03
4,2005-1-3 17:22:01
5,2005-1-3 17:22:03
7,2005-1-3 17:22:04
44,2005-1-5 17:22:04
50,2005-1-6 17:22:04
51,2005-1-6 17:23:04
52,2005-1-6 18:23:04
53,2005-1-6 19:23:04

如何统计出,入库产品最多的那一天,并且列出那一天的所有产品?






[解决办法]
select *
from ta
where datetime = (select top 1 datetime from ta group by datetime desc)
[解决办法]
select * 
from ta 
where datetime = (select top 1 datetime from ta group by datetime order by count(1) desc)
[解决办法]
select
*
from 
T
where
convert(varchar(10),INIME,120)=(select top 1 convert(varchar(10),INIME,120) INIME from T order by ount(ID) desc)

[解决办法]

SQL code
declare @tb table (id int,intime datetime)insert into @tb select 1,'2005-1-2'insert into @tb select 2,'2005-1-2'insert into @tb select 3,'2005-1-2'insert into @tb select 4,'2005-1-3'insert into @tb select 5,'2005-1-3'insert into @tb select 6,'2005-1-4'insert into @tb select 7,'2005-1-4'insert into @tb select 8,'2005-1-4'insert into @tb select 9,'2005-1-4'select * from @tb twhere convert(varchar(10),intime,120)=(select top 1 convert(varchar(10),intime,120) from @tbgroup by convert(varchar(10),intime,120)order by count(1) desc)
[解决办法]
idintime
62005-01-04 00:00:00.000
72005-01-04 00:00:00.000
82005-01-04 00:00:00.000
92005-01-04 00:00:00.000
[解决办法]
SQL code
 
select
*
from
T
where
convert(varchar(10),INIME,120)=(select top 1 convert(varchar(10),INIME,120) INIME from T group by convert(varchar(10),INIME,120) order by ount(ID) desc)

[解决办法]
SQL code
/*-- Author:Flystone -- Version:V1.001  Date:2008-05-15   初稿-- Version:V1.002  Date:2008-05-16  1、 处理空格带来的异常--                                  2、 增加了形如yyyy-mm-dd hh:mm:ss--                                               yyyy-m-d h:m:s     格式的处理*/-- Test Data: taIf object_id('ta') is not null     Drop table taGoCreate table ta(ID int,INIME datetime)GoInsert into taselect 1,'2005-1-2 17:22:01' union allselect 2,'2005-1-2 17:22:02' union allselect 3,'2005-1-2 17:22:03' union allselect 4,'2005-1-3 17:22:01' union allselect 5,'2005-1-3 17:22:03' union allselect 7,'2005-1-3 17:22:04' union allselect 44,'2005-1-5 17:22:04' union allselect 50,'2005-1-6 17:22:04' union allselect 51,'2005-1-6 17:23:04' union allselect 52,'2005-1-6 18:23:04' union allselect 53,'2005-1-6 19:23:04' Go--Startselect * from ta where convert(char(10),INIME,120) = (select top 1 convert(char(10),INIME,120) from ta group by convert(char(10),INIME,120) order by count(1) desc)--Result:/*ID          INIME                                                  ----------- ------------------------------------------------------ 50          2005-01-06 17:22:04.00051          2005-01-06 17:23:04.00052          2005-01-06 18:23:04.00053          2005-01-06 19:23:04.000(所影响的行数为 4 行)*/--End
[解决办法]
那就结贴,派分
------解决方案--------------------


select * from @tb t
where convert(varchar(10),intime,120)=(
select top 1 convert(varchar(10),intime,120) from @tb
group by convert(varchar(10),intime,120)
order by count(1) desc
)

这个好,和我想的一样!

热点排行