如: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条,不是全没有了。
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字段就要用你所需要的另外一个字段做链接,思路是一样的。
结构相同:
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'
谢谢的你回复。你的写法是正确的。 但不是我想要的结果,我想要的结果是,每个查询出的条件,都as一个字段,然后查询出。 如:
@最初的、天空: 那就用两个子查询。
要用union连接两个表的结果,然后用pivot反转,就能查询出你的结果了~
怎么写? 谢谢
@最初的、天空:
--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