0

I have a order table which i would like to index each company's first second third ... etc order.

Example of table:

|---------------------|--------------------------|
|      company_id     |     created_at           |
|---------------------|--------------------------|
|          1          |   2019-01-01 01:00:00    |
|---------------------|--------------------------|
|          1          |   2019-02-01 01:00:00    |
|---------------------|--------------------------|
|          2          |   2019-03-01 02:00:00    |
|---------------------|--------------------------|
|          3          |   2019-03-01 08:30:00    |
|---------------------|--------------------------|
|          2          |   2019-03-01 10:00:00    |
|---------------------|--------------------------|

I Would it be queried like this in the end. but im not sure how..... i keep going back to counting and grouping the fields but i cant seem to get the sum for each row based on the timestamp

|---------------------|--------------------------|-------|
|      company_id     |     created_at           |count()|
|---------------------|--------------------------|--------
|          1          |   2019-01-01 01:00:00    |   1   |
|---------------------|--------------------------|--------
|          1          |   2019-02-01 01:00:00    |   2   |
|---------------------|--------------------------|--------
|          2          |   2019-03-01 02:00:00    |   1   |
|---------------------|--------------------------|--------
|          3          |   2019-03-01 08:30:00    |   1   |
|---------------------|--------------------------|--------
|          2          |   2019-03-01 10:00:00    |   2   |
|---------------------|--------------------------|--------
1

2 Answers 2

2
SELECT 
    @row_number:=CASE
        WHEN @company_id = company_id THEN @row_number + 1
        ELSE 1
    END AS count1,
    @company_id:=company_id as company_id,
    created_at
FROM
    Table1,(SELECT @company_id:=0,@row_number:=0) as t
ORDER BY company_id;

SQL Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

how does @row_number actually work ? im not familiar with using @ in sql
Napmi : you are initializing a variable @row_number with 0 to start you count for company_id. Just saying: your answer query (for the same question) will be more time consuming
i believe you are right about the time consuming part. as my order table increases in size , im gonna be screwed by my query.
0

My solution in the end was as below , which seems more simplier than the above has suggested.

SELECT company_id
,(
    SELECT count(*) + 1
    FROM ORDERS AS orderCount
    WHERE orderCount.company_id = orders.company_id
        AND orderCount.created_at < orders.created_at
    ) AS order_count
FROM ORDERS
ORDER BY created_at

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.