Related
I have this dataset over here in R:
my_table = data.frame(id = c(1,2,3), name = c("sam", "smith", "sean"), height = c(156, 175, 191), address = c("123 first street", "234 second street", "345 third street"))
id name height address
1 1 sam 156 123 first street
2 2 smith 175 234 second street
3 3 sean 191 345 third street
I can get the summary of this table as follows:
st = capture.output(str(my_table))
>st
[1] "'data.frame':\t3 obs. of 4 variables:"
[2] " $ id : num 1 2 3"
[3] " $ name : chr \"sam\" \"smith\" \"sean\""
[4] " $ height : num 156 175 191"
[5] " $ address: chr \"123 first street\" \"234 second street\" \"345 third street\""
Further manipulation:
types = substr(st[c(2,3,4,5)], 13,15)
[1] "num" "chr" "num" "chr"
type_frame = data.frame(id = 1,2,3,4, types)
type_frame = type_frame[,c(1,5)]
type_frame$sql = ifelse(type_frame$types == "num", "int", "varchar(255)")
type_frame$name = colnames(my_table)
id types sql name
1 1 num int id
2 1 chr varchar(255) name
3 1 num int height
4 1 chr varchar(255) address
Based on this information, I would like to generate the following string output:
CREATE TABLE my_table (
id int,
name varchar(255),
height int,
address varchar(255),
);
I thought of the following approach:
first_part = "CREATE TABLE my_table ( "
second_part = c(type_frame[1,4], type_frame[1,3])
second_part = paste(second_part , collapse = " ")
third_part = c(type_frame[2,4], type_frame[2,3])
third_part = paste(third_part , collapse = " ")
fourth_part = c(type_frame[3,4], type_frame[3,3])
fourth_part = paste(fourth_part , collapse = " ")
fifth_part = c(type_frame[4,4], type_frame[4,3])
fifth_part = paste(fifth_part , collapse = " ")
final = paste0(first_part, second_part, " , ", third_part, " , ", fourth_part, " , ", fifth_part, " ,);")
The end result looks something like this:
>final
[1] "CREATE TABLE my_table ( id int , name varchar(255) , height int , address varchar(255) ,);"
In the end, I would like to take the above string and enter it into an SQL software.
The code I have written is very inefficient - can someone please show me a faster way to do this?
Thank you!
I am trying to transfer my Excel functions into Power Query (Office 365).
Here is the Source:
Key X A Z Y B
Cat 15 5 10 5 10
Cat 25 10 5 20
Cat 5 15 5 20 10
Dog 5 25 10 5 5
Dog 5 20 15
Bird 20 15 5 5 5
Here is what I am trying to achieve.
Many Thanks,
Aykut
Try below
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
all=Table.ColumnNames(Source),
start="A",
end="Y",
start2="X",
end2="B",
ColumnsStartToEnd=List.FirstN(List.Skip(all,List.PositionOf(all,start)),List.PositionOf(all,end)-List.PositionOf(all,start)+1), // all columns start to end from ColumnNames
ColumnsStartToEnd2=List.FirstN(List.Skip(all,List.PositionOf(all,start2)),List.PositionOf(all,end2)-List.PositionOf(all,start2)+1), // all columns start2 to end2 from ColumnNames
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
totals = Table.AddColumn(#"Added Index", "Sum1", each List.Sum( Record.ToList( Table.SelectColumns(#"Added Index" ,ColumnsStartToEnd){[Index]}) )),
max=Table.AddColumn(totals,"Max1",each List.Max( Record.ToList( Table.SelectColumns(#"Added Index" ,ColumnsStartToEnd){[Index]}) )),
positionof = Table.AddColumn(max,"Max_column",each ColumnsStartToEnd{List.PositionOf(Record.ToList( Table.SelectColumns(#"Added Index" ,ColumnsStartToEnd){[Index]}),[Max1])}),
max2=Table.AddColumn(positionof ,"Max2",each List.Max( Record.ToList( Table.SelectColumns(#"Added Index" ,ColumnsStartToEnd2){[Index]}) )),
#"Added Custom" = Table.AddColumn(max2, "Compare", each if [Max1]=[Max2] then "TRUE" else "FALSE"),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
in #"Removed Columns"
If you just need the final column, you can get rid of some intermediate steps
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.
I have a report in which one of the columns returns a DB field which can be 1 of 3 possible outcomes - 1, 0 or blank/"".
From what I can see, I think the problem here is that the field is blank rather than a NULL value, so any expressions I use seem to result in a #Error.
So far I have managed two different expressions which yield two halves of the results I need:
=IIF(Fields!Field1.Value = 1, "Yes", IIF ({Fields!Field1.Value = 0}, "No", "Unanswered"))
The above gets me 1 = "Yes", 2 = "No" and "" = #Error.
=IIF(Fields!Field1.Value="","Unanswered",Fields!Field1.Value)
This second one gets me 1 = 1, 0 = 0 and "" = "Unanswered".
Is there a way I can combine these and have the results appearing as 1 = "Yes", 0 = "No" and "" = "Unanswered"?
Any advice is appreciated!
Try using this SWITCH expression.
=SWITCH (
Fields!Field1.Value = 1, "Yes",
Fields!Field1.Value = 0, "No",
True, "Unanswered"
)
The final True acts like and else.
If this does not work, you may need to cast the field to text and check as follows
e.g. CStr(Fields!Field1.Value) = "1", "Yes",
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.