排序记录比较多个表中的总和

时间:2018-11-09 02:18:26

标签: sql sql-server

更新了此请求,因此很容易看到预期的结果。

我完全陷入困境,在SQL Server中,我想将所有作业恢复到

(
  SUM(Order.InvoicesReceived) > (SUM(Estimate.GrossValue) + SUM (AdditionalEstimate.GrossValue))
) OR ( 
    SUM(Order.ContractGiven) > (SUM(Estimate.GrossValue) + SUM (AdditionalEstimate.GrossValue))
)

CREATE TABLE Job (id INT, userid INT)
INSERT INTO Job ( id ,userid)VALUES  ( 1,1)    
INSERT INTO Job ( id ,userid)VALUES  ( 2,1)    
INSERT INTO Job ( id ,userid)VALUES  ( 3,2)  
INSERT INTO Job ( id ,userid)VALUES  ( 4,2)  
INSERT INTO Job ( id ,userid)VALUES  ( 5,1)  

CREATE TABLE [User] (id INT, UserName NVARCHAR (30))
INSERT INTO [User] ( id ,UserName)VALUES  ( 1,'Richard')
INSERT INTO [User] ( id ,UserName)VALUES  ( 2,'Jane')    

CREATE Table Estimate (id INT, [job] INT, [GrossValue] DECIMAL (18,2))
INSERT INTO Estimate ( id ,[job], GrossValue)VALUES  ( 1,3, 100)  
INSERT INTO Estimate ( id ,[job], GrossValue)VALUES  ( 2,4, 100)  
INSERT INTO Estimate ( id ,[job], GrossValue)VALUES  ( 3,5, 200)  
INSERT INTO Estimate ( id ,[job], GrossValue)VALUES  ( 4,5, 200)  

CREATE Table AdditionalEstimate (id INT, [job] INT, [GrossValue] DECIMAL (18,2))  
INSERT INTO AdditionalEstimate ( id ,[job], GrossValue)VALUES  ( 1,1, 100)  
INSERT INTO AdditionalEstimate ( id ,[job], GrossValue)VALUES  ( 2,2, 100)  
INSERT INTO AdditionalEstimate ( id ,[job], GrossValue)VALUES  ( 3,5, 100)  
INSERT INTO AdditionalEstimate ( id ,[job], GrossValue)VALUES  ( 4,5, 100)

CREATE Table [Order] (id INT, [job] INT, ContractGiven DECIMAL (18,2), InvoicesReceived DECIMAL (18,2))
INSERT INTO [Order] ( id ,[job], ContractGiven,InvoicesReceived)VALUES  ( 1,1, 50, 0)  
INSERT INTO [Order] ( id ,[job], ContractGiven,InvoicesReceived)VALUES  ( 2,2, 150, 0)  
INSERT INTO [Order] ( id ,[job], ContractGiven,InvoicesReceived)VALUES  ( 3,3, 50, 0)  
INSERT INTO [Order] ( id ,[job], ContractGiven,InvoicesReceived)VALUES  ( 4,4, 150, 0)  
INSERT INTO [Order] ( id ,[job], ContractGiven,InvoicesReceived)VALUES  ( 5,5, 400, 0)  
INSERT INTO [Order] ( id ,[job], ContractGiven,InvoicesReceived)VALUES  ( 6,5, 100, 0)  

为了易于查看应该得到什么结果,我添加了下表并更新了表中的插入内容。

JOB估算附加估算订单
1空100 50
2空100150
3100空50
4100空150
5200100500
5200100 NA

为简单起见,InvoicedRecieved被忽略。 作业2,4应该归还。 理查德1,珍妮1

我还需要另一条语句来带回符合上述条件的每个用户的作业数量。

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

由于笛卡尔积,戈登在比较之前求和是正确的。但是,由于他的查询不能产生正确的结果,因此这里的查询可以。

select J.id [Job]
  , coalesce(O.InvoicesReceived,0) InvoicesReceived
  , coalesce(O.ContractGiven,0) ContractGiven
  , coalesce(E.GrossValue,0) + coalesce(A.GrossValue,0) TotalQuoteCost
from Job J
left join (select Job, SUM(O.InvoicesReceived) InvoicesReceived, SUM(O.ContractGiven) ContractGiven from [Order] O group by Job) O on O.Job = J.id
left join (select Job, SUM(E.GrossValue) GrossValue from Estimate E group by Job) E on E.Job = J.id
left join (select Job, SUM(A.GrossValue) GrossValue from AdditionalEstimate A group by Job) A on A.Job = J.id
where (
  coalesce(O.InvoicesReceived,0) > (coalesce(E.GrossValue,0) + coalesce(A.GrossValue,0))
) OR ( 
  coalesce(O.ContractGiven,0) > (coalesce(E.GrossValue,0) + coalesce(A.GrossValue,0))
)

select [User Name], count(*)
from (
  select U.UserName [User Name], J.id
    , coalesce(O.InvoicesReceived,0) InvoicesReceived
    , coalesce(O.ContractGiven,0) ContractGiven
    , coalesce(E.GrossValue,0) + coalesce(A.GrossValue,0) TotalQuoteCost
  from Job J
  inner join [User] U on U.id = J.UserId
  left join (select Job, SUM(O.InvoicesReceived) InvoicesReceived, SUM(O.ContractGiven) ContractGiven from [Order] O group by Job) O on O.Job = J.id
  left join (select Job, SUM(E.GrossValue) GrossValue from Estimate E group by Job) E on E.Job = J.id
  left join (select Job, SUM(A.GrossValue) GrossValue from AdditionalEstimate A group by Job) A on A.Job = J.id
  where (
    coalesce(O.InvoicesReceived,0) > (coalesce(E.GrossValue,0) + coalesce(A.GrossValue,0))
  ) OR ( 
    coalesce(O.ContractGiven,0) > (coalesce(E.GrossValue,0) + coalesce(A.GrossValue,0))
  )
) x
group by [User Name]

DB Fiddle

答案 1 :(得分:0)

您需要在join之前汇总

select U.UserName, count(*)
from [User] U join
     Job j
     on u.id = j.userid left join
     (select o.job, sum(O.InvoicesReceived) as InvoicesReceived,
             sum(o.ContractGiven) as ContractGiven
      from [Order] O
      group by o.job
     ) o
     on o.Job = j.id left join
     (select e.job, sum(e.GrossValue) as GrossValue
      from estimate e
      group by e.job
     ) e
     on e.Job = j.id left join
     (select ae.job, sum(ae.GrossValue) as GrossValue
      from AdditionalEstimate ae
      group by ae.job
     ) ae
     on ae.Job = j.id
group by U.UserName
having coalesce(sum(o.InvoicesReceived), 0) > coalesce(sum(e.GrossValue), 0) + coalesce(sum(ae.GrossValue), 0) or
       coalesce(sum(o.ContractGiven), 0) > coalesce(sum(e.GrossValue), 0) + coalesce(sum(ae.GrossValue), 0);

否则,join s将产生笛卡尔积,而sum() s将关闭。

Here是db <>小提琴。

如果您想要满足以下条件的工作:

select U.UserName, j.id
from [User] U join
     Job j
     on u.id = j.userid left join
     (select o.job, sum(O.InvoicesReceived) as InvoicesReceived,
             sum(o.ContractGiven) as ContractGiven
      from [Order] O
      group by o.job
     ) o
     on o.Job = j.id left join
     (select e.job, sum(e.GrossValue) as GrossValue
      from estimate e
      group by e.job
     ) e
     on e.Job = j.id left join
     (select ae.job, sum(ae.GrossValue) as GrossValue
      from AdditionalEstimate ae
      group by ae.job
     ) ae
     on ae.Job = j.id
where coalesce(o.InvoicesReceived, 0) > coalesce(e.GrossValue, 0) + coalesce(ae.GrossValue, 0) or
      coalesce(o.ContractGiven, 0) > coalesce(e.GrossValue, 0) + coalesce(ae.GrossValue, 0);

哪个拥有this分贝小提琴。