1

I have a table like this:

jobid, orderid

And with some data inside:

jobid, orderid
1245, 6767
1235, 9058
6783, 6767
4991, 6767
9512, 9058
5123, 1234

Now I want the following output:

jobid, orderid, orderid(total)
1245, 6767, 3
1235, 9058, 2
6783, 6767, 3
4991, 6767, 3
9512, 9058, 2
5123, 1234, 1

Now, the COUNT() doesn't work the way I want to, and I probably need some group by but I don't know how.

Thanks in advance.

5 Answers 5

5

It looks like you're trying to get rows which look like jobid, orderid, number of times that orderid appears. For that, you could use a subquery:

 SELECT jobid, orderid, 
        (SELECT COUNT(*) FROM 
         MY_TABLE INR 
             WHERE INR.orderid = OTR.orderid) as "orderid(total)"
 FROM MY_TABLE OTR
Sign up to request clarification or add additional context in comments.

6 Comments

This is a lot more work for the SQL engine than Tim's solution.
@dtbarne It looks like what the OP is asking for.
I think your SQL statement is incorrect, since when I replace MY_TABLE with my table, I have an error. Also INNER seems out of place (and outer) but when I remove them, I get a total count of all rows and not what I need.
@Devator I had intended to abbreviate inner and outer. They're SQL keywords, so that might explain the breaking.
This is correct (although I wouldn't use INNER or OUTER as aliases.)
|
2

Why are doing it this way? You will be doing a lot of redundant countings and put a lot of unnecessary pressure on your server. I would do this with two queries:

SELECT jobid, orderid FROM my_table

to get the complete list, and then:

SELECT orderid, COUNT(*) FROM my_table GROUP BY orderid

to get the total count for each orderid. Then combine these two results in your application. This will be much faster than your solution.

5 Comments

Unnecessary pressure is not the word I'd use. The database will just be just sending a simple integer column (with few or many duplicates). In that view, your approach puts "unnecessary pressure" on the application to combine the results (some sorting or hashing will be needed).
@ypercube I don't know what language is OP using but in PHP I would fill a $count array with $orderid => $count key/value pairs from the second query and when iterating through the results from the first query I would get the count for each row from that array $count[ $orderid ] Adds very little pressure on the application side.
I agree. The same does the other approach, little pressure on the db. Only some redundance in the transfer of results.
Well this is an alternative approach. Which one performs better depends on many factors including the distribution of rows with simillar orderid in that table. OP can try both. I personally usually first go with the approach that has less redundancies.
Thank you. I'm going with this way since my this will go a lot faster. I have thought of this before but I thought it would be better using one query, but it's not the case definately.
2
SELECT jobid, orderid, count(orderid)
FROM sometable
GROUP BY orderid, jobid

2 Comments

Doesn't work either, since count(orderid) always gives me "1".
This query is not correct. It won't count all occurences of same orderid.
2
SELECT t.jobid
     , t.orderid
     , grp.orderid_total
FROM
    tableX AS t
  JOIN
    ( SELECT orderid
           , COUNT(*) AS orderid_total
      FROM tableX
      GROUP BY orderid
    ) AS grp
      ON grp.orderid = t.orderid

Comments

0
select jobid, orderid, count(*) from table group by orderid;

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.