I am rather stuck, I've had a good look around but I am not exactly sure how I can do this.
I've got to build a SP (TSQL) to bring back a navigation, but I am having a few issues with ordering the navigation correctly.
Table Example
NavID OrderID ParentID NavName
1 1 0 Home
2 2 0 About
3 3 0 Contact Us
4 1 2 About Us Page
5 2 2 About Us Page 2
6 1 4 Another SubPage
All I need to bring back is the navigation above, and one navigation below.
So if I passed NavigationID 2 I would expect the results to come back like this
Home
About
About Us Page
About Us Page 2
Contact Us
If I passed in NavigationID 6 I would expect to see ..
Home
About
About Us Page
Another SubPage
About Us Page 2
Contact Us
As you can see it takes in account the OrderID, but make's sure the Child's are in order first.
How can I achieve this?
Here's a complete script which does what you need (includes your test data):
DECLARE #nav TABLE (
NavID INT NOT NULL PRIMARY KEY,
OrderID INT NOT NULL,
ParentID INT,
NavName nvarchar(MAX) NOT NULL
);
INSERT #nav
SELECT 1, 1, 0, 'Home' UNION ALL
SELECT 2, 2, 0, 'About' UNION ALL
SELECT 3, 3, 0, 'Contact Us' UNION ALL
SELECT 4, 1, 2, 'About Us Page' UNION ALL
SELECT 5, 2, 2, 'About Us Page 2' UNION ALL
SELECT 6, 1, 4, 'Another SubPage';
DECLARE #NavigationID int;
SET #NavigationID = 2;
WITH Ancestors AS (
SELECT #NavigationID NavID
UNION ALL
SELECT n.ParentID
FROM #nav n
JOIN Ancestors a ON (n.NavID = a.NavID)
),
VisibleNav AS (
SELECT n.*, CONVERT(FLOAT, 1)/SUM(1) OVER (PARTITION BY n.ParentID) Mul, ROW_NUMBER() OVER (PARTITION BY n.ParentID ORDER BY n.OrderID)-1 Pos
FROM #nav n
JOIN Ancestors a ON n.ParentID = a.NavID
),
SortedNav AS (
SELECT vn.*, vn.Pos*vn.Mul Sort, 1 Depth
FROM VisibleNav vn
WHERE vn.ParentID = 0
UNION ALL
SELECT vn.NavID, vn.OrderID, vn.ParentID, vn.NavName, vn.Mul*sn.Mul, vn.Pos, vn.Pos*(vn.Mul*sn.Mul)+sn.Sort, sn.Depth + 1
FROM VisibleNav vn
JOIN SortedNav sn ON sn.NavID = vn.ParentID
)
SELECT sn.NavID, sn.OrderID, sn.ParentID, sn.NavName
FROM SortedNav sn
ORDER BY sn.Sort, sn.Depth;
Basically, I have a recursive CTE to create a list of all parents which need to be used in your navigation including the depth of the parent (so that the order is not dependent on the IDs), and then I join the navigation entries on that.
I'd be tempted to create a table variable, run the query for NavID <= the parent parameter where parentID = 0 and add to the table. Then get the child items, insert to table variable and finally get the > NavID Where ParentID = 0.
A fudge but it should work.
DECLARE #Table TABLE (NavName nvarchar(50), OrderID int)
INSERT #Table (NavName, OrderID) (SELECT NavName, OrderID FROM #Table1 WHERE (ParentID = 0) AND (NavID <= #ParentID))
INSERT #Table (NavName,OrderID) (SELECT NavName, OrderID+ 500 FROM #Table1 WHERE ParentID = #ParentID )
INSERT #Table (NavName,OrderID) (SELECT NavName, OrderID + 1000 FROM #Table1 WHERE ParentID = 0 AND NavID > #ParentID)
SELECT * FROM #Table ORDER BY OrderID
Common Table Expressions permit recursive queries. However, they can be very slow and can seriously confuse the query optimizer, particularly if you use (and you generally should) parametrized queries. For small tables (such as navigation) that will not participate in large joins with other (large) tables, CTE's work fine. You can also cache the result of the common table expression in a table and rerun the caching query anytime the navigation table changes (generally not frequently, I'm guessing) - the query optimizer deals with simple many-to-many relation tables much better.
However, there are also other ways of representing tree's in SQL server.
You could look at
lft/rgt style columns whereby a ordering is defined in as the order of the lft column and a node X is a descendant of Y whenever Y.lft < X.lft < Y.rgt. See: http://articles.sitepoint.com/article/hierarchical-data-database/2
HierarchyID's. Sql Server 2008 introduced a special data type for exactly this purpose; however updating the tree structure isn't always trivial with these. See: http://msdn.microsoft.com/en-us/magazine/cc794278.aspx
Finally, when I need to use CTE's I generally start with something along the lines of...
with nav_tree (ParentID, ChildID, depth_delta) as (
SELECT basenode.NavID, basenode.NavID, 0
FROM NavTable AS basenode
UNION ALL
SELECT treenode.ParentID, basenode.NavID, depth_delta+1
FROM NavTable AS basenode
JOIN nav_tree AS treenode ON basenode.ParentID=treenode.ChildID
)
--select statement here joining the nav_tree with the original table and whatnot
Note that your exact order requirements are quite tricky; in particular where you want the children of an element to immediately be listed inline; i.e. the "About Us Page, Another SubPage, About Us Page 2" segment of your second example. In particular, that means you cannot just order by depth_delta and secondarily by orderid - you'll need a path-based sort. You might want to do this in code, rather than in sql, but you can construct a path in a CTE as follows:
with nav_tree (ParentID, ChildID, depth_delta,orderpath) as (
SELECT basenode.NavID, basenode.NavID, 0, convert(varchar(MAX), basenode.OrderID)
FROM NavTable AS basenode
UNION ALL
SELECT treenode.ParentID, basenode.NavID, depth_delta+1, treenode.orderpath+','+basenode.OrderID
FROM NavTable AS basenode
JOIN nav_tree AS treenode ON basenode.ParentID=treenode.ChildID
)
...and then you can order by that orderpath. Since you also want "one level below" each ancestor-or-self unfolded, you'll still need to join with NavTable to get those.
However, given your sorting requirements, I'd recommend HierarchyIDs: they have your sorting semantics built-in, and they also avoid the potential performance issues CTE's can expose.
I am a little unclear as to precisely which nodes you wish to display... but if I understand correctly the main question here is how to order the resulting nodes. The principal difficulty is that the ordering criteria are variable length: a node must be ordered based upon the entire sequence of OrderId values for the node and all of its ancestors. For example, the ordering sequence for node 6 is '2, 1, 1'.
SQL does not handle such variable-length sequences well. I propose that we use an NVARCHAR(MAX) value for the ordering sequence. For node 6, we will use '0002.0001.0001'. In this form, the nodes can be ordered trivially using string comparison. Note that the identifier values must be zero-padded in order to ensure correct ordering (I arbitrarily chose to pad to 4 digits -- the real application may require a different choice).
So this brings us to the nuts and bolts. We'll start by creating a table called NavigationData to hold our test data:
SELECT NULL AS NavId, NULL AS OrderId, NULL AS ParentId, NULL AS NavName
INTO NavigationData WHERE 1=0
UNION SELECT 1, 1, 0, 'Home'
UNION SELECT 2, 2, 0, 'About'
UNION SELECT 3, 3, 0, 'Contact Us'
UNION SELECT 4, 1, 2, 'About Us Page'
UNION SELECT 5, 2, 2, 'About Us Page 2'
UNION SELECT 6, 1, 4, 'Another SubPage'
Now, we'll create a helper view that, for every possible desired node, lists all of the related nodes along with their computed path strings. As I said at the beginning, I feel the criteria for selecting the related nodes are underspecified, so the desired/related JOIN expression may need to be adjusted for datasets with more nodes than the simple example. With that caveat, here is the view:
CREATE VIEW NavigationHierarchy AS
WITH
hierarchy AS (
SELECT
NavId AS RootId
, 1 AS Depth
, NavId
, RIGHT('0000' + CAST(OrderId AS NVARCHAR(MAX)), 4) AS Path
, ParentId
, NavName
FROM NavigationData
WHERE ParentId = 0
UNION ALL
SELECT
parent.RootId
, parent.Depth + 1 AS Depth
, child.NavId
, parent.Path + '.'
+ RIGHT('0000' + CAST(child.OrderId AS NVARCHAR(MAX)), 4) AS Path
, child.ParentId
, child.NavName
FROM hierarchy AS parent
INNER JOIN NavigationData AS child
ON child.ParentId = parent.NavId
)
SELECT
desired.NavId AS DesiredNavId
, related.*
FROM hierarchy AS desired
INNER JOIN hierarchy AS related
ON related.Depth <= desired.Depth + 1
AND related.RootId IN (desired.RootId, related.RootId)
Most of the query is a straight-forward recursive descent of the hierarchy using a common table expression. The heart of the solution is the generation of the Path columns. Naturally, you may prefer to bake this query directly into a larger query or stored proc rather than creating a view. The view is convenient for testing, however.
Armed with the view, we can now generate the desired results, in the requested order. I have included the generated path in the query result for illustrative purposes. Here is the query for node 2:
SELECT NavName, Path
FROM NavigationHierarchy
WHERE DesiredNavId = 2
ORDER BY Path
yielding:
Home 0001
About 0002
About Us Page 0002.0001
About Us Page 2 0002.0002
Contact Us 0003
and for node 6:
SELECT NavName, Path
FROM NavigationHierarchy
WHERE DesiredNavId = 6
ORDER BY Path
yielding:
Home 0001
About 0002
About Us Page 0002.0001
Another SubPage 0002.0001.0001
About Us Page 2 0002.0002
Contact Us 0003
Related
Hello friendly internet wizards.
I am attempting to extract a levelled bill of materials (BOM) from a dataset, running in DB2 on an AS400 server.
I have constructed most of the query (with a lot of help from online resources), and this is what I have so far;
#set item = '10984'
WITH BOM (origin, PMPRNO, PMMTNO, BOM_Level, BOM_Path, IsCycle, IsLeaf) AS
(SELECT CONNECT_BY_ROOT PMPRNO AS origin, PMPRNO, PMMTNO,
LEVEL AS BOM_Level,
SYS_CONNECT_BY_PATH(TRIM(PMMTNO), ' : ') BOM_Path,
CONNECT_BY_ISCYCLE IsCycle,
CONNECT_BY_ISLEAF IsLeaf
FROM MPDMAT
WHERE PMCONO = 405 AND PMFACI = 'M01' AND PMSTRT = 'STD'
START WITH PMPRNO = :item
CONNECT BY NOCYCLE PRIOR PMMTNO = PMPRNO)
SELECT 0 AS BOM_Level, '' AS BOM_Path, MMITNO AS Part_Number, MMITDS AS Part_Name,
IFSUNO AS Supplier_Number, IDSUNM AS Supplier_Name, IFSITE AS Supplier_Part_Number
FROM MITMAS
LEFT OUTER JOIN MITVEN ON MMCONO = IFCONO AND MMITNO = IFITNO AND IFSUNO <> 'ZGA'
LEFT OUTER JOIN CIDMAS ON MMCONO = IDCONO AND IDSUNO = IFSUNO
WHERE MMCONO = 405
AND MMITNO = :item
UNION ALL
SELECT BOM.BOM_Level, BOM_Path, BOM.PMMTNO AS Part_Number, MMITDS AS Part_Name,
IFSUNO AS Supplier_Number, IDSUNM AS Supplier_Name, IFSITE AS Supplier_Part_Number
FROM BOM
LEFT OUTER JOIN MITMAS ON MMCONO = 405 AND MMITNO = BOM.PMMTNO
LEFT OUTER JOIN MITVEN ON IFCONO = MMCONO AND IFITNO = MMITNO AND IFSUNO <> 'ZGA' AND MMMABU = '2'
LEFT OUTER JOIN CIDMAS ON MMCONO = IDCONO AND IDSUNO = IFSUNO
;
This is correctly extracting the components for a given item, as well as the sub-components (etc).
Current data looks like this (I have stripped out some columns that aren't relevant to the issue);
https://pastebin.com/LUnGKRqH
My issue is the order that the data is being presented in.
As you can see in the pastebin above, the first column is the 'level' of the component. This starts with the parent item at level 0, and can theoretically go down as far as 99 levels.
The path is also show there, so for example the second component 853021 tells us that it's a 2nd level component, the paths up to INST363 (shown later in the list as a 1st level), then up to the parent at level 0.
I would like for the output to show in path order (for lack of a better term).
Therefore, after level 0, it should be showing the first level 1 component, and then immediately be going into it's level 2 components and so on, until no further level is found. Then at that point, it returns back up the path to the next valid record.
I hope I have explained that adequately, but essentially the data should come out as;
Level
Path
Item
0
10984
1
: INST363
INST363
2
: INST363 : 853021
853021
1
: 21907
21907
Any help that can be provided would be very much appreciated!
Thanks,
This is an interesting query. Frankly I am surprised it works as well as it does since it is not structured the way I usually structure queries with a recursive CTE. The main issue is that while you have the Union in there, it does not appear to be within the CTE portion of the query.
When I write a recursive CTE, it is generally structured like this:
with cte as (
priming select
union all
secondary select)
select * from cte
So to get a BOM from an Item Master that looks something like:
CREATE TABLE item (
ItemNo Char(10) PRIMARY KEY,
Description Char(50));
INSERT INTO item
VALUES ('Item0', 'Root Item'),
('Item1a', 'Second Level Item'),
('Item1b', 'Another Second Level Item'),
('Item2a', 'Third Level Item');
and a linkage table like this:
CREATE TABLE linkage (
PItem Char(10),
CItem Char(10),
Quantity Dec(5,0),
PRIMARY KEY (PItem, CItem));
INSERT INTO linkage
VALUES ('Item0', 'Item1a', 2),
('Item0', 'Item1b', 3),
('Item1b', 'Item2a', 5)
The recursive CTE to list a BOM for 'Item0' looks like this:
WITH bom (Level, ItemNo, Description, Quantity)
AS (
-- Load BOM with root item
SELECT 0,
ItemNo,
Description,
1
FROM Item
WHERE ItemNo = 'Item0'
UNION ALL
-- Retrieve all child items
SELECT a.Level + 1,
b.CItem,
c.Description,
a.Quantity * b.Quantity
FROM bom a
join linkage b ON b.pitem = a.itemno
join item c ON c.itemno = b.citem)
-- Set the list order
SEARCH DEPTH FIRST BY itemno SET seq
-- List BOM
SELECT * FROM bom
ORDER BY seq
Here are my results:
LEVEL
ITEMNO
DESCRIPTION
QUANTITY
0
Item0
Root Item
1
1
Item1a
Second Level Item
2
1
Item1b
Another Second Level Item
3
2
Item2a
Third Level Item
15
Notice the search clause, that generates a column named seq which you can use to sort the output either depth first or breadth first. Depth first is what you want here.
NOTE: This isn't necessarily an optimum query since the description is in the CTE, and that increases the size of the CTE result set without really adding anything to it that couldn't be added in the final select. But it does make things a bit simpler since the 'priming query' retrieves the description.
Note also: the column list on the with clause following BOM. This is there to remove the confusion that DB2 had with the expected column list when the explicit column list was omitted. It is not always necessary, but if DB2 complains about an invalid column list, this will fix it.
I'm working off this thread: HierarchyID: HierarchyID: Get all descendants for a list of parents
I have a table that uses a HierarchyID, and I need a query that gives me all descendants for specified parent(s) in a single set.
Here's my table, populated:
DECLARE #Ph TABLE (ProductHierarchyNode HIERARCHYID, ProductHierarchyId INT)
INSERT INTO #Ph (ProductHierarchyNode, ProductHierarchyId) VALUES
(hierarchyid::Parse('/1/'), 1),
(hierarchyid::Parse('/1/1/'), 2),
(hierarchyid::Parse('/1/1/2/'), 3),
(hierarchyid::Parse('/1/1/2/1/'), 4)
This query works perfectly for a SINGLE id: 4. It gives me back that item, plus all of its descendants.
SELECT
*
FROM
#Ph
WHERE
(SELECT ProductHierarchyNode FROM #Ph WHERE ProductHierarchyId = 4).IsDescendantOf(ProductHierarchyNode) = 1
However, that query isn't very useful for a list of ID's. So for a list, I'm trying the answer in the thread I linked to above:
SELECT
child.*
FROM
#Ph as parent
INNER JOIN #Ph as child on child.ProductHierarchyNode.IsDescendantOf(parent.ProductHierarchyNode) = 1
WHERE
parent.ProductHierarchyId in (4)
I'm sure I'm overlooking something obvious. Just not sure what it is. But this only returns me the parent item and no children.
Can't spot my error.
I feel like you have this backwards... you want DESCENDENTS, but you're asking for things where the parent is 4... nothing has a parent of 4.
Don't you really want this?
SELECT
child.*
FROM
#Ph as parent
INNER JOIN #Ph as child on child.ProductHierarchyNode.IsDescendantOf(parent.ProductHierarchyNode) = 1
WHERE
parent.ProductHierarchyId in (1)
This returns all the rows you say you're expecting, and makes more sense to me as you're asking for all descendent of 1 (the root parent). 2 is a child/descendent of 1, 3 is a child/descendent of 2, and 4 is a child/descendent of 3.
The "WHERE parent.ProductHierarchyId IN (1)" is saying "find me all nodes where 1 is a parent/ancestor".
In the first query, you're asking for all nodes where 4 is a descendent, so that makes sense.
In the second query, you're asking for all descendents of 1. If you want "all ancestors of 4" that'd be a different query.
I am having the following - simplified - layout for tables:
TABLE blocks (id)
TABLE content (id, blockId, order, data, type)
content.blockId is a foreign key to blocks.id. The idea is that in the content table you have many content entries with different types for one block.
I am now looking for a query that can provide me with an aggregation based on a blockId where all the content entries of the 3 different types are concatenated and put into respective columns.
I have already started and found the listagg function which is working well, I did the following statement and lists me all the content entries in a column:
SELECT listagg(c.data, ',') WITHIN GROUP (ORDER BY c.order) FROM content c WHERE c.blockId = 330;
Now the concatenated string however contains all the data elements of the block in one column. What I would like to achieve is that its put into separate columns based on the type. For example the following content of content would be like this:
1, 1, 0, "content1", "FRAGMENT"
2, 1, 1, "content2", "BULK"
3, 1, 3, "content4", "FRAGMENT"
4, 1, 2, "content3", "FRAGMENT"
Now I wanted to get as an output 2 columns, one is FRAGMENT and one is BULK, where FRAGMENT contains "content1;content3;content4" and BULK contains "content2"
Is there an efficient way of achieving this?
You can use case:
SELECT listagg(CASE WHEN content = 'FRAGMENT' THEN c.data END, ',') WITHIN GROUP (ORDER BY c.order) as fragments,
listagg(CASE WHEN content = 'BULK' THEN c.data END, ',') WITHIN GROUP (ORDER BY c.order) as bulks
FROM content c
WHERE c.blockId = 330;
As an alternative, if you want it more dynamic, you could pivot the outcome.
Note, that this will only work for Oracle 11.R2. Here´s an example how it could look like:
select * from
(with dataSet as (select 1 idV, 1 bulkid, 0 orderV, 'content1' dataV, 'FRAGMENT' typeV from dual union
select 2, 1, 1, 'content2', 'BULK' from dual union
select 3, 1, 3, 'content4', 'FRAGMENT' from dual union
select 4, 1, 2, 'content3', 'FRAGMENT' from dual)
select typeV, listagg(dataSet.dataV ,',') WITHIN GROUP (ORDER BY orderV) OVER (PARTITION BY typeV) dataV from dataSet)
pivot
(
max(dataV)
for typeV in ('BULK', 'FRAGMENT')
)
O/P
Bulk | FRAGMENT
-----------------
content2 | content1,content3,content4
The important things here:
OVER (PARTITION BY typeV): this acts like a group by for the listagg, concatinating everything having the same typeV.
for typeV in ('BULK', 'FRAGMENT'): this will gather the data for BULK and FRAGMENT and produce separate columns for each.
max(dataV) simply to provide a aggregate function, otherwise pivot wont work.
I have a little problem with this recursive CTE, it works fine except when I have a user without root readable rights means no entry for this element. So if I run this query on a user with rights just on the leaves inside the tree the level part of this query won't work correctly.
It will show the real level hierarchy for example 6 but its the top first readable element for him so it should be 1.
WITH Tree
AS (
SELECT
id,
parent,
0 AS Level,
id AS Root,
CAST(id AS VARCHAR(MAX)) AS Sort,
user_id
FROM SourceTable
WHERE parent IS NULL
UNION ALL
SELECT
st.id,
st.parent,
Level + 1 AS Level,
st.parent AS Root,
uh.sort + '/' + CAST(st.id AS VARCHAR(20)) AS Sort,
st.user_id
FROM SourceTable AS st
JOIN Tree uh ON uh.id = st.parent
)
SELECT * FROM Tree AS t
JOIN UserTable AS ut ON ut.id = t.user_id AND ut.user_id = '141F-4BC6-8934'
ORDER BY Sort
the level is as follows
id level
5 0
2 1
7 2
4 2
1 2
6 1
3 2
8 2
9 3
When a user now just have read rights to id 8 and 9 the level from CTE stays at 2 for id 8 and 3 for id 9 but I need for id 8 level 1 if there is no one before
You haven't told us how you know whether a user has rights to a given id. That is a necessary piece of information. I'm going to put some code below that assumes you add a column to your query called hasRights and that this column will have a zero value if the user does not have rights and a value of one if they do. You may need to tweak this, since I have no data to test with but hopefully it will get you close.
Basically, the query is altered to only add 1 to the level if the user has rights. It also only adds to the sort path if the user has rights, otherwise an empty string is appended. So, if ids 8 and 9 are the only items the user has access to, you should see levels of 1 and 2 and sort paths similar to '5/8/9' rather than '5/6/8/9'. If you still aren't able to get it working, it would help us tremendously if you posted a sample schema on SqlFiddle.
WITH Tree
AS (
SELECT
id,
parent,
0 AS Level,
id AS Root,
hasRights AS HasRights,
CAST(id AS VARCHAR(MAX)) AS Sort,
user_id
FROM SourceTable
WHERE parent IS NULL
UNION ALL
SELECT
st.id,
st.parent,
Level + st.hasRights AS Level,
st.parent AS Root,
st.hasRights AS HasRights,
uh.sort + CASE st.hasRights WHEN 0 THEN '' ELSE '/' + CAST(st.id AS VARCHAR(20)) END AS Sort,
st.user_id
FROM SourceTable AS st
JOIN Tree uh ON uh.id = st.parent
)
SELECT * FROM Tree AS t
JOIN UserTable AS ut ON ut.id = t.user_id AND ut.user_id = '141F-4BC6-8934'
ORDER BY Sort
You requite something like if the higher level(0 or 1 ) is not in existence, then the next level become the higher level..
If yes, then you have to do this when the final result
insert all the results in temp table lets say #info (with same characteristics of data)
Now after all final data ready in the table,
Please check from the top.
Select * from #info where level= 0
if this returns 0 rows then you have to update each records level. to (level = level -1)
Now again same for Level=0, then level 1, then level 2 , then level 3 in recursion. this will be easy but not easy to code. So try without recursion then try final update.
I hope this will help :)
Please reply if you are looking for something else.
Try to perform the following select and let me know if it is your desired result:
SELECT *,
DENSE_RANK() OVER (PARTITION BY t.user_id ORDER BY t.LEVEL ASC) -1 as RelativeUserLevel
FROM Tree AS t
JOIN UserTable AS ut ON ut.id = t.user_id AND ut.user_id = '141F-4BC6-8934'
ORDER BY Sort
Is conversion of your table into hierarchical types an option:
hierarchyid data type (http://technet.microsoft.com/en-us/library/bb677290(v=sql.105).aspx)
or
xml data type
SOME TIMES option (maxrecursion 10000); is very useful
I don't have time to read your problem but here snipped
declare cursorSplit Cursor for
select String from dbo.SplitN(#OpenText,'~')
where String not in (SELECT [tagCloudStopWordText]
FROM [tagCloudStopList] where [langID]=#_langID)
option (maxrecursion 10000);
open cursorSplit
I'm sorry to be a party pooper and spoil the fun of creating such an interesting piece of SQL, but perhaps you should load all relevant access data into your application and determine the user levels in the application?
I bet it would result in more maintainable code..
MySQL provides a string function named FIELD() which accepts a variable number of arguments. The return value is the location of the first argument in the list of the remaining ones. In other words:
FIELD('d', 'a', 'b', 'c', 'd', 'e', 'f')
would return 4 since 'd' is the fourth argument following the first.
This function provides the capability to sort a query's results based on a very specific ordering. For my current application there are four statuses that I need to manager: active, approved, rejected, and submitted. However, if I simply order by the status column, I feel the usability of the resulting list is lessened since rejected and active status items are more important than submitted and approved ones.
In MySQL I could do this:
SELECT <stuff> FROM <table> WHERE <conditions> ORDER BY FIELD(status, 'rejected', 'active','submitted', 'approved')
and the results would be ordered such that rejected items were first, followed by active ones, and so on. Thus, the results were ordered in decreasing levels of importance to the visitor.
I could create a separate table which enumerates this importance level for the statuses and then order the query by that in descending order, but this has come up for me a few times since switching to MS SQL Server so I thought I'd inquire as to whether or not I could avoid the extra table and the somewhat more complex queries using a built-in function similar to MySQL's FIELD().
Thank you,
David Kees
Use a CASE expression (SQL Server 2005+):
ORDER BY CASE status
WHEN 'active' THEN 1
WHEN 'approved' THEN 2
WHEN 'rejected' THEN 3
WHEN 'submitted' THEN 4
ELSE 5
END
You can use this syntax for more complex evaluation (including combinations, or if you need to use LIKE)
ORDER BY CASE
WHEN status LIKE 'active' THEN 1
WHEN status LIKE 'approved' THEN 2
WHEN status LIKE 'rejected' THEN 3
WHEN status LIKE 'submitted' THEN 4
ELSE 5
END
For your particular example your could:
ORDER BY CHARINDEX(
',' + status + ',',
',rejected,active,submitted,approved,'
)
Note that FIELD is supposed to return 0, 1, 2, 3, 4 where as the above will return 0, 1, 10, 17 and 27 so this trick is only useful inside the order by clause.
A set based approach would be to outer join with a table-valued-constructor:
LEFT JOIN (VALUES
('rejected', 1),
('active', 2),
('submitted', 3),
('approved', 4)
) AS lu(status, sort_order)
...
ORDER BY lu.sort_order
I recommend a CTE (SQL server 2005+).
No need to repeat the status codes or create the separate table.
WITH cte(status, RN) AS ( -- CTE to create ordered list and define where clause
SELECT 'active', 1
UNION SELECT 'approved', 2
UNION SELECT 'rejected', 3
UNION SELECT 'submitted', 4
)
SELECT <field1>, <field2>
FROM <table> tbl
INNER JOIN cte ON cte.status = tbl.status -- do the join
ORDER BY cte.RN -- use the ordering defined in the cte
Good luck,
Jason
ORDER BY CHARINDEX(','+convert(varchar,status)+',' ,
',rejected,active,submitted,approved,')
just put a comma before and after a string in which you are finding the substring index or you can say that second parameter.
and first parameter of charindex is also surrounded by ,