2012-12-24 30 views
-4

我有一個SQL case語句這樣SQL case語句爲組合框

select Distinct y.SupplierID,y.Name,y.AddWho , 
     "StatusDesc=CASE when y.status='N' then 'NEW' " & _ 
     "when y.status='B' then 'BLACKLISTED'" & _ 
     "when y.status='Q' then 'QUALIFIED'" & _ 
     "when y.status='R' then 'REJECTED' end , " & _ 
     "FlowStatusDesc = CASE when y.flowstatus='RC' then 'UNDER REVIEW'" & _ 
     "when y.flowstatus='PE' then 'POTENTIAL EXCLUSIVE'" & _ 
     "when y.flowstatus='PO' then 'POTENTIAL ORDINARY' ELSE '' end," & _ 
     "OrderNo=case when y.status='N' and flowstatus='' then '1'" & _ 
     "when y.status='N' and y.flowstatus<>'' then '2' " & _ 
     "when y.status='R' and y.flowstatus='' then '3'" & _ 
     "when y.status='R' and y.flowstatus<>'' then '4'" & _ 
     "when y.status='Q' and y.flowstatus='' then '5'" & _ 
     "when y.status='Q' and y.flowstatus<>'' then '6'" & _ 
     "when y.status='B' and y.flowstatus='' then '7'" & _ 
     "when y.status='B' and y.flowstatus<>'' then '8' else '9' end " & _ 
     "from AP_Supplier y" & _ 
     " left outer join SC_User u on y.addwho=u.userid " & _ 
     "left outer join SC_Company co on co.companycode=u.companycode " & _ 
     "where flowstatus is not null " & _ 
     "group by y.SupplierID,y.name,y.status,y.flowstatus,y.addwho " & _ 
     "order by orderno" 

如何,如果我可以加載所有的case語句條件,如「新」,「合格」,‘註冊’和flowstatuses成組合框在vb.net?你能給我一個例子嗎?我試過這樣做了很長一段時間,感謝。

+0

是否需要將這些狀態的單獨列表加載到組合框中?不是從這個查詢? –

+0

我的意思是,會有2個組合框,每個組合框。對於地位,它將是「新的」,「合格的」,「列入黑名單」並被拒絕,而對於流量狀態,「審查中」,「潛在普通」和「潛在排他」。 –

回答

0

有很多方法可以做到這一點,第一個是從一個組合框填充組合框在這種情況下,你應該有一個類,類似於:

public class Status(){ 
    public string Symbol { get; set; } // or Id 
    public string Name { get; set; } 
} 

那麼你有狀態列表:

var ds = new List<Status>(){ 
    new Status { Symbol = "N", Name = "New" }, 
    new Status { Symbol = "Q", Name = "Qualified" }, 
    .... 
}; 

然後你就可以使用這兩個屬性很容易填充此列表的COMBOX:

像這樣:

YourcomboboxName.DataSource = ds; 
YourcomboboxName.ValueMember = "Symbol"; 
YourcomboboxName.DisplayMember = "Name"; 

這樣做的第二種方式,就是有一個表或臨時表,包含像這樣的值,這些列表:

CREATE TABLE Statuses(StatusId INT, 
         StatusSymbol VARCHAR(2), 
         StatusName VARCHAR(20)); 

INSERT INTO Statuses(StatusId, StatusSymbol, STatusName) VALUES 
        (1, 'N' , 'NEW'), 
        (2, 'B' , 'BLACKLISTED'), 
        (3, 'Q' , 'QUALIFIED'), 
        (4, 'R' , 'REJECTED'), 
        (5, 'UR', 'UNDER REVIEW'), 
        (6, 'PE', 'POTENTIAL EXCLUSIVE'), 
        (7, 'PO', 'POTENTIAL ORDINARY'); 

然後,您可以在使用從此表讀取並填充它的數據源之前以相同的方式使用它。但是這個解決方案會讓你的查詢更容易。您可以JOIN與此表所示:

SELECT DISTINCT 
    y.SupplierID, 
    y.Name, 
    y.AddWho, 
    StatusDesc = s.STatusName, 
    FlowStatusDesc = CASE WHEN ... END 
    orderno = CASE WHEN ... 
FROM AP_Supplier y 
INNER JOIN statuses s ON y.status  = s.StatusSymbol 
LEFT JOIN SC_User  u ON y.addwho  = u.userid 
LEFT JOIN SC_Company co ON co.companycode = u.companycode 
WHERE flowstatus IS NOT NULL 
GROUP BY y.SupplierID, 
     y.name, 
     y.status, 
     y.flowstatus, 
     y.addwho 
ORDER BY orderno; 

需要注意的是:你的表這種方式,需要進行重構如果可能的話,這將是更好的擺脫這些狀態的名字,並有一個新的表Statuses在第二個表中將id作爲外鍵。