how would i write a query to output the max column3 records for each corresponding record in column 1:
SELECT t1.column1, t1.column2, MAX(t2.column3) as MAXcolumn3
FROM table1 t1
LEFT JOIN table1 t2
ON t1.column2 = t2.column2
Group by t1.column1, t1.column2
RAW OUTPUT
column1 column2 column3
a aa 33
a ab 02
a ac NULL
b ba 11
b bb 00
c ca NULL
c cb 00
d da NULL
DESIRED OUTPUT
column1 column2 column3
a aa 33
a ab 33
a ac 33
b ba 11
b bb 11
c ca 00
c cb 00
d da NULL
Join table1 to itself on column1 to get all sibling rows, then left join to table2:
SELECT t1.column1, t1.column2, MAX(t3.column3) as MAXcolumn3
FROM table1 t1
JOIN table1 t2 ON t2.column1 = t1.column1
LEFT JOIN table2 t3 ON t3.column2 = t2.column2
GROUP BY t1.column1, t1.column2
See SQLFiddle
You might try using the aggregate function MAX() as a window function:
SELECT column1, column2, MAX(column3) OVER ( PARTITION BY column1 ) AS MAXcolumn3
FROM table1;
I don't know the precise schema of your table so I don't know if the above would return duplicates.
If you have two tables, then you might take your original query and do something similar with a subquery or CTE:
WITH cte AS (
SELECT t1.column1, t1.column2, MAX(t2.column3) as MAXcolumn3
FROM table1 t1 LEFT JOIN table2 t2 -- supposed to be table1?
ON t1.column2 = t2.column2
GROUP BY t1.column1, t1.column2
)
SELECT column1, column2, MAX(MAXcolumn3) OVER ( PARTITION BY column1 ) AS MAXcolumn3
FROM cte;
select tt.*
from (select t1.*,
row_number() over (partition by t1.column1 , t2.column2
order by t1.column1 desc) as column3
from table t1
LEFT JOIN table1 t2
ON t1.column2 = t2.column2
Group by t1.column1, t1.column2
) tt
where tt.column3 = 1;
Related
I have table1 and table2
table1
nameID column1 column2 column3
Joe 10 10 99
table2
nameID column1 cost
Joe 99 100
Joe 10 100
Joe 30 200
My goal to have column3 from table1 match with column1 from table2.
IF table2 have 2 or more records even if table1 column3 match with table2 column1 (99) and expect nothing output.
IF table 2 have only one record on column1 (99) match with table1 column3 (99) then expect result
nameID column3 cost
Joe 99 100
I have tried
select t1.name, t1.column3
from table1 t1
join table2 t1 on t1.nameID = t2.nameID
where
t1.column3 = t2.column1
and t1.column1 <> t2.column1
and t1.column2 <> t2.column1
Not sure how to make it works. Thank you.
WITH CTE_X
AS (
SELECT count(*) AS countX
FROM table2 t2
)
SELECT table1.nameId
,table1.column3
FROM table1
INNER JOIN table2 ON table1.column3 = table2.column1
JOIN CTE_X ON 1 = 1
WHERE CTE_X.countX = 1
Please see this
I have two tables:
TABLE1 TABLE2
COLUMN1 COLUMN2 COLUMN1 COLUMN2
--------------- ---------------
John 56 45 A
Bob 45 45 B
Eva 68 68 C
Alex 56 47 D
Android 48 45 L
Mum 68 68 C
… … 56 Q
… ...
And I need add column to one table with fact I will insert into new column value from other table using function join
COLUMN1 COLUMN2 COLUMN3
-----------------------
John 56 Q
Bob 45 B
Bob 45 A
Bob 45 L
Alex 56 Q
Eva 68 C
Android 48 NULL
Mum 68 C
… … …
ALTER TABLE [dbo].[Table1]
ADD Column3 NVARCHAR(255);
INSERT INTO [dbo].[Table1] (column3)
SELECT table2.column2
FROM [dbo].[Table2]
LEFT JOIN [dbo].[table1] ON table1.column2 = table2.column1
But I am getting
COLUMN1 COLUMN2 COLUMN3
------------------------
John 56
Bob 45
Eva 68
NULL NULL A
NULL NULL D
NULL NULL C
… … …
Can you help me to fix my insert?
I don't see what left join has to do with this:
UPDATE t1
SET column3 = t2.column2
FROM [dbo].[Table1] t1 JOIN
[dbo].[Table2] t2
ON t1.column2 = t2.column1;
The values that are not found in Table2 will be set to NULL.
EDIT:
Hold on. You are inserting rows as well as values into columns. You seem to want to accomplish this query:
SELECT t1.column1, t1.column2, t2.column3
FROM [dbo].[Table1] t1 LEFT JOIN
[dbo].[Table2] t2
ON t1.column2 = t2.column1;
My recommendation would be to put this into a new table, not Table1:
SELECT t1.column1, t1.column2, t2.column3
INTO [dbo].[Table3]
FROM [dbo].[Table1] t1 LEFT JOIN
[dbo].[Table2] t2
ON t1.column2 = t2.column1;
If you really want to replace the data in Table1, then use an intermediate table:
SELECT t1.column1, t1.column2, t2.column3
INTO #temp
FROM [dbo].[Table1] t1 LEFT JOIN
[dbo].[Table2] t2
ON t1.column2 = t2.column1;
TRUNCATE TABLE dbo.Table1;
INSERT INTO dbo.Table1 (column1, column2, column3)
SELECT column1, column2, column3
FROM #temp;
Seems to me like you need an update, not an insert... Try this instead:
UPDATE t1
SET column3 = t2.column2
FROM dbo.Table2 t2
LEFT JOIN dbo.Table1 t1 ON t1.column2 = t2.column1
I joined two tables
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND t1.col LIKE 'A%'
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND t1.col LIKE 'B%'
How could i intersect these tables so I can have the following output (ids that have both the A% and B%)
join 1
id | col
---┼------
1 | Axxxx
2 | Axxxx
join 2
id | col
---┼-------
1 | Bxxxx
3 | Bxxxx
Final output
id | col
---┼-------
1 | Axxxx
1 | Bxxxx
Thank you
If I understood correctly, and what you want is only the ID's that have both A% and B%, then this is your answer
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND ((t1.col LIKE 'A%' AND t2.col like 'B%')
OR (t1.col LIKE 'B%' AND t2.col like 'A%'))
Based on your sample you don't need intersect but a simple union
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND t1.col LIKE 'A%'
union
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND t1.col LIKE 'B%'
adapt the sintax for union at your db/sql
Also try it this way ... as you are looking for only repeatable IDs ... you could just count them from inner select
select column1, column2 from (
select column1, column2, count(column1) over (partition by column1) [Counted] from (
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND t1.col LIKE 'A%'
UNION
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND t1.col LIKE 'B%'
) src
) src2 where Counted > 1
Please, help me with join results of commands (MS SQL):
SELECT name,value FROM table1 WHERE idfoo1 IN(SELECT _id FROM table3 where id = 1);
SELECT value FROM table2 WHERE idfoo2 IN(SELECT _id_2 FROM table3 where id = 1) AND name='fooname';
And I get:
name value
John 2
Bill 32
Alex 11
value
434
234
144
But I need join results.
name value value
John 2 434
Bill 32 234
Alex 11 144
So, id == id, _id != _id_2,
Use this query:
SELECT t1.name,
t1.value,
t2.value
FROM table1 t1
INNER JOIN table3 t3 ON t1.idfoo1 = t3._id
INNER JOIN table2 t2 ON t2.idfoo2 = t3._id_2
WHERE t3.id=1 AND t2.name = 'fooname'
Select a.name,a.value,c.value FROM table1 as a inner join table3 as b
on a.idfoo1=b.id and b.id=1 inner join table3 as c
on c.idfoo2=b._id_2 and b.id=1 and c.name='fooname'
i guess this is what you need-
SELECT t1.name, t1.value, t2.value
FROM table1 t1, table2 t2, table3 t3
WHERE
t1.idfoo1 = t3._id
AND t2.idfoo2 = t3._id_2
AND t3.id = 1
AND t2.name='fooname';
Tables structure are below :
Table1 (ID int, value1 int,...)
ID Value1
---- --------
1 10
2 20
5 12
Table2 (ID int, value2 int,...)
ID Value2
---- --------
1 13
3 24
4 11
Table3 (ID int, value3 int,...)
ID Value3
---- --------
4 150
5 100
My expected output is below.
ID Value1 Value2 Value3
---- -------- -------- --------
1 10 13 NULL
2 20 NULL NULL
3 NULL 24 NULL
4 NULL 11 150
5 12 NULL 100
It should be noted that above tables is huge and I want to have best performance.
My query suggestion is below :
Select ID,
SUM(Value1) AS Value1,
SUM(Value2) AS Value2,
SUM(Value3) AS Value3
From (
Select ID, Value1 , NULL as value2, NULL as value 3
From Table1
Union ALL
Select ID, NULL , value2, NULL
From Table2
Union ALL
Select ID, NULL, NULL, value 3
From Table3
)Z
Group By Z.ID
Assuming you only have one value per id, this should do the trick:
SELECT aux.ID, t1.Value1, t2.Value2, t3.Value3
FROM
(SELECT ID FROM Table1
UNION
select ID FROM Table2
UNION
SELECT ID FROM Table3) aux
LEFT OUTER JOIN Table1 t1 ON aux.ID = t1.ID
LEFT OUTER JOIN Table2 t2 ON aux.ID = t2.ID
LEFT OUTER JOIN Table3 t3 ON aux.ID = t3.ID
If you've more than one value:
SELECT aux.ID, SUM(t1.Value1) as 'Value1', SUM(t2.Value2) as 'Value2', SUM(t3.Value3) as 'Value3'
FROM
(SELECT ID FROM Table1
UNION
select ID FROM Table2
UNION
SELECT ID FROM Table3) aux
LEFT OUTER JOIN Table1 t1 ON aux.ID = t1.ID
LEFT OUTER JOIN Table2 t2 ON aux.ID = t2.ID
LEFT OUTER JOIN Table3 t3 ON aux.ID = t3.ID
GROUP BY aux.ID
I intially wrote the same answer as aF. did above. So, removed it, and used a different approach.
Here,
1st query get all from table1
2nd query gets all from table2 skipping
those already present in table1 3rd query gets all remaining skipping those in above two query.
SELECT T1.ID, T1.VALUE1, T2.VALUE2, T3.VALUE3 --all T1
FROM TABLE1 T1
LEFT JOIN TABLE2 ON T1.ID=T2.ID
LEFT JOIN TABLE3 ON T1.ID=T3.ID
UNION
SELECT T2.ID, T1.VALUE1, T2.VALUE2, T3.VALUE3 --all T2 where T1 is NULL
FROM TABLE1 T2
LEFT JOIN TABLE1 ON T2.ID=T1.ID
LEFT JOIN TABLE3 ON T2.ID=T3.ID
WHERE T1.ID IS NULL
UNION
SELECT T3.ID, T1.VALUE1, T2.VALUE2, T3.VALUE3 --all T3 where T1 is NULL AND T2 IS NUL
FROM TABLE1 T3
LEFT JOIN TABLE1 ON T3.ID=T1.ID
LEFT JOIN TABLE2 ON T3.ID=T2.ID
WHERE T1.ID IS NULL
AND T2.ID IS NULL