我每天使用SQL查询生成一个Excel:
SELECT COUNT(res_id) as TotalReservation
FROM bup_vw_res_reservation
WHERE res_deleted = 0
AND DATEPART(DAY,res_date_start) = 1
每次我想要更改DAY时都会执行此查询。要在我的Excel文件中生成1张纸,我需要在页面中打开和关闭30个连接。如果我需要为另一家公司制作另一张表,那么我需要再次执行相同的30查询。页面变得非常慢:(
有没有其他方法可以在1个查询中获得相同的结果?
答案 0 :(得分:1)
如果你需要总数,你可以做
DATEPART(DAY, res_date_start) BETWEEN 1 AND 30
如果您需要每天的每个预订计数,请执行此操作
SELECT COUNT(res_id) as TotalReservation, DATEPART(DAY,res_date_start)
FROM bup_vw_res_reservation
WHERE res_deleted = 0
GROUP BY DATEPART(DAY,res_date_start)
答案 1 :(得分:0)
是的,按DATEPART(DAY,res_date_start)对结果进行分组:
SELECT COUNT(res_id) as TotalReservation, DATEPART(DAY,res_date_start)
FROM bup_vw_res_reservation
WHERE res_deleted = 0
and res_date_start between beginning_of_month and end_of_month
GROUP BY DATEPART(DAY,res_date_start)
现在您拥有了所需的所有结果,您可以遍历它们以生成每行的Excel电子表格。