首页 新闻 会员 周边 捐助

同一张表,不同的查询条件,查询出的结果合为一张表。

0
悬赏园豆:20 [已解决问题] 解决于 2013-07-25 15:46

如:SQL

select *
from
(select mnyAmount from work_Order as o 
inner join work_OrderDtl as dtl on o.orderno = dtl.orderno 
where SUBSTRING(succeedrate,7,3) > '50%' and replace(Convert(varchar(7),orderdate,120),'-','')='201307'
)as T1,
(select mnyAmount as mny from work_Order as o 
inner join work_OrderDtl as dtl on o.orderno = dtl.orderno 
where SUBSTRING(succeedrate,7,3) > '50%' and replace(Convert(varchar(7),orderdate,120),'-','')='201301'
)as T2

查询出的结果是为空的。 

如果单独查询的时候:

1、条件为201307的时候有数据

2、条件为201301的时候是没有数据的。

所以查询出的整个是没有数据的。 目前这种怎么解决?  求解....

 

我想要的结果就是如果条件为201307的时候,有两条,201301的时候没有。 那我查询出的结果应该是两条和0条,不是全没有了。

最初的、天空的主页 最初的、天空 | 初学一级 | 园豆:40
提问于:2013-07-25 09:55
< >
分享
最佳答案
0
select a.mnyAmount,b.mny
from
(select
1 as flag,mnyAmount from work_Order as o inner join work_OrderDtl as dtl on o.orderno = dtl.orderno where SUBSTRING(succeedrate,7,3) > '50%' and replace(Convert(varchar(7),orderdate,120),'-','')='201307' )a full join (select 1 as falg,mnyAmount as mny from work_Order as o inner join work_OrderDtl as dtl on o.orderno = dtl.orderno where SUBSTRING(succeedrate,7,3) > '50%' and replace(Convert(varchar(7),orderdate,120),'-','')='201301'
)b on a.flag=b.flag

大概就是这样了,如果可能出现更多行,那么flag字段就要用你所需要的另外一个字段做链接,思路是一样的。

收获园豆:10
清海扬波 | 小虾三级 |园豆:825 | 2013-07-25 11:34
其他回答(2)
0

结构相同:

select mnyAmount from work_Order as o
inner join work_OrderDtl as dtl on o.orderno = dtl.orderno
where SUBSTRING(succeedrate,7,3) > '50%' and replace(Convert(varchar(7),orderdate,120),'-','') in ('201307','201301')

 

select mnyAmount from work_Order as o
inner join work_OrderDtl as dtl on o.orderno = dtl.orderno
where SUBSTRING(succeedrate,7,3) > '50%' and (replace(Convert(varchar(7),orderdate,120),'-','')  ='201307' ) or replace(Convert(varchar(7),orderdate,120),'-','')  ='201301')

结构不同

 select mnyAmount from work_Order as o
inner join work_OrderDtl as dtl on o.orderno = dtl.orderno
where SUBSTRING(succeedrate,7,3) > '50%' and replace(Convert(varchar(7),orderdate,120),'-','') ='201301'

Union

select mnyAmount from work_Order as o
inner join work_OrderDtl as dtl on o.orderno = dtl.orderno
where SUBSTRING(succeedrate,7,3) > '50%' and replace(Convert(varchar(7),orderdate,120),'-','') ='201301'

收获园豆:10
happydaily | 园豆:260 (菜鸟二级) | 2013-07-25 10:18

谢谢的你回复。你的写法是正确的。 但不是我想要的结果,我想要的结果是,每个查询出的条件,都as一个字段,然后查询出。  如:  

支持(0) 反对(0) 最初的、天空 | 园豆:40 (初学一级) | 2013-07-25 10:33

@最初的、天空: 那就用两个子查询。

支持(0) 反对(0) happydaily | 园豆:260 (菜鸟二级) | 2013-07-25 14:40
0

要用union连接两个表的结果,然后用pivot反转,就能查询出你的结果了~

幻天芒 | 园豆:37205 (高人七级) | 2013-07-25 10:39

怎么写? 谢谢

支持(0) 反对(0) 最初的、天空 | 园豆:40 (初学一级) | 2013-07-25 10:40

@最初的、天空: 

--Step1、将结果存入临时表
INSERT INTO #tmp
--临时表
--
SELECT Head = 'mnyAmount',
       Amount = mnyAmount
FROM   work_Order AS o
       INNER JOIN work_OrderDtl AS dtl
            ON  o.orderno = dtl.orderno
WHERE  SUBSTRING(succeedrate, 7, 3) > '50%'
       AND REPLACE(CONVERT(VARCHAR(7), orderdate, 120), '-', '') = '201307'
UNION ALL
SELECT 'my',
       Amount = mnyAmount AS mny
FROM   work_Order AS o
       INNER JOIN work_OrderDtl AS dtl
            ON  o.orderno = dtl.orderno
WHERE  SUBSTRING(succeedrate, 7, 3) > '50%'
       AND REPLACE(CONVERT(VARCHAR(7), orderdate, 120), '-', '') = '201301'

--Step2、反转
SELECT * #tmp
       PIVOT(SUM([Amount]) FOR [Head] IN ([mnyAmount], [my])) AS pvt
支持(0) 反对(0) 幻天芒 | 园豆:37205 (高人七级) | 2013-07-25 12:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册