日期:2014-05-17 浏览次数:20681 次
--1. select convert(varchar(10),a.rq,120) rq,a.xm from (select * from @test where cb=N'早餐' and je>=5) a, (select * from @test where cb=N'中餐' and je>=10) b where convert(varchar(10),a.rq,120)=convert(varchar(10),b.rq,120) and a.xm=b.xm --2. select convert(varchar(10),a.rq,120) rq,a.xm from (select * from @test where cb=N'早餐' and je>=5) a, (select * from @test where cb=N'中餐' and je>=8) b, (select * from @test where cb=N'晚餐' and je>=10) c where convert(varchar(10),a.rq,120)=convert(varchar(10),b.rq,120) and convert(varchar(10),a.rq,120)=convert(varchar(10),c.rq,120) and a.xm=b.xm and a.xm=c.xm
------解决方案--------------------
--> 测试数据:[tb] IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb] GO CREATE TABLE [tb]([rq] DATETIME,[cb] VARCHAR(4),[je] NUMERIC(6,4),[xm] VARCHAR(6)) INSERT [tb] SELECT '2012-07-09 00:00:00','晚餐',11.9,'艾凤琴' UNION ALL SELECT '2012-07-09 00:00:00','中餐',10.4,'艾凤琴' UNION ALL SELECT '2012-07-09 00:00:00','早餐',5.2,'艾凤琴' UNION ALL SELECT '2012-07-10 00:00:00','早餐',4.9,'艾凤琴' UNION ALL SELECT '2012-07-10 00:00:00','中餐',10.5,'艾凤琴' UNION ALL SELECT '2012-07-10 00:00:00','晚餐',12,'艾凤琴' UNION ALL SELECT '2011-12-22 00:00:00','晚餐',6.0,'艾胡蓉' UNION ALL SELECT '2011-12-22 00:00:00','中餐',3.2,'艾胡蓉' UNION ALL SELECT '2011-12-22 00:00:00','早餐',4.2,'艾胡蓉' UNION ALL SELECT '2012-06-24 00:00:00','晚餐',7.93,'白玲' UNION ALL SELECT '2012-06-24 00:00:00','中餐',6.89,'白玲' UNION ALL SELECT '2012-06-24 00:00:00','早餐',4.669,'白玲' UNION ALL SELECT '2012-07-04 00:00:00','晚餐',8.23,'白珊' UNION ALL SELECT '2012-07-04 00:00:00','中餐',6.759,'白珊' UNION ALL SELECT '2012-07-04 00:00:00','早餐',4.190,'白珊' UNION ALL SELECT '2011-11-20 00:00:00','晚餐',6.25,'班小会' UNION ALL SELECT '2011-11-20 00:00:00','中餐',7.37,'班小会' UNION ALL SELECT '2011-11-20 00:00:00','早餐',4.280,'班小会' UNION ALL SELECT '2011-12-19 00:00:00','晚餐',7.7,'包双双' UNION ALL SELECT '2011-12-19 00:00:00','中餐',7.5,'包双双' UNION ALL SELECT '2011-12-19 00:00:00','早餐',5.04,'包双双' UNION ALL SELECT '2011-12-22 00:00:00','晚餐',8.6,'卜丽玲' UNION ALL SELECT '2011-12-22 00:00:00','中餐',7.73,'卜丽玲' UNION ALL SELECT '2011-12-22 00:00:00','早餐',5.629,'卜丽玲' UNION ALL SELECT '2012-04-27 00:00:00','晚餐',5.5,'卜美华' UNION ALL SELECT '2012-04-27 00:00:00','中餐',6.29,'卜美华' UNION ALL SELECT '2012-04-27 00:00:00','早餐',4.2999,'卜美华' --------------开始查询-------------------------- --1 SELECT * FROM [tb] AS t WHERE EXISTS (SELECT 1 FROM tb WHERE [xm]=t.[xm] AND [rq]=t.[rq] AND [cb]='早餐' AND [je]>=5.0 ) AND EXISTS (SELECT 1 FROM tb WHERE [xm]=t.[xm] AND [rq]=t.[rq] AND [cb]='中餐' AND [je]>=10.0 )