How to transfer data from one table into another? - css

Okay, I've 2 tables. One is called users2 and one is called user_totals. ive rows that are called clicks(impression_count), fclicks(use_count), completed_stus(completion_count). the ones in braces are the new ones in the user_totals table. Anyway I'd like to add all the ints from the first table into the second table but only if the stats in the second table are 0. also it should be added to the same user id which is id in the first table and user_id in the second. Could somebody give me a working mysqli statement to perform that ?

You can create a insert with select:
INSERT INTO x
(a, b, c, d)
SELECT a, b, c, d
FROM y
WHERE z=2

Related

Ordering SQLite table by sum of two columns

I have a database that has two integer columns, and I'm trying to find a way to select the top 'x' amount of rows with the highest sums of these two columns. I'm trying to eliminate the need of creating a third column that stores the sum of the two, unless there's a way to to automatically update this column every time one of the other two are altered. I'm using SQLite by the way, as I know there are some slight differences here and there between SQL/SQLite syntax.
Any help is appreciated.
Something like
SELECT a, b
FROM yourtable
ORDER BY a + b DESC
LIMIT :x
should do it.

SQLite: Add up integer tables based on shared indicators

I am aggregating numbers from different sqlite databases into a single output database table.
I need to add up integer columns i1,i2,i3 in the output table based on three indicating columns a,b,c that tell me which rows to update:
ATTACH DATABASE "out.db" AS output;
INSERT INTO output.rows(a,b,c,i1,i2,i3)
SELECT DISTINCT "some_value", b, c, 0, 0, 0 FROM main.rows
ON CONFLICT IGNORE;
#THE FOLLOWING LINES MIGHT SHOW WHAT I MEAN...
UPDATE output.rows SET i1=i1+i1_,i2=i2+i2_, i3=i3+i3_
WHERE a="some_value" AND b=b_ and c=c_
SELECT i1_, i2_, i3_, b_, c_ FROM main.rows;
I do not want to type in all the combinations of a,b,c. As you can see, a does not come from main but from external information (the filename).
In newer versions of SQLite that support UPSERT, the following seems to work:
ATTACH DATABASE "$out.db" AS output;
INSERT INTO output.rows(a,b,c,i1,i2,i3)
SELECT "some_value", b, c, i1, i2, i3 FROM main.rows WHERE true
ON CONFLICT (a,b,c) DO UPDATE SET i1=i1+i1, i2=i2+i2, i3=i3+i3;
In my case, the columns i1,i2,i3 coming from main actually had a different name (say I1,I2,I3) than their counterpart in output, therefore the UPDATE was clearer (i1=i1+I1). I failed to reference as main.rows.i1 inside the UPDATE statement. If you know how to solve that ambiguity, please comment.

In SQLite3 how to group by those having no certain values?

A table named "example" is like this:
enter image description here
I would like to SELECT Name FROM example GROUP BY NAME. But at the same time, if anyone's value contains X, then that one should be excluded from the result. In the "example" table, A has three values and one of them is X so A should be excluded from the result. So does B.
The result produced by SQL clauses should be:
C
D
Could anyone help me write corresponding SQL clauses so that I can get the result I want?
Thank you!!
I tried to write something like this:
SELECT Name FROM example WHERE Value <> X GROUP BY NAME.
It didn't work because A and B still have other values which prevent them from being excluded. I just have no idea of what else I can do. I'm very new in SQL.
A comparison results in either 0 or 1.
So taking the largest result in a group shows whether there is a match in the group:
SELECT Name
FROM Example
GROUP BY Name
HAVING MAX(Value = 'X') = 0;

Inserting and combining data between 2 table

We have a table students with ID,NAME,SURNAME. We want have another table (created) students_2 with ID1,NAME1,SURNAME1.
Starting from table students, i want to fill data in the second table in the following way : I want to have in the second table combinations of names ( example : NAME,SURNAME1; NAME1,SURNAME1). Moreover, i want to generate combination of names.
How can I do that ? I tried something like :
INSERT INTO students_2 (ID1,NAME1,SURNAME1) SELECT ID,NAME,NAME from students;
But it's not correct cause I don't generate combinations, just inserting . A solution is appreciated, but mainly i need ideas.
You could write something like
INSERT INTO students2(NAME, VALUE) FROM
SELECT s1.name, s2.value from students1 s1 cross join students1 s2
This will do Cartesian product , and will get a NxN rows with combinations

Can you only use one Select command w/SqlDataSource

This is a pretty simple question that I haven't been able to find an answer for. Is it possible to have two separate SELECT commands (from the same table) in the same SqlDataSource command to populate two different cells in a given GridView?
I haven't been able to find current information so far.
::EDIT::
The challenge is that I'm attempting to manupulate one cell with a COUNT command and the second cell with a numerical grand total from the same information.
You can Combine results from two separate SELECT Statements by doing something like this..
SELECT X.A , Y.B
FROM (SELECT Column1 AS A FROM TableName) X, (SELECT Column2 AS B FROM TableName) Y

Resources