I have a table like this (unsorted): risk category Low A Medium B High C Medium A Low B High A Low C Low E Low D High B I need to sort rows by category, but first based on the value of risk. The desired result should look like this (sorted): risk category Low A Low B Low C
Tag: sql-order-by
How would I make my SQL statement show only the first line (lowest price + total purchases)
I’ve got this SQL exercise I’m trying to complete but I can’t figure out how to specifically show one row that should contain the media type with the lowest # of purchases and purchase value. I’m completely unsure of what I should add after my ‘group by’? So this is what I want my output to be: https://i.stack.imgur.com/5biiM.png And this
Mysql multiple order, one order by date with condition
I have a simple SQL request which orders the result by the number of sales and then by the number of views. But I would like the new images with a date greater than for example “2022-05-01” to appear just after the ones that have been sold. Is it possible to have multiple order and in one of this order
How to select best customers and group them by how much they paid
I am using SQL server to select this table and group the customer id and first name according to their total payments made to the store. I tried using but it returns the following error: not contained in either an aggregate function or the GROUP BY clause Could you please clarify what I did wrong? this is my customer table
MySQL ignore NULL Value while using LAG() function
I want to calculate the MonthByMonth difference of payments happening throughout the year. For example, if I had 100 payments in February & 120 in the month of March, the difference would be 20. I have already made a query that uses LAG(), but the only problem I’m facing is that the query is showing NULL Value. Since there are
SQL Query, What have I done wrong? I am fairly new to mySQL
Solution problem solution uploaded Answer You have to tell the count() function what to count. You can insert an individual column, or * for all of it etc. But you have to count something. This is a fiddle, showing how it works: https://www.db-fiddle.com/f/dbPnE4BXv8oRRkQY4WQs8v/1
Select the row with the most recent modified date
I have this table: user_id name email modified_date 1 John a@gmail.com 2022-01-01 1 John b@gmail.com 2022-01-02 1 Lucy c@gmail.com 2022-01-03 2 Joey d@gmail.com 2021-12-24 3 Mike e@gmail.com 2022-01-01 3 Mary f@gmail.com 2022-01-02 I’m trying to get unique user_id’s email with the most recent modified_date. This is my expected output: user_id email name 1 c@gmail.com Lucy 2 d@gmail.com Joey 3 f@gmail.com
How to SUM values of a column and then order by another column using SQL?
When I SELECT * FROM table; I have the following result: So I try to sum the total of votes with SUM(votes) as tt_votes and then group the result by candidate to have a desc order. state column shows the first (or the last?) record for the candidate, but what I’d really like to have is not only group by
SQLite – calculate percentage increase in values of a numeric column
I am interested in getting the % increase or decrease of the values in one column with respect to the previous value. Can someone please advice how I should do this? Thanks This is what my table looks like And this is what I would like Answer Use LAG() window function to get the previous value of sales for each
SQL – Ordering table with subquery for select
I am trying to do ordering on query with subquery for a name from uuid. Let’s have those two tables in MySQL: bans: id uuid time reason 1 c6b8eade-7db9-345b-b838-8f8361552cf5 1642369382 Test 2 0138c279-c5fa-3acd-adaa-8edb7b4569d8 1642384565 Spam 3 3c01262c-a3c3-3133-ba43-92a9ded01c27 1631876477 Hax users: id uuid name 45 c6b8eade-7db9-345b-b838-8f8361552cf5 John 100 0138c279-c5fa-3acd-adaa-8edb7b4569d8 Mike 35 3c01262c-a3c3-3133-ba43-92a9ded01c27 Norman With this query, I can select whole ban