Below is a table im working with. I would like to take the field from [Field1] column and move directly across to [Field2] column.
Results would be like this
Field2
Browser
com.android.browser
com.android.browser.BrowserActivity
Email
com.android.email
com.android.email.activity.Welcome
Phone
com.android.contacts
com.android.contacts.activities.PCUDialtactsActivity
Gallery
com.android.gallery3d
com.android.gallery3d.app.Gallery
Tắt mà n hình
com.katecca.screenofflockdonate
com.katecca.screenofflockdonate.MainHelper
Messaging
com.android.mms
com.android.mms.ui.ConversationList
Simple mode
com.pantech.app.skysettings.simplemode
com.pantech.app.skysettings.simplemode.simplemodesettingshrotcutActivity
This might do what you need:
SELECT iif(isnull(Field2), Field1, Field2) as [yourColumnTitle]
FROM yourTableName
Aside from this you'll need to add a column to the table datatype=YesNo and set it to true on the rows you want to affect, then you can
SELECT iif(NewColumn <> 0, Field1, Field2) as [yourColumnTitle]
FROM yourTableName
Related
I'm trying to select rows from my table where I look at the DocumentNo and Description, and if the Description repeats, it gets neglected / tossed / looked over. This is in MS Access, but can switch to DBeaver if necessary.
My table is of the sort
DocumentNo Description
SSPT284886 Tongs
SSPT284894 Kit
SSPT284894 Tongs
SSPT284895 Tubing
SSPT284895 Tubing
SSPT284895 Countertop
In this case, my query should return everything except the 5th line.
I've tried
Select *
FROM Table1
WHERE Table1.DocumentNo <> Table1.DocumentNo AND Table1.Description <> Table1.Description;
But this yields nothing, as I assume it looks for values that aren't equal to itself, essentially.
I think you simply want SELECT DISTINCT:
select distinct DocumentNo, Description
from table1;
Using MS Access, via VBA, I have been trying to update an main Access database table from an identically structured external Access database table. Same columns, same fields. The external databases are used to update a central main database.
What I have been trying to develop is a line of SQL that only appends the entries that are completely unique. Because there is no foreign keys or unique identifiers for the incoming data that can be referenced, I am required to check each field to make sure that there are no exact duplicates. So, if the table had 2 fields, if entries failed the logic test
intTable.field1 = extTable.field1 And intTable.field2 = extTable.field2
then those are the entries that would be appended.
The code I came up with is below, and when it runs it tries to append (in a Gary Oldman voice) EVERYTHING. I can't find out what could be wrong with it, as it's almost like it ignores the WHERE or WHERE NOT EXIST. I have tried dozens of small edits and alternate versions. Either is appends all or none.
INSERT INTO Table1
SELECT field1, field2, field3, field4, field5
FROM [;DATABASE=C:\extDB.accdb].[Table1] sourceDB
WHERE NOT EXISTS (SELECT *
FROM [;DATABASE=C:\extDB.accdb].[Table1] sourceDB1
WHERE ('Table1.[field1]'='sourceDB1.[field1]' And
'Table1.[field2]'='sourceDB1.[field2]' And
'Table1.[field3]'='sourceDB1.[field3]' And
'Table1.[field4]'='sourceDB1.[field4]' And
'Table1.[field5]'='sourceDB1.[field5]'));
FINAL CODE (after implementing ArcherBird's solution):
INSERT INTO Table1
SELECT field1, field2, field3, field4, field5
FROM [;DATABASE=C:\extDB.accdb].[Table1] sourceDB
WHERE NOT EXISTS (SELECT *
FROM [;DATABASE=C:\extDB.accdb].[Table1] sourceDB1
WHERE ((sourceDB.[field1] = sourceDB1.[field1] Or (sourceDB.[field1] IS NULL And sourceDB1.[field1] IS NULL)) And
(sourceDB.[field2] = sourceDB1.[field2] Or (sourceDB.[field2] IS NULL And sourceDB1.[field2] IS NULL)) And
(sourceDB.[field3] = sourceDB1.[field3] Or (sourceDB.[field3] IS NULL And sourceDB1.[field3] IS NULL)) And
(sourceDB.[field4] = sourceDB1.[field4] Or (sourceDB.[field4] IS NULL And sourceDB1.[field4] IS NULL)) And
(sourceDB.[field5] = sourceDB1.[field5] Or (sourceDB.[field5] IS NULL And sourceDB1.[field5] IS NULL)) ));
1) Remove the string quotes from your field comparison logic.
2) You have aliased Table1 in your query as sourceDB, so use that reference.
3) The table where you check for non-existence ought to be the "local" version of Table1 (the one you are inserting into).
INSERT INTO Table1
SELECT field1,
field2,
field3,
field4,
field5
FROM [;DATABASE=C:\extDB.accdb].[Table1] sourceDB
WHERE NOT EXISTS (SELECT *
FROM [Table1] sourceDB1
WHERE (sourceDB.[field1] = sourceDB1.[field1] And
sourceDB.[field2] = sourceDB1.[field2] And
sourceDB.[field3] = sourceDB1.[field3] And
sourceDB.[field4] = sourceDB1.[field4] And
sourceDB.[field5] = sourceDB1.[field5]));
EDIT:
To deal with the case where these fields might have null values, you cannot do a simple = comparison since null does not equal null. The best way I can think of to get around this in MS Access would be to replace null with some other value that is not likely to be used. In doing so, you also need to make sure you chose replacement values that are consistent with the data type. I will make an assumption that all your fields are text.
INSERT INTO Table1
SELECT field1,
field2,
field3,
field4,
field5
FROM [;DATABASE=C:\extDB.accdb].[Table1] sourceDB
WHERE NOT EXISTS (SELECT *
FROM [Table1] sourceDB1
WHERE (nz(sourceDB.[field1],"!##") = nz(sourceDB1.[field1],"!##") And
nz(sourceDB.[field2],"!##") = nz(sourceDB1.[field2],"!##") And
nz(sourceDB.[field3],"!##") = nz(sourceDB1.[field3],"!##") And
nz(sourceDB.[field4],"!##") = nz(sourceDB1.[field4],"!##") And
nz(sourceDB.[field5],"!##") = nz(sourceDB1.[field5],"!##")));
This is what I am working with:
Select fields 1-24 dbo.tablename where multiple !=2;
However, in the original table, tablename, some fields are titled differently. This is a work in progress so pardon some of the inefficiencies. One example is the 6th column/field, which is titled xacct in tablename, but titled xaccount in result.
In short: Can one set up a command such as above and still account for differing field names all the while leaving the data and it's type unchanged?
if you are doing an insert/select, column names are irrelevant, only order is important. If you are trying to rename the columns in a select statement, use the standard SQL:
SELECT field1 AS column1
, field2 AS column2
, field3 AS column3
, multiple AS multiply
FROM dbo.tablename
WHERE multiple != 2;
Where 'FIELD1' is original column name, 'COLUMN1' is new name you are providing.
You don't have to specify new names for all of the columns, only those you are changing:
SELECT field1 AS tedd_e_bear
, field2
, field3 AS spin_fidget
, multiple AS multiply
FROM dbo.tablename
WHERE multiple != 2;
In the above example, field 2 still has the name field2.
I'm trying to create a temporary table with a select into statement in a stored procedure as follows:
SELECT *
INTO #GENEALOGY
FROM
(
SELECT field1, field2,
(SELECT fieldA from tableY WHERE...) as field3,
(SELECT fieldB from tableY WHERE...) as field4
FROM table
WHERE condition
)
Any command I type after this closing bracket fails the syntax check (such as another SELECT statement).
I've tried putting BEGIN and END before and after the whole statement and then starting my next command.
I've tried adding
AS tablename
after the closing bracket and then the next statement but it doesn't like that either
I've tried removing the # but same problem.
I actually need to run a WHILE loop after this and INSERT more records into the same table.
What am i doing wrong?
thanks
Out of curiosity, why are you using a subquery?
SELECT field1, field2,
(SELECT fieldA from tableY WHERE...) as field3,
(SELECT fieldB from tableY WHERE...) as field4
INTO #GENEAOLOGY
FROM table
WHERE condition;
The structure of your query is ok. You should include a semi-colon at the end, if this is in a programming block. I assume table is a real name and that the other parts of the query are syntactically correct.
What does the error mention? If you run this in SSMS more than once, the second time the temp table already exists and could be an error.
You said an alias was not enough, but this is a right sentence:
SELECT *
INTO #GENEALOGY
FROM
(
SELECT field1, field2,
(SELECT fieldA from tableY WHERE...) as field3,
(SELECT fieldB from tableY WHERE...) as field4
FROM table
WHERE condition
) as TableNameAlias
thanks guys for the help
in the end I changed to an INSERT INTO rather than a SELECT INTO because
a) i needed to do 2 different select queries to get the data i need (1 for the first record and a 2nd for all subsequent records) and the second query would have had to be an INSERT anyway
b) and i wasn't sure how long sql server would keep the temp table open if i was calling the procedure from a crystal report
so i created a fixed table, use SELECT INTO and just delete all records as first line of my sp to clear out unwanted data
ta!
I want to produce a map that contains the original room ID and its new room ID equivalent for every room created.
Try using the "OUTPUT" clause with your insert statement. You can then map all of the inserted values and the keys that went with it to a different "mapping" table. Edit: I should add, this is all done in a SINGLE transaction, which makes the performance GREAT!
Insert Into TableA
(
Field1,
Field2,
Field3
)
Output Inserted.Field1, Field2, Field3, MappingID
Into MappingTable
Select Field1, Field2, Field3
From Rooms
Here's a link to the msdn site for the OUTPUT clause: http://msdn.microsoft.com/en-us/library/ms177564.aspx
Though I would suggest you to alter the table design and have a separate column each for New and Original site Id, but in present design I think you are looking for following:
SELECT
Name, #origSiteID, ControlsSiteNum, ControlsRoomNum, IsActive
FROM Rooms
WHERE SiteID = #newSiteID