在日期与查询之间搜索有计算和计数器

时间:2010-08-11 18:13:34

标签: sql-server-2005 tsql

大家好,我对诊断计算和计数器有很大的疑问

SELECT nc.ID AS ClinicID, nc.Name AS ClinicName, 
       SUM(cr.CountRecept * cs.Price) AS TotalPriceService, SUM(cr.TotalPaid) AS TotalPaid, 
       SUM(cs.Price * cr.Company_Percentage / 100) AS TotalInsurance, 
       SUM(cr.CountRecept) AS TotalCountRecept
FROM ClinicsServices AS cs INNER JOIN
       (SELECT  tc.Date_Write, COUNT(ID) AS CountRecept, Clinic_Service_ID,  
        company_Percentage, Company_ID, SUM(Paid_Patient) AS TotalPaid
        FROM dbo.TicketsClinics AS tc WHERE  (Status = 1) 
        GROUP BY Clinic_Service_ID, Company_Percentage, Company_ID, tc.Date_Write) AS cr ON  
        cs.ID = cr.Clinic_Service_ID INNER JOIN
       (SELECT  ID, NAME FROM dbo.Clinics AS c GROUP BY ID, Name) AS nc ON cs.Clinic_ID = c.ID
GROUP BY nc.Name, nc.ID

这是真正的查询,但我希望在日期之间添加

 AND tc.Date_Write BETWEEN tc.Date_Write AND tc.Date_Write
子查询中的

 Select tc.Date_Write
 Group by tc.Date_Write
主查询中的

喜欢这个

   SELECT nc.ID AS ClinicID, nc.Name AS ClinicName, 
          SUM(cr.CountRecept * cs.Price) AS TotalPriceService, 
          SUM(cr.TotalPaid) AS TotalPaid, 
          SUM(cs.Price * cr.Company_Percentage / 100) AS TotalInsurance, 
          SUM(cr.CountRecept) AS TotalCountRecept, cr.Date_Write
   FROM dbo.ClinicsServices AS cs INNER JOIN
          (SELECT tc.Date_Write, COUNT(ID) AS CountRecept, Clinic_Service_ID,  
                  Company_Percentage, Company_ID, SUM(Paid_Patient) AS TotalPaid
           FROM dbo.TicketsClinics AS tc
           WHERE  (Status = 1) AND tc.Date_Write BETWEEN tc.Date_Write AND tc.Date_Write 
           GROUP BY Clinic_Service_ID, Company_Percentage, Company_ID, tc.Date_Write) 
                     AS cr ON cs.ID = cr.Clinic_Service_ID 
           INNER JOIN (SELECT  ID, NAME FROM dbo.Clinics AS c GROUP BY ID, Name) 
                              AS nc ON  cs.Clinic_ID = nc.ID
           GROUP BY nc.Name, nc.ID, cr.Date_Write

这是错误的查询为什么因为它显示每个收据但我想显示 1 - TotalPriceService 2 - TotalPaid 3 - TotalInsurance 4 - TotalCounterReceipt 5 - FromDate 6 - ToDate

返回计算和计数器的真实查询我希望按日期添加搜索我知道第二个查询是错误但我想按日期搜索BETWEEN tc.Date_Write FROMDATE AND TODATE这是怎么感谢你的帮助我< /强>

2 个答案:

答案 0 :(得分:1)

您的BETWEEN子句检查日期是否介于其自身之间。每个记录都会返回true。

要正确使用BETWEEN,您需要提供另外两个日期。此查询似乎是具有两个日期参数的存储过程的候选者,“from”日期和“to”日期,如下所示:

CREATE PROCEDURE usp_GetClinicStats(
    @FromDate DATETIME,
    @ToDate DATETIME
    )
AS
BEGIN
  SELECT nc.ID AS ClinicID, nc.Name AS ClinicName, 
          SUM(cr.CountRecept * cs.Price) AS TotalPriceService, 
          SUM(cr.TotalPaid) AS TotalPaid, 
          SUM(cs.Price * cr.Company_Percentage / 100) AS TotalInsurance, 
          SUM(cr.CountRecept) AS TotalCountRecept, cr.Date_Write
   FROM dbo.ClinicsServices AS cs INNER JOIN
          (SELECT tc.Date_Write, COUNT(ID) AS CountRecept, Clinic_Service_ID,  
                  Company_Percentage, Company_ID, SUM(Paid_Patient) AS TotalPaid
           FROM dbo.TicketsClinics AS tc
           WHERE Status = 1
             AND tc.Date_Write BETWEEN CONVERT(VARCHAR, @FromDate, 111) AND CONVERT(VARCHAR, @ToDate, 111) 
           GROUP BY Clinic_Service_ID, Company_Percentage, Company_ID, tc.Date_Write) 
                     AS cr ON cs.ID = cr.Clinic_Service_ID 
           INNER JOIN (SELECT  ID, NAME FROM dbo.Clinics AS c GROUP BY ID, Name) 
                              AS nc ON  cs.Clinic_ID = nc.ID
           GROUP BY nc.Name, nc.ID, cr.Date_Write
END

答案 1 :(得分:0)

     CREATE PROCEDURE usp_GetClinicStats(
@FromDate DATETIME,
@ToDate DATETIME
)
AS
  BEGIN
      SELECT nc.ID AS ClinicID, nc.Name AS ClinicName, 
             SUM(cr.CountRecept * cs.Price) AS TotalPriceService, 
             SUM(cr.TotalPaid) AS TotalPaid, 
             SUM(cs.Price * cr.Company_Percentage / 100) AS TotalInsurance, 
             SUM(cr.CountRecept) AS TotalCountRecept, cr.Date_Write
      FROM dbo.ClinicsServices AS cs INNER JOIN
      (SELECT tc.Date_Write, COUNT(ID) AS CountRecept, Clinic_Service_ID,  
              Company_Percentage, Company_ID, SUM(Paid_Patient) AS TotalPaid
       FROM dbo.TicketsClinics AS tc
       WHERE  (Status = 1) AND convert(varchar,tc.Date_Write,111) BETWEEN  
       convert(varchar,@FromDate,111) AND convert(varchar,@ToDate,111)
       GROUP BY Clinic_Service_ID, Company_Percentage, Company_ID, tc.Date_Write) 
                 AS cr ON cs.ID = cr.Clinic_Service_ID 
       INNER JOIN (SELECT  ID, NAME FROM dbo.Clinics AS c GROUP BY ID, Name) 
                          AS nc ON  cs.Clinic_ID = nc.ID
       GROUP BY nc.Name, nc.ID, cr.Date_Write
    END