Matching ID if more than one in column then nothing - sql

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

Related

if conditions inside where sql

I'm trying this logic not sure what am missing in this case
select *
from table1 t1
join table2 t2 on t1.column1=t2.column1
where t1.column1 between 1 and 10 if t2.column2='value1'
and t1.column1 between 11 and 20 if t2.column2='value2'
You could use a case when aproach
CREATE tABLE table2(column1 int, column2 varchar(10))
INSERT INTO table2 VALUEs(1,'value1'),(2,'value1'),(13,'value2'),(44,'value2')
CREATE tABLE table1(column1 int)
INSERT INTO table1 VALUES (1),(2),(13),(44)
select *
from table1 t1
join table2 t2 on t1.column1=t2.column1
where
CASE WHEN t1.column1 between 1 and 10 AND t2.column2 like 'value1' THEN TRUE
WHEN t1.column1 between 11 and 20 AND t2.column2 like 'value2' THEN TRUE
ELSE FALSE END
column1 | column1 | column2
------: | ------: | :------
1 | 1 | value1
2 | 2 | value1
13 | 13 | value2
db<>fiddle here
I assume you've left out key details, like the ON for the JOIN and the wildcard for the LIKE, in the interest of simplicity.
select *
from table1 t1
join table2 t2
where (t1.column1 between 1 and 10 and t2.column2 like 'value1')
or (t1.column1 between 11 and 20 and t2.column2 like 'value2')

Aggregate functions as column results from multiple tables

I have the following table structures:
Table1
--------------
Table1Id
Field1
Field2
Table2
------------
Table2Id
Table1Id
Field1
Field2
Table3
-----------
Table3Id
Table1Id
Field1
Field2
I need to be able to select all fields in Table1, count of records in Table2, and count of records in Table3 Where count of records in Table2 > count of records in Table3
Here is an example of expected output with the given data:
Table1 Data
-------------
1 Record1Field1 Record1Feild2
2 Record2Field1 Record2Feild2
3 Record3Field1 Record3Feild2
4 Record4Field1 Record4Feild2
Table2 Data
------------
1 1 Record1Field1 Record1Feild2
2 1 Record2Field1 Record2Feild2
3 2 Record3Field1 Record3Feild2
4 2 Record4Field1 Record4Feild2
5 2 Record5Field1 Record5Feild2
6 4 Record6Field1 Record6Feild2
7 4 Record6Field1 Record6Feild2
8 4 Record6Field1 Record6Feild2
Table3 Data
------------
1 2 Record1Field1 Record1Feild2
2 2 Record2Field1 Record2Feild2
3 3 Record3Field1 Record3Feild2
4 3 Record4Field1 Record4Feild2
5 3 Record5Field1 Record5Feild2
6 4 Record6Field1 Record6Feild2
Desired Results
Table1Id Field1 Field2 Table2Count Table3Count
1 Record1Field1 Record1Field2 2 0
2 Record2Field1 Recird2Field2 3 2
4 Record4Field1 Recird4Field2 3 1
Notice record 3 in Table 1 is not shown because the record count in Table2 is less than the record count in Table3. I was able to make this work using a very ugly query similar to the one below but feel there is a much better way to do this using joins.
SELECT
t1.Table1Id,
t1.Field1,
t1.Field2
(Select Count(Table2Id) From Table2 t2 Where t2.Table1Id = t1.Table1Id) as Table2Count,
(Select Count(Table3Id) From Table3 t3 Where t3.Table1Id = t1.Table1Id) as Table3Count,
From
Table1 t1
Where
(Select Count(Table2Id) From Table2 t2 Where t2.Table1Id = t1.Table1Id) > (Select Count(Table3Id) From Table3 t3 Where t3.Table1Id = t1.Table1Id)
Hard to test it without working examples but something along these lines should be a good starting point.
SELECT
t1.Table1Id,
t1.Field1,
t1.Field2,
COUNT(DISTINCT t2.Table2Id),
COUNT(DISTINCT t3.Table3Id)
From Table1 t1
LEFT OUTER JOIN Table2 t2 ON t1.Table1Id = t2.Table1Id
LEFT OUTER JOIN Table3 t3 ON t1.Table1Id = t3.Table1Id
GROUP BY t1.Table1Id
HAVING COUNT(DISTINCT t2.Table2Id) > COUNT(DISTINCT t3.Table3Id)
You could get all the value in t1 and the data form t2 e t3 for your comparision using a couple of join on grouped values
SELECT
t1.Table1Id
,t1.Field1
,t1.Field2
, tt2.count_t2
, tt3.count_t3
from table1 t1
join (
select Table1Id, count(*) count_t2
From Table2
group by Table1Id
) tt2 on tt2.Table1Id = t1.Table1Id
join (
select Table1Id, count(*) count_t3
From Table3
group by Table1Id
) tt3 on tt3.Table1Id = t1.Table1Id
where tt2.count_t2 < tt3.count_t3 <

SQL Server insert left Join

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

SQL intersect two joined tables

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

display max on one column but display multiple from multiple tables

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;