I have been working on this on for months. I just cannot get the natural (True alpha-numeric) results. I am shocked that I cannot get them as I have been able to in RPG since 1992 with EBCDIC.
I am looking for any solution in SQL, VBS or simple excel or access. Here is the data I have:
299-8,
3410L-87,
3410L-88,
420-A20,
420-A21,
420A-40,
4357-3,
AN3H10A,
K117GM-8,
K129-1,
K129-15,
K271B-200L,
K271B-38L,
K271D-200EL,
KD1051,
KD1062,
KD1092,
KD1108,
KD1108,
M8000-3,
MS24665-1,
SK271B-200L,
SAYA4008
The order I am looking for is the true alpha-numeric order as below:
AN3H10A,
KD1051,
KD1062,
KD1092,
KD1108,
KD1108,
K117GM-8,
K129-1,
K129-15,
MS24665-1,
M8000-3,
SAYA4008,
SK271B-200L
The inventory is 7800 records so I have had some problems with processing power as well.
Any help would be appreciated.
Jeff
In native Excel, you can add multiple sorting columns to return the ASCII code for each character, but if the character is a number, then add a large number to the code (e.g 1000).
Then sort on each of the helper columns, including the first column in the table, but not in the sort.
The formula:
=IFERROR(CODE(MID($A1,COLUMNS($A:A),1))+AND(CODE(MID($A1,COLUMNS($A:A),1))>=48,CODE(MID($A1,COLUMNS($A:A),1))<=57)*1000,"")
The Sort dialog:
The results:
You can implement a similar algorithm using VBA, and probably SQL also. I dunno about VBS or Access.
You could try using format for left padding the string in order by
select column
from my_table
order by Format(column, "0000000000")
Add a sorting column:
, iif (left(fieldname, 1) between '0' and '9', 1, 0) sortField
etc
order by sortField, FieldName
Lets say you have your data in column "A". If you put this formula in column "B" =IFERROR(IF(LEFT(A1,1)+1>0,"ZZZZZZZ "&A1,A1),A1), it will automatically add Z in front of all numerical values, so that they will naturally appear after all alphabetical values when you sort A-Z. later you can find&replace that funny ZZZZZZ string...
There a number of approaches, but likely the least amount of work is to build two columns that split out the delimiter (-) in this case.
You then “pad” the results (spaces, or 0) right justified, and then sort on the two columns.
So in the query builder we have this:
SELECT Field1,
Format(
Mid(field1,1,IIf(InStr(field1,"-")=0,50,InStr(field1,"-")-1)),
">##########") AS Expr1,
Format(
Mid(field1,IIf(InStr(field1,"-")=0,99,InStr(field1,"-")+1)),
">##########") AS Expr2
FROM Data
When we run the above raw query we get this:
So now in the query builder, simply sort on the first derived column, and then sort on the 2nd derived column.
Eg this:
Run the query, and we get this result:
Edit:
Looking at you desired results, it looks like above sort is wrong. We have to RIGHT just and pad with 0’s.
So this 2nd try:
SELECT Field1,
Left(Mid(field1,1,IIf(InStr(field1,"-")=0,30,InStr(field1,"-")-1))
& String(30,"0"),30) AS Expr1,
Left(Mid(field1,IIf(InStr(field1,"-")=0,99,InStr(field1,"-")+1))
& String(30,"0"),30) AS Expr2
FROM Data
The results are thus this:
Given your small table size, then the above query should perform quite well.
I have a Pentaho CDE project in development and i wanted to display a chart wich depends on several parameters (like month, year, precise date, country, etc). But when i want to "add" another parameter to my query, it doesn't work anymore... So i'm sure i'm doing something wrong but what ? Please take a look for the parameter month for example :
Select_months_query : (this is for my checkbox)
SELECT
"All" AS MONTH(TransactionDate)
UNION
SELECT DISTINCT MONTH(TransactionDate) FROM order ORDER BY MONTH(TransactionDate);
Select_barchart_query : (this is for my chart, don't mind the other tables)
SELECT pginit.Family, SUM(order.AmountEUR) AS SALES
FROM pginit INNER JOIN statg ON pginit.PG = statg.PGInit INNER JOIN order ON statg.StatGroup = order.StatGroup
WHERE (MONTH(order.TransactionDate) IN (${month}) OR "All" IN (${month}) OR ${month} IS NULL) AND
/*/* Apply the same pattern for another parameter (like year for example) *\*\
GROUP BY pginit.Family
ORDER BY SALES;
(Here, ${month} is a parameter in CDE)
Any ideas on how to do it ?
I read something there that said to use CASE clauses... But how ?
http://forums.pentaho.com/showthread.php?136969-Parametrized-SQL-clause-in-CDE&highlight=dynamic
Thank you for your help !
Try simplifying that query until it runs and returns something and work from there.
Here are some things I would look into as possible causes:
I think you need single quotes around ${parameter} expressions if they're strings;
"All" should probably be 'All' (single quotes instead of double quotes);
Avoid multi-line comments. I don't think you can have multi-line comments in CDE SQL queries, although -- for single line comments usually works.
Be careful with multi-valued parameters; they are passed as arrays, which CDA will convert into comma separated lists. Try with a single valued parameter, using = instead of IN.
Heres the query which is giving the issue
select * from
(select user,logdate,[in time],[out time],[worked time] from tmp_phys_table) as sr1
PIVOT
(SUM([worked time]) FOR [LOGDATE] IN ([1])) AS TMPQ
I am unable to understand what the issue is. I was doing pivot as I wanted to make the rows into columns.
I want the output in the below format. The image is the one which I had created in excel for the visualization of data for my report purposes,the same I want in sql,but I am not getting the idea for how to go about the dates hence the query which I had written and which is giving the above error
No column was specified for column 1 of 'sr1' for pivot function
I think your issue is the use of a reserved word in you field name; User is a reserved word, you will need to alias it though I would highly recommend changing the field name.
SELECT *
from (SELECT user As Usr,logdate,[in time],[out time],[worked time]
from tmp_phys_table) as sr1
PIVOT (SUM([worked time]) FOR [LOGDATE] IN (1)) AS TMPQ
NOTE: I have not tested this, I am just going off what stands out as the issue
I would also strongly suggest changing some of your other columns so they do not have spaces in, as you have correctly typed they require square brackets, which will become frustrating over time.
My current SQl statement is:
SELECT distinct [Position] FROM [Drive List] ORDER BY [Position]ASC
And the output is ordered as seen below:
1_A_0_0_0_0_0
1_A_0_0_0_0_1
1_A_0_0_0_0_10
1_A_0_0_0_0_11
1_A_0_0_0_0_12
1_A_0_0_0_0_13 - 1_A_0_0_0_0_24, and then 0_2-0_9
The field type is Text in a Microsoft Access Database. Why is the order jumbled and is there any way of correctly sorting the values?
"Why the order is jumbled":
The order is only jumbled because you are compiling it with your human brain and are applying more value than the computer does because of your symbolic understand of what the values represent. Parse the output as though you could only understand it as an array of character strings, and you were trying to determine which string is the greatest, all the while knowing nothing about the symbolic value of each character. You will find that the output your query generated is perfectly logical and not at all jumbled.
"Any way of correctly sorting the values"
This is a design issue and it should be addressed if it really is a problem.
Change 1_A_0_0_0_0_0 to 1_A_0_0_0_0_00
Change 1_A_0_0_0_0_1 to 1_A_0_0_0_0_01
Change 1_A_0_0_0_0_2 to 1_A_0_0_0_0_02
etc
This will make the problem go away.
Use these two separate queries:
SELECT distinct [Position] FROM [Drive List] WHERE [Position] LIKE '1_A_0_0_0_0_?' ORDER BY [Position] ASC
SELECT distinct [Position] FROM [Drive List] WHERE [Position] LIKE '1_A_0_0_0_0_??' ORDER BY [Position] ASC
...add to a temp table and append to get the results to display properly.
If you want sorting which incorporates the numerical values of those substrings, you can cast them to numbers.
In the simplest case, you're concerned with only the digit(s) after the 12th character. That case would be fairly easy.
SELECT
sub.Position,
Left(sub.Position, 12) AS sort_1,
Val(Mid(sub.Position, 13)) AS sort_2
FROM
(
SELECT DISTINCT [Position] FROM [Drive List]
) AS sub
ORDER BY 2, 3;
Or if you want to display only the Position field, you could do it this way ...
SELECT
sub.Position
FROM
(
SELECT DISTINCT [Position] FROM [Drive List]
) AS sub
ORDER BY
Left(sub.Position, 12),
Val(Mid(sub.Position, 13));
However, your actual situation could be much more challenging ... perhaps the initial substring (everything up to and including the final _ character) is not consistently 12 characters long, and/or includes digits which you also want sorted numerically. You could then use a mix of InStr(), Mid(),and Val() expressions to parse out the values to sort. But that task could get scary bad real fast! It could be less effort to alter the stored values so they sort correctly in character order as #Justin suggested.
Your jumbled order is caused because it is a text field. As for solutions you could attempt to add an additional column in your table that is numeric and order by that instead of Position. I would need more information about what data you have and what it means to suggest a good way to do this.
This is the right sort for a string.
In alphabetical order 10 come before 2.
For orderby the last number you can try with SUBSTRING and CAST (or CONVERT) commands
I need to run a query on a massive (650 MB) data table in MS Access 2010. All the data types for the fields are text, but some fields contain all numbers, even though the data type is text. I therefore have to convert from text to long int in order to select, for example, all the records who have a value greater than 10 in a specified field. But whenever I do this, I get a type mismatch error, presumably due to the comparison of a numerical value of a text field. Here is the SQL for the query which should select from fields that are all of the text data type.
SELECT [Organization legal name], [Number of Group Practice members], City, State
FROM massivetable
WHERE Clng([Number of Group Practice members])>10
AND State='CT';
I have tried to convert the datatype of the field to long integer in design view, but the operation fails with a message saying there is not enough memory to perform the operation.
Can anyone show me how to fix the code above so that it selects all the records while values greater than 10, without throwing a type conversion error?
Use IsNumeric function:
SELECT [Organization legal name], [Number of Group Practice members], City, State
FROM massivetable
WHERE Iif(IsNumeric([Number of Group Practice members]), Clng([Number of Group Practice members]), 0) >10
AND State='CT';