redshift SQL anyway to count the item number of a Select statement - sql

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.

Related

Show data difference in columns of two tables in same database

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;

Drop column in DBVisualizer-query

I'm looking for a way to drop single columns from an extended query in DBVisualizer. I have a couple of tables with more than one hundred columns each. Using a number of subqueries I want to drop a single column from one of the tables, something like that:
select *
from table1 a
join table2 b
on a.key = b.key
drop b.key;
I defenitely do not want to write down all the single columns, because the query is supposed to be used for a long time and the tables get new columns to often to change the query each time.
Is there a way to do this?
Thanks
Felix

SQL Query to count multiple values from one table into specific view

I like to request your help. I can get the results seperated but now i want to create a query which has it perfect for a external person. my explanation:
I have a statistics database with in this database a table when some records comes in and each records has several columns with values etc...
Now one of these columns is called "MT"
MT Column can have only one of the following values per records: A,B,C,D,E
The records also have a columne called TotalAmount which indicate a size of a value outside the database. This TotalAmount column is numeric without decimals and can have a value between 1 and 10.000.
And the last part is the records it self, the table has X amount of records.
So Basicly i need to create a query which seperates each MT value and calculates the amount of records per MT and the sum of TotalAmount.
This is on SQL Server 2005.
Many thanks for your assistance!
Very hard to guess without a full db schema. But I think you need.
SELECT MT, Count(*), SUM (TotalAmout)
FROM YourTable
GROUP BY MT

Access loop through query results using variables from a table

I am new to VBA so I apologize in advance if this seems basic to you experts but I appreciate all of the help I can get.
I have a table containing a column of reference numbers that can grow or shrink weekly. I also have a query pulling back price list data that has changed since last week. The query results vary weekly. What I need to do is assign all of the query results to each reference number and have all of that end up in a make table. For example if there are 10 reference numbers and the query result is 10 rows then 100 lines would be added to the table (adding the reference number to the beginning of each row). This sounds like some sort of loop but your the experts, not me.
Thanks in advance!
You can solve it with a cross join. In a cross join you join two tables without specifying a join clause. Such a query returns all possible combinations of rows of the two tables (this is called a Cartesian product)
SELECT col_a, col_b INTO newTable
FROM table_a, table_b
If table_a contains 10 rows and table_b contains 5 rows, this returns 50 rows.

Copy records missing from one table to a new table

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.