I have a stored procedure in which I joined two queries. my queries are
我有一个存储过程,我加入了两个查询。我的疑问是
alter PROCEDURE test
@SDate datetime,
@EDate datetime
As
如
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
(Select count(quoteid) as TotalQuote, sum(totalamount) as QuoteAmount from dbo.QuoteBase
where CreatedOn BETWEEN @SDate AND @EDate)
union All
(select count(salesorderid)as TotalOrders, sum(totalamount) as OrderAmount from dbo.SalesOrderBase Where
CreatedOn BETWEEN @SDate AND @EDate)
and I got result in to column
我得到了结果列
Total Quote Quote Amount
17 700
118 5000
but I want result like
但我想要结果
Total Quote Quote Amount Total Orders Order Amount
17 700 118 5000
If anyone have idea please share with me
如果有人有想法请与我分享
2 个解决方案
#1
5
Try this one -
试试这个 -
ALTER PROCEDURE dbo.usp_test
@SDate DATETIME
, @EDate DATETIME
AS BEGIN
SET NOCOUNT ON;
SELECT
t.TotalQuote
, t.QuoteAmount
, t2.TotalOrders
, t2.OrderAmount
FROM (SELECT a = 1) a
CROSS JOIN (
SELECT
TotalQuote = COUNT(quoteid)
, QuoteAmount = SUM(totalamount)
FROM dbo.QuoteBase
WHERE CreatedOn BETWEEN @SDate AND @EDate
) t
CROSS JOIN (
SELECT
TotalOrders = COUNT(salesorderid)
, OrderAmount = SUM(totalamount)
FROM dbo.SalesOrderBase
WHERE CreatedOn BETWEEN @SDate AND @EDate
) t2
END
Update:
更新:
SELECT
t.TotalQuote
, t.QuoteAmount
, t2.TotalOrders
, t2.OrderAmount
FROM (
SELECT
TotalQuote = COUNT(quoteid)
, QuoteAmount = SUM(totalamount)
FROM dbo.QuoteBase
WHERE CreatedOn BETWEEN @SDate AND @EDate
) t
FULL OUTER JOIN
(
SELECT
TotalOrders = COUNT(salesorderid)
, OrderAmount = SUM(totalamount)
FROM dbo.SalesOrderBase
WHERE CreatedOn BETWEEN @SDate AND @EDate
) t2 ON 1 = 1
#2
4
You could use a cross join
instead of a union
:
您可以使用交叉连接而不是联合:
select q1.TotalQuote
, q1.QuoteAmount
, q2.TotalOrders
, q2.OrderAmount
from (
... first query ...
) q1
cross join
(
... second query ...
) q2
#1
5
Try this one -
试试这个 -
ALTER PROCEDURE dbo.usp_test
@SDate DATETIME
, @EDate DATETIME
AS BEGIN
SET NOCOUNT ON;
SELECT
t.TotalQuote
, t.QuoteAmount
, t2.TotalOrders
, t2.OrderAmount
FROM (SELECT a = 1) a
CROSS JOIN (
SELECT
TotalQuote = COUNT(quoteid)
, QuoteAmount = SUM(totalamount)
FROM dbo.QuoteBase
WHERE CreatedOn BETWEEN @SDate AND @EDate
) t
CROSS JOIN (
SELECT
TotalOrders = COUNT(salesorderid)
, OrderAmount = SUM(totalamount)
FROM dbo.SalesOrderBase
WHERE CreatedOn BETWEEN @SDate AND @EDate
) t2
END
Update:
更新:
SELECT
t.TotalQuote
, t.QuoteAmount
, t2.TotalOrders
, t2.OrderAmount
FROM (
SELECT
TotalQuote = COUNT(quoteid)
, QuoteAmount = SUM(totalamount)
FROM dbo.QuoteBase
WHERE CreatedOn BETWEEN @SDate AND @EDate
) t
FULL OUTER JOIN
(
SELECT
TotalOrders = COUNT(salesorderid)
, OrderAmount = SUM(totalamount)
FROM dbo.SalesOrderBase
WHERE CreatedOn BETWEEN @SDate AND @EDate
) t2 ON 1 = 1
#2
4
You could use a cross join
instead of a union
:
您可以使用交叉连接而不是联合:
select q1.TotalQuote
, q1.QuoteAmount
, q2.TotalOrders
, q2.OrderAmount
from (
... first query ...
) q1
cross join
(
... second query ...
) q2