Solved

Order By in Union

Posted on 2009-05-18
7
171 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

12 Experts available now in Live!

Get 1:1 Help Now