查询对具有变体的项目进行分组

时间:2015-12-03 11:32:27

标签: mysql sqlite

我有这些数据:

"progress" "description" "quantity"
"1" "crodino"   "1"
"2" "bitter"    "1"
"3" "+ghiaccio" "1"
"4" "crodino"   "2"
"5" "bitter"    "1"
"6" "crodino"   "1"
"7" "bitter"    "1"
"8" "+limone"   "1"
"9" "-oliva"    "1"
"10" "bitter"   "1"
"1" "bitter"    "2"

没有" +"的项目或" - "在第一个字符是正常的产品,但项目与" +"或" - "是变种(例如:+ limone和 - oliva是苦味的变种,其进展数为7)

我需要一个查询结果:

"progress" "description" "quantity"
"1" "bitter" "4"
"2" "bitter" "1"
"3" "+limone" "1"
"4" "- oliva" "1"
"5" "bitter" "1"
"6" "+ghiaccio" "1"
"7" "crodino" "4"

此:

  • 首先在下一个进度编号中获取带有变体的产品并显示
  • 按字母顺序排列主要产品,然后是其中的变体。
  • 对其他产品中的数量进行分组和总和

这样做的查询是什么?

我的数据库是sqlite。

1 个答案:

答案 0 :(得分:0)

我赢了! (但在Chrome中的WebSQL中不起作用:()

select table1.progressivo,table1.desc,table1.qt from  comanda as table1 
inner join  comanda as table2
on table1.progressivo = (table2.progressivo -1)
and (substr(table1.desc,1,1)!='-' and substr(table1.desc,1,1)!='+')
and (substr(table2.desc,1,1)='+' or substr(table2.desc,1,1)='-') 

union 

select progressivo,desc as descrizione, qt from comanda where 
    (substr(desc,1,1)='+' or substr(desc,1,1)='-')  

    union

select table3.progressivo,table3.desc,sum(table3.qt) from  comanda as table3 
inner join  comanda as table4
on table3.progressivo = (table4.progressivo -1)
and (substr(table3.desc,1,1)!='-' and substr(table3.desc,1,1)!='+')
and (substr(table4.desc,1,1)!='+' or substr(table4.desc,1,1)!='-')  
    group by         table3.desc

order by progressivo asc ;

这是一个铸造问题。 我修好了这个:

select table1.prog_inser,table1.desc_art,table1.quantita from  comanda as table1,comanda as table2
                    where table1.prog_inser =cast( cast(table2.prog_inser as int)-1 as varchar)
                    and table1.ntav_comanda='25' and table2.ntav_comanda='25'
                    and (substr(table1.desc_art,1,1)!='-' and substr(table1.desc_art,1,1)!='+')
                    and (substr(table2.desc_art,1,1)='+' or substr(table2.desc_art,1,1)='-')
                    union
                    select prog_inser,desc_art as descrizione, quantita from comanda where
                    (substr(desc_art,1,1)='+' or substr(desc_art,1,1)='-')
                    and ntav_comanda='25'
                    union
                    select table3.prog_inser,table3.desc_art,sum(table3.quantita) from  comanda as table3,comanda as table4
                    where table3.prog_inser =cast( cast(table4.prog_inser as int)-1 as varchar)
                    and table3.ntav_comanda='25' and table4.ntav_comanda='25'
                    and (substr(table3.desc_art,1,1)!='-' and substr(table3.desc_art,1,1)!='+')
                    and (substr(table4.desc_art,1,1)!='+' or substr(table4.desc_art,1,1)!='-')
                    group by table3.desc_art
                    order by prog_inser asc;