Not getting max value more than 'D99999' in MySQL from varchar column - mysql

Deposit_number
--------------
D100000
D99999
D50
.....
D4
D3
D2
D1
I am trying to get record by using following JPA query deposit
Deposit findTopByCompanyOrderByDepositNumberDesc(Company company);
But in result I am getting record which containing 'D99999' and I am expecting 'D100000'. Please help if any one have idea to fix this problem. I want to solve this using a JPA query, not by using a SQL native query.

The problem is that your Deposit_number column is text, but you are expecting the numbers to sort as numbers, not text. Assuming the D is a constant prefix, and only non numeric character, throughout the column, we can try the following MySQL query:
SELECT *
FROM yourTable
ORDER BY CAST(SUBSTRING(Deposit_number, 2) AS SIGNED) DESC;
Does this query look ugly? Yes, it does, but then again the way you are storing your deposit numbers is also ugly. If you don't want to have to use the above query, then consider storing the deposit numbers in a separate column.

Related

How to get a random row from database in more optimized way

Hi i know how to get random row but i need an optimized way, i am migrating huge data from one schema to another in oracle.Other than performing count validation on each table. I am doing random record validation (for any random row , i am checking whether all column values match or not between the databases).I am using
SELECT * FROM (SELECT * FROM ADM_USER ORDER BY dbms_random.value) WHERE rownum = 1;
Before that i was using:
select * from ADM_USER where ADM_USER_ID=(select Round(dbms_random.value(1,max(ADM_USER_ID))) from ADM_USER)
The problem with the latter one is that values in ADM_USER_ID are not contiguous.So most of the times the query returns empty result set. The first one is good but for tables with huge cardinality it takes 6 to 7 seconnds.
Thanks in advance.
For Oracle, look at the SAMPLE clause. The following will look at random 1% of a table
select * from MDSYS.SDO_COORD_REF_SYS sample(1);
You can still add the rownum=1 filter on top of that.

How To Order A Hierarchical Table By Two Parameters In MySQL?

I have a table with some hierarchical data in it. I've handled ordering it in a hierarchical way via calculating the path with a trigger but now I wanna sort them with another parameter too.
Take a look at these pictures:
This Is The Table With Hierarchical Data Ordered By Path:
I Expect These Two Rows To Swap Because Row ID = 4 Has A Date Before Row ID = 2:
So How Can I Order Each Level By Date Column?
NOTE:
The ID Is A Random Number Generated By A TRIGGER.
You can use FIND_IN_SET so as to extract the hierarchy level of each row. Then use this in the ORDER BY clause:
SELECT ID, Name, ParentId, Date, Path
FROM mytable
ORDER BY FIND_IN_SET(ID, REPLACE(Path, '.', ',')), Date
Note: We have to use REPLACE function to replace '.' characters with ','
so that FIND_IN_SET works as expected.
Demo here
Alternatively you can modify your trigger so as to generate an additional 'level' field and use this field in the ORDER BY clause of your query.
I think you have to add the date in the path-column on each level, since you cannot simply order by date.
So the path-column should look something like this:
0.date-2015-12-09 22:15:12.parent1.date-2015-12-09 22:15:14.parent4
0.date-2015-12-09 22:15:12.parent1.date-2015-12-09 22:15:17.parent2
In that case, date superceeds the parent-level.
In this situation 1.4 would appear before 1.2, because 1.4 happend before 1.2
The path-column does get a little lengthy, but its the only way you can incorporate your own hierachy, I think
Did I get it this time? :-)
Hope I could help :-)

Selecting column name for frequency count in sqlite

I have a table in sqlite that contains roughly about 3 billion values (a lot of them will be repeats). It's basically a giant vector of values. I'm trying to calculate the frequency in which values appear in the table by performing this:-
SELECT abs(diffs), count(*) as total FROM mzdiff GROUP by abs(diffs);
abs(diffs) is the name of my column and mzdiff is my table name, but when I try performing the code above it comes up with an error message saying that the column diffs doesn't exist. I know that the naming of my column isn't really ideal for sql, but is there any way I can get around this?
Thanks
The answer to this is not an alias since the column name must be identified before it can be aliased so use the backtick to quote the name and make it a habit to always quote identifiers.
SELECT `abs(diffs)`, count(*) as total FROM `mzdiff` GROUP by `abs(diffs)`;

SUMing Dynamically Created Columns in MySQL

UPDATE: Not sure if what I'm trying to achieve is possible but thanks for all the help - is it appropriate to request this be deleted? I don't want the contributors to lose the upvotes I've given them for their help.
UPDATE: Just to be clear, when I say columns are created 'dynamically' I mean without developer input so they are an unknown. They are still properly defined columns in a standard database table - I just don't know the names of all of them. :-D
I have a table with columns created dynamically (very rarely but I'm trying to make this as robust as possible). I need to output the SUM of these columns, ordered by highest first but obviously also need the column names in the first row (as otherwise the data is useless). I've retrieved the columns using the information_schema.columns method in to PHP and thought I'd iterate through the columns performing a SUM but if I do that, they are not ordered numerically.
This can be built in to an SP (I'm assuming it will have to be done in an SP due to complexity). I believe I probably need to involve 'PIVOT' somewhere but that is the limit of my knowledge!
So to SUMmarise (see what I did there :-D )
I have a table definition with columns like this:
volunteerID INT
yearAdded DATETIME
willySize111to120 INT
willySize121to130 INT
willySize131to140 INT
willySize141to150 INT
I'd like to return a dataset like this in a query where I can specify the year:
sizeBracket count
willySize111to120 98
willySize121to130 76
willySize131to140 54
willySize141to150 23
Every time I think I've figured out a way to do it, I hit another wall.
Thanks for any help or pointers!
Bob
Assuming that your original table has a 1 in the correct bracket for each volunteer and a 0 in all other brackets:
SELECT bracket.sizeBracket, COUNT(*) count
FROM (
SELECT CASE
WHEN willySize111to120 THEN 'willySize111to120'
WHEN willySize121to130 THEN 'willySize121to130'
WHEN willySize131to140 THEN 'willySize131to140'
WHEN willySize141to150 THEN 'willySize141to150'
END CASE sizeBracket
FROM ... -- < Table Name
WHERE ... -- < Date Selection Logic
) bracket
GROUP BY sizeBracket
ORDER BY count DESC
UPDATE
Based on a raw data table willySize with columns
volunteerID INT
yearAdded DATETIME
willySize INT
You could run the following query
SELECT
CONCAT(
'willySize',
ROUND(willySize-6,-1)+1,
'to',
ROUND(willySize+4,-1)
) sizeBracket,
COUNT(*) count
FROM willySize
GROUP BY sizeBracket
ORDER BY count DESC

SQL query to check if any cell in a column matches with a set of given strings.

I have this data in a column
Steffi | ND Baumecker | Cassy
I would like to do a query to find if any of the above exist in another column
example of other column (Artist being column name)
Artist
Steffi
Derrick Carter
Ben Klock
Craig Richards
I don't think a LIKE will work here so wondering what query I can use to return the artist name from column 'Artist' when a match is made - so in the above example 'Steffi' would be returned.
Would I also need to remove the spaces before and after the | in the first column?
Thanks!
If I understand properly your problem: you want to filter rows using values from a column and searching for these values in another column?
SELECT a.first_name, a.last_name, a.nickname
FROM artist AS a
WHERE a.related_nickname IN (
SELECT sa.nickname
FROM artist AS sa
WHERE sa.popularity > 30
)
MySQL documentation: http://dev.mysql.com/doc/refman/5.1/en/any-in-some-subqueries.html
It seems that you are trying to achieve a complicated task and I'd advise you to try a couple of things.
Subqueries are useful but makes your queries much slower so using two queries might speed things up. The first query would pick the values that will be used for filtering and the second query will search rows.
If you filter by string, consider using indexes on your table: http://dev.mysql.com/doc/refman/5.1/en/create-index.html