我试图从商店程序中选择结果,它几乎可以工作但问题是它没有选择BusNo,它存在于Transport.Buses表中并且在dbo中是外来的.Tickets为Bus_No
ALTER PROCEDURE [dbo].[ReportConductorBusWiseTransactions]
@FromDate date,
@ToDate date
AS
BEGIN
with myquery ([DateTime],ConductorName,BusNo,amount,Quantity)
as
(
select Convert(varchar(20),serverdatetime,101) 'DateTime',b.BusNo,c.Name,T.amount,1 'Quantity'
from tickets T join transport.Conductors c on c.Conductorid = T.Conductor_ID
join transport.buses b on b.BusID= T.Bus_ID
WHERE CONVERT(DATE,T.ServerDateTime) BETWEEN @FromDate and @ToDate
)
select *, 10 * c.[10Quantity] '10Amount',20 * c.[20Quantity] '20Amount',30 * c.[30Quantity] '30Amount',((10 * c.[10Quantity])+(20 * c.[20Quantity])+(30 * c.[30Quantity]))'GrandTotal' from (
SELECT DateTime,ConductorName,BusNo,[10] AS '10Quantity', [20] AS '20Quantity', [30] AS '30Quantity'
FROM
(SELECT [DateTime], ConductorName,BusNo, amount,quantity
FROM myquery) p
PIVOT
(
COUNT ([Quantity])
FOR [Amount] IN
( [10], [20], [30])
) AS pvt
) as c
END
我无法调整我的巴士, 注意:每个总线都与Conductor相关联,并且都存在于Tickets表中,我试图在他关联的每辆公共汽车上显示总交易,即发票等等,但问题是它没有选择BusNo
答案 0 :(得分:0)
ALTER PROCEDURE [dbo].[ReportConductorBusWiseTransactions] '03-10-2015','03-13-2015'
@FromDate date,
@ToDate date
AS
BEGIN
with myquery ([DateTime],ConductorName,BusNo,amount,Quantity)
as
(
select Convert(varchar(20),serverdatetime,101) 'DateTime',c.Name,b.BusNo,T.amount,1 'Quantity'
from tickets T join transport.Conductors c on c.Conductorid = T.Conductor_ID
join transport.buses b on b.BusID= T.Bus_ID
WHERE CONVERT(DATE,T.ServerDateTime) BETWEEN @FromDate and @ToDate
)
select *, 10 * c.[10Quantity] '10Amount',20 * c.[20Quantity] '20Amount',30 * c.[30Quantity] '30Amount',((10 * c.[10Quantity])+(20 * c.[20Quantity])+(30 * c.[30Quantity]))'GrandTotal' from (
SELECT DateTime,ConductorName,BusNo,[10] AS '10Quantity', [20] AS '20Quantity', [30] AS '30Quantity'
FROM
(SELECT [DateTime], ConductorName,BusNo, amount,quantity
FROM myquery) p
PIVOT
(
COUNT ([Quantity])
FOR [Amount] IN
( [10], [20], [30])
) AS pvt
) as c
END