This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I would like my result to look something like this
FirstName1 LastName1 FirstName2 LastName2
Amy Smith Bob Stone
Fred Joker Gina White
Where FirstName1 and FirstName2 have same data types but nothing I can use to join (assume no one has same names) and the same goes for LastName1 and LastName2.
I tried to create 2 tables. First table contains FirstName1 and LastName1. Second table contains Firstname2 and LastName2.
Then I use
SELECT table1.FirstName1, table1.LastName1, table2.FirstName2, table2.LastName2
FROM table1, table2;
But this gives me a lot of duplicates. Any suggestions?
SELECT t1.FirstName1, t1.LastName1, t2.FirstName2, t2.LastName2
FROM
(SELECT
FirstName1,
LastName1,
ROW_NUMBER() OVER (ORDER BY FirstName1) 'RowNumber'
FROM table1
) AS t1
FULL OUTER JOIN
(SELECT
FirstName2,
LastName2,
ROW_NUMBER() OVER (ORDER BY FirstName2) 'RowNumber'
FROM table2
) AS t2
ON t1.RowNumber = t2.RowNumber
FULL OUTER JOIN will handle the cases where the number of rows from the two tables are not the same.
You should never do what you're trying to do, but try to use DISTINCT
SELECT Distinct
table1.FirstName1,
table1.LastName1,
table2.FirstName2,
table2.LastName2
FROM table1, table2;
The problem with your approach is that the retrieved dataset doesn't make any sense. Why do you want to do it like this anyways?
Edit: you can always do this do this:
select table1.FirstName1, table1.LastName1, table2.FirstName2, table2.LastName2
from table1, table2
where table1.rownum= table2.rownum;
"Oh haha I am really just trying to make it looks pretty. I am used to
programming in C and Java where all kinds of output are pretty
possible. "
Database programming is different. In this realm content is more important than mere prettiness. So when it comes to result sets the database insists that all the columns in a row share a meaningful relationship (the clue is in the name, relational database).
Your problem is, you have no tables with no key in common and hence no way to join the two sets of rows together. Without a proper WHERE clause the database implements a cross join, i.e. a cartesian product, and that's where all the duplication comes from.
Given that you don't have meaningful keys your only option is to fake them; the approach shown by Vikdor is one way to achieve this.
Related
I have a training application in which I need a union of two tables as a view in SQL Server. My company has a list of skills that are coded. Drilling is code 10, sanding is code 30, and so on. Employees are measured against these skill codes in our factory.
To illustrate, let's pick "Bob". Bob is a Metal Finisher who maintains several skills in the EmplSkills tables.
Secondly, in the JobSkills table are the skill requirements for "Metal Finisher".
By comparing the two tables you can see that Bob's skills don't exactly match the requirements for his position as Metal Finisher ...but they're pretty close.
However, from an AS9100 Quality Audit standpoint, I need to know exactly how close. I need all of Bob's skills that match the job requirements UNION'ed with all of Bob's extra skills that are not in the job scope but are nice-to-haves UNION'ed with all of Bob's missing skills that are extremely important to get trained up right away.
So, I'm creating the union view on the two tables with the SQL code below and
You can see that Bob can cut and deburr metal ...that's nice, but Bob has no sanding or scraping skills in his job position and that's very bad.
(SELECT a bunch of columns..., , row_number() over (order by t1.ref_no) as RowNum
FROM emplskills as t1
LEFT OUTER JOIN jobskills as t2 on t1.skill_ID = t2.skill_ID
WHERE t2.skill_ID is null)
UNION
(SELECT the same bunch of columns..., , row_number() over (order by t1.ref_no) as RowNum
FROM jobskills as t2
LEFT OUTER JOIN emplskills as t1 on t2.skill_ID = t1.skill_ID
WHERE t1.skill_ID is null)
UNION
(SELECT the same bunch of columns..., , row_number() over (order by t1.ref_no) as RowNum
FROM emplskills as t1, jobskills AS t2
WHERE t1.skill_ID=t2.skill_ID);
I'm using an application environment (Catavolt) in which all of the data tables need at least one unique data column that it uses remotely as a virtual "primary key" for its own housekeeping. So I need to invent one column of unique values and I can't figure out how to do this. I'd tried row_number () over (order by). I'd tried the two unique ref_no columns from each source table
SELECT ... , ((t1.ref_no*100000)+t2.ref_no) as UniqueKey
But the NULLs in the results blow out the math and I lose the uniqueness.
Is there something else I can add instead? It is of no consequence where the values come from, what they mean, or how big they are as long as they are unique.
TIA,
John
EDIT: RowNum suggestion from Tab on row_number() over (order by t1.ref_no)
Since it does sound like you really do just need to add an arbitrary unique column to your existing results, it's simply this:
SELECT *, ROW_NUMBER() OVER (ORDER BY {any column, really, it doesn't matter}) rn
FROM (
{Your existing query}
) t
I've been beating my head for two days and I finally concede that I need help doing this seemingly simple task.
I'm trying to find a way to produce a list of possible duplicate accounts in a single table. Fields are similar to AccountNumber, FirstName, LastName, DOB, SSN, Address, City, State, Zip.
I need to find a way to query the DB and find accounts that have different AccountNumbers but similar names/DOB/etc that are likely the same person.
Any help would be greatly appreciated.
Thanks!
select distinct t1.AccountNumber
from table t1
join table t2 on t2.Name = t1.Name and t2.DoB = t1.DoB
and t2.AccountNumber <> t1.AccountNumber
Look at using recursive selects. Here's an article on it. http://technet.microsoft.com/en-us/library/ms186243(v=sql.105).aspx
Basically, it creates a temporary table that allows you to perform actions on without requerying the database directly. This allows you to perform multiple sub selects more efficiently.
Your query would end up looking something like the following.
With accounts as (select a, b, c from table)
Select a, b, c
From table tbl
Where exists (select 1 from accounts act where act.a like '%' + tbl.a + '%') ...
Etc for more information on how to check if a column is like another check this out Compare Columns Where One is Similar to Part of Another
I got a query from our software vendor that did exactly what I needed it to do. Their solution is kind of a combination of the two suggestions above. It creates a temp table to put accounts that have more than one firstname lastname matching, then rates the likelihood that they are duplicates by checking for matching address, state, city. It also notes the last time each account had been used.
I don't fully understand the syntax, but that's ok, it gets the job done.
Thanks for the help!
Good morning/afternoon! I was hoping someone could help me out with something that probably should be very simple.
Admittedly, I’m not the strongest SQL query designer. That said, I’ve spent a couple hours beating my head against my keyboard trying to get a seemingly simple three way join working.
NOTE: I'm querying a Vertica DB.
Here is my query:
SELECT A.CaseOriginalProductNumber, A.CaseCreatedDate, A.CaseNumber, B.BU2_Key as BusinessUnit, C.product_number_desc as ModelNumber
FROM pps_sfdc.v_Case A
INNER JOIN reference_data.DIM_PRODUCT_LINE_HIERARCHY B
ON B.PL_Key = A.CaseOriginalProductLine
INNER JOIN reference_data.DIM_PRODUCT C
ON C.product_line_code = A.CaseOriginalProductLine
WHERE B.BU2_Key = 'XWT'
LIMIT 20
I have a view (v_Case) that I’m trying to join to two other tables so I can lookup a value from each of them. The above query returns identical data on everything EXCEPT the last column (see below). It's like it's iterating through the last column to pull out the unique entries, sort of like a "GROUP BY" clause. What SHOULD be happening is that I get unique rows with specific "BusinessUnit" and "ModelNumber" for that record.
DUMEPRINT 5/2/2014 8:56:27 AM 3002845327 JJT Product 1
DUMEPRINT 5/2/2014 8:56:27 AM 3002845327 JJT Product 2
DUMEPRINT 5/2/2014 8:56:27 AM 3002845327 JJT Product 3
DUMEPRINT 5/2/2014 8:56:27 AM 3002845327 JJT Product 4
I modeled my solution after this post:
How to deal with multiple lookup tables for beginners of SQL?
What am I doing wrong?
Thank you for any help you can provide.
Data issue. General rule in trouble shooting these is the column that is distinct (in this case C.product_number_desc as ModelNumber) for each record is generally where the issue is going to be...and why I pointed you towards dim_product.
If you receive duplicates, this query below will help identify if this table is giving you the issues. Remember key in this statement can be multiple fields...whatever you are joining the table on:
Select key,count(1) from table group by key having count(1)>1
Other options for the future...don't assume it's your code, duplicates like this almost always point towards dirty data (other option is you are causing cross joins because keys are not correct). If you comment out the 'c' table and the column referred to in the select clause, you would have received one row...hence your dupes were coming from the 'c' table here.
Good luck with it
I have two tables, TABLE1 and TABLE2. TABLE1 Has 4 columns: Name, Client, Position, and ID. TABLE2 has 3 columns: Amount, Time, and ID. For every ID in TABLE1 there are one or more entries in TABLE2, all with identical ID and Time values but varying Amount values.
In a view, I have a concatenated string of Name, Client, Position, and ID from TABLE1, but I also need to concatenate the Time for each ID from TABLE2 to this string. If I do a join as I create the view, I get a ton of duplicate strings in the view as it lists the same ID multiple times for each value of Amount in TABLE2.
I need to get rid of the duplicates, so I either need to avoid the replication that occurs from the join, or find a way to simply delete all the duplicates from the view.
Hopefully this is all clear enough. Thank you for reading and for any help you can provide!
DISTINCT could be appropriate:
CREATE VIEW [dbo].[View1]
AS
SELECT distinct
dbo.Table1.Id,
dbo.Table1.Name,
dbo.Table1.Client,
dbo.Table1.Position,
dbo.Table2.[Time]
FROM dbo.Table1
LEFT OUTER JOIN dbo.Table2
ON dbo.Table1.Id = dbo.Table2.Id
SqlFiddle: http://sqlfiddle.com/#!3/65651/1
On a side note, depending on what your table structures really are, you might consider having a surrogate primary key on the table2 if you don't have a natural one. I have not put one in the example, because chances are that you already have one - just making sure.
In my tables I have for example
CountyID,County and CityID in the county table and in the city table I have table I have for example
City ID and City
How do I create a report that pulls the County from the county table and pulls city based upon the cityid in the county table.
Thanks
Since this is quite a basic question, I'll give you a basic answer instead of the code to do it for you.
Where tables have columns that "match" each other, you can join them together on what they have in common, and query the result almost as if it was one table.
There are also different types of join based on what you want - for example it might be that some rows in one of the tables you're joining together don't have a corresponding match.
If you're sure that a city will definitely have a corresponding county, try inner joining the two tables on their matching column CityID and querying the result.
The obvious common link between both tables is CityID, so you'd be joining on that. I think you have the data organized wrong though, I'd put CountryID in the City table rather than CityID in the country table. Then, based on the CountryID selected, you can limit your query of the City table based on that.
To follow in context of Bridge's answer, you are obviously new to SQL and there are many places to dig up how to write them. However, the most fundamental basics you should train yourself with is always apply the table name or alias to prevent ambiguity and try to avoid using column names that might be considered reserved words to the language... they always appear to bite people.
That said, the most basic of queries is
select
T1.field1,
T1.field2,
etc with more fields you want
from
FirstTable as T1
where
(some conditional criteria)
order by
(some column or columns)
Now, when dealing with multiple tables, you need the JOINs... typically INNER or LEFT are most common. Inner means MUST match in both tables. LEFT means must match the table on the left side regardless of a match to the right... ex:
select
T1.Field1,
T2.SomeField,
T3.MaybeExistsField
from
SomeTable T1
Join SecondTable T2
on T1.SomeKey = T2.MatchingColumnInSecondTable
LEFT JOIN ThirdTable T3
on T1.AnotherKey = T3.ColumnThatMayHaveTheMatchingKey
order by
T2.SomeField DESC,
T1.Field1
From these examples, you should easily be able to incorporate your tables and their relationships to each other into your results...