I am struggling to come up with a query for sqlite table to UPDATE OR INSERT at rowid. The problem I have is that I have a database where it relies on the rowid but when populating the table there may not be enough rows. So the table may look like this:
rowID data1 data2 data3
------ ------ ----- ------
0 18 1543
1 5
2 35 918
And for my query I want to be able to insert data2 = 453 at rowID = 16 for example. Also just to specify I do not have a column of rowID.
I am thinking that the only way possible would be to insert empty rows from rowID 3 -> 15 before inserting the 16th row. It does not matter for me if rows are empty as they will eventually be populated. I will not be working with more than 50 rows or more than 8 columns so it is a reasonably small table. Anyone know a way forward for me to work?
Also additional thing to note, I am using the query on Titanium, so the programming language is JavaScript.
The documentation says:
The rowid value can be accessed using one of the special case-independent names "rowid", "oid", or "rowid" in place of a column name.
[...]
An INSERT statement may provide a value to use as the rowid for each row inserted.
So just specify the values you want:
INSERT INTO MyTable(rowid, data2) VALUES(16, 453)
Related
I'm implementing an app script that automatically inserts a new record to a spreadsheet containing multiple records. Currently, the target sheet is formatted as follows - with each record/name referring to several different data points:
Column A
Column B
Column C
Record Name
data 1
data 1
data 2
data 2
Record Name
data 1
data 1
data 2
data 2
data 3
data 3
data 4
data 4
Record Name
data 1
data 1
Record Name
data 1
data 1
...
...
...
Ultimately each new data point should be added under it's related record name if it exists or create a new record name otherwise. What I'm struggling to figure out is how to sort column A while maintaining the order of columns B and C relative to A since the number of data points isn't consistent and the record name is only in the same row as the first data point.
Things I've tried:
Counting the number of empty cells under each record name and moving the data as necessary. The biggest issue here is performance since this method requires multiple loops through the data.
Presorting the data (by hand, unfortunately). This works, but isn't very robust as multiple users can edit the sheet directly and makes adding new records more difficult.
I'm relatively new to Apps Script, so I feel like there's some library or function I'm missing. (It's also difficult to search for this problem, so I apologize if this is a duplicate)
You can try creating a pivot table based on your current table. You will need to fill Column A with data first. It has built in function for sorting your data. Unfortunately i don't think that it can expand its range dynamically. So you either have to give it a bigger range from the get go, or you can right a script that will delete old pivot table and create a new one after you finished adding new records into your table.
Here is a function that creates pivot table:
function createPivotTable() {
var app = SpreadsheetApp;
var ss = app.getSpreadSheet();
var s = ss.getActiveSheet();//Sheet with your source data
var pivotRange = sheet.getRange(2,1,(sheet.getLastRow()-1),3);
var pivotSheet = ss.insertSheet("Pivot Sheet");//Sheet with your formated data
const pivotTable = pivotSheet.getRange("A1").createPivotTable(pivotRange);
pivotTable.addRowGroup(1).showTotals(false);
pivotTable.addRowGroup(2).showTotals(false);
pivotTable.addRowGroup(3).showTotals(false);
}
If you want to create one manually you click Insert => Pivot Table and then choose which columns you want
I have a table which was the following:
ID Pttributes Pvalue
1 Name Jason
2 Age 14
3 Address 12 Test Road
A linq query has been produced which selects each individual value by their ID
from p in db.people
where p.ptribbutes.contain(string) || p.value.contains(string)
select p.tribbutes , p.value ,
Now this is where it gets tricky , when it is outputted towards JSON in MVC View , it outputted as separate rows in individual div elements (which has been incremented) where the code is written in javascript as
item.ptribbutes + item.pvalue
Output value
Div 1
-------
Name - Jason
Div 2
--------
Age - 14
Div 3
---------
Address - 12 Test Road
How do I get it so one div can contain all these rows rather than being in separate div elements? (or possibly recognize the name , age and address as columns)?
1) Understand what is getting returned as JSON data. It is an array of objects.
2) How do you want it to display the JSON data in UI? I understand that as an single div for each array element. So what do you need to do is to loop thru the JSON data and either bind/emit the values in UI element inside (div) whichever way you prefer to do it. Take a look at the answer for the question at loop through JSON result with jQuery
Hope this helps. Happy Coding!
I've searched into the documentation of data tables of how one can add data to the table here
https://datatables.net/examples/data_sources/index.html but no where I could find a way to insert data into a single cell for a given column. For example, I need sth like:
"columns": [ {target (0), "value which will be inserted"} ]
There is a way to inser data from an array but each section of the array contains values for the whole row ( see here https://datatables.net/examples/data_sources/js_array.html) but I need to insert data into different cells in respect with the columns since I don't know the column labels initially. This is because the data will be in json format and first I need to extract the unique dates for each json object. These will be my column headers. And then for each of the objects based on its date I need to put it into the relevant date column. So the logic should be sth like:
if this date column (from the table) == json object date then put it there
Thanks
It seems like after a lot of researching this feature is not available in DataTables. So that's why I've solved the problem with jQuery. Using the code below I can iterate through a column and insert values in it.
//change 1 to the column at hand
$("#table tr > :nth-child(1)").each(function(index) {
//skip the column header
if (index > 0){
//insert value
$(this).html("<span class='fixed-size-square'> value to be inserted");
}
});
I have a project that consist of a function that generate a table based on some filter. Then, I found a problem that when some combination of some filter are return nothing so, when it rendered in browser as a table is less than number of combination.
To achieve it, I want to give "-" mark to the loss record that not saved in the table.
My first approach is check one-by-one the array with the filter and if the condition is match to 'loss rows', it will push array untill all missing record in result is filled with '-'. But, it'll dealing with time because, the last must be sorted again and then to be constructed into the wanted table.
Sample schema of main table "A":
amount_of_product, id_product, id_people, id_place.
The primary key columns are:
id_product, id_people, id_place.
The content of the main table is:
23456, book-a, 1, aa
5678, book-b, 1, cc
2587, book-b, 1, aa
The source query will be something like:
select * from A
where
id_product in ('book-a', 'book-b')
and id_people in ('1') and id_place in ('aa', 'bb', 'cc')
order by id_product, id_place
Then, we want to show in a table view (html) all combinations of all selected filters. 6 rows should be rendered, with empty cells shown as "-".
Sample result table:
Can I do it efficiently or are there any solution to this?
Thanks in advance.
Thanks for some advice above.
I've try to use crosstab function in postgresql to produce the pivot table but I can't produce for more complex table.
So, I use another approach to retrieve all datas based on combination row-coloumn gradually (example query from id_place 'aa', id_people '1', id-product 'aa' etc) and contruct each table head row and coloumn then contruct the data.
Regard
(This project is all written in JS)
So, I have a table called paid_offers, and two columns called offer_id and entity_id
What I'm trying to do is get a count of all the offer_id values associated with a single entity_id.
EDIT: Example: entity_id A has 35 offers available (so, there are 35 rows with unique offer_ids, but all the same entity_id)
So, getting the count is no problem with arrayList.length, but I just need an approach to getting the actual array. Thanks for the help!
EDIT: As per request, more information! I'm going to be using the output as an array.
So, this project is written using Titanium (From appcelerator). I don't need anything besides the query. So, It'd be what's inside the quotes here, for those who don't know.
var offersList = db.execute("SELECT entity_id FROM paid_offers");
Now, the goal is not to just get the list of ID's but instead the list of offer_id values associated with each unique entity_id value. I would think it would look close to:
var offersList = db.execute("SELECT offer_id FROM paid_offers WHERE entity_id = entity_id
Except that wouldn't work, but that's my train of thought while looking through this.
If you're trying to count the offers for each entity, it's
SELECT entity_id, COUNT(offer_id) As Count
FROM paid_offers
GROUP BY entity_id
This will only find entities with at least one offer.
I think what you're after is this...
SELECT offer_id, COUNT(*) As Count
FROM paid_offers
GROUP BY offer_id
Which would give something like.
offer_id Count
1 1
2 4
3 2
...
Note that it will exclude any offer_id that isn't present in the table so there will never be a count of 0
One other note on using the Array.length() to do a count - it returns a (potentially large) recordset to JS which then counts it. This uses a lot of resources for not a lot of benefit - better to do the COUNT in SQL (as shown above) and just get the result.
This isn't simple or possible with pure SQL since each column in the result can always only be a single value but you need a list. Try this approach instead:
select entity_id, offer_id
from paid_offers
In JavaScript, read the results and create an array for each entity_id and append the offer_ids to that array. After processing all the results of the query, you will have the structure that you want. Pseudocode:
data = {}
for (entity_id, offer_id in rows) {
var a = data[entity_id]
if (a === undefined) {
a = []
data[entity_id] = a
}
a.push(offer_id)
}
So how about getting the other column in an array list . Then have an outlet for loop that goes through each offer_id . Then on the inside go through each Entity_id . If more then one entity Id shows up don't delete that offer_id from your final array list .
Or why not just do it with SQL calls ?