2011-03-07 85 views
0

我創建了一個存儲過程,它在tbluser表中有5000個用戶在數據庫中有一些過濾條件運行。有4個過濾條件(FC1,FC2,FC3,FC4)。過濾條件有部門的一些列表框和下拉列表和countries.I希望輸出,如下所示:在Sql Server 2005中需要T-Sql幫助

ID Name StaffNo department Points 
1 KK  111  dep1  2 
2 NN  222  dep2  1 
3 DD  333  dep3  4 

我ID,姓名,StaffNo,部門的ResultSet而不是點。

點計算將基於像

過濾條件,如果與之匹配FC1用戶獲得點1,如果FC1和FC2二者匹配用戶獲得了2點,如果兩個FC1,FC2和FC3匹配用戶獲得了3點等。

--in stored procedure i m using dynamic query 
    DECLARE @SQL VARCHAR(2000) 
    SET @SQL = 'SELECT U.UserID, U.StaffNo,U.FirstName+'' ''+ U.LastName AS EmployeeName,''?'' AS Points FROM tblUser U ' 
    SET @SQL = @SQL+' WHERE U.Department in (' + @SqlDepartment + ') ' 
    ---------------------Update--------------------------------------- 
IF @SqlLanguage <> '' 
    SET @SQL = @SQL+' OR U.UserID IN (SELECT UserID FROM Country WHERE LCValues IN ('+ @SqlLanguage +'))' 
IF @SqlAreas <> '' 
    SET @SQL = @SQL+' OR U.UserID IN (SELECT UserID FROM tblAreas WHERE '[email protected]+')' 
    ---------------------Update--------------------------------------- 
    ...other filtering condition 
    EXEC (@SQL) 

所有的過濾條件都是用OR邏輯來實現的。

回答

1

你試過實施CASE聲明來計算「積分」?

--in stored procedure i m using dynamic query 
DECLARE @SQL VARCHAR(2000) 
SET @SQL = ' 
    SELECT 
     U.[UserID], 
     U.[StaffNo], 
     U.[FirstName]+'' ''+ U.[LastName] AS EmployeeName, 
     (
      CASE WHEN EXISTS(SELECT 1 FROM [Country] WHERE /*Your filter comes in here*/) THEN 1 ELSE 0 END + 
      CASE WHEN EXISTS(SELECT 1 FROM [tblAreas] WHERE /*Your filter comes in here*/) THEN 1 ELSE 0 END + 
      CASE WHEN EXISTS(SELECT 1 FROM [OtherTable1] WHERE /*Your filter comes in here*/) THEN 1 ELSE 0 END + 
      CASE WHEN EXISTS(SELECT 1 FROM [OtherTable2] WHERE /*Your filter comes in here*/) THEN 1 ELSE 0 END 
     ) AS Points 
    FROM [tblUser] U' 
SET @SQL = @SQL+' WHERE U.Department in (' + @SqlDepartment + ') OR'... 
...other filtering condition 
EXEC (@SQL) 
+0

感謝您提供解決方案。但在我的情況下,FC1,2,3,4與4個不同的表無關tbluser表 – Sukhjeevan 2011-03-07 12:20:14

+0

您是否可以根據外鍵,也許[UserID]加入表?你可能會發表一個表格的例子,這樣我可以給你一個更好的解決方案嗎? – FarligOpptreden 2011-03-07 12:22:08

+0

plz查看更新後的問題。 – Sukhjeevan 2011-03-07 12:48:06

0

我想你可能會更好的工作出在你的代碼點,而不是在SQL

我能想到這樣做的唯一方法是與工會這將是可怕的

0

您可以使用左外連接到您的過濾條件,按用戶名不重複獲得,爲命中加1的值,並且使用聚結,因爲沒有命中設定爲0。

一些示例代碼,顯示我的意思是使用區域和國家作爲過濾條件。

declare @U table (UserID int, Name varchar(50), StaffNo char(3), department char(4)) 
declare @C table (CountryID int, UserID int) 
declare @A table (AreaID int, UserID int) 

insert into @U values (1, 'KK', '111',  'dep1') 
insert into @U values (2, 'NN', '222',  'dep2') 
insert into @U values (3, 'DD', '333',  'dep3') 

insert into @C values(1, 1) 
insert into @C values(2, 1) 
insert into @C values(3, 2) 
insert into @C values(3, 3) 

insert into @A values(1, 1) 
insert into @A values(2, 1) 
insert into @A values(3, 2) 

select 
    U.UserID, 
    U.Name, 
    U.StaffNo, 
    U.department, 
    coalesce(C.Point, 0)+coalesce(A.Point,0) as Points 
from @U as U 
    left outer join 
    (select UserID, 1 as Point 
    from @C 
    -- where ...? 
    group by UserID) as C 
    on U.UserID = C.UserID 
    left outer join 
    (select UserID, 1 as Point 
    from @A 
    -- where ...? 
    group by UserID) as A 
    on U.UserID = A.UserID