Compare data against sql column - sql-server-2008

I have collection of numbers like: 11111, 12345, 12346 stored in a list in c# code. I need to compare this list against sql database column of numbers similar to this and find out if matching numbers exist. Below is what i am doing:
foreach (number in numbers)
{
//get column data through sql reader and iterate through it:
foreach(column in columnData)
{
if(number == column)
{
// do something
}
}
my question is this right approach? Or is there a better way to do this? As it looks like this requires lots of processing.

I would so something like this..
var matches = columnData.Where(z=> numbers.Contains(z=>z.columnData)).ToList();
or
var matches = columnData.Select(z=> z.columnData).Intersect(numbers);

Related

Is there anyway to match name and number?

For each client we have, we associate their name with an ID number in a database. However, we sign people in by name. I am trying to convert the names into their ID number in a spreadsheet.
I have a list of all the names and corresponding IDs. I realize that I could hard code it so that it would look something like:
for (i=0; i < 31; i++) {
if name = 'john doe'
id = 256589
elseif name = 'jane doe'
id = 248352...}
and repeat that for each client. I've tested with a couple names and this solution does work. Since we don't have that many individuals come in it wouldn't be impossible to just repeat it. However, I would like to know if there are any shortcuts available.
It depends where you're doing this lookup.
This looks like script so you could use an object with the names as keys:
function getIdFromName(name) {
// list of all employees and ids
let employees = {
"john doe": 256589,
"jane doe": 248352
}
if (employees[name]) {
return employees[name]
} else {
// this covers the case if name not found
return false
}
}
// in rest of your code
var id = getIdFromName(name)
If you want to do the lookup in the sheet, you can use a lookup table containing names and ids then use VLOOKUP/INDEX(MATCH()) to find the corresponding ID

How should I search through this json structure?

I'm working with PHP, I have a json structure which looks like that :
{
"events": [
{
"timestamp": 1468774519,
"id": 75964,
},
{
"timestamp": 1468771410,
"id": 24891,
},
// etc
I need to fetch 5 events in a row, but starting from one specific id, so my first idea is to loop every event from the beginning and check if the id is the offset i'm looking for, and then when i get it i can loop the next 5 events.
But is there a better way to do so ? It could possibly loop through hundreds of events so maybe there's a better way to get there ? thanks
Since the ids aren't in numeric order, you can't use a binary search, so you need to use a sequential search. Here's an example in JavaScript. Also note this code assumes the id is present and there are at least four more events after it in the array.
var index = 0;
var id = 12345; // for example
var json = {...}; // whatever that object was
while(json.events[index].id!=id) {
index++;
}
// found the one, do something with the next five
for(var i=0; i<5; i++) {
var event = json.events[index+i];
// do something
}
In my opinion, you can only take one loop over the events with a filter event.id >= theId,and then check if the filtered array contains theId. if you get it, you can sort this smaller array and take the 5 events.
First I would make a key:value hash object (a lookup object), where the key would be the id from your structure, and the value would be the reference to the event. As a result, you iterate over the structure only once and then get all the events from the lookup structure, by just accessing them by their keys.
You could sort it as well(ideally, you could get it already sorted by id from your source of data) and then use a binary search algorithm.

How sort json array multiple times in swift

I am trying to sort an array by a few values, not just one but three I can sort it like this
mysocialArray.sort({$0["date"] > $1["date"]})
This works fine, but i can find any where how to then sort it again so i get them sorted on time and another sort. SO should sort three times. When i just add another sort it is not taking the date into account.
The sorted json output will then be loaded in arrays as strings to display on the viewcontroller
How should I solve this?
thanks
Given your requirements of "This way the latest post that not yet have been read are at the top", looks like you are looking to sort by multiple criteria. I typed this in the dark so you may have to adjust it to fit your case:
mySocialArray.sort {
// First sort by read status. Assuming read is of type Bool
let (read1, read2) = ($0["read"] ? 1 : 0, $1["read"] ? 1 : 0)
if read1 != read2 {
return read1 > read2
}
// Then short by date. I assume date is of type NSDate
let (date1, date2) = ($0["date"], $1["date"])
if date1.timeIntervalSince1970 != date2.timeIntervalSince1970 {
return date1 > date2
}
// And lastly sort by time
return $0["time"] > $1["time"]
}

Append value to JSON decode array parameter stored in MySQL

Im trying to work out how to append a zero to a specific JSON decoded array value for multiple records stored in a MySQL table according to some conditions.
for example, for table 'menu', column 'params'(text) have records containing JSON decoded arrays of this format:
{"categories":["190"],"singleCatOrdering":"","menu-anchor_title":""}
and column 'id' has a numeric value of 90.
my goal is to add a zero to 'categories' value in menu.params whenever (for example) menu.id is under 100.
for this records the result being
{"categories":["1900"],"singleCatOrdering":"","menu-anchor_title":""}
so im looking for a SQL Query that will search and find the occurrences of "categories": ["999"] in the Database and update the record by adding a zero to the end of the value.
this answer is partially helpful by offering to use mysql-udf-regexp but its referring to REPLACE a value and not UPDATE it.
perhaps the REGEXP_REPLACE? function will do the trick. i have never used this library and am not familiar with it, perhaps there is an easier way to achieve what i need ?
Thanks
If I understand your question correctly, you want code that does something like this:
var data = {
"menu": {
"id": 90,
"params": {
"categories": ["190"],
"singleCatOrdering": "",
"menu-anchor_title": ""
}
}
};
var keys = Object.keys(data);
var columns;
for (var ii = 0, key; key = keys[ii]; ii++) {
value = data[key];
if (value.id < 100) {
value.params.categories[0] += "0";
alert(value.params.categories[0]);
}
}
jsFiddle
However, I am not using a regular expression at all. Perhaps if you reword the question, the necessity of a regex will become clearer.

Couchbase Multiple Keys

I presume a simple question. I have the following data.
I want to search for all rows where the ID is > 2 but < 8 and the Price is > 30
I have used various versions of: startkey=["2", null] or even something like startkey=["2", "30"] just for testing.
It only ever seems to run both conditions on the first row. So if I do: startkey=["2", "30"] then I get back:
{"id":"3","key":["3","30"],"value":null},
{"id":"4","key":["4","30"],"value":null},
{"id":"5","key":["5","20"],"value":null},
{"id":"6","key":["6","60"],"value":null},
{"id":"8","key":["8","60"],"value":null}
Why is row 5 there?
I am starting to get the view that I need to handle this in the code (.net) and make multiple calls somehow... I can't seem to find anything on this that works....
Note: I have tried doing say a loop with for (i = 0; i < doc.ID.length; i++) and then using doc.ID[i] but it never returns anything....
Currently I just have
function (doc, meta) {
emit([doc.ID, doc.Price ],null);
}
Essentially I want to have a search where there are 5 input keys that a user has. So do I need to make 5 calls and then keep taking data from the previous output as the source for the next???
Other references I have looked at include: the manual
Thanks in advance,
Kindest Regards
Robin
This is a common misconception, with a compound array index key, it's still treated as a string, therefore the index key [2,10] is actually "[2,10]", and the index key [5,20], is actually "[5,20]".
So the reason that startkey=["2", "30"]shows the {"id":"5","key":["5","20"],"value":null}, row is because as a string it is > startkey.
Likewise, the Query startkey=[2,10]&endkey=[5,10] returns
{"total_rows":7,"rows":[
{"id":"2","key":[2,20],"value":null},
{"id":"3","key":[3,30],"value":null},
{"id":"4","key":[4,30],"value":null}
]
}
because startkey="[2,10]" < "[2,20]" && "[4,30]" < "[5,10]"=endkey, but "[5,20]" is not within that string Range.
Range Queries with startkey and endkey
startkey => endkey is a Range query using strcmp(), the group and group level is based on the string, where the comma is separating string tokens.
A Good Reference Link (since Couchbase Views work much like Apache CouchDB Views (inspired by them))
http://wiki.apache.org/couchdb/View_collation#Collation_Specification
Spatial View/Query
To achieve the result you are trying for, you could also write a Spatial View to have multi-dimensional Queries, numeric only. While you might not initially think of it
function (doc, meta) {
emit({
type: "Point",
coordinates: [doc.ID, doc.Price]
}, meta.id);
}
The Query would be a Bounding Box Query:
&bbox=2,0,8,30
{"total_rows":0,"rows":[
{"id":"2","bbox":[2,20,2,20],"geometry":{"type":"Point","coordinates":[2,20]},"value":"2"},
{"id":"3","bbox":[3,30,3,30],"geometry":{"type":"Point","coordinates":[3,30]},"value":"3"},
{"id":"4","bbox":[4,30,4,30],"geometry":{"type":"Point","coordinates":[4,30]},"value":"4"},
{"id":"5","bbox":[5,20,5,20],"geometry":{"type":"Point","coordinates":[5,20]},"value":"5"}
]
}
Another Query:
&bbox=2,30,8,30
{"total_rows":0,"rows":[
{"id":"3","bbox":[3,30,3,30],"geometry":{"type":"Point","coordinates":[3,30]},"value":"3"},
{"id":"4","bbox":[4,30,4,30],"geometry":{"type":"Point","coordinates":[4,30]},"value":"4"}
]
}