How to select the closest matching row of values from a table using c#? - sql

Suppose I have a table(3 rows x 4 columns).
Consider a database table where the first three rows are inputs. If the fourth row is added as a input to the table, the algorithm should match the incoming "A" input with previous all "A" inputs in the table and find the nearest match and it should do the same for B, C and D also. And finally it should select the appropriate row which has a closest match with the input and it should be selected. In this case the first row has closest match with the incoming fourth row and hence it should be selected.
P.S : SQL can also be used to achieve this.

Something on the line of:
select *, abs(A-newA) from table group by A
having abs(A-newA)=min(abs(A-newA))
;
// etc..
where newA, ... are to be substituted (prepared statement with value binding) with the new entry coordinates
select * from table where (abs(A-newA)+abs(B-newB)+...)=
(select min((abs(A-newA)+abs(B-newB)+...) from table)
;

Related

Get table with non table column(Created by own names) names through SQL Query

I have two tables Names as like TableHead and Tableasist
From these above two tables I want to get one table result like below:
I explain result table column conditions in below
Conditions:
Get all records where MICR04 value 122 and above this is first condition
Select * From TableHead where MICR04 >= '122'
Date time: this column should be get as it is no condition
Parameter Name: I have to change column name as parameter and all values fill like MICR04(header of first table of 2nd header)
Set Value: this value get from Tableasist (2nd Table) of Set Temp value 120.. so should be filled 120 in all rows like below
Process Value: process value means MICR04 column value where we get all rows
HighLimt: This value fixed like 150 (Put as it is 150)
Deviation: devation = (High Limit – Process Value)
Event: we can take Event value from first table as usual
These result table unknown columns can add after Event in first table so that I will append these table to gridview. I will hide remaining columns.
You can cross join two tables as follows:
Select th.datetime,
'MICR04' as parameter,
120 as set_value,
TH.MICR04 as process_value,
150 as high_limit,
150 - th.micr04 as deviation
Th.event
From TableHead th cross join tableasist ta
where th.MICR04 >= '122'

Is there a function to break ID Field into Octets and check field values based on Octets(MS Access)?

I am trying to breakdown the ID field (eg. AAABBB123 as AAA BBB and 123) and compare with another field in the Table to check if it has the desired value. (if first octet is AAA, then the other field should have value1, if the first octet is ZZZ then the other field should have value2) If the values don't match I am trying to return those records.
This is what I have tried so far.
SELECT LEFT([SUB_ID], 3) AS TrimmedColumn
FROM Table1
I am able to get the First Octet using this but I don't know how to compare with another field and get the record that don't match with the desired value.
DETAILED PROBLEM DESC:
Let's say first octet is AAA, then the field "A" should have value 1, if it doesn't have the value 1 I need to return those records and so on.
Similarly, if suppose the first octet is ZZZ, then the field "A" should have value2, if it doesn't I need to return those records and so on.
I would also like to do the same with the second octet and a different field "B" to check if it has the desired value based on the Octet.
This is an example table
Am I heading in the right direction? Is there a function to do it? How should I optimize my solution to this problem?
Build query that extracts desired part and join to mapping table. If data field does not match value in mapping table, return record. I expect you will need to do 3 queries. Here is one example:
SELECT Map.*, T1.* FROM Map INNER JOIN (
SELECT Table1.ID, Table1.A, Left([SUB_ID],3) AS T FROM Table1) AS T1
ON T1.T = Map.Oct
WHERE T1.A <> Map.V;
Use Mid() function to parse the other parts.

Getting rows for values greater by 3 numbers or letters

Am trying to come up with a query where I can return back values where the the distance between the letters could be one or more than one for the chosen letter.
For example:
I have two columns which have letters in Column A and in Column B. I want to return back with rows when column B distance is more than Column A by one or more letters.
It's not clear to me, when you say "greater" if you mean that the distance between any two letters is 2 or 3 (Column B can be alphabetically before or after Column A, by a distance of 2 or 3).. Or if Column B has to be alphabetically after Column A, by a distance of 2 or 3
Because I'm not certain what you're talking about, I present two options. Read the "if" rule and choose the one that applies to your situation, then use the query under it:
If columnA is D and columnB can be any of: A B F G
SELECT * FROM table WHERE ABS(ASCII(columna) - ASCII(columnb)) IN (2,3)
If columnA is D and columnB can be any of: F G
SELECT * FROM table WHERE ASCII(columnb) - ASCII(columna) IN (2,3)
Edit1: Per your later comment, you are now saying that the distance is not just 2 or 3 letters (the first line of your question states "2 or 3") but any number of letters distance equal to or greater than 2:
SELECT * FROM table WHERE ASCII(columnb) - ASCII(columna) >= 2
Overall the technique isn't much different to the above queries and there are many ways to specify what you want:
SELECT * FROM table
WHERE
ASCII(columnb) - ASCII(columna)
BETWEEN <some_number_here> AND <other_number_here>
Ultimately the most important thing is to note the use of ASCII function, which gives us the ascii char code of the first letter in a string:
ASCII('ABCD') => 65
And we can use maths on this to work out if a letter distance from 'A' is more than 1 etc..
Probably also worth noting that ASCII() works on single byte ascii characters. If your data is multibyte (Unicode), you might need to use ORD() instead:
Edit2: Your latest edit to the question revises the limit to "B greater than A by one or more" which is equivalent to >= 1 ..
The question seems not to have a clear spec, please treat the answer as a guide for the general technique:
--for an open ended distance, ascii chars
SELECT * FROM table WHERE ASCII(columnb) - ASCII(columna) >= <some_distance>
--for an open ended distance, unicode
SELECT * FROM table
WHERE ORD(columnb) - ORD(columna) >= <some_distance>
--for a definite range of distances (replace … appropriately)
SELECT * FROM table
WHERE ... BETWEEN <some_distance> AND <some_other_distance>
this will work indeed:
select * from table_name where ascii(col_1)+2=ascii(col_2);
You can use something like this if you need it to be exactly 2 or 3 letters greater
select Column A, ColumnB from table name where ASCII(ColumnB) - ASCII(ColumnA) in (2,3)
If you want all those rows where the the difference is equal more than 2, then use this
select Column A, ColumnB from table name where ASCII(ColumnB) - ASCII(ColumnA) >=2
this is where you can make ascii in action..
select * from SampleTable where (ASCII(sampleTable.ColumnB) - ASCII(ColumnA)) >= 2;

DAX - selecting rows with partial match

I have a powerpivot table that contains 2 columns:
Column 1 contains strings.
Column 2 contains comma delimited strings.
I would like to be able to display all the rows from column 1 when rows from column 2 contains the selection from a filter or slicer. For example:
String Values
ABCD A,A,B
EFGH A,C
if A is selected I would display both rows, if B is selected I would display only row 1...etc.
I know I can split the records - but this is not practical for me - the above is only the top of the iceberg. VBA is out of the question since this will published in SharePoint. Anybody has an idea on how I could do that ? Thanks.
I found the solution in a blog from Javier Guillem:
http://javierguillen.wordpress.com/2012/02/10/simulating-an-approximate-match-vlookup-in-powerpivot/
If in my example the name of the table is "facts", I create a second unlinked table called dimRef that I populate with all possible values that I am interested to match: A,B,C,D...etc.
Then I define the measure M as:
M:=If ( Hasonevalue(facts[Values] ),
Calculate (
LASTNONBLANK (dimRef[String], 1 ),
Filter ( dimRef, SEARCH(dimRef[String],Values(facts[String]),1,0) > 0 )
)
)
I can then use the string column of the facts table and the measure in a pivot table and use dimRef as a selector. If filters the row as per the selection.
One small detail: the measure is not available in PowerView...Anybody knows why ?

SQL Server SQL Select: How do I select rows where sum of a column is within a specified multiple?

I have a process that needs to select rows from a Table (queued items) each row has a quantity column and I need to select rows where the quantities add to a specific multiple. The mulitple is the order of between around 4, 8, 10 (but could in theory be any multiple. (odd or even)
Any suggestions on how to select rows where the sum of a field is of a specified multiple?
My first thought would be to use some kind of MOD function which I believe in SQL server is the % sign. So the criteria would be something like this
WHERE MyField % 4 = 0 OR MyField % 8 = 0
It might not be that fast so another way might be to make a temp table containing say 100 values of the X times table (where X is the multiple you are looking for) and join on that