-3

我正在尝试加入这两个表,但在使用任何联接时都会出错。Union 将起作用,但返回两行中的所有值,而不是我需要的 3。这是我到目前为止所拥有的,它返回的是它下面。任何帮助都会很棒:)

select C.CustId ,SUM(Ia.Amount) as total
from Invoice I 
    inner join InvoiceAmtSummary Ia  on I.GUIDInvoice=Ia.GUIDInvoice
    inner join Customer C on  C.GUIDCustomer=I.GUIDCustomer
WHERE DATEPART(mm, I.InvoiceDate) = DATEPART(mm, DATEADD(mm, -1, getdate()))
and DATEPART(yyyy, I.InvoiceDate) = DATEPART(yyyy, DATEADD(yyyy, -0, getdate()))
group by C.CustId
union
select C.CustId ,SUM(Ia.Amount) as total2
from Invoice I 
    inner join InvoiceAmtSummary Ia  on I.GUIDInvoice=Ia.GUIDInvoice
    inner join Customer C on  C.GUIDCustomer=I.GUIDCustomer
WHERE DATEPART(mm, I.InvoiceDate) = DATEPART(mm, DATEADD(mm, -2, getdate()))
and DATEPART(yyyy, I.InvoiceDate) = DATEPART(yyyy, DATEADD(yyyy, -0, getdate()))
group by C.CustId

返回的结果在下面,所需的结果在下面

Custid 总
a2
b 9
b 12
16
d 3
d 12

Custid 总计 总计 2
a2         
乙 9 12
16
d 3 12
 
4

3 回答 3

0

Since you are using MySQL, I would approach by implementing in-line sqlvariables (via my first "from" alias. The @FirstOfThisMonth is computed by subtracting whatever the current day of month is from the current date (less 1).

Ex: Today is April 3rd (3rd day of month). So, -3 = March 31... PLUS 1 brings back to April 1 of current month. Once that date is determined, I can now subtract an entire month for first of PREVIOUS month (March 1st), and subtract 2 months for Feb 1st.

Now that this complexity is done ONCE, we can use these variables instead of constant datepart extraction for month and year. Especially if you consider trying to do two months ago from Feb 10th. (First of current is Feb 1, one month back is Jan 1, two months back is Dec 1 of prior year).

NOW, I simply SUM() based on a case when condition.

if the Invoice Date is >= @TwoMonthsAgo AND LESS THAN @LastMonth, that goes into the two-months-ago total. the next is Invoice Date >= @LastMonth AND LESS THAN @FirstOfThisMonth for last month's total.

Now, you have a rolling two-month window... If you wanted to go longer, just add another monthly variable reference and SUM() its criteria.

Finally the WHERE clause can only include invoices that are greater or equal to the @TwoMonthsAgo so it doesn't have to go through the entire database

select 
      C.CustId,
      SUM( CASE when I.InvoiceDate >= @TwoMonthsAgo AND I.InvoiceDate < @LastMonth
           then Ia.Amount else 0 end ) as TotalTwoMonthsAgo,
      SUM( CASE when I.InvoiceDate >= @LastMonth AND I.InvoiceDate < @FirstOfThisMonth
           then Ia.Amount else 0 end ) as TotalLastMonth
   from 
      ( select @FirstOfThisMonth := date_add( now(), INTERVAL -dayofmonth(now())+1 day ),
               @LastMonth := date_add( @myDate, INTERVAL -1 month ),
               @TwoMonthsAgo := date_add( @myDate, INTERVAL -2 month )) sqlvars,
      Invoice I 
         inner join InvoiceAmtSummary Ia  
            on I.GUIDInvoice = Ia.GUIDInvoice
         inner join Customer C 
            on  C.GUIDCustomer = I.GUIDCustomer
   WHERE 
          I.InvoiceDate >= @TwoMonthsAgo
      AND I.InvoiceDate < @FirstOfThisMonth
   group by 
      C.CustID
于 2013-04-03T19:40:02.757 回答
0
select c.custid, c.total, c2.total2
from (select C.CustId ,SUM(Ia.Amount) as total
  from Invoice I 
      inner join InvoiceAmtSummary Ia  on I.GUIDInvoice=Ia.GUIDInvoice
      inner join Customer C on  C.GUIDCustomer=I.GUIDCustomer
  WHERE DATEPART(mm, I.InvoiceDate) = DATEPART(mm, DATEADD(mm, -1, getdate()))
  and DATEPART(yyyy, I.InvoiceDate) = DATEPART(yyyy, DATEADD(yyyy, -0, getdate()))
  group by C.CustId) c
 inner join
  (select C.CustId ,SUM(Ia.Amount) as total2
  from Invoice I 
      inner join InvoiceAmtSummary Ia  on I.GUIDInvoice=Ia.GUIDInvoice
      inner join Customer C on  C.GUIDCustomer=I.GUIDCustomer
  WHERE DATEPART(mm, I.InvoiceDate) = DATEPART(mm, DATEADD(mm, -2, getdate()))
  and DATEPART(yyyy, I.InvoiceDate) = DATEPART(yyyy, DATEADD(yyyy, -0, getdate()))
  group by C.CustId) c2 on c.custid = c2.custid
于 2013-04-03T18:45:39.433 回答
0

我认为您正在尝试拥有 2 列不同月份的 SUM。这是您可以执行的操作:

Select C.CustId ,
Sum(Case DATEPART(mm, I.InvoiceDate) 
When  DATEPART(mm, DATEADD(mm, -1, getdate())) Then Ia.Amount 
Else 0 End) As 'Total1',
Sum(Case DATEPART(mm, I.InvoiceDate) 
When  DATEPART(mm, DATEADD(mm, -2, getdate())) Then Ia.Amount 
Else 0 End) As 'Total2'
from Invoice I 
    inner join InvoiceAmtSummary Ia  on I.GUIDInvoice=Ia.GUIDInvoice
    inner join Customer C on  C.GUIDCustomer=I.GUIDCustomer
WHERE 
 DATEPART(yyyy, I.InvoiceDate) = DATEPART(yyyy, DATEADD(yyyy, -0, getdate()))
group by C.CustId

我正在使用 Case 来避免递归连接。

于 2013-04-03T18:56:03.333 回答