Skip to content

Tag: mysql

How can I group data from multiple rows into the same row?

I have a query: That returns I want to combine the rows and take one value from each column (doesn’t matter which, first, last, can be any) So one elementary school, one high school, one university. It would look like: However, when I try to group by: I’m getting only a value for one of the column…

Count of line item in MySQL

I have a complex query joined to multiple tables to show a “transaction history”, I want a quick and easy way to count the occurrences of order_id to make it as the line_number. I had it done with code but for performance reason I want to have it done with MySQL. Here is an example of what I have:…

Ranking subcategories while keeping order of ID

I have the following table: I would like to rank the subcategory (cat2). Desired outcome: I use DENSE_RANK with PARTITION BY to get the following result: Statement: As you can see, the only thing I’m missing is the order of the result. Currently, the rank is based on the alphabetic order of cat2. Howeve…

SQL:pivot: convert each row into many rows depends on a condition

I have been trying to solve this but could not figure it out. so this is the table and more columns could be added: So if the value is one then I will display it in a different row but with the value being the column header itself My boss suggested using pivot to solve this but I am open