按分数划分的SQL组

时间:2014-02-28 14:38:28

标签: sql group-by

我有一个查询,显示有关采购订单的一些信息, 现在我有一张桌子有得分的机会。

这包含以下数据:

0% - Geen kans
100% order
0% - Klant
20% FM > 1 jaar
20% > 1 jaar
40% 6 maanden
50% 3 maanden
60% 2 maanden
60% FM 2 maanden
70% 1 maand
80& 2 weken
90% 1 week

现在我想根据这些数据对结果进行分组,最高机会, 所以100% - > 90% - > 80% - >等

但;如果我使用ORDER BY score.SCORECODE ASC它会向我显示第一个NULL,然后是0%,然后是100%。 有谁知道如何解决这个问题?

编辑:这是我的完整查询:

select distinct 
rela.name "Relatie" 
,            dealer.NAME "Dealer"
,      (addr.STREET+' '+CAST(addr.HOUSENUMBER as varchar(30))+' '+CAST(addr.ADDITIONHOUSENUMBER AS varchar(30))+' '+addr.ZIPCODE+' '+addr.CITY+' '+coun.[NAME]) as "Relatieadres"
,      atyp.AUTOTYPECODE "Autotype"
,      score.SCORECODE "Verkoopstadium"
,      type.[DESCRIPTION] "Taaktype"
,      offe.OFFERDATE "Offertedatum"
,      (pers.FIRSTNAME+' '+pers.LASTNAME) as "Werknemer"
,            todo.DATECREATED as "Begindatum"
,            todo.DUEDATE "Vervaldatum" 
,      crel.AUTOAANPASSINGCODE "type aanpassing"
,      stat.STATE "Workflowstatus"
,      rela.PHONE1 "Telefoonnummer"
,      rela.PHONE2 "Relatie mobiel"
,      (pers1.FIRSTNAME+' '+pers1.LASTNAME) as "Contactpersoon"
,      posi.[DESCRIPTION] "Functie contactpersoon"
,      todo.PLAINTEXT_INTERNALMEMO "Interne memo"
from r_todo todo
LEFT OUTER JOIN r_relation rela
ON todo.FK_RELATION        = rela.PK_R_RELATION
LEFT OUTER JOIN R_DEALER dela
ON dela.FK_RELATION = rela.FK_DEALER
LEFT OUTER JOIN r_relation dealer
ON rela.FK_DEALER = dela.FK_RELATION
LEFT OUTER JOIN r_todotype type
ON todo.FK_TODOTYPE        = type.PK_R_TODOTYPE
LEFT OUTER JOIN R_EMPLOYEE empl
ON   todo.FK_ASSIGNEDTO          = empl.PK_R_EMPLOYEE
LEFT OUTER JOIN r_person pers
ON   empl.FK_PERSON          = pers.PK_R_PERSON
LEFT OUTER JOIN r_address addr
ON   rela.FK_VISITINGADDRESS = addr.PK_R_ADDRESS
LEFT OUTER JOIN r_country coun
ON   addr.FK_COUNTRY         = coun.PK_R_COUNTRY
LEFT OUTER JOIN r_workflowstate stat
ON  todo.FK_WORKFLOWSTATE   = stat.PK_R_WORKFLOWSTATE
LEFT OUTER JOIN r_contact cont
ON cont.FK_RELATION        = rela.PK_R_RELATION
LEFT OUTER JOIN R_POSITION posi
ON cont.FK_POSITION        = posi.PK_R_POSITION
LEFT OUTER JOIN r_person pers1
ON   cont.FK_PERSON          = pers1.PK_R_PERSON
LEFT OUTER JOIN R_offer offe
ON   offe.FK_RELATION        = rela.PK_R_RELATION
LEFT OUTER JOIN x_r_relation xrela
ON xrela.FK_R_RELATION        = rela.PK_R_RELATION
LEFT OUTER JOIN c_relatieautotype atyp
ON   xrela.FK_AUTOTYPE       = atyp.PK_C_RELATIEAUTOTYPE
LEFT OUTER JOIN c_relatieautoaanpassing crel
ON   xrela.FK_AUTOAANPASSING       = crel.PK_C_RELATIEAUTOAANPASSING
LEFT OUTER JOIN C_RELATIESCORE score
ON xrela.FK_SCORE        = score.PK_C_RELATIESCORE
where todo.DUEDATE is not null
ORDER BY score.SCORECODE ASC

1 个答案:

答案 0 :(得分:0)

ORDER BY CASE WHEN Verkoopstadium IS NULL THEN '101%' ELSE 0 END, Verkoopstadium