Solved

Order By in Union

Posted on 2009-05-18
7
172 Views
Last Modified: 2012-05-07
I want to add a Order By "O.CreatedOn Desc" to this SQL stored procedure but I keep getting this error:

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

--How can I do this??
select   'Pending',count(*)

from

(

SELECT  

  

O.[Id] AS [Id],

O.[CreatedOn] AS [CreatedOn],

O.[Number] AS [Number],

O.[CustomerName] AS [CustomerName],

O.[BillingPhoneNumber] AS [BillingPhoneNumber], 

O.[Status] AS [Status],  

COALESCE(OHST.[Name], OHS.[Name]) AS [HoldStatus], 

O.[Score] AS [Score], 

SUM(CASE WHEN OI.[Status] = 1 THEN 0 

         ELSE OI.[ItemPrice] - OI.[DiscountAmount] END) 

        - MAX(O.[OrderDiscountAmount]) +  MAX(O.[TaxAmount]) + MAX(O.[ShippingPrice] - O.[ShippingDiscountAmount]) 

        AS [GrandTotal], 

isnull(Uc.[UserName],'') AS [CreatedBy]  

FROM [ORDOrder] O WITH (NOLOCK) 

    LEFT OUTER JOIN [aspnet_Users] Uc WITH (NOLOCK) ON Uc.[UserId] = O.[CreatedBy]  

    INNER JOIN [ORDOrderItem] OI WITH (NOLOCK) ON OI.[OrderId] = O.[Id] 

    LEFT OUTER JOIN [ORDOrderHoldStatus] OHS WITH (NOLOCK) ON OHS.[Id] = O.[HoldStatusId] 

    LEFT OUTER JOIN [ORDOrderHoldStatusText] OHST WITH (NOLOCK) ON OHST.[Id] = OHS.[Id] AND OHST.[LocaleId] = 1033 

WHERE O.[Status] = 2

      And O.SiteID = @SiteID

 

GROUP BY COALESCE(OHST.[Name], OHS.[Name]),

         O.[BillingPhoneNumber], 

         O.[CreatedOn], 

         O.[CustomerName], 

         O.[Id], 

         O.[Number], 

         O.[Score], 

         O.[Status], 

         Uc.[UserName] 
 

) a
 

Union

select  ',10',count(*)

from

(

SELECT

  

O.[Id] AS [Id],

O.[CreatedOn] AS [CreatedOn],

O.[Number] AS [Number],

O.[CustomerName] AS [CustomerName],

O.[BillingPhoneNumber] AS [BillingPhoneNumber], 

O.[Status] AS [Status],  

COALESCE(OHST.[Name], OHS.[Name]) AS [HoldStatus], 

O.[Score] AS [Score], 

SUM(CASE WHEN OI.[Status] = 1 THEN 0 

         ELSE OI.[ItemPrice] - OI.[DiscountAmount] END) 

        - MAX(O.[OrderDiscountAmount]) +  MAX(O.[TaxAmount]) + MAX(O.[ShippingPrice] - O.[ShippingDiscountAmount]) 

        AS [GrandTotal], 

isnull(Uc.[UserName],'') AS [CreatedBy]  

FROM [ORDOrder] O WITH (NOLOCK) 

    LEFT OUTER JOIN [aspnet_Users] Uc WITH (NOLOCK) ON Uc.[UserId] = O.[CreatedBy]  

    INNER JOIN [ORDOrderItem] OI WITH (NOLOCK) ON OI.[OrderId] = O.[Id] 

    LEFT OUTER JOIN [ORDOrderHoldStatus] OHS WITH (NOLOCK) ON OHS.[Id] = O.[HoldStatusId] 

    LEFT OUTER JOIN [ORDOrderHoldStatusText] OHST WITH (NOLOCK) ON OHST.[Id] = OHS.[Id] AND OHST.[LocaleId] = 1033 

WHERE O.[Status] = 2 and O.[Score] < 10

       And O.SiteID = @SiteID

 

GROUP BY COALESCE(OHST.[Name], OHS.[Name]),

         O.[BillingPhoneNumber], 

         O.[CreatedOn], 

         O.[CustomerName], 

         O.[Id], 

         O.[Number], 

         O.[Score], 

         O.[Status], 

         Uc.[UserName] 
 

) a

Open in new window

0
Comment
Question by:Camillia
  • 4
  • 2
7 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24417100
can you explain why you need an order by in your query?
because, all it does in the end is to return 2 rows for the 2 counts ...
0
 
LVL 41

Expert Comment

by:pcelba
ID: 24417138
Union ALL could help
select   'Pending',count(*)

from

(

SELECT   

O.[Id] AS [Id],

O.[CreatedOn] AS [CreatedOn],

O.[Number] AS [Number],

O.[CustomerName] AS [CustomerName],

O.[BillingPhoneNumber] AS [BillingPhoneNumber], 

O.[Status] AS [Status],  

COALESCE(OHST.[Name], OHS.[Name]) AS [HoldStatus], 

O.[Score] AS [Score], 

SUM(CASE WHEN OI.[Status] = 1 THEN 0 

         ELSE OI.[ItemPrice] - OI.[DiscountAmount] END) 

        - MAX(O.[OrderDiscountAmount]) +  MAX(O.[TaxAmount]) + MAX(O.[ShippingPrice] - O.[ShippingDiscountAmount]) 

        AS [GrandTotal], 

isnull(Uc.[UserName],'') AS [CreatedBy]  

FROM [ORDOrder] O WITH (NOLOCK) 

    LEFT OUTER JOIN [aspnet_Users] Uc WITH (NOLOCK) ON Uc.[UserId] = O.[CreatedBy]  

    INNER JOIN [ORDOrderItem] OI WITH (NOLOCK) ON OI.[OrderId] = O.[Id] 

    LEFT OUTER JOIN [ORDOrderHoldStatus] OHS WITH (NOLOCK) ON OHS.[Id] = O.[HoldStatusId] 

    LEFT OUTER JOIN [ORDOrderHoldStatusText] OHST WITH (NOLOCK) ON OHST.[Id] = OHS.[Id] AND OHST.[LocaleId] = 1033 

WHERE O.[Status] = 2

      And O.SiteID = @SiteID

 GROUP BY COALESCE(OHST.[Name], OHS.[Name]),

         O.[BillingPhoneNumber], 

         O.[CreatedOn], 

         O.[CustomerName], 

         O.[Id], 

         O.[Number], 

         O.[Score], 

         O.[Status], 

         Uc.[UserName] 

 ORDER BY O.CreatedOn Desc

) a 

Union All

select  ',10',count(*)

from

(

SELECT

O.[Id] AS [Id],

O.[CreatedOn] AS [CreatedOn],

O.[Number] AS [Number],

O.[CustomerName] AS [CustomerName],

O.[BillingPhoneNumber] AS [BillingPhoneNumber], 

O.[Status] AS [Status],  

COALESCE(OHST.[Name], OHS.[Name]) AS [HoldStatus], 

O.[Score] AS [Score], 

SUM(CASE WHEN OI.[Status] = 1 THEN 0 

         ELSE OI.[ItemPrice] - OI.[DiscountAmount] END) 

        - MAX(O.[OrderDiscountAmount]) +  MAX(O.[TaxAmount]) + MAX(O.[ShippingPrice] - O.[ShippingDiscountAmount]) 

        AS [GrandTotal], 

isnull(Uc.[UserName],'') AS [CreatedBy]  

FROM [ORDOrder] O WITH (NOLOCK) 

    LEFT OUTER JOIN [aspnet_Users] Uc WITH (NOLOCK) ON Uc.[UserId] = O.[CreatedBy]  

    INNER JOIN [ORDOrderItem] OI WITH (NOLOCK) ON OI.[OrderId] = O.[Id] 

    LEFT OUTER JOIN [ORDOrderHoldStatus] OHS WITH (NOLOCK) ON OHS.[Id] = O.[HoldStatusId] 

    LEFT OUTER JOIN [ORDOrderHoldStatusText] OHST WITH (NOLOCK) ON OHST.[Id] = OHS.[Id] AND OHST.[LocaleId] = 1033 

WHERE O.[Status] = 2 and O.[Score] < 10

       And O.SiteID = @SiteID

 GROUP BY COALESCE(OHST.[Name], OHS.[Name]),

         O.[BillingPhoneNumber], 

         O.[CreatedOn], 

         O.[CustomerName], 

         O.[Id], 

         O.[Number], 

         O.[Score], 

         O.[Status], 

         Uc.[UserName] 

 ORDER BY O.CreatedOn Desc

) b

Open in new window

0
 
LVL 41

Expert Comment

by:pcelba
ID: 24417147
Of course, the union in above select does not make sense.
0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
LVL 41

Expert Comment

by:pcelba
ID: 24417151
Not UNION but ORDER BY does not make sense, of course. Sorry.
0
 
LVL 7

Author Comment

by:Camillia
ID: 24417279
I want to bring back rows by order of date. Oldest date first. Why doesnt it make sense??
Let me try the UNion ALL and see. Maybe it doesnt make sense but let me see..
0
 
LVL 7

Author Comment

by:Camillia
ID: 24417287
I think you're right. I posted a wrong statement. Let me post back.
0
 
LVL 41

Expert Comment

by:pcelba
ID: 24417460
Now I understand.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now