i have table like this
Table
I just want to display the data based on the most data appearing in the second field and I do not want to display a little data. So the data appears only the most and count the total value in the third field. And I've tried but no results.
How to query the database to display the most data only
You can do something like this,
SELECT `col_name_of_frequent_value`,
COUNT(`col_name_of_frequent_value`) AS `frequent_value`
FROM `table_name`
GROUP BY `col_name_of_frequent_value`
ORDER BY `frequent_value` DESC // This will sort the result by putting max count at top.
LIMIT 1; // This will only show the TOP-Most value in sorted result.
Please change the names as per your table structure.
SELECT
kd_masalah, total_bobot
FROM
(SELECT -- SubQuery
SUM(bobot) AS total_bobot, -- Gives the sum of values in bobot column relating to a particular group (grouped column)
kd_masalah
FROM
your_table_name
GROUP BY kd_masalah -- Grouping of same values in the column
)AS s having MAX(total_bobot); -- return the values having max sum in total_bobot
Related
I am attempting to display only one maximum value for a date value (I would like to see only one value in other columns for the newest possible date).
Are there any options how to achieve this in MS Query sql?
My table is this:
My current code is this:
SELECT stof_0."arti-cd-base", stof_0."dcmf-nr", Max(stof_0."stof-dt") AS 'Maximum z stof-dt', stof_0."stof-qty-op"
FROM NILOS.PUB.stof stof_0
GROUP BY stof_0."arti-cd-base", stof_0."dcmf-nr", stof_0."stof-qty-op", stof_0."arti-cd-sfx", stof_0."adfc-cd-diffco"
HAVING (stof_0."arti-cd-base"=1) AND (stof_0."arti-cd-sfx"=15) AND (stof_0."adfc-cd-diffco"=0)
ORDER BY stof_0."arti-cd-base", Max(stof_0."stof-dt") DESC
Would anyone know how to arrange the code so that only one value (one line) will be the result for the latest date in the table?
If you just one one row with the latest date in the table use order by and some form of limiting the results. In standard SQL, you can do:
select s.*
from NILOS.PUB.stof s
order by "stof-dt"
fetch first 1 row only;
In MySQL, you would use limit instead of fetch.
In SQL Server, you would use one of:
select top (1)
or:
offset 0 row fetch first 1 row only
I want to write a query to pull a certain set of results. While doing that I want to be able to keep a count of number of occurrences of a certain value in a certain field. I'm trying to pull a record of part-numbers from a table for devices, There are various partnumbers that are pulled from tables.
For instance this is my result set
CISCO1841
CISCO2610
CISCO2650
CISCO2610
CISCO2650
CISCO2500
I should keep track that
CISCO1841 occured one time, CISCO2610 occured twice, CISCO2650 occured twice and CISCO2500 occured once. I need to store this count and set another column value based on this count. Is there an efficient way to do this in MYSQL?
Group your query and COUNT() the results:
SELECT myColumn, COUNT(*) FROM myTable GROUP BY myColumn
See it on sqlfiddle.
You can use a trigger on the table where your data is inserted and that trigger will keep count of the occurrences by updating the counter for each word (ex : CISCO2650) in a separate table.
Or you can try a query like below but its not dynamic :
SELECT
SUM(IF(M_PART_NBR = 'CISCO2610',1,0)) as NBR1,
SUM(IF(M_PART_NBR = 'CISCO2811',1,0)) as NBR2,
SUM(IF(M_PART_NBR = 'CISCO2911',1,0)) as NBR3
FROM
C2_INST_PRODUCT WHERE M_PART_NBR LIKE '%CISCO2%'
I have an EAV table in SQL which as usual has several records for each ID. Each of these records has a numerical value in a column called 'weight' and I'm trying to sort this table so that for each ID the records are ranked in descending order of weight. This is going to be a one off process because I intend to make sure that data is sorted when it goes into the table in future.
Also is the normal protocol for doing something like this to SELECT all of the data, sort it the way you want, and then use the REPLACE command to replace the old data in the table?
I know that I can do this for one id by doing:
SELECT * FROM my_table WHERE id = 'X' ORDER BY weight DESC
but I need to somehow do this for each id in my table. How is this normally done?
you will ALWAYS return the data in the order you wish in this case:
SELECT * from theTable order by id, weight desc
you do not store the data in any particular order. (even if you try this will not matter).
Remove the WHERE clause and add id to ORDER BY. This way, your result set will be ordered first by id, and for each id, it will be ordered by weight.
SELECT * FROM my_table ORDER BY id, weight DESC
I am trying to create a search engine from a mysql datadase which displays information in order based on the value of one of the columns in the row, so if row x has a 'quantity' column
with an integer value of 10 any row y has a 'quantity'value of 20, row x should be echoed first so the greatest value is on top and the least on the bottom (no preference for equal values). or alliteratively find a way that the mysql table automatically orders the data this way. is there some kind of function that I could use. thanks
Use the ORDER BY clause Like this:
SELECT * FROM table ORDER BY quantity DESC
To return rows from a query in a specific order, use the ORDER BY clause. To get them returned from highest to lowest values, specify the DESC (descending) keyword. For example:
SELECT f.quantity
FROM foo f
ORDER BY f.quantity DESC
You are correct, there is no guarantee as to how the rows will be physically ordered when they are stored. Your ONLY guarantee is the ORDER BY clause.
When we are retrieving data from database, we use something like this
SELECT * FROM Names
However, we will get all the data inside the specific table. Since I am going to update some data to the database and want to make a comparison bewteen the last row of data in the db and the most updated data, how can I select and retrieve the last two row of the database only?
If you were using SQL Server, you would do something like this:
SELECT TOP 2 * FROM Names ORDER BY Name DESC
SQL Server syntax:
SELECT TOP 2 column_name FROM table_name ORDER BY column_name DESC;
Example:
If you want to retrieve the last value of the customer_name column from the customers table, you need to write the following query:
SELECT LAST (CUSTOMER_NAME) AS LAST_CUSTOMER FROM CUSTOMERS;
You should include a column in the Names table to keep track of when a name was added to the table since you cannot guarantee that the rows are in the order that they were inserted. With that column you can use the order by clause..
In MySQL Syntax:
SELECT *
FROM Names
ORDER BY order_column DESC
LIMIT 2;
If you want to get the last rows as they are, you cannot order the table by the inserted names because that is just getting the 2 names that come last in an alphabetically sorted list of names. You could try something like this where you include an offset in the limit clause if you get the number of rows in the table:
SELECT *
FROM Names
LIMIT *num_rows*-2, 2;
You would have to know the number of rows to use this query or use an additional query to implement a row count that works with limit. This, however, still may not be accurate since the system may not order the rows as they were inserted. I still recommend the first option where you keep track of order/time a name was inserted.