SQL table column forming from another columns - sql

I have a create table query, where I'm creating a table from another table's data. But I'm supposed to create new column from the same row's data.
In example below, I need to get the first letter from ID from each row.
For example what I have tried:
SELECT (SELECT SUBSTRING((SELECT ID
FROM tableWhereDataComes), 1, 1)) AS PERSONTYPE,
ID , --This is a string like M101 or F101 etc.
FIRSTNAME,
LASTNAME
FROM tableWhereDataComes
How do I get the required M or F to the first column PERSONTYPE?

Are you just looking for LEFT()?
SELECT LEFT(ID, 1) AS PERSONTYPE, ID, FIRSTNAME, LASTNAME
FROM tableWhereDataComes;
I would note that this is probably not necessary. You can add a computed column to the table:
alter table tableWhereDataComes add persontype as (left(id, 1));
This is then calculated when the table is queried (or if persisted whenever the data changes), so it is always up-to-date.

Related

How to execute a select with a WHERE using a not-always-existing column

Simple example: I have some (nearly) identical tables with personal data (age, name, weight, ...)
Now I have a simple, but long SELECT to find missing data:
Select ID
from personal_data_a
where
born is null
or age < 1
or weight > 500
or (name is 'John' and surname is 'Doe')
Now the problem is:
I have some personal_data tables where the column "surname" does not exit, but I want to use the same SQL-statement for all of them. So I have to check (inside the WHERE clause) that the last OR-condition is only used "IF the column surname exists".
Can it be done in a simple way?
You should have all people in the same table.
If you can't do that for some reason, consider creating a view. Something like this:
CREATE OR REPLACE VIEW v_personal_data
AS
SELECT id,
born,
name,
surname,
age,
weight
FROM personal_data_a
UNION ALL
SELECT id,
born,
name,
NULL AS surname, --> this table doesn't contain surname
age,
weight
FROM personal_data_b;
and then
SELECT id
FROM v_personal_data
WHERE born IS NULL
OR age < 1
OR ( name = 'John'
AND ( surname = 'Doe'
OR surname IS NULL))
Can it be done in a simple way?
No, SQL statements work with static columns and the statements will raise an exception if you try to refer to a column that does not exist.
You will either:
need to have a different query for tables with the surname column and those without;
have to check in the data dictionary whether the table has the column or not and then use dynamic SQL to build your query; or
to build a VIEW of the tables which do not have that column and add the column to the view (or add a GENERATED surname column with a NULL value to the tables that are missing it) and use that instead.
While dynamic predicates are usually best handled by the application or by custom PL/SQL objects that use dynamic SQL, you can solve this problem with a single SQL statement using DBMS_XMLGEN, XMLTABLE, and the data dictionary. The following code is not what I would call "simple", but it is simple in the sense that it does not require any schema changes.
--Get the ID column from a PERSONAL table.
--
--#4: Get the IDs from the XMLType.
select id
from
(
--#3: Convert the XML to an XMLType.
select xmltype(personal_xml) personal_xmltype
from
(
--#2: Convert the SQL to XML.
select dbms_xmlgen.getxml(v_sql) personal_xml
from
(
--#1: Use data dictionary to create SQL statement that may or may not include
-- the surname predicate.
select max(replace(replace(
q'[
Select ID
from #TABLE_NAME#
where
born is null
or age < 1
or weight > 500
or (name = 'John' #OPTIONAL_SURNAME_PREDICATE#)
]'
, '#TABLE_NAME#', table_name)
, '#OPTIONAL_SURNAME_PREDICATE#', case when column_name = 'SURNAME' then
'and surname = ''Doe''' else null end)) v_sql
from all_tab_columns
--Change this literal to the desired table.
where table_name = 'PERSONAL_DATA_A'
)
)
where personal_xml is not null
)
cross join xmltable
(
'/ROWSET/ROW'
passing personal_xmltype
columns
id number path 'ID'
);
See this db<>fiddle for a runnable example.

Copying data from one table to another different column names

I'm having an issue copying one table's data to another. I have around 100 or so individual tables that have generally the same field names but not always. I need to be able to copy and map the fields. example: source table is BROWARD and has column names broward_ID, name, dob, address (the list goes on). The temp table I want to copy it to has ID, name, dob, address etc.
I'd like to map the fields like broward_ID = ID, name = name, etc. But many of the other tables are different in column name, so I will have to write a query for each one. Once I figure out the first on, I can do the rest. Also the column in both tables are not in order either..thanks in advance for the TSQL...
With tables:
BROWARD (broward_ID, name, dob, address) /*source*/
TEMP (ID, name, address,dob) /*target*/
If you want to copy information from BROWARD to TEMP then:
INSERT INTO TEMP SELECT broward_ID,NAME,ADDRESS,DOB FROM BROWARD --check that the order of columns in select represents the order in the target table
If you want only copy values of broward_ID and name then:
INSERT INTO TEMP(ID, name) SELECT broward_ID,NAME FROM BROWARD
Your question will resolve using update
Let's consider we have two different table
Table A
Id Name
1 abc
2 cde
Table B
Id Name
1
2
In above case want to insert Table A Name column data into Table B Name column
update B inner join on B.Id = A.Id set B.Name = A.Name where ...

Create table with other table data

Following query works but i want with 2 fields
CREATE TABLE TOTAPPS (ANUM) AS SELECT a.A# FROM APPLICANT a.
This is work that
//TOTAPPS
ANUM
--------
if i want to create with 2 fields?
//Totapps
ANUM NUMBER
---------------------------
how should i create the table? in order to get the correct out?
and the NUMBER is refer to the ANUM , for example
CREATE TABLE TOTAPPS (ANUM) AS SELECT a.A# FROM APPLICANT a,
(NUMBER) AS SELECT COUNT(*) FROM a.A#;
but it's failed to work.
Calling a column NUMBER is not correct as it is a reserved word. Assuming from your second query that you want to add to your table distinct values of a.A# and the count of these values in the table, you should try this:
CREATE TABLE TOTAPPS (ANUM, MYNUMBER) AS
SELECT a.A#, COUNT(a.A#) FROM APPLICANT a GROUP BY a.A#;

Need a count query in access but one column only needs unique values

I have a table 'tblBaseResults' setup like this
Column 1 = Name
Column 2 = Activity
The Activity column is supposed to be unique. However there are multiple Name fields. So im not sure how to get the Activity column to unique values and keep just one value from Name.
I would prefer to just keep the values from name that DO NOT have any '?' characters.
The current Count query i have works great. However i need only ONE of the Name fields to carry over with it
SELECT tblBaseResults.Activity, Count(tblBaseResults.Activity) AS CountOfActivity INTO tblCountResults
FROM tblBaseResults
GROUP BY DISTINCT tblBaseResults.Activity;
In Excel if i do a VLookup i get what i need, but want to do it in access.
INSERT INTO tblCountResults (Activity, Name, CountOfActivity)
SELECT Activity, min(Name), Count(tblBaseResults.Activity)
FROM tblBaseResults
WHERE instr(Name, '?') = 0
GROUP BY Activity

Underlying rows in Group By

I have a table with a certain number of columns and a primary key column (suppose OriginalKey). I perform a GROUP BY on a certain sub-set of those columns and store them in a temporary table with primary key (suppose GroupKey). At a later stage, I may need to get more details about one or more of those groupings (which can be found in the temporary table) i.e. I need to know which were the rows from the original table that formed that group. Simply put, I need to know the mappings between GroupKey and OriginalKey. What's the best way to do this? Thanks in advance.
Example:
Table Student(
StudentID INT PRIMARY KEY,
Level INT, --Grade/Class/Level depending on which country you are from)
HomeTown TEXT,
Gender CHAR)
INSERT INTO TempTable SELECT HomeTown, Gender, COUNT(*) AS NumStudents FROM Student GROUP BY HomeTown, Gender
On a later date, I would like to find out details about all towns that have more than 50 male students and know details of every one of them.
How about joining the 2 tables using the GroupKey, which, you say, are the same?
Or how about doing:
select * from OriginalTable where
GroupKey in (select GroupKey from my_temp_table)
You'd need to store the fields you grouped on in your temporary table, so you can join back to the original table. e.g. if you grouped on fieldA, fieldB, and fieldC, you'd need something like:
select original.id
from original
inner join temptable on
temptable.fieldA = original.fieldA and
temptable.fieldB = original.fieldB and
temptable.fieldC = original.fieldC