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
Related
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;
I have a situation where I want to combine two tables for queries in a select statement, and I haven't found a working solution yet.
The Situation:
Table A
Table B
Both A and B have identical fields but distinct populations. I have other queries that are pulling from each table separately.
I want to build a query that pulls from them as if they were one table. There are no instances of records being in both tables.
My research so far led me to think the FULL OUTER JOIN was what I wanted, but I'm not entirely sure how to do that when I'm not really joining them on any field and it failed in my tests. So I searched for append options thinking that might more accurately represent what I'm trying to do and the INSERT INTO looked promising but less so for select statements. Is there a way to do this or do I need to create a third table that's the combination of the first two to query from?
.
This is being done as an Excel VBA query to Access via DAO. I'm building up SQL statements piece by piece in my VBA code based on user-selected options and then pulling the results into Excel for use. AS such my hope is to be able to only alter the FROM statement (since I'm building up the queries piecemeal) to effect this so that any other aspects of the select statement won't be impacted. Any suggestions or help will be much appreciated!
You can UNION the tables to do this:
SELECT StuffYouWant
FROM (SELECT *
FROM TableA
UNION ALL
SELECT *
FROM TableB) c
Something like this:
SELECT * FROM a
UNION
SELECT * FROM b
Make sure the a table and the b table have the same number of columns and the corresponding columns have the same data type
I am working on a project where I have inherited an SQL Join that uses join
criteria in a format I have not seen before. The basic format of the join
is this:
Proc Sql;
create table mytest as
select t1.var1,
t1.var2,
t1.var3
from mysource1 t1
left join mysource2 t2 on
(t1.var1 = t2.var1), myparam t3;
quit;
The bit I am confused about is why myparam is included as a join
condition within the ON statement of the LEFT JOIN. The contents of
'myparam' is derived from the SAS Parameter File we have defined on our
system and contains just one row, with two columns. One contains month
start date, the other month end date.
None of the columns in this parameter file are in the other two source
tables and none of the columns in the parameter file appear in the final
output (they aren't referenced in the SELECT statement so they won't do).
I'm guessing that including the 'myparam' dataset in this context is
somehow using the date values within in it to cut the data in mysource1 and
mysource2, but could someone please provide confirmation that this is the
case and the exact mechanism at work please?
Thanks
This is an unusual construction for a join in SAS, but it's basically a Cartesian product. The myparam table isn't part of the LEFT JOIN condition but a new table, starting a new join. Any table included using a comma and no join condition causes it to be joined with all rows from one table matching to all rows in the other. This can be dangerous when two large tables are used (as the amount of rows is multiplied) but in your case the myparam table has one row, so it's only 1 x n.
However, saying all that, the query you have come across doesn't use any values from myparam (or mysource2 for that matter), so I don't see why these tables are being joined on at all. I'm fairly certain the following query would be equivalent:
proc sql;
select var1,var2,var3
from mysource1;
quit;
I'm aware this answer might come across as incomplete, so please feel free to comment...
If I join two tables together with left join and one of the tables is completely empty, I get a bunch of empty columns in the joined table.
Here is a fiddle demonstrating what I mean.
I would like the resulting joined table to not contain all those null columns
The number of columns that a query returns is fixed. It cannot change depending on whether a table is empty or not. So the answer is nope.
I want to get the results of a left join between two tables, with both having a column of the same name, the column on which I join. The following query is seen as valid by the import/export wizard in SQL Server, but it always gives an error. I have some more conditions, so the size wouldn't be too much. We're using SQL Server 2000 iirc and since we're using an externally developed program to interact with the database (except for some information we can't retrieve that way), we can not simply change the column name.
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2 ON table1.samename = table2.samename
At least, I think the column name is the problem, or am I doing something else wrong?
Do more columns than just your join key have the same name? If only your join key has the same name then simply select one of them since the values will be equivalent except for the non-matching rows (which will be NULL). You will have to enumerate all your other columns from one of the tables though.
SELECT table2.samename,table1.othercolumns,table2.*
FROM table1
LEFT JOIN table2 ON table1.samename = table2.samename
You may need to explicitly list the columns from one of the tables (the one with less fields), and leave out the 2nd instance of what would be the duplicate field..
select Table1.*, {skip the field Table2.sameName} Table2.fld2, Table2.Fld3, Table2.Fld4... from
Since its a common column, it APPEARS its trying to create twice in the result set, thus choking your process.
Since you should never use select *, simply replace it with the column names of the columns you want. THe join column has the same value (or null) in both sides of the join, so only select one of themm the one from table1 which will always have the value.
If you want to select all the columns from both tables just use Select * instead of including the tables separately. That will however leave you with duplicate column names in the result set, so even reading them out by name will not work and reading them by index will give inconsistent results, as changing the columns in the database will change the resultset, breaking any code depending on the ordinals of the columns.
Unfortunately the best solution is to specify exactly the columns you need and create aliases for the duplicates so they are unique.
I quickly get the column headings by setting the query to text mode and copying the top row ...