简体   繁体   中英

Query to get all the rows that have the same date with respect to their invoice numbers?

I have table from which I have to get invoiceNo, Sum of all the weights, count the number of barcodes and the last value in sum_total from table called selected_items for a particular date.

    SELECT invoiceNo, SUM(weight), COUNT(barcode), sum_total
FROM selected_items
WHERE date LIKE '07-Jan-2016' GROUP BY invoiceNo ORDER BY id DESC;

This is what my database looks like. 在此处输入图片说明

This what I get in output when I run this query in php my admin. 在此处输入图片说明

What I am wanting to do is get the invoiceNo, sum of weight,count of barcodes and the last value in sum_total for that particular invoiceNo on a certain date. In my current query I am getting the sum of weight for 6 items,barcode count as 6 and the first value in sum_total. I have even tried this query but it does not give the expected answer.

SELECT invoiceNo, SUM(weight), COUNT(barcode), sum_total
FROM selected_items
WHERE invoiceNo IN ('IN1001','ES102');

This is what I am expecting to achieve. 在此处输入图片说明

To achieve what you need, you should group your result by invoiceNo , so your query looks like this:

SELECT invoiceNo, SUM(weight), COUNT(barcode), sum_total
FROM selected_items
WHERE date LIKE '07-Jan-2016' GROUP BY invoiceNo;

This might help you do give it a try.

SELECT invoiceNo, SUM( weight ) , COUNT( barcode ) , sum_total
FROM (SELECT * FROM  `selected_items` 
WHERE DATE LIKE  '07-Jan-2016'
ORDER BY id DESC
)a
GROUP BY invoiceNo;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM