繁体   English   中英

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

[英]How to combine two select query results into one result using sp?

我声明了两个比较日期,我想要两个日期的数据。 我正在使用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,但我想要两个表中的总 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

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

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

尝试这个

你创建一个函数如下

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

也许这(简化):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM