Showing posts with label order. Show all posts
Showing posts with label order. Show all posts

Monday, March 12, 2012

Migartion issue with ORDER BY Clause

Please help me how to write the following query in SQL Server 2005

SELECT 3,2,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
CONVERT(char(10), fulldate, 126),
CONVERT(char(8), fulldate, 108),
flag = CASE Invalid WHEN 'Y' THEN 2 ELSE 1 END,
srk
FROM #raw
ORDER BY tag, parent, 'Value!3!date' asc

HUH?

What's the error?

And what's

Code Snippet

'Value!3!date' asc

|||

Sorry. i had not copied full query.Actually i am migrating Database from SQL Server 2000 to SQL Server 2005.I used SQL Server 2005 upgrade advisor.It shows where i need to modify the query.

Error is:Upgrade Advisor detected the use of noninteger constants in the ORDER BY clause of one or more statements. Noninteger constants are allowed (and ignored) in the ORDER BY clause when the database compatibility mode is set to 80 and earlier. Noninteger constants in the ORDER BY clause will cause the statement to fail when the database compatibility mode is set to 90.Upgrade Advisor detected the use of noninteger constants in the ORDER BY clause of one or more statements. Noninteger constants are allowed (and ignored) in the ORDER BY clause when the database compatibility mode is set to 80 and earlier. Noninteger constants in the ORDER BY clause will cause the statement to fail when the database compatibility mode is set to 90.

SELECT 1 AS tag,
NULL AS parent,
ISNULL(wmeWWTWMeter.wmeEARefNo,'not set')
AS [Station!1!stationReference],
sitSite.sitDescription AS [Station!1!stationName],
NULL AS [SetofValues!2!parameter],
NULL AS [SetofValues!2!qualifier],
NULL AS [SetofValues!2!dataType],
NULL AS [SetofValues!2!period],
NULL AS [SetofValues!2!characteristic],
NULL AS [SetofValues!2!units],
NULL AS [SetofValues!2!startDate],
NULL AS [SetofValues!2!startTime],
NULL AS [SetofValues!2!endDate],
NULL AS [SetofValues!2!endTime],
NULL AS [SetofValues!2!dayOrigin],
NULL AS [SetofValues!2!valuesPerDay],
NULL AS [Value!3!date],
NULL AS [Value!3!time],
NULL AS [Value!3!flag1],
NULL AS [Value!3!!element]
FROM sitsite INNER JOIN wmewwtwmeter ON sitsite.sitIdentifier = wmewwtwmeter.sitidentifier
WHERE sitSite.sitIdentifier = @.sitID
UNION ALL
SELECT 2,1,NULL,NULL,parameter,qualifer,dataType,period,characteristic,units,
CONVERT(char(10), mindate, 126),CONVERT(char(8), mindate, 108),
CONVERT(char(10), maxdate, 126),CONVERT(char(8), maxdate, 108),
dayOrigin,valuesPerDay,NULL,NULL,NULL,NULL
FROM #huwj INNER JOIN #minmax ON dummyKey = 'EA'
UNION ALL
SELECT 3,2,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
CONVERT(char(10), fulldate, 126),
CONVERT(char(8), fulldate, 108),
flag = CASE Invalid WHEN 'Y' THEN 2 ELSE 1 END,
Reading
FROM #raw
ORDER BY tag, parent, 'Value!3!date' asc

Could you please help in this.

Thank you,

Regards

Srikar

Friday, March 9, 2012

Microsoft's AdventureWorks CTE example - different ordering desired

I pulled this example from Books Online. I cannot figure out how to make
the CTE return the data in a different order.
I don't want the data ordered by the Level. I want the child data to appear
directly under the parent data. Is that possible?
USE AdventureWorks;
GO
WITH DirectReports (ManagerID, EmployeeID, Title, DeptID, Level)
AS
(
-- Anchor member definition
SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID, 0 AS Level
FROM HumanResources.Employee AS e
INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
ON e.EmployeeID = edh.EmployeeID AND edh.EndDate IS NULL
WHERE ManagerID IS NULL
UNION ALL
-- Recursive member definition
SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID, Level + 1
FROM HumanResources.Employee AS e
INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
ON e.EmployeeID = edh.EmployeeID AND edh.EndDate IS NULL
INNER JOIN DirectReports AS d
ON e.ManagerID = d.EmployeeID
)
-- Statement that executes the CTE
SELECT ManagerID, EmployeeID, Title, Level
FROM DirectReports
INNER JOIN HumanResources.Department AS dp
ON DirectReports.DeptID = dp.DepartmentID
WHERE dp.GroupName = N'Research and Development' OR Level = 0
GO
-- Here is the output from the CTE:
ManagerID EmployeeID Title
Level
-- -- ---- -
--
NULL 109 Chief Executive Officer 0
109 12 Vice President of Engineering 1
12 3 Engineering Manager 2
3 4 Senior Tool Designer 3
3 9 Design Engineer 3
3 11 Design Engineer 3
3 158 Research and Development Manager 3
3 263 Senior Tool Designer 3
3 267 Senior Design Engineer 3
3 270 Design Engineer 3
263 5 Tool Designer 4
263 265 Tool Designer 4
158 79 Research and Development Engineer 4
158 114 Research and Development Engineer 4
158 217 Research and Development Manager 4
-- Here is my desired output:
ManagerID EmployeeID Title
Level
-- -- ---- -
--
NULL 109 Chief Executive Officer 0
109 12 Vice President of Engineering 1
12 3 Engineering Manager 2
3 9 Design Engineer 3
3 11 Design Engineer 3
3 270 Design Engineer 3
3 158 Research and Development Manager 3
158 79 Research and Development Engineer 4
158 114 Research and Development Engineer 4
158 217 Research and Development Manager 4
3 267 Senior Design Engineer 3
3 263 Senior Tool Designer 3
263 5 Tool Designer 4
263 265 Tool Designer 4
3 4 Senior Tool Designer 3
Keith KratochvilKeith,
Here is an idea:
USE AdventureWorks;
GO
WITH DirectReports (ManagerID, EmployeeID, Title, DeptID, Level,
PathToLevel)
AS
(
-- Anchor member definition
SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID, 0 AS Level,
CAST(N'.' AS nvarchar(50)) AS PathToLevel
FROM HumanResources.Employee AS e
INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
ON e.EmployeeID = edh.EmployeeID AND edh.EndDate IS NULL
WHERE ManagerID IS NULL
UNION ALL
-- Recursive member definition
SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID, Level + 1,
CAST(PathToLevel+CAST(e.EmployeeId AS nvarchar(5))+N'.' AS nvarchar(50))
FROM HumanResources.Employee AS e
INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
ON e.EmployeeID = edh.EmployeeID AND edh.EndDate IS NULL
INNER JOIN DirectReports AS d
ON e.ManagerID = d.EmployeeID
)
-- Statement that executes the CTE
SELECT ManagerID, EmployeeID, Title, Level, PathToLevel
FROM DirectReports
INNER JOIN HumanResources.Department AS dp
ON DirectReports.DeptID = dp.DepartmentID
WHERE dp.GroupName = N'Research and Development' OR Level = 0
ORDER BY PathToLevel
GO
Dejan Sarka
"Keith Kratochvil" <sqlguy.back2u@.comcast.net> wrote in message
news:OufHP8liGHA.4284@.TK2MSFTNGP05.phx.gbl...
>I pulled this example from Books Online. I cannot figure out how to make
>the CTE return the data in a different order.
> I don't want the data ordered by the Level. I want the child data to
> appear directly under the parent data. Is that possible?
>
> USE AdventureWorks;
> GO
> WITH DirectReports (ManagerID, EmployeeID, Title, DeptID, Level)
> AS
> (
> -- Anchor member definition
> SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID, 0 AS Level
> FROM HumanResources.Employee AS e
> INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
> ON e.EmployeeID = edh.EmployeeID AND edh.EndDate IS NULL
> WHERE ManagerID IS NULL
> UNION ALL
> -- Recursive member definition
> SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID, Level + 1
> FROM HumanResources.Employee AS e
> INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
> ON e.EmployeeID = edh.EmployeeID AND edh.EndDate IS NULL
> INNER JOIN DirectReports AS d
> ON e.ManagerID = d.EmployeeID
> )
> -- Statement that executes the CTE
> SELECT ManagerID, EmployeeID, Title, Level
> FROM DirectReports
> INNER JOIN HumanResources.Department AS dp
> ON DirectReports.DeptID = dp.DepartmentID
> WHERE dp.GroupName = N'Research and Development' OR Level = 0
> GO
>
> -- Here is the output from the CTE:
> ManagerID EmployeeID Title Level
> -- -- ----
> --
> NULL 109 Chief Executive Officer
> 0
> 109 12 Vice President of Engineering
> 1
> 12 3 Engineering Manager
> 2
> 3 4 Senior Tool Designer
> 3
> 3 9 Design Engineer
> 3
> 3 11 Design Engineer
> 3
> 3 158 Research and Development Manager
> 3
> 3 263 Senior Tool Designer
> 3
> 3 267 Senior Design Engineer
> 3
> 3 270 Design Engineer
> 3
> 263 5 Tool Designer
> 4
> 263 265 Tool Designer
> 4
> 158 79 Research and Development Engineer
> 4
> 158 114 Research and Development Engineer
> 4
> 158 217 Research and Development Manager
> 4
>
> -- Here is my desired output:
> ManagerID EmployeeID Title Level
> -- -- ----
> --
> NULL 109 Chief Executive Officer
> 0
> 109 12 Vice President of Engineering
> 1
> 12 3 Engineering Manager
> 2
> 3 9 Design Engineer
> 3
> 3 11 Design Engineer
> 3
> 3 270 Design Engineer
> 3
> 3 158 Research and Development Manager
> 3
> 158 79 Research and Development Engineer
> 4
> 158 114 Research and Development Engineer
> 4
> 158 217 Research and Development Manager
> 4
> 3 267 Senior Design Engineer
> 3
> 3 263 Senior Tool Designer
> 3
> 263 5 Tool Designer
> 4
> 263 265 Tool Designer
> 4
> 3 4 Senior Tool Designer
> 3
>
> --
> Keith Kratochvil
>
>|||Thanks for the solution, Dejan. I did some more poking around within Books
Online and found this:
http://msdn2.microsoft.com/en-us/library/ms175972.aspx
F. Using a recursive common table expression to display a hierarchical list
Now I have a couple of methods that I can use.
Keith Kratochvil
"Dejan Sarka" <dejan_please_reply_to_newsgroups.sarka@.avtenta.si> wrote in
message news:eZuw%23OmiGHA.4304@.TK2MSFTNGP03.phx.gbl...
> Keith,
> Here is an idea:
> USE AdventureWorks;
> GO
> WITH DirectReports (ManagerID, EmployeeID, Title, DeptID, Level,
> PathToLevel)
> AS
> (
> -- Anchor member definition
> SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID, 0 AS Level,
> CAST(N'.' AS nvarchar(50)) AS PathToLevel
> FROM HumanResources.Employee AS e
> INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
> ON e.EmployeeID = edh.EmployeeID AND edh.EndDate IS NULL
> WHERE ManagerID IS NULL
> UNION ALL
> -- Recursive member definition
> SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID, Level + 1,
> CAST(PathToLevel+CAST(e.EmployeeId AS nvarchar(5))+N'.' AS nvarchar(50))
> FROM HumanResources.Employee AS e
> INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
> ON e.EmployeeID = edh.EmployeeID AND edh.EndDate IS NULL
> INNER JOIN DirectReports AS d
> ON e.ManagerID = d.EmployeeID
> )
> -- Statement that executes the CTE
> SELECT ManagerID, EmployeeID, Title, Level, PathToLevel
> FROM DirectReports
> INNER JOIN HumanResources.Department AS dp
> ON DirectReports.DeptID = dp.DepartmentID
> WHERE dp.GroupName = N'Research and Development' OR Level = 0
> ORDER BY PathToLevel
> GO
> --
> Dejan Sarka
> "Keith Kratochvil" <sqlguy.back2u@.comcast.net> wrote in message
> news:OufHP8liGHA.4284@.TK2MSFTNGP05.phx.gbl...
>