SQL查询构造

时间:2017-07-01 08:46:13

标签: sql-server sql-server-2012

我有三张桌子。我想从所有这些表中获取数据并将其放入虚拟表中。我正在使用SQL Server 2012。 很抱歉,如果我的格式或标签错误,因为我收到错误Stack overflow requires external javascrip from another source domain, which is blocked of failed to load.

预订表

BookingId   |   date
======================
2           |   7/1/2017 (MM/dd/yyyy)
3           |   7/1/2017

BookingCost表

Id  |   bookinId | Cost
==========================
1   |   2        | 2000
2   |   3        | 4000

费用表

Id  |   ExpenseCost | Date
======================
1   |   1400        | 7/2/2017 (MM/dd/yyyy)
2   |   1422        | 7/1/2017
3   |   4000        | 6/3/2017

我希望获得如下表所示的月度结果。

Date        | Expense  | Bookings
===================================
jan/2017    |   0      |    0
feb/2017    |   0      |    0
    .       |   .      |    .
    .       |   .      |    .
    .       |   .      |    .
jun/2017    |   4000   |    0
jul/2017    |   2822   |    6000    
    .       |   .      |    .
    .       |   .      |    .
    .       |   .      |    .

2 个答案:

答案 0 :(得分:1)

这是怎么回事(假设你的日期是DATE类型而不是VARCHAR - 否则你可以转换它们。)

SELECT COALESCE(EXPENSE.MONTH, BOOKINGS.MONTH) [Date], EXPENSE.Cost Expense, BOOKINGS.Cost Bookings
FROM (
  SELECT DATEADD(DD,1-DAY([date]),[date]) MONTH, SUM(Cost) Cost
  FROM Booking
  INNER JOIN BookingCost
    ON Booking.BookingID = BookingCost.BookingID
  GROUP BY DATEADD(DD,1-DAY([date]),[date])
) BOOKINGS
FULL JOIN (
  SELECT DATEADD(DD,1-DAY([date]),[date]) MONTH, SUM(ExpenseCost) Cost
  FROM Expense
  GROUP BY DATEADD(DD,1-DAY([date]),[date])
) EXPENSE
  ON EXPENSE.MONTH = BOOKINGS.MONTH
ORDER BY 1

答案 1 :(得分:0)

要获得0计数,您可以将总计加入计数表,该计数表包含一年中的所有月份。

sql使用FORMAT转换日期

例如:

;WITH MONTHS AS
(
  select 
  [Year], [Month], 
  format(datefromparts([Year],[Month],1),'MMM/yyyy') as [MonthYear]
  from (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) m([Month])
  cross join (values (2017)) y([Year])
)
select 
 m.[MonthYear] as [Date], 
 coalesce(e.TotalExpense,0) as Expense,
 coalesce(bc.TotalCost,0) as Bookings
from MONTHS m
left join (
 select 
  datepart(year,[Date]) as [Year],
  datepart(month,[Date]) as [Month],
  sum(ExpenseCost) as TotalExpense
 from Expense
 where datepart(year,[Date]) in (select distinct [Year] from MONTHS)
 group by datepart(year,[Date]), datepart(month,[Date])
)e on (e.[Year] = m.[Year] and e.[Month] = m.[Month])
left join (
   select 
    datepart(year,b.[date]) as [Year],
    datepart(month,b.[date]) as [Month],
    sum(c.Cost) as TotalCost
   from Booking b
   join BookingCost c on c.BookingId = b.BookingId
   where datepart(year,b.[date]) in (select distinct [Year] from MONTHS)
   group by datepart(year,b.[date]), datepart(month,b.[date])
) bc 
on (bc.[Year] = m.[Year] and bc.[Month] = m.[Month])
order by m.[Year], m.[Month];

我用过的测试数据

declare @Booking table (BookingId int, [date] date);
insert into @Booking (BookingId,[date]) values (2,'2017-07-01'),(3,'2017-07-01');
declare @BookingCost table (Id int, BookingId int, Cost int);
insert into @BookingCost (Id, BookingId, Cost) values (1,2,2000),(2,3,4000);
declare @Expense table (Id int, ExpenseCost int, [Date] date);
insert into @Expense (Id, ExpenseCost, [Date]) values
(1,1400,'2017-07-02'),(2,1422,'2017-07-01'),(3,4000,'2017-06-03');