我有一个返回的UNIONed查询:
ReceiptFolderID FolderParentID FolderTypeID FolderType FolderName FolderDescription ReceiptCount
--------------- -------------- ------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------
3 0 1 Pending Receipts Pending Receipts System folder; user cannot delete 1
7 0 2 Uncategorized Receipts Uncategorized Receipts System folder; user cannot delete 2
26 8 3 User-Defined Folder European Travel Folders created by users 0
25 0 3 User-Defined Folder Family Receipts Folders created by users 0
SELECT ReceiptFolderID, FolderParentID, tbl_ReceiptFolders.FolderTypeID,
FolderType,
CASE tbl_ReceiptFolderTypes.FolderTypeID
WHEN 1 THEN tbl_ReceiptFolderTypes.FolderType
WHEN 2 THEN tbl_ReceiptFolderTypes.FolderType
ELSE tbl_ReceiptFolders.FolderName
END AS FolderName,
tbl_ReceiptFolderTypes.FolderDescription,
dbo.GetFolderReceiptCount(ReceiptFolderID) AS ReceiptCount
INTO #tmp_UserFolders
FROM tbl_ReceiptFolders LEFT JOIN tbl_ReceiptFolderTypes ON tbl_ReceiptFolders.FolderTypeID=tbl_ReceiptFolderTypes.FolderTypeID
WHERE UserID=@UserID
AND (tbl_ReceiptFolderTypes.FolderTypeID = 1
OR tbl_ReceiptFolderTypes.FolderTypeID = 2)
--ORDER BY tbl_ReceiptFolderTypes.FolderTypeID ASC
UNION
SELECT ReceiptFolderID, FolderParentID, tbl_ReceiptFolders.FolderTypeID,
FolderType,
CASE tbl_ReceiptFolderTypes.FolderTypeID
WHEN 1 THEN tbl_ReceiptFolderTypes.FolderType
WHEN 2 THEN tbl_ReceiptFolderTypes.FolderType
ELSE tbl_ReceiptFolders.FolderName
END AS FolderName,
tbl_ReceiptFolderTypes.FolderDescription,
dbo.GetFolderReceiptCount(ReceiptFolderID) AS ReceiptCount
INTO #tmp_UserFolders
FROM tbl_ReceiptFolders LEFT JOIN tbl_ReceiptFolderTypes ON tbl_ReceiptFolders.FolderTypeID=tbl_ReceiptFolderTypes.FolderTypeID
WHERE UserID=@UserID
AND (tbl_ReceiptFolderTypes.FolderTypeID <> 1
OR tbl_ReceiptFolderTypes.FolderTypeID <> 2)
正如您所看到的,Pending和Uncategorized Receipts将始终首先显示,其余的按字母顺序排列。
现在,把它扔进SPROC,这是我用#temp文件夹做的事情不起作用。
通过什么方法让sproc返回这些结果?
谢谢!
答案 0 :(得分:1)
在第二个查询中,您有
(tbl_ReceiptFolderTypes.FolderTypeID <> 1
OR tbl_ReceiptFolderTypes.FolderTypeID <> 2)
好吧,想一想......每条记录都会满足这个谓词。无论FolderTypeID的值是多少,它都不是1,或者不是2。
如果我理解你想要的东西,试试这个:
Select ReceiptFolderID, FolderParentID,
f.FolderTypeID, FolderType,
Case t.FolderTypeID
When 1 Then t.FolderType
When 2 Then t.FolderType
Else f.FolderName End FolderName,
t.FolderDescription,
dbo.GetFolderReceiptCount(ReceiptFolderID) ReceiptCount
From tbl_ReceiptFolders f
Left Join tbl_ReceiptFolderTypes t
On f.FolderTypeID = t.FolderTypeID
Where UserID = @UserID
Order By Case When t.FolderTypeID In (1,2)
Then 0 Else 1 End
编辑添加Count()试试这个:
Select ReceiptFolderID, FolderParentID,
f.FolderTypeID, FolderType,
Case t.FolderTypeID
When 1 Then t.FolderType
When 2 Then t.FolderType
Else f.FolderName End FolderName,
t.FolderDescription,
Count(h.ReceiptFolderID) ReceiptCount
From tbl_ReceiptFolders f
Left Join tbl_ReceiptFolderTypes t
On f.FolderTypeID = t.FolderTypeID
Left Join tbl_ReceiptFolderLnk h
On h.ReceiptFolderID = f.ReceiptFolderID
Where UserID = @UserID
Group By ReceiptFolderID, FolderParentID,
f.FolderTypeID, FolderType, t.FolderDescription,
Case t.FolderTypeID
When 1 Then t.FolderType
When 2 Then t.FolderType
Else f.FolderName End,
Case When t.FolderTypeID In (1,2)
Then 0 Else 1 End
Order By Case When t.FolderTypeID In (1,2)
Then 0 Else 1 End