For some reason I can't change the table or update the data for now. Here the problem :
I have menu_user
table below :
userID menuID
(null) 2
(null) 3
1 3
2 1
3 2
4 5
5 0
userID
and menuID
not duplicated. The problem is how to ORDER BY userID, menuID
but when userID
has NULL value, it will look for another row that has same menuID
and place it after this row. menuID
just have max 2 same value and if it have, another one must be NULL
The order result expected :
userID menuID
1 3
(null) 3
2 1
3 2
(null) 2
4 5
5 0
Here the script sample :
CREATE TABLE [dbo].[menu_user](
[userID] [int] NULL,
[menuID] [int] NULL
);
INSERT [dbo].[menu_user] ([userID], [menuID]) VALUES (NULL, 3);
INSERT [dbo].[menu_user] ([userID], [menuID]) VALUES (1, 3);
INSERT [dbo].[menu_user] ([userID], [menuID]) VALUES (2, 1);
INSERT [dbo].[menu_user] ([userID], [menuID]) VALUES (3, 2);
INSERT [dbo].[menu_user] ([userID], [menuID]) VALUES (4, 5);
INSERT [dbo].[menu_user] ([userID], [menuID]) VALUES (5, 0);
INSERT [dbo].[menu_user] ([userID], [menuID]) VALUES (NULL, 2);
ADDED If possible I want this script as View (just SELECT with No Variable).