Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should be able to solve most of this problem using a database query, but you will still need another programming language wrapped around this query (such as java along with JDBC). Assuming you are using a SQL database of some sort that is. By the looks of your structure I am thinking you are going to need to use a group by clause. While I don't know your database structure since you didn't mention it so I am going to for the sake of this say that your table is called "products"</p> <p>Let's first design a query to get the number of each product:</p> <pre><code>SELECT pid, COUNT(*) AS NUM FROM products p GROUP BY pid ORDER BY NUM DESC; </code></pre> <p>So this query will return something as follows:</p> <pre><code>pid, NUM pid34 120 pid29 120 pid20 20 </code></pre> <p>So, this is getting closer but still not exactly what is wanted. However, now by using the query combined with a programming language, things can easily be done. I have done some java code below as an example.</p> <pre><code>// Assumes that database connection has already been made and is in the variable: conn1 Statement stmt1 = conn1.createStatement(); ResultSet rs1 = stmt1.executeQuery("SELECT pid, COUNT(*) AS NUM FROM products p GROUP BY pid ORDER BY NUM DESC"); int prevNum = -1; while(rs1.next()) { int thisNum = rs1.getInt("NUM"); if(thisNum != prevNum) { // this means it is a different number than the last result, start a new line system.out.print("\n" + thisNum + ": "); prevNum = thisNum; } system.out.print(rs1.getString("pid") + ", "); } stmt1.close(); conn1.close(); </code></pre> <p>While I may not have all the logic correct, especially when it comes to formatting, this should get you going on the right path with how you need to go about it. Really this problem needs to be solved by using a combination of queries and a programming language.</p> <p>Queries can do a small subset of problems, however they do an extremely good job at doing those problems, whereas a programming language can solve a much wider array of problems. However, a programming language will in many cases not be as efficient as the database is at solving the same problems, which is why combining the two many times can allow for the solving of complex problems with a lot of efficiency.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload