如何使用sp将两个选择查询结果合并为一个结果?

时间:2016-05-30 07:42:05

标签: sql sql-server join stored-procedures left-join

我宣布两个比较日期,我想要两个日期的数据。我正在使用left join进行组合提议,但这不是正确的方法。我错过了一些数据。而不是左连接哪一个最适合?

结果

productid   FirstQty    SecondQty   FirstProductRevenue SecondProductRevenue

COCAK117    1             2          1370.00              1440.00
COCAK632    1             2          1125.00              2250.00
COCAK656    1             NULL        795.00               NULL
COCAK657    1             2           720.00              2090.00
COCAK775    3             1           2475.00             825.00

我从第一张表中获取数据并从第二张表中获得匹配的productid,但我想从两个表中获得总产量。

CREATE PROCEDURE [dbo].[Orders]    
(        
     @StartDate     DATETIME,        
     @EndDate       DATETIME,
     @StartDate1    DATETIME,        
     @EndDate1      DATETIME,
     @Rowname       VARCHAR(100), 
     @AssociateName VARCHAR(50)         
)
--[Orders] '05/03/2015','05/03/2015','05/05/2015','05/07/2015','Most Gifted Cakes','all'
AS BEGIN 
 if(@AssociateName='all')
BEGIN
        ----First duration for all associates-----

select t1.productid,t1.FirstQty,t2.SecondQty,t1.FirstProductRevenue,t2.SecondProductRevenue 
from 
(select op.Productid
      , count(op.ProductId)as FirstQty
      , Round(Sum(op.Price*op.Quantity),0) as FirstProductRevenue 
 from Orderdetails od 
 inner join (select Distinct Orderid,productid,Price,Quantity from Orderproducts)  op on op.Orderid=od.Orderid 
 inner JOIN City ct  ON od.RecipientCityName = ct.CityName 
 INNER JOIN Associates ass ON Ct.AssociateId = ass.AssociateId
 Inner join HomepageRowWiseProducts hr on op.ProductId=hr.Productid 
 where  Convert(datetime,Convert(Varchar(50),od.DeliveryDate,101)) between @StartDate and @EndDate 
   and (od.TransactionId IS NOT NULL or ltrim(od.TransactionId) !=  '') 
   and @Rowname=hr.HomepageRow_name and hr.status=1 
   Group by op.Productid
) t1
   ----Second duration for all associates-----
left join 
 (select op.Productid
       , count(op.ProductId)as SecondQty
       , Round(Sum(op.Price*op.Quantity),0) as SecondProductRevenue 
 from Orderdetails od 
 inner join (select Distinct Orderid,productid,Price,Quantity from Orderproducts)  op on op.Orderid=od.Orderid 
 inner JOIN City ct  ON od.RecipientCityName = ct.CityName 
 INNER JOIN Associates ass ON Ct.AssociateId = ass.AssociateId
 Inner join HomepageRowWiseProducts hr on op.ProductId=hr.Productid 
 where  Convert(datetime,Convert(Varchar(50),od.DeliveryDate,101)) between @StartDate1 and @EndDate1 
   and (od.TransactionId IS NOT NULL or ltrim(od.TransactionId) !=  '') 
   and @Rowname=hr.HomepageRow_name and hr.status=1
   Group by op.Productid
) t2 on t1.productid=t2.productid
END

3 个答案:

答案 0 :(得分:1)

您可以尝试一次获取所有数据,然后仅汇总所需的日期范围。 我可能在这里犯了一些错误,因为我没有你的数据结构。 但是你应该知道如何实现它。

select op.Productid
       , sum(  case when Convert(datetime,Convert(Varchar(50),od.DeliveryDate,101)) between @StartDate and @EndDate then 
          1 else 0 end) FirstQty
       , sum(  case when Convert(datetime,Convert(Varchar(50),od.DeliveryDate,101)) between @StartDate1 and @EndDate1 then 
          1 else 0 end)  SecondQty,
       , Round(Sum( case when Convert(datetime,Convert(Varchar(50),od.DeliveryDate,101)) between @StartDate and @EndDate 
                        then op.Price*op.Quantity 
                         else 0 end),0) as FirstProductRevenue 
       , Round(Sum( case when Convert(datetime,Convert(Varchar(50),od.DeliveryDate,101)) between @StartDate1 and @EndDate1 
                        then op.Price*op.Quantity 
                         else 0 end),0) as SecondProductRevenue 
 from Orderdetails od 
 inner join (select Distinct Orderid,productid,Price,Quantity from Orderproducts)  op on op.Orderid=od.Orderid 
 inner JOIN City ct  ON od.RecipientCityName = ct.CityName 
 INNER JOIN Associates ass ON Ct.AssociateId = ass.AssociateId
 Inner join HomepageRowWiseProducts hr on op.ProductId=hr.Productid 
 where  ( Convert(datetime,Convert(Varchar(50),od.DeliveryDate,101)) between @StartDate and @EndDate 
       Or Convert(datetime,Convert(Varchar(50),od.DeliveryDate,101)) between @StartDate1 and @EndDate1 )
   and (od.TransactionId IS NOT NULL or ltrim(od.TransactionId) !=  '') 
   and @Rowname=hr.HomepageRow_name and hr.status=1
   Group by op.Productid

答案 1 :(得分:0)

试试这个

你创建一个函数如下

CREATE FUNCTION OrderDetails (
@StartDate     DATETIME,
@EndDate       DATETIME
)
RETURNS  @tblList TABLE  
(  
orderId  VARCHAR(1000) 

)   
AS 
BEGIN       
 select orderid
 insert into @tblList
  from Orderdetails od inner JOIN City ct  ON od.RecipientCityName = ct.CityName  
     INNER JOIN Associates ass ON Ct.AssociateId = ass.AssociateId
 Inner join HomepageRowWiseProducts hr on op.ProductId=hr.Productid 

   and (od.TransactionId IS NOT NULL or ltrim(od.TransactionId) !=  '') 
   and @Rowname=hr.HomepageRow_name and hr.status=1 
  where  Convert(datetime,Convert(Varchar(50),od.DeliveryDate,101)) between @StartDate and @EndDate 
return
end

然后根据您传递的日期调用存储过程中的函数,不要错过任何记录

CREATE PROCEDURE [dbo].[Orders]    
(        
     @StartDate     DATETIME,        
     @EndDate       DATETIME,
     @StartDate1    DATETIME,        
     @EndDate1      DATETIME,
     @Rowname       VARCHAR(100), 
     @AssociateName VARCHAR(50)         
)
--[Orders] '05/03/2015','05/03/2015','05/05/2015','05/07/2015','Most Gifted Cakes','all'
AS BEGIN 
 if(@AssociateName='all')
BEGIN
        ----First duration for all associates-----

select op1.Productid
      , count(op.ProductId)as FirstQty
      ,count(op1.ProductId)as SecondQty
      , Round(Sum(op.Price*op.Quantity),0) as FirstProductRevenue 
          , Round(Sum(op1.Price*op1.Quantity),0) as FirstProductRevenue 
 from (select Distinct Orderid,productid,Price,Quantity from Orderproducts opp  inner join [Your_ScemaName].[OrderDetails](@StartDate,@EndDate) od on opp.Orderid=od.Orderid  ) op 
 inner join  (select Distinct Orderid,productid,Price,Quantity from Orderproducts  opp inner join [Your_ScemaName].[OrderDetails](@StartDate1,@EndDate1) od on opp.Orderid=od.Orderid  ) op1 
-- since 1=1 is always true you will get all data
 on 1=1
   Group by op.Productid
end
end

答案 2 :(得分:0)

也许这(简化):

select coalesce(t1.productid, t2.productid) as productid, t1.FirstQty, t2.SecondQty, t1.FirstProductRevenue, t2.SecondProductRevenue 
from 
(select ...) t1
   ----Second duration for all associates-----
full join 
(select ...) t2 on t1.productid = t2.productid