为什么我的查询中没有交叉加入工作?

时间:2018-05-22 06:44:25

标签: sql sql-server tsql sql-server-2012

我试图获取inv.ServicePrice的总和而不使用sum和group by,但我的查询不起作用。

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'.

**

3 个答案:

答案 0 :(得分:2)

这是你的问题:

from dbo.Invoices inc as TotalCost

SQL Server将隐式使用AS关键字对您的表进行别名。因此,您的查询实际上被视为如下:

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语句,并在查询中删除了冗余转换运算符,但不需要它们。

答案 1 :(得分:0)

您使用两次别名,然后使用第一个别名
修复:

(Select SUM(inc.ServicePrice) as TotalCost from dbo.Invoices inc)

答案 2 :(得分:0)

只需删除' AS'用于'作为TotalCost'在交叉加入

<DepositNotificationFile>
    <Header>
        <BankName>SAMA</BankName>
        <CashCenterName>SAMA Riyadh</CashCenterName>
    </Header>
    <DepositList>
        <ReferenceNumber>000000001</ReferenceNumber>
        <Carrier>
            <CarrierName>tns1:Commercial CIT</CarrierName>
            <CarrierNumber>10001</CarrierNumber>
            <CarrierLocationName>tns1:Riyadh</CarrierLocationName>
            <CarrierLocationNumber>100011</CarrierLocationNumber>
            <CarrierLocationRouteName>tns1:R1</CarrierLocationRouteName>
            <CarrierLocationRouteNumber>R1</CarrierLocationRouteNumber>
        </Carrier>
        <Customer>
            <AccountNumber>ISB</AccountNumber>
            <LocationNumber>10065100</LocationNumber>
        </Customer>
        <ContainerList>
            <ContainerNumber>903000033102</ContainerNumber>
            <ContainerContentList>
                <ContentCategory>
                    <ISOCurrencyCode>SAR</ISOCurrencyCode>
                    <InventoryType>Fit Currency</InventoryType>
                    <InventorySubType>Fit</InventorySubType>
                </ContentCategory>
                <ContentCategoryItemList>
                    <ItemFaceValue>5.0000</ItemFaceValue>
                    <ItemCount>10000</ItemCount>
                    <DeclaredAmount>50000.00</DeclaredAmount>
                    <CategoryItemUnitList>
                        <InventoryUnitName>Bundle</InventoryUnitName>
                        <UnitQuantity>10</UnitQuantity>
                        <UnitAmount>5000.0</UnitAmount>
                        <UnitWeight />
                        <MeasurementUnit />
                        <BeginSerialNumber />
                        <Series />
                        <EndSerialNumber />
                    </CategoryItemUnitList>
                </ContentCategoryItemList>
            </ContainerContentList>
            <DeclaredAmount>50000.00</DeclaredAmount>
        </ContainerList>
        <ContainerList>
            <ContainerNumber>903000033103</ContainerNumber>
            <ContainerContentList>
                <ContentCategory>
                    <ISOCurrencyCode>SAR</ISOCurrencyCode>
                    <InventoryType>Fit Currency</InventoryType>
                    <InventorySubType>Fit</InventorySubType>
                </ContentCategory>
                <ContentCategoryItemList>
                    <ItemFaceValue>10.0000</ItemFaceValue>
                    <ItemCount>10000</ItemCount>
                    <DeclaredAmount>100000.00</DeclaredAmount>
                    <CategoryItemUnitList>
                        <InventoryUnitName>Bundle</InventoryUnitName>
                        <UnitQuantity>10</UnitQuantity>
                        <UnitAmount>10000.0</UnitAmount>
                        <UnitWeight />
                        <MeasurementUnit />
                        <BeginSerialNumber />
                        <Series />
                        <EndSerialNumber />
                    </CategoryItemUnitList>
                </ContentCategoryItemList>
            </ContainerContentList>
            <DeclaredAmount>100000.00</DeclaredAmount>
        </ContainerList>
        <PreparedBy>FZE</PreparedBy>
        <TotalContainerCount>2</TotalContainerCount>
        <ExpectedDate>2018-04-19T09:13:10-05:00</ExpectedDate>
        <DeclaredAmount>150000.00</DeclaredAmount>
    </DepositList>
</DepositNotificationFile>