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

查询的sql语句增加一个查询条件解决方案

2012-05-12 
查询的sql语句增加一个查询条件SELECT top 50 username,num1,num2,CAST(num2 AS INT)-CAST(num1 AS INT),C

查询的sql语句增加一个查询条件
SELECT top 50 username,num1,num2,CAST(num2 AS INT)-CAST(num1 AS INT),CAST(num1 AS DECIMAL(10,2)),
  (CAST(num2 AS INT)-CAST(num1 AS INT))/CAST(num1 AS DECIMAL(10,2)) as b FROM (
  select c.username,
  case WHEN num1 IS NULL THEN 0 ELSE num1 END AS num1,
  case WHEN num2 IS NULL THEN 0 ELSE num2 END AS num2
  FROM (SELECT username, count(1) AS num1 FROM tblorders a join tbluser b ON a.ProviderID= userid WHERE OrderStatus>0
  and ordertime between '2012-01-01 00:00:00' and '2012-02-01 23:59:59' group by b.username) AS c
  LEFT JOIN 
  (SELECT username,count(1) AS num2 FROM tblorders a join tbluser b ON a.ProviderID= b.userid WHERE OrderStatus>0 
  and ordertime between '2012-02-01 00:00:00' and '2012-02-05 23:59:59' group by b.username) AS d
  ON c.username=d.username
  ) e order by b desc;

这是tblorders表 和tbluser 表联合查询
tblorders 表有个字段城市 Scity 比如 where Scity ='值'
怎么把这个条件加上啊 ,谢谢 了

[解决办法]
Try this

SELECT top 50 username,num1,num2,CAST(num2 AS INT)-CAST(num1 AS INT),CAST(num1 AS DECIMAL(10,2)),
(CAST(num2 AS INT)-CAST(num1 AS INT))/CAST(num1 AS DECIMAL(10,2)) as b FROM (
select c.username,
case WHEN num1 IS NULL THEN 0 ELSE num1 END AS num1,
case WHEN num2 IS NULL THEN 0 ELSE num2 END AS num2
FROM (SELECT username, count(1) AS num1 FROM tblorders a join tbluser b ON a.ProviderID= userid WHERE OrderStatus>0 and a.Scity ='值'
and ordertime between '2012-01-01 00:00:00' and '2012-02-01 23:59:59' group by b.username) AS c
LEFT JOIN
(SELECT username,count(1) AS num2 FROM tblorders a join tbluser b ON a.ProviderID= b.userid WHERE OrderStatus>0
and ordertime between '2012-02-01 00:00:00' and '2012-02-05 23:59:59' group by b.username) AS d
ON c.username=d.username
) e order by b desc;

热点排行