2012-12-24 32 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作为外键。