我如何修改这个 SELECT 查询 return 给我一组包含相同数据的行以获得单行?
How can I modify this SELECT query that return to me a set of rows that contains the same data to obtain a single row?
我不太喜欢数据库,我正在开发一个在 Oracle 上执行此 SELECT 查询的项目数据库:
SELECT coda.desc_unita1, coda.pk_coda, coda.stato, coda.fk_tipo_doc, coda.descrizione,
coda.num_doc, coda.data_in as data_in, coda.indirizzamento, coda.segnalazione, coda.autorizza , coda.fornitore,
coda.data_app, coda.motivo_rifiuto, coda.tot_fattura, coda.data_doc, coda.fk_piva_mittente, coda.fk_piva_destinatario
FROM (SELECT d.rag_soc1 desc_unita1, a.pk_coda, a.fk_tipo_doc, b.descrizione, a.num_doc,
a.data_in, c.descrizione AS stato, a.indirizzamento, a.segnalazione ,'true' as autorizza,
f.rag_soc1 fornitore ,a.data_lavorazione_approvatore data_app,a.motivo_rifiuto,
a.tot_fattura,a.data_doc, a.fk_piva_mittente, a.fk_piva_destinatario
FROM coda_rx a, tipo_documenti b, stati c, fornitori d ,aggregazioni e, fornitori f
WHERE a.fk_tipo_doc = b.pk_tipo_doc AND a.fk_piva_mittente = d.pk_piva
AND e.piva_destinatario = f.pk_piva AND a.fk_stato = c.pk_stato
AND b.pk_tipo_doc = 9 AND A.CANALE IN ('WEB','BDE')AND A.FK_STATO IN( 12)
AND e.pk_coda_rif = a.pk_coda AND A.NUM_DOC=e.NUM_DOC_PADRE AND e.PIVA_MITTENTE=A.FK_PIVA_DESTINATARIO
AND e.tipo_doc in (0,3) ORDER BY A.PK_CODA) coda
这个查询return给我一些包含所有相同数据的记录(不是一个,而是多个),像这样:
COMPANY SRL 70028 PARKING 9 FATTURA 2222211111 26-MAG-15 true COMPANY SRL 01392380547 05779711000
COMPANY SRL 70028 PARKING 9 FATTURA 2222211111 26-MAG-15 true COMPANY SRL 01392380547 05779711000
COMPANY SRL 70028 PARKING 9 FATTURA 2222211111 26-MAG-15 true COMPANY SRL 01392380547 05779711000
COMPANY SRL 70028 PARKING 9 FATTURA 2222211111 26-MAG-15 true COMPANY SRL 01392380547 05779711000
COMPANY SRL 70028 PARKING 9 FATTURA 2222211111 26-MAG-15 true COMPANY SRL 01392380547 05779711000
如何修改此查询以从先前的 returned 行集中获取单行?因此我需要获取如下内容:
COMPANY SRL 70028 PARKING 9 FATTURA 2222211111 26-MAG-15 true COMPANY SRL 01392380547 05779711000
阅读 SQL 文档,在我看来我可以使用 DISTINCT 操作来完成它,还是我遗漏了什么?你能帮我解决这个问题吗?
一个简单的 "group by" 就可以解决问题:
您当前的结果集(快速示例):
select 'COMPANY SRL' desc_unita1 ,
70078 as pk_coda,
'PARKING 9' stato
from dual
connect by level < 5 ;
DESC_UNITA1 PK_CODA STATO
----------- ---------- ---------
COMPANY SRL 70078 PARKING 9
COMPANY SRL 70078 PARKING 9
COMPANY SRL 70078 PARKING 9
COMPANY SRL 70078 PARKING 9
添加 GROUP BY 子句:
select desc_unita1,pk_coda,stato
from
(
select 'COMPANY SRL' desc_unita1 , 70078 as pk_coda, 'PARKING 9' stato
from dual
connect by level < 5
)
group by desc_unita1,pk_coda,stato;
DESC_UNITA1 PK_CODA STATO
----------- ---------- ---------
COMPANY SRL 70078 PARKING 9
这是您期望的结果吗?
我不太喜欢数据库,我正在开发一个在 Oracle 上执行此 SELECT 查询的项目数据库:
SELECT coda.desc_unita1, coda.pk_coda, coda.stato, coda.fk_tipo_doc, coda.descrizione,
coda.num_doc, coda.data_in as data_in, coda.indirizzamento, coda.segnalazione, coda.autorizza , coda.fornitore,
coda.data_app, coda.motivo_rifiuto, coda.tot_fattura, coda.data_doc, coda.fk_piva_mittente, coda.fk_piva_destinatario
FROM (SELECT d.rag_soc1 desc_unita1, a.pk_coda, a.fk_tipo_doc, b.descrizione, a.num_doc,
a.data_in, c.descrizione AS stato, a.indirizzamento, a.segnalazione ,'true' as autorizza,
f.rag_soc1 fornitore ,a.data_lavorazione_approvatore data_app,a.motivo_rifiuto,
a.tot_fattura,a.data_doc, a.fk_piva_mittente, a.fk_piva_destinatario
FROM coda_rx a, tipo_documenti b, stati c, fornitori d ,aggregazioni e, fornitori f
WHERE a.fk_tipo_doc = b.pk_tipo_doc AND a.fk_piva_mittente = d.pk_piva
AND e.piva_destinatario = f.pk_piva AND a.fk_stato = c.pk_stato
AND b.pk_tipo_doc = 9 AND A.CANALE IN ('WEB','BDE')AND A.FK_STATO IN( 12)
AND e.pk_coda_rif = a.pk_coda AND A.NUM_DOC=e.NUM_DOC_PADRE AND e.PIVA_MITTENTE=A.FK_PIVA_DESTINATARIO
AND e.tipo_doc in (0,3) ORDER BY A.PK_CODA) coda
这个查询return给我一些包含所有相同数据的记录(不是一个,而是多个),像这样:
COMPANY SRL 70028 PARKING 9 FATTURA 2222211111 26-MAG-15 true COMPANY SRL 01392380547 05779711000
COMPANY SRL 70028 PARKING 9 FATTURA 2222211111 26-MAG-15 true COMPANY SRL 01392380547 05779711000
COMPANY SRL 70028 PARKING 9 FATTURA 2222211111 26-MAG-15 true COMPANY SRL 01392380547 05779711000
COMPANY SRL 70028 PARKING 9 FATTURA 2222211111 26-MAG-15 true COMPANY SRL 01392380547 05779711000
COMPANY SRL 70028 PARKING 9 FATTURA 2222211111 26-MAG-15 true COMPANY SRL 01392380547 05779711000
如何修改此查询以从先前的 returned 行集中获取单行?因此我需要获取如下内容:
COMPANY SRL 70028 PARKING 9 FATTURA 2222211111 26-MAG-15 true COMPANY SRL 01392380547 05779711000
阅读 SQL 文档,在我看来我可以使用 DISTINCT 操作来完成它,还是我遗漏了什么?你能帮我解决这个问题吗?
一个简单的 "group by" 就可以解决问题:
您当前的结果集(快速示例):
select 'COMPANY SRL' desc_unita1 ,
70078 as pk_coda,
'PARKING 9' stato
from dual
connect by level < 5 ;
DESC_UNITA1 PK_CODA STATO
----------- ---------- ---------
COMPANY SRL 70078 PARKING 9
COMPANY SRL 70078 PARKING 9
COMPANY SRL 70078 PARKING 9
COMPANY SRL 70078 PARKING 9
添加 GROUP BY 子句:
select desc_unita1,pk_coda,stato
from
(
select 'COMPANY SRL' desc_unita1 , 70078 as pk_coda, 'PARKING 9' stato
from dual
connect by level < 5
)
group by desc_unita1,pk_coda,stato;
DESC_UNITA1 PK_CODA STATO
----------- ---------- ---------
COMPANY SRL 70078 PARKING 9
这是您期望的结果吗?