Sql 为什么';在我的查询中不交叉连接工作?
我试图在不使用sum和group by的情况下获取inv.ServicePrice的总和,但我的查询不起作用Sql 为什么';在我的查询中不交叉连接工作?,sql,sql-server,tsql,sql-server-2012,Sql,Sql Server,Tsql,Sql Server 2012,我试图在不使用sum和group by的情况下获取inv.ServicePrice的总和,但我的查询不起作用 ALTER PROCEDURE [dbo].[ServicesDetailedReport] @FromDate date= '01-Jun-2010', @ToDate date= null AS BEGIN Set @ToDate= case when @ToDate IS NULL then Convert(varchar(11
ALTER PROCEDURE [dbo].[ServicesDetailedReport]
@FromDate date= '01-Jun-2010',
@ToDate date= null
AS
BEGIN
Set @ToDate= case when @ToDate IS NULL then Convert(varchar(11), getdate(), 106) else @ToDate end
Select inv.InvoiceNo, inv.EntryDateTime, s.ServiceName, c.VehicleRegNo, inv.ServicePrice, c.CustomerName, inv.fk_BookingID
from dbo.[Services] s
inner join invoices inv
on inv.fk_ServiceID= s.ServiceID
inner join customers c
on c.CustomerID= inv.fk_CustomerID
Cross join (Select SUM(inc.ServicePrice) as TotalCost from dbo.Invoices inc as TotalCost) t
where Convert(varchar(11), inv.EntryDateTime, 106) between @FromDate and @ToDate
END
更新:
**
Msg 156, Level 15, State 1, Procedure ServicesDetailedReport, Line 23
Incorrect syntax near the keyword 'as'.
**这是你的问题:
from dbo.Invoices inc as TotalCost
SQL Server将隐式地将您的表别名,而不将作为
关键字。因此,您的查询实际上按如下方式处理:
from dbo.Invoices AS inc AS TotalCost
^ ^
| |
| |
| |
基本上,您的表有两次别名,这是无效的语法
因此,这将是更新和正确的程序代码:
ALTER PROCEDURE dbo.ServicesDetailedReport
@FromDate DATE = '01-Jun-2010',
@ToDate DATE = NULL
AS
BEGIN
SET @ToDate = ISNULL(@ToDate, GETDATE());
SELECT inv.InvoiceNo, inv.EntryDateTime, s.ServiceName, c.VehicleRegNo, inv.ServicePrice, c.CustomerName, inv.fk_BookingID
FROM dbo.services AS s
INNER JOIN invoices AS inv
ON inv.fk_ServiceID = s.ServiceID
INNER JOIN customers AS c
ON c.CustomerID = inv.fk_CustomerID
CROSS JOIN ( SELECT SUM(inc.ServicePrice) AS TotalCost
FROM dbo.Invoices AS inc) AS t
WHERE inv.EntryDateTime BETWEEN @FromDate AND @ToDate;
END;
另外请注意,我用一个简单的ISNULL()
替换了您的case语句,并删除了查询中的冗余convert运算符,它们是不需要的。这是您的问题:
from dbo.Invoices inc as TotalCost
SQL Server将隐式地将您的表别名,而不将作为
关键字。因此,您的查询实际上按如下方式处理:
from dbo.Invoices AS inc AS TotalCost
^ ^
| |
| |
| |
基本上,您的表有两次别名,这是无效的语法
因此,这将是更新和正确的程序代码:
ALTER PROCEDURE dbo.ServicesDetailedReport
@FromDate DATE = '01-Jun-2010',
@ToDate DATE = NULL
AS
BEGIN
SET @ToDate = ISNULL(@ToDate, GETDATE());
SELECT inv.InvoiceNo, inv.EntryDateTime, s.ServiceName, c.VehicleRegNo, inv.ServicePrice, c.CustomerName, inv.fk_BookingID
FROM dbo.services AS s
INNER JOIN invoices AS inv
ON inv.fk_ServiceID = s.ServiceID
INNER JOIN customers AS c
ON c.CustomerID = inv.fk_CustomerID
CROSS JOIN ( SELECT SUM(inc.ServicePrice) AS TotalCost
FROM dbo.Invoices AS inc) AS t
WHERE inv.EntryDateTime BETWEEN @FromDate AND @ToDate;
END;
还要注意的是,我用一个简单的
ISNULL()
替换了您的case语句,并删除了查询中多余的convert运算符,它们是不需要的。您使用两次别名,然后使用第一个别名修正:
您可以使用两个别名,然后使用第一个别名
修正:
只需删除交叉联接中“AS TotalCost”中使用的“AS”
Select inv.InvoiceNo, inv.EntryDateTime, s.ServiceName, c.VehicleRegNo, inv.ServicePrice, c.CustomerName, inv.fk_BookingID
from dbo.[Services] s
inner join invoices inv
on inv.fk_ServiceID= s.ServiceID
inner join customers c
on c.CustomerID= inv.fk_CustomerID
Cross join (Select SUM(inc.ServicePrice) as TotalCost from dbo.Invoices inc ) t
where Convert(varchar(11), inv.EntryDateTime, 106) between @FromDate and @ToDate
只需删除交叉联接中“AS TotalCost”中使用的“AS”
Select inv.InvoiceNo, inv.EntryDateTime, s.ServiceName, c.VehicleRegNo, inv.ServicePrice, c.CustomerName, inv.fk_BookingID
from dbo.[Services] s
inner join invoices inv
on inv.fk_ServiceID= s.ServiceID
inner join customers c
on c.CustomerID= inv.fk_CustomerID
Cross join (Select SUM(inc.ServicePrice) as TotalCost from dbo.Invoices inc ) t
where Convert(varchar(11), inv.EntryDateTime, 106) between @FromDate and @ToDate
它怎么不起作用?你能进一步解释一下吗?没有结果?结果是错误的?为什么要将日期转换为字符串?@Squirrel error updated它是如何工作的?你能进一步解释一下吗?没有结果?结果是错误的?为什么要将日期转换为字符串?@Squirrel error updated