How to find which column value is differ after using except command - sql

I have two tables. Using "except" command I can find the mismatch values between two tables.
EX:
(select * from test_table3) except (select * from test_table1)
Now I need to find the difference in which column's value? How can i find that column?
I am using SQL 2005.

Related

Using dot notation with sum() to query the same, but multiple columns, in multiple databases

SQLite
There are multiple databases, one database for each time period (i.e. quarter). The column headers in each table are the same. Some of the columns. The data is identical between databases (e.g. ID, Name, Address, State, Website, etc). Some of the columns, the column header is the same but the
data in the column is different between databases.
The goal is to:
Select multiple columns from multiple databases, sum each column, convert the output from 000000000 to $000,000,000,000, adding three zero's to the output
(currently the data is represented in 000's).
Following is an iteration of queries that work, ending in the queries that fail.
Selecting one column from one database. This query works.
select dep
From AllReports19921231AssetsAndLiabilities;
output
"11005"
"34396"
"42244"
Adding a sum(columnName) method to this same query works.
select sum(dep)
From AllReports19921231AssetsAndLiabilities;
results: 3562807353
Attempting to sum(columnName) from multiple databases causes an error.
select sum(dep)
From AllReports19921231AssetsAndLiabilities,
AllReports19930331AssetsAndLiabilities;
error:
ambiguous column name: dep: select sum(dep)
From AllReports19921231AssetsAndLiabilities,
AllReports19930331AssetsAndLiabilities;
Using dot notation to attach a database to a column. Query works.
select AllReports19921231AssetsAndLiabilities.dep
From AllReports19921231AssetsAndLiabilities;
Output:
"11005"
"34396"
"42244"
However when I attempt to include dot notation and add sum(columnName) to the query, it fails.
select AllReports19921231AssetsAndLiabilities.sum(dep)
From AllReports19921231AssetsAndLiabilities;
I receive this error:
near "(": syntax error: select AllReports19921231AssetsAndLiabilities.sum(
What are correct ways to write this query?
The end goal is to select the same columns (e.g. col1, col2, col3, etc) from multiple databases (Q1, Q2, Q3, Q4).
Sum each column, add three zero's the output, then convert from 000000000 to $000,000,000,000
Note: There are 103 databases (i.e. one for each time period/quarter).
select AllReports19921231AssetsAndLiabilities.sum(dep),
AllReports19930331AssetsAndLiabilities.sum(dep),
AllReports19930630AssetsAndLiabilities.sum(dep)
From AllReports19921231AssetsAndLiabilities,
AllReports19930331AssetsAndLiabilities,
AllReports19930630AssetsAndLiabilities;
The above query outputs an error:
near "(": syntax error: select AllReports19921231AssetsAndLiabilities.sum(
Your syntax is wrong :
select sum(AllReports19921231AssetsAndLiabilities.dep)
From AllReports19921231AssetsAndLiabilities
Learn to use aliases!
select sum(aal.dep)
From AllReports19921231AssetsAndLiabilities aal;
The query is much easier to write and to read. The table alias (whether the full table name or an abbreviation) is attached to the column name. In SQL, this results in a qualified column reference. The qualification specifies what table it is coming from.
The table alias is not attached to a function, because SQL does not currently allow tables to contain functions.

Is it possible to avoid specifying a column list in a SQL Server CTE?

Is it possible to avoid specifying a column list in a SQL Server CTE?
I'd like to create a CTE from a table that has many columns so that the structure is identical. There probably is a way to accomplish this without relisting every column name.
I've tried (unsuccessfully):
with pay_cte as
(select * from payments)
select * from pay_cte
I'm encouraged in my quest by this statement in the msdn documentation:
The list of column names is optional only if distinct names for all resulting columns are supplied in the query definition.
https://msdn.microsoft.com/en-us/library/ms175972.aspx
Yes, assuming you mean that you don't have to name every column in the with cte(Col1, Col2) as section.
You can easily try this yourself with a very simple test query along the lines of:
with cte as
(
select *
from sys.tables
)
select *
from cte

SQL Server full text search not returning correct number rows

I have strange problem. Table has a computed column with full text search in this column.
When I using query:
SELECT *
FROM Table
WHERE CONTAINS (ComputedColumn, 'David')
2 rows returned.
And when using:
SELECT *
FROM Table
WHERE ComputedColumn LIKE N'%David%'
3 rows returned.
All three rows are identical.
Why? What is the problem? Maybe SQL Server has patch?

Using values from two tables to run query in Hive

I would like to run a hive query to be able to divide a column from one table by the total sum of a column from another table.
Do I have to join the tables?
The code below generates errors:
Select 100*(Num_files/total_Num_files) from jvros_p2, jvros_p3;
FAILED: Parse Error: line 1:75 mismatched input ',' expecting EOF near 'jvros_p2'
Yes, jvros_p3 is a single row single column table
Num_files is a column in jvros_p2 and total_Num_files is a single value in jvros_p3.
Your older version may be why your notation isn't working. Try this:
SELECT 100 * (Num_files / total_Num_files) FROM jvros_p2 JOIN jvros_p3;
I suspect that if you are eventually able to upgrade to at least 0.13, implicit join notation via comma-separated tables will be supported per HIVE-5558.

Copy data from one table to another without duplicates based on more then one column comparision

I am stuck in the following query. This was working properly on mySQL but it gives error on MSSQL-2005. The main purpose of the query is to copy data from one table to another without duplicates based on multiple columns comparison from both tables.
I can do this to compare one column for duplication, but I can't do when I compare more then one column for duplication.
Here is my query.
INSERT INTO eBayStockTaking (OrderLineItemID,Qty,SKU,SubscriberID,eBayUserID)
SELECT OrderLineItemID,Qty,SKU,SubscriberID,eBayUserID
FROM tempEBayStockTaking WHERE (OrderLineItemID,SubscriberID,eBayUserID)
Not In (SELECT OrderLineItemID,SubscriberID,eBayUserID FROM eBayStockTaking)
Note: I have been through many similar questions but all in vain.
Thanks
Rather try NOT EXISTS
Something like
INSERT INTO eBayStockTaking (OrderLineItemID,Qty,SKU,SubscriberID,eBayUserID)
SELECT OrderLineItemID,
Qty,
SKU,
SubscriberID,
eBayUserID
FROM tempEBayStockTaking t
WHERE Not EXISTS (
SELECT *
FROM eBayStockTaking e
WHERE e.OrderLineItemID = t.OrderLineItemID
AND e.SubscriberID = t.SubscriberID
AND e.eBayUserID = t.eBayUserID)
)
I know MySQL allows Row Subqueries, nut SQL Server does not allow this.