.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Multiple Order by clauses with Union

Posted By:      Posted Date: September 20, 2010    Points: 0   Category :Sql Server

I'm trying to figure out how to run this code, I have over 40 tables that I need to get data from, the only thing I can think of is to use "Union" , I need to get the last record in each table by StartTS (StartTimeStamp) so I figured the best approach is to use "Select Top (1) and use Order by StartTS to get the data.  This works great if you are only grabbing data from 1 table, however like I said I need to grab from over 40. 

My example uses only 2 tables:

             (SELECT   [Unit Name]
              FROM     dbo.UnitVerificationData
              WHERE   (Unit_Code = dbo.CP_BCELP.Unit_Code)) AS UnitName, Operation_type, StartTS, StopTS
             (SELECT   [Unit Name]
              FROM     dbo.UnitVer

View Complete Post

More Related Resource Links

order by in combination with union


 Hi All,

How can I order this by count? so if it appears above and below the union statement, I want it on top

	SELECT distinct BIER.Naam, BIER_L2.L2_Naam, BIER_L3.L3_Naam from BIER_MATCH, BIER, BIER_L2, BIER_L3

	SELECT distinct BIER.Naam, BIER_L2.L2_Naam, BIER_L3.L3_Naam from BIER_MATCH, BIER, BIER_L2, BIER_L3
	where BIER_L2.L2_ID = BIER.L2_ID and BIER_L2.L3_ID = BIER_L3.L3_ID and RECEPT_ID = @RECEPT_ID 
	and BIER_MATCH.BIER_ID is null


order by sort both select in union



I have DropDownList that Show all groups. in that, I want to show "select one group" as default in DDL. for that i used code below. but it sort all return data and "select one group" will not be the first line. How can i fix it?

Best Regards. Morteza


	SELECT   -1 as id,'select one group' as groupName
	SELECT   id, groupName
	FROM     [group] 
	order by groupNam

Multiple Xpath expressions returned in document order

Hi all

I am using XQuery in SQL Server in my project. I need to retrieve multiple elements located in different places in the document and I need the retrieved elements in the document order.
My program should work with any XML file so I do not have a specific structure. It is just a function that gets multiple XPath expressions and return the retrieved elements in the document order.

For example
   <Book ID="1"/>
   <Book ID="2"/>
   <Book ID="3">
       <Chapter ID="1"/>

Assuming the previous xml is stored in column x in Table.

select x.query('
') as result
from Table where ID =1

the previous query will return the elements in the order they have in the query not the order they have in the document, the result is
  <Book ID="2"/>
<Chapter ID="1"/>
<Book ID="1"/>

When retrieving multiple elements using one Xpath expressions, SQL serve



It is interesting but when I try to execute query with union word and ORDER BY like that:

Select EmpId, OrderDate, OrderId
From [TestSQL].[Sales].[Orders]
SELECT TOP 2 EmpId, OrderDate, OrderId From [TestSQL].[Sales].[Orders] WHERE EmpId=3
Select EmpId, OrderDate, OrderId
From [TestSQL].[Sales].[Orders]
SELECT TOP 2 EmpId, OrderDate, OrderId From [TestSQL].[Sales].[Orders] WHERE EmpId=5
I get the mistake:

Incorrect syntax near the keyword 'UNION'.

But when I use derived table like that :

How to set the order of custom fields in custom list definition of Multiple Content Types


Hi all,

First of all I am sorry for my poor english.

I have a custom list definition and I have mentioned two content types in the "Schema.xml" file of this list. I need to change the order of fields in NewForm.aspx and EditForm.aspx.

For e.g. I have two content types ContentType1 and ContentType2. ContentType2 is inheriting ContentType1.

Fields of ContentType1 are

1. Field1

2. Field2


and Fields of ContentType2 are

1. Field3

2 Field4


When these fields display on new form the order of fields is

1. Field1

2. Field2

3. Field3

4. Field4


But I need to change the order of field by

1. Field1

2. Field3

3. Field2

4. Field4


Can anyone help me to change the display order of fields in new and edit form?


Thanks & Regards,



Using Union and Order in query


SQL Server 2008. Guys,  I need to do following. I have tables Employees(empid,country,region,city) and Suppliers(supplyid,country,region,city). So I have a query:

SELECT country, region, city
FROM HR.Employees


SELECT country, region, city
FROM Production.Suppliers;

So I need

to add logic to the query 
such that it would guarantee that the rows from Employees
would be returned in the output before the rows from Customers,
and within each segment, the rows sh

Selecting data from multiple tables whilst using a union statement.



any help on this would be great.

I basically have a series of tables from one database that have an identical structure thus making retrieval of all records fairly easy (I just use a UNION ALL statement). However I need a list of values from another table within a different  database that contains information regarding the group of the data.

I so far have the following:

SELECT Table1.* FROM Table1 UNION ALL SELECT Table2.* FROM Table2 UNION ALL SELECT Table3.* FROM Table3 UNION SELECT Database.dbo.SRFILE.SR_GROUP FROM Database.dbo.SRFILE INNER JOIN Database.dbo.SRFILE.SRONUMBER = Table1.Incidentx

I keep receiving a unable to parse message however all I need to retreive is the SR_GROUP value but just don't know the correct syntax. Is it actually possible to do this as the structure of SRFILE is not the same as Table1/2/3.

Thanks again,


Multiple database support with Entity Framework

One of the features introduced in Entity Framework is being database independent. Which mean each database provider can support Entity Framework by implementing its provider.

This feature allows you build applications independent from the underplaying database provider. In this post I'm going to show how to build an application that support multiple databases using same conceptual model made by Entity Framework.

Binding the multiple drop down boxes in GridView using JQuery in ASP.NET

Here is the code for binding the multiple drop down boxes in GridView using JQuery in ASP.NET

Upload Multiple File using JQuery in asp.net

In this article I have explained how to upload multiple files using file upload control. I have used jQuery plugin for uploading multiple files.

I have also explained how to check for file type, file maximum size limit using jQuery & also using server side validation code.

Adding Multiple Columns and Rows in GridView without using a Database

This article describes on how to add multiple columns and rows in GridView without using a database. Basically the GridView will be populated with data based on the values entered in each TextBoxes on Button Click and retain the GridView data on post back.

GridView Multiple Filter AJAX Control

This AJAX control enables the user to filter data within any column inside a GridView. The user can add multiple filters and can delete existing ones too.

Multiple Column Dropdownlist for the ASP.NET DataGrid

Based on my previous control "Multiple Column DropDownList for ASP.NET", I received many emails asking for the same control to be used in the DataGrid for web applications. Here we go.. This control can be used as the regular MS DropDownList in the DataGrid and also as a regular dropdownlist. It has all the properties, like DataTextField, DataValueField, DataSource, SelectedIndex etc. The download file contains the samples both in VB.NET and C#. In this sample, I have used the Northwind database of SQL Server.

ASP.NET Multiple Selection DropDownList with AJAX HoverMenuExtender

Recently, I was looking for a multiple selection dropdownlist control for my new project. After spending some time researching for it, I decided to put together all my finding in one web user control. This web user control consists of an ASP.NET AJAX HoverMenuExtender, JavaScript, StyleSheet and CheckBoxListExCtrl . The final product will work with or without a MasterPage and you can drag and drop more than one instances of the control on to the page.

jquery autocomplete multiple values


i got some samples and i did autocomplete using jquery , its working well for single input values

but i need to pass multiples values after one by one in the textbox. Is it possible to pass multiple

values in the textbox for autocomplete in jquery in asp.net?


Gmail style multiple Sign-in type combo...is it possible in asp.net?


I am developing a multi-company application and want my users to give the functionality like the following to switch companies.


Is this possible? then how?

Please help


Multiple Instances of User Control


I have a user control that I place on a page severl times.  When I make a selection on a ddl in one one control the SelectedIndexChanged event fires for all the control on the page for that ddl.  So I get duplication of an insert that I do.  Is there a way to make sure these events are isolated so this doesn't happen.


ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend