How I can apply Inner Join to filter the data in Sql Server - sql

I have table in which I inserted all the liked places records.
Like:
I have table PlaceLikes;
Id placeId likedByUserID
1 ABC 1
2 DEF 1
3 ABC 2
4 FFF 2
Result: User 1 want to get all placeID that matches with itself.
Id placeId likedByUserID
3 ABC 2
Here User 2 with ABC placeId is similar with Requestor User ID 1.
So, How I can filter the data like this

You can use exists:
select t.*
from mytable t
where
t.likedByUserID <> 1
and exists (
select 1 from mytable t1 where t1.place_id = t.place_id and t1.likedByUserID = 1
)

I think this piece of query will be able to solve your problem statement as understood by me.
DECLARE #sample_name VARCHAR (10);
SET #sample_name = 'placeId to be searched'
SELECT Id, placeId, likedByUserID FROM demo_table dt
WHERE dt.placeId = #sample_name
Do let me know if this was helpful.

Related

SQL UPDATE on the same table with WHERE

I have the following table and I want to fill up the empty values of the name column with the same value of the name where id_lang=2.
Any idea of what the sql query should be?
id_product
id_lang
name
1
1
-
1
2
name1
2
1
-
2
2
name2
3
1
-
3
2
name3
4
1
-
4
2
name4
One general approach which should work uses a correlated subquery:
UPDATE yourTable t1
SET name = (SELECT name FROM yourTable t2
WHERE t2.id_product = t1.id_product AND t2.id_lang = 2)
WHERE
name IS NULL;
In a query, you can simply use window function:
select t.*,
coalesce(name,
max(case when id_lang = 2 then name end) over (partition by id_product)
) as imputed_name
from t;
Note: This assumes that - really means NULL. If it is a string, the above can be tweaked to use CASE.
You can easily do this in an update as well, if you want to change the data in the table. However, the best way to do that depends on the database.
You can use a subquery: UPDATE table SET name = (SELECT DISTINCT name FROM table WHERE id_lang = 2)

Return List of Records where 1 ID is linked to 2 different PKs

First of all, apologies for the title - wasn't sure how to word it.
Essentially I have this data:
ID
RuleID
AccountID
1
1234
ABC001
2
5678
DEF001
3
1234
GHI001
4
1234
ABC001
I want to be able to return all rows where the RuleID is matched to 2 distinct AccountID's, with the data in this table, a single RuleID should only be linked to a single AccountID - if it's shared then there's a problem.
I have tried using code such as this:
SELECT *
FROM #rules
GROUP BY RuleID, AccountID
HAVING COUNT(*) > 1
and trying to change it to work with matching how I want, however, I can't get it to work.
Thank you.
You can try EXISTS as follows:
SELECT * FROM #rules r
where exists
(select 1 from #rules rr
where rr.RuleID = r.RuleID and rr.AccountID <> r.AccountID);
SELECT RuleID
FROM #rules
GROUP BY RuleID
HAVING COUNT(distinct AccountID) > 1
or
SELECT RuleID
FROM #rules
GROUP BY RuleID
HAVING min(AccountID) != max(AccountID)

SQL Server : update multiple rows one by one while incrementing id

I am pretty new to SQL and I thought I was comfortable using it after a while but it still is tough. I am trying to increment ids. I know I could use auto-increment but in this case there are id has relationship with several categories so it has to start with different numbers so I can't do it.
The table looks something like this:
id category
----------------
1000 1
1000 1
...
2000 2
2000 2
...
And I want to make it:
id category
------------------
1000 1
1001 1
1002 1
...
2000 2
2001 2
...
I tried:
UPDATE T1
SET id = CASE
WHEN EXISTS (SELECT id FROM STYLE WHERE T1.id = id)
THEN (SELECT MAX(CAST(id AS INT)) + 1
FROM STYLE
WHERE category = T1.category)
END
FROM STYLE T1
WHERE idStyle = idStyle
But it just added 1 to all rows. How could I go 1 by 1 so it could actually get the incremented max id? Thank you.
In the absense of real sample data, this is a pseudo-sql, however, something like...
UPDATE YT
----SELECT NULL as Ihave no context of other fields in your table
SET id = id + ROW_NUMBER() OVER (PARTITION BY category ORDER BY (SELECT NULL)) - 1
FROM YourTable YT;
You can use row_number() function instead :
select *,
concat(cid, row_number() over (partition by id order by category)-1) as NewId
from style s;

SQL Select from 1 table rows with 2 specific column value that are not equal

I have a table
id number name update_date
1 123 asd 08.05.18
2 412 ddd 08.05.18
3 123 dsa 14.05.18
4 125 dsa 05.05.18
Whole table consist from that rows like that. I need to select row 1 and 3 because I need different update_dates but same number. How to do that? I need to see the changes from specific Number between 2 update dates 08.05.18 and 14.05.18. I have more update dates in my table.
I tried:
SELECT *
FROM legal_entity_history a
JOIN legal_entity_history b ON a.BIN = b.BIN
WHERE ( a.update_date <> b.update_date AND
a.update_date = "08.05.18" AND
b.update_date = "14.05.18" )
A relatively simple method is:
select leh.*
from legal_entity_history leh
where exists (select 1
from legal_entity_history leh2
where leh2.number = leh.number and leh2.update_date <> leh.update_date
);
For performance, you want an index on legal_entity_history(number, update_date).
TRY THIS: Assuming that same number may not appear more than once under same update_date, so, you can achieve that using GROUP BY with HAVING as below
SELECT t.*
FROM test t
INNER JOIN (SELECT number
FROM test
GROUP BY number
HAVING COUNT(DISTINCT update_date) > 1) t1 ON t1.number = t.number
OUTPUT:
id number name update_date
1 123 asd 08.05.18
3 123 dsa 14.05.18

select distinct records where multiple rows exist for one ID based on values in another column

So I'm not exactly sure if my title is correct or misleading. It sounds simple, but I can't figure it out and haven't found a good example.
I want to select the distinct ID's from a table where the ID does not match to a certain code. For instance I have tableA as below:
tableA
ID Code
==== ====
1 AAA
1 BBB
1 CCC
2 AAA
2 DDD
2 EEE
3 BBB
3 GGG
3 HHH
The only result I would like to return is ID 3 since ID 1 and ID 2 match to code 'AAA'.
I've tried:
SELECT disctinct(ID) from tableA where code <> 'AAA'
but this returns ID 1, 2, and 3. I'm not sure if a group by would work either because I don't even want ID 1 and 2 to be returned.
Try using NOT IN:
SELECT ID
FROM TableA
WHERE ID NOT IN(SELECT ID
FROM TableA
WHERE CODE='AAA')
IN determines whether a specified value matches any value in a subquery or a list. Read more here.
Explanation:
Inner query selects all IDs which as CODE=AAA. The outer query will select all IDs which are not in the result return by inner query.
i.e., With the given data, inner query will return (1,2). Outer query will select ID which are not in (1,2) which is ofcourse 3.
This returns all rows for a given id:
select *
from tab as t1
where not exists
(select * from tab as t2
where t1.id = t2.id
and code = 'AAA')
And this just the ids without 'AAA':
select id
from tab
group by id
having count(case when code = 'AAA' then 1 end) = 0