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)
Related
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.
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;
It is possible in SQL (ORACLE) to select all entry from a table where an atribute equals an parameter and if not select all the others entries?
like in this example:
COD | Name
1 | Monday
2 | Thursday
3 | Saturday
parameter=3
when cod equals parameter(cod=3) return entry of cod parameter(cod=3) (including cod and name)
else
return all others entries different from parameter(cod=3) (including cod and name) (like 1 Monday and 2 Thursday)
Is it possible with SQL (oracle), or i need something like PLSQL?
I'd use a correlated query and a non-correlated query:
SELECT COD, NAME
FROM TABLE a
WHERE EXISTS (SELECT 1 FROM TABLE b WHERE b.COD = a.COD AND b.COD = 3)
OR NOT EXISTS (SELECT 1 FROM TABLE c WHERE c.COD = 3)
I'm not sure if I'm following your logic, entirely, however.
And, actually, in cases where it's all from one table it can be simplified to just:
SELECT COD, NAME
FROM TABLE a
WHERE a.COD = 3
OR NOT EXISTS (SELECT 1 FROM TABLE c WHERE c.COD = 3)
IF EXISTS(SELECT 1 FROM TABLE WHERE COD=3)
THEN
SELECT COD, NAME FROM TABLE WHERE COD=3
ELSE
SELECT COD, NAME FROM TABLE
END IF
I have a table with only one column about 100 rows of only names. But I need to display the 3 names in a row. So that I will get 34 rows each row with 3 names.
Example:
Name
_____
Raj
Sam
Guru
Tej
Avin
Sami
Fanst
I need to display above data as
Name Name1 Name2
____ _____ ______
Raj Sam Guru
Tej Avin Sami
Fanst
No condition just need to covert single column value into 3 columns data.
Oracle DB
You can do this using conditional aggregation and rownum. Something like this:
select max(case when mod(rn, 3) = 1 then name end) as name1,
max(case when mod(rn, 3) = 2 then name end) as name2,
max(case when mod(rn, 3) = 0 then name end) as name3
from (select name, rownum as rn
from table t
) t
group by trunc((rn - 1) / 3);
You can do it using CASE, make a try on it using PIVOT
Try to PIVOT Your Name column like this
SELECT Name,Name1,Name2
FROM
(SELECT Name FROM table_name) AS SourceTable
PIVOT
(
FOR Name IN (Name,Name1,Name2)
) AS PivotTable;
I have a Table1:
ID Property
1 Name
2 City
3 Designation
and Table2:
ID RecordID Table1ID Value
1 1 1 David
2 1 2 Tokyo
3 2 1 Scott
4 2 3 Manager
The Table1ID of Table2 maps to Table1's ID. Now I wish to show the Table1 Property column values as column headers and have a result set in format like:
RecordID Name City Designation
1 David Tokyo NULL
2 Scott NULL Manager
What is the best/efficient way to achieve this in T-SQL considering that the number of records in Table1 (i.e. the columns in result set) can change and thus should be handled dynamically.
Although I tried PIVOT and CASE based queries, but have been struggling with both of them. :(
Any help/guidance would be appreciated.
Thanks!
Update:
I've been able to create the dynamic query, but one thing which I am still not able to understand is why MAX has been used in the CASE statements. Kindly ignore my noobness.
Use:
SELECT t2.recordid,
MAX(CASE WHEN t1.property = 'Name' THEN t2.value END) AS name,
MAX(CASE WHEN t1.property = 'City' THEN t2.value END) AS city,
MAX(CASE WHEN t1.property = 'Designation' THEN t2.value END) AS designation
FROM TABLE2 t2
JOIN TABLE1 t1 ON t1.id = t2.table1id
GROUP BY t2.recordid
ORDER BY t2.recordid