2012-05-12 128 views
0

我在做什麼有屬性的不同組合行:獲取從另一個表

我正在寫一個C#應用程序,這將幫助員工處理電子郵件。我有一個包含郵件的數據庫(MS SQL Server 2008 Express)。一個表格包含郵件本身(每一行都有msgId,發件人,收件人,主題,正文等欄目),另一個表格包含員工如何處理郵件的數據 - 只要電子郵件分配了一些屬性,新的一行與該屬性保存到第二個表。第二個表包含列:msgId,forTeam,notForTeam,processedByTeam。每當員工將消息標記爲與其團隊相關時,就會在forTeam列中以TEAMXX值保存一行。只要員工在電子郵件上執行其他操作,具有TEAMXX值的行就會保存在processedByTeam列中。

我試圖讓它們具有以下值的郵件:
forTeam: 'TEAM01'
notForTeam:一切比 'TEAM01' 不同
processedByTeam:一切比 'TEAM01'

跟我不一樣使用MailAssignments表在Mail郵件表上添加LEFT JOIN,然後在WHERE中添加上述條件。結果應該顯示已分配給TEAM01但尚未由該團隊的員工處理的消息。



問題:

我不能得到的SQL查詢才能正常工作。任何條件的組合都會返回額外的行(例如,當我想忽略這些行時,使用屬性notForTeam ='TEAM01')或給我一個錯誤,說明由於某種原因,datetime的格式不正確(我按收到的時間對郵件進行排序)。

例子查詢:

SELECT TOP 30 Mails.* 
FROM Mails 
LEFT JOIN MailAssignments 
ON Mails.msgId = MailAssignments.msgId 
WHERE 
(MailAssignments.forTeam='TEAM01') 
AND (MailAssignments.notForTeam<>'TEAM01' OR MailAssignments.notForTeam IS NULL) 
AND (MailAssignments.processedByTeam<>'TEAM01' OR MailAssignments.processedByTeam IS NULL) 
ORDER BY Mails.sortingTime DESC 

即使我降低條件之後WHERE(和ORDER BY之前),以(MailAssignments.notForTeam<>'TEAM01')進行測試,我仍然得到錯誤的某些原因。

我在想什麼或做錯了什麼?



UPDATE:

似乎(MailAssignments.notForTeam<>'TEAM01')由於某種原因造成的問題。其他一切工作確定(也等號是OK:MailAssignments.notForTeam =「TEAM01」),但是當我添加了notForTeam<>條件我得到不正確的日期時間錯誤(WTF:/)

System.Data.SqlTypes中.SqlTypeException:SqlDateTime溢出。必須介於1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之間。



更新 - 例如:

table with mails ("Mails"): 
msgId sender subject body received (sortingTime) 
53 [email protected] test test 2012-05-11 11:00 
54 [email protected] test2 test2 2012-05-10 10:00 
55 [email protected] blah blah 2012-05-11 12:00 
56 [email protected] dfgg dfgg 2012-05-11 12:30 


table with assignments ("MailAssignments"): 
msgId forTeam notForTeam processedByTeam 
54 null TEAM02  null   - this means TEAM02 hid the mail from their view 
54 TEAM01 null  null   - this means TEAM01 assigned the mail to them 
54 null null  TEAM01   - TEAM01 has processed the mail 

53 TEAM01 null  null   - invisible to other teams 
53 null TEAM01  null   - invisible for TEAM01 (and also for others because of the above) 

56 TEAM01 null  null 
56 null null  TEAM01   - mail processed by TEAM01 

55 TEAM02 null  null   - assigned to TEAM02 but not processed yet 


Now, I want my C# app to have a view which will display mails as "This Team's", ie.: 
1. mail 54 was first hidden for TEAM02 - should be invisible for them 
2. mail 54 was assigned to TEAM01 - should be now visible for them in "This Team's" view 
3. mail 54 was processed by TEAM01 - should be now invisible in "This Team's" view 

4. mail 53 was assigned to TEAM01 - should visible in their "This Team's" view (and ONLY for them) 
5. mail 53 was hidden for TEAM01 - should be invisible to TEAM01 (and for anyone else because of prev. action) 

6. mail 56 was assigned to TEAM01 - should be now visible only to TEAM01 in "This Team's" 
7. mail 56 was processed by TEAM01 - should be now invisible for TEAM01 in "This Team's" 

So, with the present data in both tables, finally, TEAM02 for example should see in their "This Team's" view only: 
mail 55 
+0

請不要用 「C#/ T-SQL」 前綴的稱號。這就是標籤的用途。 –

+1

這是NotForTeam列的類型? – ivowiblo

+0

此外,請嘗試在您的查詢分析器中運行查詢,並告訴是否發生錯誤 – ivowiblo

回答

2
Select TOP 30 * 
From Mails As M 
Where Exists (
       Select 1 
       From MailAssignments As MA1 
       Where MA1.msgid = M.msgid 
        And MA1.forTeam = 'TEAM01' 
       Except 
       Select 1 
       From MailAssignments As MA1 
       Where MA1.msgid = M.msgid 
        And (MA1.processedByTeam = 'TEAM01' 
         Or MA1.notForTeam = 'TEAM01') 
       ) 
Order By M.received Desc 

SQL Fiddle版本。

掌握樣本數據和模式使世界上的所有差異。錯過的是每行Mails有多行MailAssignments行。所以,當你說你想排除notForTeam = 'TEAM01'的行時,真正意義的是排除任何具有該值的相關聯的msgid。我已更新我的查詢以適應該請求。您可能會懷疑Except子句會消除第一個查詢中存在的第二個查詢中的任何匹配值。由於我在兩個查詢中都使用了常量,因此如果第二個查詢返回給定msgid的任何行,它將使整個Exists查詢不返回任何行並排除msg。

這就是說,我們沒有得到行的TEAM01因爲:和這樣的:

MsgId = 54 is excluded because according to Rule #3, messages 
    that were processed by the team should be excluded. 
MsgId = 53 is excluded because notForTeam = 'TEAM01' (Rule #5) 
MsgId = 56 is excluded because processedBy = 'TEAM01' (Rule #7) 
MsgId = 55 is excluded because forTeam = 'TEAM02' 
+0

@ValCool - 你確定你寫出了正確的查詢嗎? – Thomas

+0

@ValCool - 我添加了一個SQL Fiddle示例,它可以正常使用上述查詢。 – Thomas

+0

@ValCool - 如果有問題的查詢返回正確的結果,現在更多的是巫婆。有些事情要檢查:1. C#應用程序連接到您在SSMS中運行成功測試的相同數據庫。 2.錯誤數據類型的值未傳遞給Command對象中的某個參數。 3.如果真正的查詢中有UDF,那些UDF不會引發錯誤。我的建議是將部分查詢註釋掉,直到找到引發錯誤的行。 – Thomas

0

嘗試:

WHERE 
MailAssignments.forTeam='TEAM01' 
AND MailAssignments.notForTeam<>'TEAM01' 
AND MailAssignments.processedByTeam<>'TEAM01' 
ORDER BY Mails.sortingTime DESC 

這應該做的伎倆

+0

但是這很可能也會返回'forTeam'列中沒有任何值的郵件,尚未與任何球隊相關。我想在我的查詢中顯示已經分配給指定團隊的郵件(並且可能已被其他團隊標記爲「不適合他們的團隊」),但也未被團隊處理 – Val

+0

我遵循您的問題:forTeam:'TEAM01'or nothing, notForTeam:與'TEAM01'不同的所有東西, processedByTeam:與'TEAM01'不同的所有東西 – ivowiblo

+0

是的,你說得對。我的錯。經過這麼多嘗試修復之後,我在代碼中迷失了方向。我已更正問題 – Val

相關問題