SQL报告生成重复行

时间:2018-08-21 04:57:31

标签: mysql sql duplicates

我是SQL报表编写的新手,尝试从SQL表生成报表,但结果中的每个所需行都得到3个重复行。但是,输出数据符合我的要求。下面给出了完整的代码,请帮我解决。

SELECT 
   v.dateaccessioned AS 'Date', v.barcode AS 'Acc. No.', w.title AS 'Title',
   w.author AS 'Author/Editor', concat_ws(' , ', o.editionstatement, 
   v.enumchron) AS 'Ed./Vol.',
   concat_ws(' ', o.place, o.publishercode) AS 'Place & Publisher',
   w.copyrightdate AS 'Year', o.pages AS 'Page(s)', s.name AS 'Source',
   v.itemcallnumber AS 'Class No./Book No.', concat_ws(', ₹', concat(' ',
   z.symbol, x.listprice), x.rrp_tax_included) AS 'Cost', concat_ws(' , ',
   r.invoicenumber, r.shipmentdate) AS 'Bill No. & Date', '' AS 'Withdrawn 
   Date', '' AS 'Remarks'

FROM biblioitems o
LEFT JOIN items v ON v.biblioitemnumber=o.biblioitemnumber
LEFT JOIN biblio w ON w.biblionumber=o.biblionumber
LEFT JOIN aqorders x ON x.biblionumber=o.biblionumber
LEFT JOIN currency z ON z.currency=x.currency
LEFT JOIN aqinvoices r ON r.booksellerid=v.booksellerid
LEFT JOIN aqbooksellers s ON s.id=v.booksellerid

WHERE v.barcode BETWEEN <<Accession Number>> AND <<To Accession Number>>
ORDER BY v.barcode ASC

3 个答案:

答案 0 :(得分:0)

如果行相同,则可以使用如下不同的关键字:

SELECT distinct
v.dateaccessioned AS 'Date', v.barcode AS 'Acc. No.', w.title AS 'Title',
w.author AS 'Author/Editor', concat_ws(' , ', o.editionstatement, 
v.enumchron) AS 'Ed./Vol.',
concat_ws(' ', o.place, o.publishercode) AS 'Place & Publisher',
w.copyrightdate AS 'Year', o.pages AS 'Page(s)', s.name AS 'Source',
v.itemcallnumber AS 'Class No./Book No.', concat_ws(', ₹', concat(' ', 

z.symbol, x.listprice), x.rrp_tax_included) AS 'Cost', concat_ws(' , ', 

r.invoicenumber, r.shipmentdate) AS 'Bill No. & Date', '' AS 'Withdrawn 

Date', '' AS 'Remarks'

FROM biblioitems o
LEFT JOIN items v ON v.biblioitemnumber=o.biblioitemnumber
LEFT JOIN biblio w ON w.biblionumber=o.biblionumber
LEFT JOIN aqorders x ON x.biblionumber=o.biblionumber
LEFT JOIN currency z ON z.currency=x.currency
LEFT JOIN aqinvoices r ON r.booksellerid=v.booksellerid
LEFT JOIN aqbooksellers s ON s.id=v.booksellerid

WHERE v.barcode BETWEEN <<Accession Number>> AND <<To Accession Number>>
ORDER BY v.barcode ASC

答案 1 :(得分:0)

我找到了一个窍门并完成了。 v.barcode是唯一记录,所以我刚刚添加了

按条码分组

并且每条记录仅获得一行。

答案 2 :(得分:-1)

您与众不同或分组。在下面的代码中,我使用了分组依据。

SELECT 
v.dateaccessioned AS 'Date'
, v.barcode AS 'Acc. No.'
, w.title AS 'Title'
, w.author AS 'Author/Editor'
, concat_ws(' , ', o.editionstatement,     v.enumchron) AS 'Ed./Vol.'
,    concat_ws(' ', o.place, o.publishercode) AS 'Place & Publisher'
,    w.copyrightdate AS 'Year', o.pages AS 'Page(s)', s.name AS 'Source'
,    v.itemcallnumber AS 'Class No./Book No.'
, concat_ws(', ₹', concat(' ',     z.symbol, x.listprice), x.rrp_tax_included) AS 'Cost'
, concat_ws(' , ', r.invoicenumber, r.shipmentdate) AS 'Bill No. & Date'
, '' AS 'Withdrawn Date'
, '' AS 'Remarks'

FROM biblioitems o
LEFT JOIN items v ON v.biblioitemnumber=o.biblioitemnumber
LEFT JOIN biblio w ON w.biblionumber=o.biblionumber
LEFT JOIN aqorders x ON x.biblionumber=o.biblionumber
LEFT JOIN currency z ON z.currency=x.currency
LEFT JOIN aqinvoices r ON r.booksellerid=v.booksellerid
LEFT JOIN aqbooksellers s ON s.id=v.booksellerid

WHERE v.barcode BETWEEN <<Accession Number>> AND <<To Accession Number>>
GROUP BY 
v.dateaccessioned 
, v.barcode 
, w.title 
,w.author 
, concat_ws(' , ', o.editionstatement, v.enumchron) 
,concat_ws(' ', o.place, o.publishercode) 
,w.copyrightdate 
, o.pages 
, s.name 
,v.itemcallnumber
, concat_ws(', ₹', concat(' ', z.symbol, x.listprice), x.rrp_tax_included) 
, concat_ws(' , ', r.invoicenumber, r.shipmentdate)
ORDER BY v.barcode ASC