Filling up an ms-access table with sql - sql

I am very new to databases and I'm currently working with Microsoft Access 2013. The situation is that I have a huge amount of data which I wanna fill in in an already created table (Inventory) by using an SQL-statement in a query.
What I have is the following:
INSERT INTO Inventory (Col 1, Col 2, Col 3, Col 4)
VALUES ("Val 1", "Val 2", "Val 3", "Val 4"),
("Val 5", "Val 6", "Val 7", "Val 8"),
....
("Val 9", "Val 10", "Val 11", "Val 12");
And what I want is simply this table:
Col 1 | Col 2 | Col 3 | Col 4
| | |
Val 1 | Val 2 | Val 3 | Val 4
Val 5 | Val 6 | Val 7 | Val 8
Val 9 | Val 10 | Val 11 | Val 12
The problem is, that I keep getting the error Missing semicolon at the end of sql-statement. Therefore I suppose that I should add a semicolon after each line. If I do this tho, I get the error that access found characters after the semicolon.
What is the right syntax to achieve my multiple-lined INSERT INTO-Statement?

I think MS Access only allows you to insert one record at a time using INSERT . . . VALUES:
INSERT INTO Inventory (Col 1, Col 2, Col 3, Col 4)
VALUES ("Val 1", "Val 2", "Val 3", "Val 4");
INSERT INTO Inventory (Col 1, Col 2, Col 3, Col 4)
VALUES ("Val 5", "Val 6", "Val 7", "Val 8");
....
INSERT INTO Inventory (Col 1, Col 2, Col 3, Col 4)
VALUES ("Val 9", "Val 10", "Val 11", "Val 12");

You can bulk insert using INSERT INTO ... SELECT and a union query:
INSERT INTO Inventory (Col 1, Col 2, Col 3, Col 4)
SELECT "Val 1", "Val 2", "Val 3", "Val 4"
FROM (SELECT First(ID) FROM MSysObjects) dummy
UNION ALL
SELECT "Val 5", "Val 6", "Val 7", "Val 8"
FROM (SELECT First(ID) FROM MSysObjects) dummy
UNION ALL
SELECT "Val 9", "Val 10", "Val 11", "Val 12"
FROM (SELECT First(ID) FROM MSysObjects) dummy
However, the overhead might not make this construct worthwhile, and Access does have a maximum length on single queries of ~65K characters.
To serialize and deserialize tables, I recommend using ADO and persistence. This can properly store field properties, serializing to different file formats or database formats will cause information loss.

Related

Use column value as column name when inserting (Clickhouse)

I have a source table:
Data
122435
2912
32
I want to select data from this table and insert into a destination table. The desired output for the destination table is:
Index_1
Index_2
Index_3
2
4
5
2
9
2
The logic behind this is numbers in odd positions are indexes (columns), and numbers in even positions are values:
1) "122435" -> "12", "24", "35" -> "Index_1 = 2", "Index_2 = 4", "Index_3 = 5"
2) "2912" -> "29", "12" -> "Index_2 = 9", "Index_1 = 2"
3) "32" -> "Index_3 = 2"
My problem is I don't know if it is possible to use column value as column name in Clickhouse.

Column contains column 3

I have a dataframe. I would like to test whether, (C), on each row, the number in column (B) is in the string, column (A).
df = pd.DataFrame({'A': ["me 123", "me-123", "1234", "me 12", "123 me"],
'B': [123, 123, 123, 123, 6]})
I can do that using extract
df['C'] = df.A.str.extract('(\d+)', expand=False).astype(int).eq(df.B,0).astype(int)
A B C
0 me 123 123 1
1 me-123 123 1
2 1234 123 0
3 me 12 123 0
4 123 me 6 0
However, if one of the A values does not contain a number:
df = pd.DataFrame({'A': ["me 123", "me-123", "1234", "me 12", "123 me", "me"],
'B': [123, 123, 123, 123, 6, 123]})
Then I get:
ValueError: cannot convert float NaN to integer
Values NaNs are floats, so you can convert output to floats:
df['C'] = df.A.str.extract('(\d+)', expand=False).astype(float).eq(df.B,0).astype(int)

trim trailing tabs from a string/column in awk

I have a file containing 4 columns separated by tabs. In the last column there can be sometimes trailing tabs between the quotation marks.
It is a similar question to trim leading and trailing spaces from a string in awk. Here is an example:
col1 col2 col3 col4
"12" "d" "5" "this is great"
"13" "d" "6" "this is great<tab>"
"14" "d" "7" "this is great<tab><tab>"
"15" "d" "8" "this is great"
"16" "d" "9" "this is great<tab>"
This is what I come up with so far:
gawk --re-interval -F '"' 'NF = 9 {if ($8 ~ /\t$/) {gsub(/[\t]+$,"",$8)} ;}'
The problem is that it destroys my format meaning I get no quotation marks for each column. The good thing is that the tabs in between the columns are still there:
col1 col2 col3 col4
12 d 5 this is great
13 d 6 this is great
14 d 7 this is great
15 d 8 this is great
16 d 9 this is great
What do I do wrong?
You need to tell awk that the output field separator (OFS) is also a quote. For example:
awk -v OFS='"' -F '"' 'NF == 9 {
if ($8 ~ /\t$/) {
gsub(/[\t]+$/,"",$8)
}
}
1' input.txt
Output:
col1 col2 col3 col4
"12" "d" "5" "this is great"
"13" "d" "6" "this is great"
"14" "d" "7" "this is great"
"15" "d" "8" "this is great"
"16" "d" "9" "this is great"

Weird Table Output When Using Alias and Case Statement on Khan Academy's Challenge

I'm fairly new to SQL and I taught myself to SQL on Khan Academy. I create a database table as follows:
CREATE TABLE Data_Exp (ID INTEGER PRIMARY KEY AUTOINCREMENT, Subject TEXT,
Label TEXT, Gender TEXT, X1 INTEGER, X2 INTEGER,
X3 INTEGER, X4 INTEGER, X5 INTEGER);
INSERT INTO Data_Exp(Subject, Label, Gender, X1, X2, X3, X4, X5) VALUES("S01", "A", "F", 5, 7, 6, 5, 4);
INSERT INTO Data_Exp(Subject, Label, Gender, X1, X2, X3, X4, X5) VALUES("S02", "B", "M", 8, 8, 6, 4, 6);
INSERT INTO Data_Exp(Subject, Label, Gender, X1, X2, X3, X4, X5) VALUES("S03", "A", "M", 6, 1, 4, 3, 3);
INSERT INTO Data_Exp(Subject, Label, Gender, X1, X2, X3, X4, X5) VALUES("S04", "C", "F", 3, 3, 7, 2, 5);
INSERT INTO Data_Exp(Subject, Label, Gender, X1, X2, X3, X4, X5) VALUES("S05", "B", "F", 9, 2, 5, 3, 7);
The output of above code is:
Then I continue by adding this code:
SELECT Subject, Gender, Label, ROUND(0.4*X1 + 0.15*(X2+X3+X4+X5),1) AS Y FROM Data_Exp;
and get the following result (as expected):
Then I use CASE statement to evaluate column Level:
SELECT Subject, Gender, Label, (SELECT CASE WHEN Y > 6 THEN "Level 6"
WHEN Y > 5 THEN "Level 5"
WHEN Y > 4 THEN "Level 4"
WHEN Y > 3 THEN "Level 3"
WHEN Y > 2 THEN "Level 2"
WHEN Y > 1 THEN "Level 1"
END AS "Level" FROM (SELECT ROUND(0.4*X1 + 0.15*(X2+X3+X4+X5),1) AS Y FROM Data_Exp)) AS Level FROM Data_Exp;
but instead of getting this result:
I got this weird output in column Level
Where did I get wrong?
Your query will not work as you are doing some sort of cartesian join
What you are looking is something like the solution below
SELECT Subject,
Gender,
Label,
CASE WHEN ROUND(0.4*X1 + 0.15*(X2+X3+X4+X5),1) > 6 THEN "Level 6"
WHEN ROUND(0.4*X1 + 0.15*(X2+X3+X4+X5),1) > 5 THEN "Level 5"
WHEN ROUND(0.4*X1 + 0.15*(X2+X3+X4+X5),1) > 4 THEN "Level 4"
WHEN ROUND(0.4*X1 + 0.15*(X2+X3+X4+X5),1) > 3 THEN "Level 3"
WHEN ROUND(0.4*X1 + 0.15*(X2+X3+X4+X5),1) > 2 THEN "Level 2"
WHEN ROUND(0.4*X1 + 0.15*(X2+X3+X4+X5),1) > 1 THEN "Level 1"
END AS "Level"
FROM Data_Exp;
Otherwise you can use the template below.
The query below will need tinkering and is probably tedious for what you are trying to achieve
SELECT Subject,
Gender,
Label,
(
SELECT CASE WHEN Y > 6 THEN "Level 6"
WHEN Y > 5 THEN "Level 5"
WHEN Y > 4 THEN "Level 4"
WHEN Y > 3 THEN "Level 3"
WHEN Y > 2 THEN "Level 2"
WHEN Y > 1 THEN "Level 1"
END AS "Level"
FROM
(SELECT ROUND(0.4*X1 + 0.15*(X2+X3+X4+X5),1) AS "Y" FROM Data_Exp) AS "lol"
INNER JOIN Data_Exp ON Data_Exp.Gender = lol.Gender AND Data_Exp.Label = lol.Label
) AS "Level"
FROM
FROM Data_Exp;
Last but not least, knowing your database (Postgres , MySql or else) will help as there are other solutions like declarative variables you can use to store your formula and that are definitely easier.

SQL - CASE WHEN troubles

Running into an issue with a CASE WHEN statement. Sample script below:
SELECT
CASE WHEN Column1 = "Example 1" THEN "Name 1"
WHEN Column1 = "Example 2" THEN "Name 2"
WHEN Column1 = "Example 3" THEN "Name 3"
WHEN Column1 = "Example 3" AND Column2 IN ("Sample1", "Sample2") THEN "Name4"
WHEN Column1 = "Example 3" AND Column2 IN ("Sample3", "Sample4") THEN "Name5"
ELSE "-" END AS Name,
[aggregation language that doesn't affect the script]
FROM Table1
GROUP BY Name
HAVING Name IN ("Name1", "Name2", "Name3", "Name4", "Name5"
ORDER BY Name ASC
The issue I'm having is that when executing the script "Name1", "Name2", and "Name3" all pull (and pull accurately), but "Name4" and "Name5" won't pull at all, presumably because they share a condition with "Name3" (Column1 = "Example3").
Essentially, I'm trying to pull both the aggregate that is "Name3" and it's components that are "Name4" and "Name5".
One way to think about it is that "Name3" is the NFL and "Name4" and "Name5" are the AFC and NFC, respectively. Because I'm pulling in the NFL with the condition {Column1 = "Example3"}, it won't pull in the AFC and NFC, despite having a second required "AND" condition.
Would LOVE if someone could help here. I've tried using parentheses, changing the order of the WHENs...no luck.
Thanks in advance!
My recommendation would be to change the ordering of your cases:
SELECT
CASE WHEN Column1 = "Example 1" THEN "Name 1"
WHEN Column1 = "Example 2" THEN "Name 2"
WHEN Column1 = "Example 3" AND Column2 IN ("Sample1", "Sample2") THEN "Name4"
WHEN Column1 = "Example 3" AND Column2 IN ("Sample3", "Sample4") THEN "Name5"
WHEN Column1 = "Example 3" THEN "Name 3"
ELSE "-" END AS Name,
[aggregation language that doesn't affect the script]
FROM Table1
GROUP BY Name
HAVING Name IN ("Name1", "Name2", "Name3", "Name4", "Name5"
ORDER BY Name ASC
With your current ordering, If "Name4" or "Name5" is true, "Name 3" will always be true, so it will get executed first. With the modified ordering, "Name 3" will be true only if "Name4" and "Name5" come out to be false. Make sense?
Would LOVE if someone could help here. I've tried using parentheses, changing the order of the WHENs...no luck.
You're not being entirely honest, are you?
http://sqlfiddle.com/#!6/11381/3/0 -- a simple switch of the WHEN conditions fixes your little problem. The lesson here is that testing stops at the first condition that's true.
For "Name 3", you need to exclude the conditions of "Name 4" and "Name 5":
CASE WHEN Column1 = "Example 1" THEN "Name 1"
WHEN Column1 = "Example 2" THEN "Name 2"
WHEN Column1 = "Example 3"
AND Column2 NOT IN ("Sample1", "Sample2", "Sample3", "Sample4") THEN "Name 3"
WHEN Column1 = "Example 3" AND Column2 IN ("Sample1", "Sample2") THEN "Name4"
WHEN Column1 = "Example 3" AND Column2 IN ("Sample3", "Sample4") THEN "Name5"
ELSE "-" END AS Name,
case when returns from the first matching condition and then ignores the rest, so you need to make the earlier condition narrower. Alternatively, you could put the Name4/5 conditions before the Name 3 condition.