Get first and last record number in every date exists in table

Ahmed

I am trying to show invoices for every single day, so for that purpose I used group by on created date and sum on subtotal. This is how I done it :

  SELECT 
         `main_table`.*, 
         SUM(subtotal) AS `total_sales` 
  FROM 
        `sales_invoice` AS `main_table` 
  GROUP BY 
        DATE_FORMAT(created_at, "%m-%y")

Its working, but I also want to get the Invoice # from and Invoice # to for every date. Is it possible to do it with single query ?

EDIT :

Table Structure :

------------------------------------------------
| id | inoice_no | created_at           | subtotal

| 1  | 34        | 2015-03-17 05:55:27  | 5 
| 2  | 35        | 2015-03-17 12:35:00  | 7
| 3  | 36        | 2015-03-20 01:40:00  | 3
| 4  | 37        | 2015-03-20 07:05:13  | 6 
| 5  | 38        | 2015-03-20 10:25:23  | 1
| 6  | 39        | 2015-03-24 12:00:00  | 6
------------------------------------------------

Output

---------------------------------------------------------------
| id | inoice_no | created_at           | subtotal | total_sales

| 2  | 35        | 2015-03-17 12:35:00  | 7        | 12
| 5  | 38        | 2015-03-20 10:25:23  | 1        | 10
| 6  | 39        | 2015-03-24 12:00:00  | 6        | 6
-----------------------------------------------------------------

What I Expect

---------------------------------------------------------------
| id | inoice_no | created_at           | subtotal | total_sales | in_from | in_to

| 2  | 35        | 2015-03-17 12:35:00  | 7        | 12          | 34      | 35
| 5  | 38        | 2015-03-20 10:25:23  | 1        | 10          | 36      | 38
| 6  | 39        | 2015-03-24 12:00:00  | 6        | 6           | 39      | 39
-----------------------------------------------------------------
Saharsh Shah

If your invoice number is INTEGER then below query will give you the result what you want:

SELECT DATE_FORMAT(A.created_at, "%m-%y") AS InvoiceDate, 
       MIN(A.invoiveNo) AS FromInvoiceNo, 
       MAX(A.invoiveNo) AS ToInvoiceNo, 
       SUM(A.subtotal) AS total_sales 
FROM sales_invoice AS A 
GROUP BY InvoiceDate;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get first record and last record in a date

From Dev

Get values of first record and last record by date in mysql subquery

From Dev

Get First login record and last logout record for each date

From Dev

How to get first and last record of every month in Sqlite

From Dev

How to get last first and last from table with same date (query)?

From Dev

Get last record by date for multiple table with many-to-one relationship

From Dev

Get date of first and last day of week knowing week number

From Dev

How to get first and last date of a given week number and year?

From Dev

Get date of first and last day of week knowing week number

From Dev

How to get first and last date of a given week number and year?

From Dev

How to get the first and the last record of every month within a time range in sql

From Dev

Get last record in mysql table

From Dev

SQL query to get last record of every minute

From Dev

Get first record by latest date

From Dev

Get the first record of every 1000 items in laravel

From Dev

How to select last record by date in a joined table

From Dev

get Date for the last of a number of weeks

From Dev

How to find a last record for a record in first table in mysql

From Dev

Need to get the First ordered date and last ordered date for all customers from order table

From Dev

How to get last 7 days record from table as per date using PHP and MySQL

From Dev

Spring Data - get last record from the table

From Dev

Get Last Five Record From Table

From Dev

how to get last inserted record in a table?

From Dev

get last month first date and last date in batch file

From Dev

How to get first and last date from MySQL table using EF in C#?

From Dev

Get the first and last date of the previous month in JQuery

From Dev

How to get the first and the last date in a file

From Dev

Get first and last date from a parsed array

From Dev

How to get the date of every last Monday from a giving date in MySQL

Related Related

  1. 1

    How to get first record and last record in a date

  2. 2

    Get values of first record and last record by date in mysql subquery

  3. 3

    Get First login record and last logout record for each date

  4. 4

    How to get first and last record of every month in Sqlite

  5. 5

    How to get last first and last from table with same date (query)?

  6. 6

    Get last record by date for multiple table with many-to-one relationship

  7. 7

    Get date of first and last day of week knowing week number

  8. 8

    How to get first and last date of a given week number and year?

  9. 9

    Get date of first and last day of week knowing week number

  10. 10

    How to get first and last date of a given week number and year?

  11. 11

    How to get the first and the last record of every month within a time range in sql

  12. 12

    Get last record in mysql table

  13. 13

    SQL query to get last record of every minute

  14. 14

    Get first record by latest date

  15. 15

    Get the first record of every 1000 items in laravel

  16. 16

    How to select last record by date in a joined table

  17. 17

    get Date for the last of a number of weeks

  18. 18

    How to find a last record for a record in first table in mysql

  19. 19

    Need to get the First ordered date and last ordered date for all customers from order table

  20. 20

    How to get last 7 days record from table as per date using PHP and MySQL

  21. 21

    Spring Data - get last record from the table

  22. 22

    Get Last Five Record From Table

  23. 23

    how to get last inserted record in a table?

  24. 24

    get last month first date and last date in batch file

  25. 25

    How to get first and last date from MySQL table using EF in C#?

  26. 26

    Get the first and last date of the previous month in JQuery

  27. 27

    How to get the first and the last date in a file

  28. 28

    Get first and last date from a parsed array

  29. 29

    How to get the date of every last Monday from a giving date in MySQL

HotTag

Archive