使用子集将结果按三组排序

时间:2016-01-04 08:28:01

标签: sql sqlite transactions relational-database

select stampata_sn,numero_conto,dest_stampa,portata,categoria,prog_inser,nodo,desc_art,prezzo_un,quantita from comanda where contiene_variante='1' and ntav_comanda='25' and posizione='CONTO' and stato_record='ATTIVO'   and numero_conto ="1" 

                        union all

select stampata_sn,numero_conto,dest_stampa,portata,categoria,prog_inser,nodo,desc_art,prezzo_un,sum(quantita) as quantita from comanda where (contiene_variante !='1' or contiene_variante is null) and length(nodo)=3 and ntav_comanda='25' and posizione='CONTO' and stato_record='ATTIVO'  and numero_conto ="1"  group by desc_art

                        union all

select stampata_sn,numero_conto,dest_stampa,portata,categoria,prog_inser,nodo,desc_art,prezzo_un,quantita from comanda where length(nodo)=7 and ntav_comanda='25'  and posizione='CONTO' and stato_record='ATTIVO'  and numero_conto ="1"  order by nodo asc;

我有这三个问题。

如果"之前的第一个查询全部"是查询A,第二个和第三个分别是B和C,我该怎么做:

(A(B ORDER BY B ASC),C) ORDER BY (A,C) ASC

我需要这样做:

Extract A
Extract B of A (by node) AND order alphabetical in the internal
Extract C 
Then order by A (and B is an A subset),C alphabetical

然后结果将是例如:

C   Bitter
A   Coca
B   + Ice
B   + Lemon
B   + Orange juice
A   Juice
B   + Lemon
C   Orange cup
C   Spaghetti

而不是按节点的随机顺序,例如:

C   Orange cup
A   Juice
B   + Lemon
C   Bitter
A   Coca
B   + Orange juice
B   + Ice
B   + Lemon
C   Spaghetti

我希望我已经清楚了。

1 个答案:

答案 0 :(得分:0)

我修复了这个问题:

select  substr(desc_art,1,3)||'MA'|| substr(nodo,1,3) as ord,stampata_sn,numero_conto,dest_stampa,portata,categoria,prog_inser,nodo,desc_art,prezzo_un,quantita from comanda as c1 where contiene_variante='1' and ntav_comanda='34'  and posizione='CONTO' and stato_record='ATTIVO' and  numero_conto ="1"  
union all
select  substr(desc_art,1,3)||'MA'||substr(nodo,1,3) as ord,stampata_sn,numero_conto,dest_stampa,portata,categoria,prog_inser,nodo,desc_art,prezzo_un,sum(quantita) as quantita from comanda as c1 where (contiene_variante !='1' or contiene_variante is null) and length(nodo)=3 and ntav_comanda='34' and posizione='CONTO' and stato_record='ATTIVO'  and numero_conto ="1"   group by desc_art
union all       
select  (select substr(desc_art,1,3) from comanda where nodo=substr(c1.nodo,1,3)  and ntav_comanda='34')||'MB' ||substr(nodo,1,3) as ord,stampata_sn,numero_conto,dest_stampa,portata,categoria,prog_inser,nodo,desc_art,prezzo_un,quantita from comanda as c1 where length(nodo)=7 and substr(desc_art,1,1)='-' and ntav_comanda='34'   and posizione='CONTO' and stato_record='ATTIVO'  and numero_conto ="1"  
union all
select (select substr(desc_art,1,3) from comanda where nodo=substr(c1.nodo,1,3) and ntav_comanda='34')||'MC'||substr(nodo,1,3) as ord,stampata_sn,numero_conto,dest_stampa,portata,categoria,prog_inser,nodo,desc_art,prezzo_un,quantita from comanda as c1 where length(nodo)=7 and substr(desc_art,1,1)!='-' and ntav_comanda='34'  and posizione='CONTO' and stato_record='ATTIVO'  and numero_conto ="1"   
order by ord ASC;

截图:

enter image description here

感谢您的帮助。