merge two columns into one column and get unique - sql-server-2005

I have two columns in a table. I need to merge these two columns into 1 column, while making sure all the values in the resulting column are unique (no repetition of the IDs)
ex:
ChildCard PrimaryCard
123456 123456
123450 123456
123446 123446
123456 123446
156456 155456
157456 155456
121290 124290
234567 204567
Result
CardID
123456
123450
123446
123456
156456
157456
121290
234567
124290
204567
Any help would be great here.
Thanks.

SELECT ChildCard AS CardID FROM tbl UNION SELECT PrimaryCard FROM tbl
This should give you a list of distinct card entries, both child and primary.

You can use the SQL UNION command to create a view based on the two sets of data.
UNION drops duplicate entries during the merge.
UNION ALL keeps duplicate entries.
Refer here for more info http://msdn.microsoft.com/en-us/library/ms180026.aspx

You can select distinct IDs like this:
SELECT ChildCard AS ResultID FROM Table1
UNION
SELECT PrimaryCard AS ResultID FROM Table1

SELECT ChildCard CardID
FROM tbl
UNION
SELECT PrimaryCard
FROM tbl

Your safest option is to use an ID that is just the concatenation of both IDs.
A query to generate all new IDs would be:
select PrimaryCard + '_' + ChildCard as CardId
from your_table;

Related

Matching a substring to a string

Please advise me on the following question:
I have two tables in an Oracle db, one that contains full numbers and the other that contains parts of them.
Table 1:
12323543451123
66542123345345
16654232423423
12534456353451
64565463345231
34534512312312
43534534534533
Table 2:
1232
6654212
166
1253445635
6456546
34534
435345
Could you please suggest a query that joins these two tables and shows the relation between 6456546 and 64565463345231, for example. The main thing is that Table 2 contains a lot more data than Table 1, and i need to find all the substrings from Table 2 that are not present in Table 1.
Thanks in advance!
Try this:
with t as (
select 123 id from dual union all
select 567 id from dual union all
select 891 id from dual
), t2 as (
select 1112323 id from dual union all
select 32567321 id from dual union all
select 44891555 id from dual
)
select t.id, t2.id
from t, t2
where t2.id||'' like '%'||t.id||'%'
You could try using the CONTAINS operator like this :
SELECT * FROM Table2 JOIN Table1 ON Table1.id=Table2.id
WHERE NOT CONTAINS (Table2.data, Table1.data)
Are numbers from table two in a set place in table 1? For example is the 1232 in the same place each time or do you have to search a sting for the numbers. If it's set you could use an inline select or a temp table and create a substring of the string your searching and then join the table or temp table on that field.
you first need to say if the number in Table 1 and 2 are repeated, if is not then I think this query would help you:
SELECT *
FROM Table_1
JOIN Table_2 ON Table_1.ID = Table_2.ID
WHERE Table_2.DATA LIKE Table_1.DATA

ISNULL or any alternative in sql server when not data present for subquery

I have a situation, Where I need to get some data in case I find no data for certain query. e.g.
select id from abc where userid='XYZ'
In this case I will get the Id, only if the record with userid='XYZ' is present in the table abc.
Below given code will give 123 only if id is present as null .This is not what I'm expecting.
select isnull(id,123) from abc where userid='XYZ'
I want something like:If no data in abc with userid='XYZ', then some specific value should be output. e.g.
select isnull((select id from abc where userid='XYZ'),123)
Is there any similar shortcut for the above query?? Please suggest. Thanks in advance
isnull work on the "column" level, what you need is if a row is null, then print something else.
I would UNION ALL your select with a "dummy" select containing only one row with the data you want to be displayed in case nothing returns, then select top 1 from that with a order. Like
select top 1 id
from (
select id, 1 from abc where userid='XYZ'
UNION ALL
select 123, 0
) X
order by 1 DESC
select top 1 id
from
(
select 1 as id, value from abc where userid='xyz'
union
select 2, 123
) v
order by ranking
You could use a variable if the result is only going to return one row.
DECLARE #Id INT;
SELECT #Id = id FROM abc WHERE userid='XYZ';
SET #Id = ISNULL(#Id, 123);
SELECT #Id AS Id;

Multiple SQL tables added together without a JOIN

I have two tables:
FirstField | SecondField | ThirdField
FirstValue SecondValue ThirdValues
----------------------------------------------
FirstField | SecondField | ThirdField
OtherValue1 OtherValue2 OtherValue3
What I need it to add those two tables together into one SQL query. They can not be joined as I don't have anything to join them on and that's not what I want. I want my new table to look like:
FirstField | SecondField | ThirdField
FirstValue SecondValue ThirdValues
OtherValue1 OtherValue2 OtherValue3
This may be very simple but I am new to SQL and have been unable to find any help elsewhere.
Try UNION ALL:
SELECT FirstField ,SecondField ,ThirdField
FROM Table1
UNION ALL
SELECT FirstField ,SecondField ,ThirdField
FROM Table2
If you want to remove duplicate rows use UNION instead.
SELECT FirstField ,SecondField ,ThirdField
FROM Table1
UNION
SELECT FirstField ,SecondField ,ThirdField
FROM Table2
Have a lok at using a UNION/UNION ALL
Combines the results of two or more queries into a single result set
that includes all the rows that belong to all queries in the union.
The UNION operation is different from using joins that combine columns
from two tables.
So something like
SELECT Field1,
Field2,
...
Fieldn
FROM Table1
UNION ALL
SELECT Field1,
Field2,
...
Fieldn
FROM Table2
Provided that the column types and count match, use UNION ALL:
SELECT * FROM T1
UNION ALL
SELECT * FROM T2

SQL Join Issue return DISTINCT Names

I have the following two tables with data like so:
Table Values
var_label
1
2
2
1
3
Table Codes
var_code
1
2
4
2
I need to join these tables and get the distinct result. The var_label and var_code are equal pieces of data. I want to have the joined output like so:
MyColumn
1
2
3
4
Wht's the best way to do this?
Use UNION without ALL(implicit distinct) like so:
SELECT var_label AS MyColumn
FROM Values
UNION
SELECT var_code
FROM Codes
Live Demo
SELECT var_label
FROM Table1 as MyColumn
UNION
SELECT var_data as MyColumn
FROM Table2
you can give aliases for getting only one colum name.
SQLFiddle DEMO
SELECT distinct(var_label)
FROM Table1
UNION
SELECT distinct(var_data)
FROM Table2

find the missing values from a set of values, using SQL

How can I find a missing values from a set of values, using SQL (Oracle DB)
e.g.
SELECT NAME
FROM ACCOUNT
WHERE ACCOUNT.NAME IN ('FORD','HYUNDAI','TOYOTA','BMW'...)
(The "IN" clause may contain hundreds of values)
If 'HYUNDAI' is missing in the ACCOUNT table, I need to get the result as "HYUNDAI".
Currently I use the result of the above query to do a Vlookup against the original set of values to find the missing values, I want to directly get the missing values without doing the Vlookup.
Thanks
Kiran,
You got it reversed. Do this: http://www.sqlfiddle.com/#!2/09239/3
SELECT Brand
FROM
(
-- Oracle can't make a row without a table, need to use DUAL dummy table
select 'FORD' as Brand from dual union
select 'HYUNDAI' from dual union
select 'TOYOTA' fom dual union
select 'BMW' from dual
) x
where Brand not in (select BrandName from account)
Sample Account data:
create table account(AccountId int, BrandName varchar(10));
insert into account(AccountId, BrandName) values
(1,'FORD'),
(2,'TOYOTA'),
(3,'BMW');
Output:
| BRAND |
-----------
| HYUNDAI |
Better yet, materialized the brands to a table:
select *
from Brand
where BrandName not in (select BrandName from account)
Output:
| BRANDNAME |
-------------
| HYUNDAI |
Sample data and live test: http://www.sqlfiddle.com/#!2/09239/1
CREATE TABLE Brand
(`BrandName` varchar(7));
INSERT INTO Brand
(`BrandName`)
VALUES
('FORD'),
('HYUNDAI'),
('TOYOTA'),
('BMW');
create table account(AccountId int, BrandName varchar(10));
insert into account(AccountId, BrandName) values
(1,'FORD'),
(2,'TOYOTA'),
(3,'BMW');
You should use Except: EXCEPT returns any distinct values from the left query that are not also found on the right query.
WITH SomeRows(datacol) --It will look for missing stuff here
AS( SELECT *
FROM ( VALUES ('FORD'),
('TOYOTA'),
('BMW')
) AS F (datacol)),
AllRows (datacol) --This has everthing
AS( SELECT *
FROM ( VALUES ('FORD'),
('HYUNDAI'),
('TOYOTA'),
('BMW')
) AS F (datacol))
SELECT datacol
FROM AllRows
EXCEPT
SELECT datacol
FROM SomeRows
You can do:
SELECT a.val
FROM
(
SELECT 'FORD' val UNION ALL
SELECT 'HYUNDAI' UNION ALL
SELECT 'TOYOTA' UNION ALL
SELECT 'BMW' UNION ALL
etc...
etc...
) a
LEFT JOIN account b ON a.val = b.name
WHERE b.name IS NULL
This worked perfectly, thanks Michael.
SELECT Brand
FROM
( -- Oracle can't make a row without a table, need to use DUAL dummy table
select 'FORD' as Brand from dual union
select 'HYUNDAI' from dual union
select 'TOYOTA' fom dual union
select 'BMW' from dual
)
where Brand not in (select BrandName from account)
Luxspes and Zane thank you for your inputs
Contributing Excel code to make the typing of the answer easier:
Say column A has the values (Ford, Hyundai,...).
In column B, put this in every cell:
select 'x' as brand from dual union
In column C, write this formula, and copy it down.
=REPLACE(A2,9,1,A1)
All of the select/union statements should appear in column C.