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
The Union operator combines the results of two or more queries into a distinct single result set that includes all the rows that belong to all queries in the Union. In this operation, it combines two more queries and removes the duplicates.
Unions combine data into new rows. If two tables are “unioned” together, then the data from the first table is in one set of rows, and the data from the second table in another set. The rows are in the same result.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With