I am working with SQL Server 2008 and doing data analysis by using different queries. In my database I have 70 columns each in two different tables in same schema. The data in those tables were entered twice. Now I am comparing data of each column and showing records which have differences. Below is my query.
SELECT
[NEEF_Entry].[dbo].[tbl_TOF].Student_Class4_15,
[NEEF_Entry].[dbo].[tbl_TOF_old].Student_Class4_15
FROM
[NEEF_Entry].[dbo].[tbl_TOF]
INNER JOIN
[NEEF_Entry].[dbo].[tbl_TOF_old] ON [NEEF_Entry].[dbo].[tbl_TOF].FormID = [NEEF_Entry].[dbo].[tbl_TOF_old].FormID
WHERE
[NEEF_Entry].[dbo].[tbl_TOF].Student_Class4_15 <> [NEEF_Entry].[dbo].[tbl_TOF_old].Student_Class4_15
The join is based in the form ID which is same in both the tables. Now the column here is Student_Class4_15 in table tbl_TOF and in table tbl_TOF_old which is being compared here and the output is here
It shows what is the difference when data was entered before and after. Now the problem with this is that I have to manually replace column names of 70 columns each time which is time consuming.
What I want is that SQL query should pick all columns and compare them and return results.
I would use except to compare two tables, If the query returns no rows then the data is the same.
SELECT *
FROM table1
EXCEPT
SELECT *
FROM table2;
In case table2 has an extra rows:
SELECT *
FROM table2
EXCEPT
SELECT *
FROM table1;
Related
I have 2 tables, let's say Table1 and Table2. Both the tables have more than 50 columns. I want to write a query which returns all the columns. After that I will create one model class and map the columns returned from query result which has around 60 columns from both the tables.
Below are the sample columns for 2 tables. I have not made any mappings in entities in java classes.
Table1
id
search_key
col3 and so on.. 50+ cols
Table2
id
t1_id
col3 and so on.. 50+ cols
I have written one native query as below:
#Query(value="SELECT * FROM Table1 t1 INNER JOIN Table2 t2 ON t1.id = t2.t1_id WHERE t1.search_key IN ('12345')", nativeQuery=true)
But this native sql query is throwing NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [id] during auto-discovery of a native sql query.
My Requirement:
I need to write a query either native or hql which returns all the columns from both the tables based on matching criteria. and then that returned object by query I will map the columns which I want (since I want around 60 columns from both the tables, I don't want to write each column in select statement)
Is there a way to achieve this???
Please let me know if you need more info in question.
Actually if you run your query on a SQL client you can see that it returns all columns (from both tables) so we can understand that the problem is that your query is Associated with an Entity or generally a Java class that you have created. If you run your query and save the result on a List<Object> and not on a List<YourCustomObject> in your code then you will see that every Object of your list would contain all rows from both tables. Your SQL query as a plain sql query returns all rows from both tables as it is.
Imagine there is a SQL statement with 300+ columns
create table if not exists (
300+ columns
);
Insert into
select
300 columns
from
a inner join b
on a.key=b.key
;
It just keeps showing the error message
Invalid operation: INSERT has more expressions than target columns;
It is really hard to find which column is miss matching since there are too many columns.
Is there any way I can count the number of columns in a SELECT statement?
I know we can count the number of columns in information schema, but I want to count the number of columns/ items in a select statement, not an existing SQL table.
Well, you can use information_schema tables. For instance, you could use:
create table tempt as
select 300 columns
from a inner join
b
on a.key = b.key;
(I would add something like limit 1 because you may not care about the data.)
Then you can look in information_schema.columns to get the columns lists in order, with their types. You can even compare the columns to the original table, using SQL statements.
I have 2 tables in my SQL database:
And I want to merge them in a way the result will be:
This is just an example for 2 tables which need to be merged into one new table (The tables contain an example data, the statement should work for any amount of data inside the tables).
The ID which got different value in CSV should be updated into the new table for example:
ID 3's value is 'KKK' and in table T is 'CCC', then what should be updated is the CSV table.
You seem to want a left join and to match to the second table if available:
select t.id, coalesce(csv.value, t.value) as value
from t left join
csv
on t.id = csv.id;
If you want this in a new table, use the appropriate construct for your database, or use insert to insert into an existing table.
I'm having trouble joining two temp tables where all columns are varchar(MAX).
I'm trying to join on columns that both contains value 'SV-001', when I change value to 'SV-0' there are no problems, but when I add 1 more '0' it fails?
Values on both tables are collected from different standard tables and I have tested the results before I join them - even excel can compare values, so I'm sure values are identical.
I'm joining them like this:
SELECT *
FROM #Speedwell_setup
JOIN #Speedwell_data ON #Speedwell_setup.Productcode = #Speedwell_data.Product1
All I get is a empty result, no errors or anything, I hope that you can help me out here.
Thanks in advance
I managed to delete 4,000 rows from a table in my 129,000-row production database (Postgres 9.4 on Heroku), but only identified the problem a few days later.
I have a backup from before the loss, but only want to selectively restore the missing rows back to the table, preserving their id's. (A complete restore is not an option as new data has since been added to the table.)
Into a local testing database I have imported the backed-up table as articles_backup, alongside the actual articles table. I want to find all the rows in articles_backups that are missing from articles and then copy these to a new table articles_restores that I will then restore to the production database, back into the articles table (preserving record id's).
This query successfully returns all the id's of the deleted records:
select articles_backups.id
from articles_backups
left outer join articles on (articles_backups.id = articles.id)
where articles.id is null
But I have not been able to copy the result to a new table. I have unsuccessfully tried:
select *
into articles_restores
from articles_backups
left outer join articles on (articles_backups.id = articles.id)
where articles.id is null;
Which gives:
ERROR: column "id" specified more than once
Basically your query with LEFT JOIN / IS NULL does what you are after:
Select rows which are not present in other table
You get the error because you select all columns from both tables, and there is an id column in both. It's not possible to create a new table with duplicate column names, and it's not what you want to begin with. Only select columns from articles_backups:
CREATE TABLE articles_restores AS
SELECT ab.*
FROM articles_backups ab
LEFT JOIN articles a USING (id)
WHERE a.id IS NULL;
While being at it I simplified your query syntax with table aliases. The USING clause is just for the convenience of shorter code. It folds the two id columns into one, but all other columns are still in there twice if you SELECT *.
Use CREATE TABLE AS. SELECT INTO is also defined by the SQL standard and implemented in Postgres, but its use is discouraged. It's used in PL/pgSQL functions for a different purpose. Details:
Creating temporary tables in SQL
You could use an except to retrieve all the rows from articles_backup that are different from articles:
(assuming both tables have the same columns in the same order)
you could also create a temp table with this info to make it easy on your repairing statements:
create table temp_articles as
select * from articles_backup
except
select * from articles
step 1 - update rows from 'articles_backup' present in articles.
This step needs attention... you will have to establish a rule to choose between the data present in articles and the one present in temp_articles.
UPDATE articles a
SET a.col1=b.col1,
a.col2=b.col2,
(... other columns ...)
FROM (SELECT * FROM temp_articles) AS b
WHERE a.id = b.id and /* your rule for data to be (or not) updated goes here */
step 2 - insert rows from 'articles_backup' not present in articles (your deleted records):
insert into articles
select * from temp_articles where id not in (select id from articles)
Let us know if you need more help.