Concatenate values - sql

I want to concatenate all pair options in my column.
I have an table:
ID num
1 10
2 20
3 30
and I want to create a query that will give me the following result:
10,20
10,30
20,10
20,30
30,10
30,20
Also i want second query that will return without multiples(10,20=20,10)
10,20
10,30
20,30
How can I get the 2 above in 2 different queries?
Thanks

For the first one I would do a cartesian product filtering the equal ones.
SELECT a.num, b.num
FROM tablename a, tablename b
WHERE a.num != b.num // Unless you want to exclude by ID
For the second, I would force one side to be bigger than the other.
SELECT a.num, b.num
FROM tablename a, tablename b
WHERE a.num < b.num

Let's say your table name is "mytable".
1st Query:
Select
t1.num,
t2.num
from mytable t1 join mytable t2 on
t1.ID!=t2.ID
2nd Query:
Select
t1.num,
t2.num
from mytable t1 join mytable t2 on
t1.ID<t2.ID

You have to use self join to get desired results. Use || to concatenate the columns. If the table name is t
Query for the first will be:
select t1.num||','|| t2.num
from t t1 join t t2
on t1.id!=t2.id
The second result can be obtained using below query:
Select
t1.num||','|| t2.num
from t t1 join t t2 on
t1.id<t2.id

Related

SQL join greater than / less then

I have 2 tables
I
So i need to get a table in which rows will be excluded for which the date for each "id" is more than in the second table "datestop"
I tried inner join with double condition:
id = id and Date < DateStop, but in this way also excluded rows which not contained in table 2 (id1)
For imagine, thats what i want to get:
I would use NOT EXISTS which is very efficient:
select t1.*
from table1 t1
where not exists (
select 1 from table2 t2
where t2.id = t1.id and t2.datestop <= t1.date
)
It's not clear if you need the value of Status must be 'Stopped' for your requirement.
So maybe you need to change the WHERE clause of the subquery to:
where t2.id = t1.id and t2.datestop <= t1.date and t2.status = 'Stopped'
Consider:
select a.*
from tablea a
left join tableb b on b.id = a.id
where b.id is null or b.datestop > a.date
This phrases as: get all records in tablea for which either there is no record with the same id in tableb, or whose date is less than the datestop of the corresponding record in tableb.
This is a situation where all can be helpful:
select a.*
from a
where a.date < all (select b.datestop
from b
where b.id = a.id and b.status = 'Stopped'
);

Selecting the right column

I have a query.
with result as
(
select t1.name, t1.number, t2.number from table1 t1, table2 t2 where some conditions
union all
select t1.name, t1.number, t3.number from table1 t1, table3 t3 where some conditions
)select * from result
I need to insert in table5 t1.name and t2.number
table5 has the same columns as t1.
If I do something like
insert in table5(name, number)
select r.name, r.number from result r
what would be considered r.number? t1.number or t2.number? Because columns have the same name. Or is there a way to defferentiate? How can I make it so the query skips every row with t3.number? Can I even do it?
For example I have table1
A (+1)11111111
B (+1)22222222
C (+1)33333333
table2
(+2)44444444
(+2)55555555
first select will get me
A (+1)11111111 (+2)44444444
B (+1)22222222 (+2)55555555
table3
(+3)66666666
(+3)88888888
(+3)97898789
result of second select
B (+1)22222222 (+3)88888888
C (+1)33333333 (+3)97898789
this will be the result of union all
A (+1)11111111 (+2)44444444
B (+1)22222222 (+2)55555555
B (+1)22222222 (+3)88888888
C (+1)33333333 (+3)97898789
what I want in the end is
A (+2)44444444
B (+2)55555555
the end result should not have this rows
B (+1)22222222 (+3)88888888
C (+1)33333333 (+3)97898789
Both. In some rows, t2.number is number and in others t3.number is number.
The result of the union all in a single result set. The result set doesn't know the origin of the values in any particular column (although you could include another column with this information).

using where clause with Union

I have two tables, t1 and t2, with identical columns(id, desc) and data. But one of the columns, desc, might have different data for the same primary key, id.
I want to select all those rows from these two tables such that t1.desc != t2.desc
select a.id, b.desc
FROM (SELECT * FROM t1 AS a
UNION ALL
SELECT * FROM t2 AS b)
WHERE a.desc != b.desc
For example, if t1 has (1,'aaa') and (2,'bbb') and t2 has(1,'aaa') and (2,'bbb1') then the new table should have (2,'bbb') and (2,'bbb1')
However, this does not seem to work. Please let me know where I am going wrong and what is the right way to do it right.
Union is not going to compare the data.You need Join here
SELECT *
FROM t1 AS a
inner join t2 AS b
on a.id =b.id
and a.desc != b.desc
UNION ALL dumps all rows of the second part of the query after the rows produced by the first part of the query. You cannot compare a's fields to b's, because they belong to different rows.
What you are probably trying to do is locating records of t1 with ids matching these of t2, but different description. This can be achieved by a JOIN:
SELECT a.id, b.desc
FROM t1 AS a
JOIN t2 AS b ON a.id = b.id
WHERE a.desc != b.desc
This way records of t1 with IDs matching records of t2 would end up on the same row of joined data, allowing you to do the comparison of descriptions for inequality.
I want both the rows to be selected is the descriptions are not equal
You can use UNION ALL between two sets of rows obtained through join, with tables switching places, like this:
SELECT a.id, b.desc -- t1 is a, t2 is b
FROM t1 AS a
JOIN t2 AS b ON a.id = b.id
WHERE a.desc != b.desc
UNION ALL
SELECT a.id, b.desc -- t1 is b, t2 is a
FROM t2 AS a
JOIN t1 AS b ON a.id = b.id
WHERE a.desc != b.desc
The UNION operator is used to combine the result-set of two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types.
So, if it has same number of columns and same datatype, then use Union otherwise join only Can be used.
SELECT *
FROM t1 AS a
inner join t2 AS b
on a.id =b.id
and a.desc != b.desc

MS Access 2007 Inner Join on Same Table

I have a table t1. It has columns [id] and [id2].
Select count(*) from t1 where id=1;
returns 31,189 records
Select count(*) from t1 where id=2;
returns 31,173 records
I want to know the records where id2 is in id=1 but not in id=2.
So, I use the following:
Select * from t1 a left join t1 b on a.id2=b.id2
Where a.id=2 And b.id=1
And b.id2 Is Null;
It returns zero records.
Using an inner join to see how many records have id2 in common, I do...
Select * from t1 a inner join t1 b on a.id2=b.id2
Where a.id=2 And b.id=1;
And that returns 31,060. So where are the extra records in my first query that don't match?
I am sure I must be missing something obvious.
Sample Data
id id2
1 101
1 102
1 103
2 101
2 102
My expected results is to find the record with '103' in it. 'id2' not shared.
Thanks for any help.
Jeff
You are attempting to do what is generally called an exclude join. This involves doing a LEFT JOIN between two tables, then using a WHERE clause to only select rows where the right table is null, i.e. there was no record to join. In this way, you select everything from the left table except what exists in the right table.
With this data, it would look something like this:
SELECT
t1.id,
t1.id2
FROM test_table t1
LEFT JOIN
(SELECT
id,
id2
FROM test_table
WHERE id = 2) t2
ON t2.id2 = t1.id2
WHERE t1.id = 1
AND t2.id IS NULL --This is what makes the exclude join happen
And here is a SQLFiddle demonstrating this in MySQL 5.7 with the sample data you provided.
I think maybe Access changes the left join to an inner join when you add a where clause to filter rows (I know SQL Server does this), but if you do the filtering in derived tables it should work:
select
a.*
from
(select * from t1 where id = 1) a
left join
(select * from t1 where id = 2) b
on a.id2 = b.id2
where b.id2 is null

How Can i select a field and check it with itself?

i want to say
select col1,col2,col3
from table1
inner join table2 on table1.col1=table2.col1
and ..... ( ? )
? : i want just 1 record or first record from table1 joined with first record from table2. but the command cause all record joined that can be join. for example if 2 records are in the table1 that col1=1432 and just 1 record in table2 exists that col1=1432 command joined all. but i want to join just first from table1 with first from table2
i want to display all record that are more than 1 record to be join.
i want just 1 record or first record from table1 joined with first
record from table2
Here you go:
select top 1 col1,col2,col3
from table1
inner join table2 on table1.col1=table2.col1
and .....
Try this query -
SELECT *
FROM dbo.table1 t1
JOIN (
SELECT TOP 1 *
FROM dbo.table2
) t2 ON t1.col1 = t2.col1
If you're on SQL Server 2005 or above, you can try a ranking function to grab only the first record of each group:
select *
from (
select a.col1
, a.col2
, a.col3
-- Use the order by to determine which rows will be ranked first for each group
, row_number() over (partition by a.col1 order by a.col2) as rownum
from table1 as a
join table2 as b on a.col1 = b.col1
) as q
where rownum = 1 -- Only get the first row of each group
i want to display all record that are more than 1 record to be join.
You can add this to your where clause:
and exists (
select col1
from table1
where col1 = q.col1
group by col1
having count(*) > 1
)
To offer another solution that doesn't use a ranking function, I think we would need to know more about your tables, especially unique keys and how you define the first record of each group.