T-SQL to search ID in a table [closed] - sql-server

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a mapping table below and I want to check either ID exists in the corresponding Table or not. For example: check ID 001 in "xxabc" database table and return "Yes" if table has 001 ID present else "No".
ID TableName
--------------------------
001 xxabc
003 xxabc
004 xxpqr
009 xxghi

Try this:
SELECT S.[ID]
,CASE WHEN M.[ID] IS NULL THEN 'No' ELSE 'Yes' END
--,IIF(M.[ID] IS NULL, 'No', 'Yes') -- for SQL Server 2012+
FROM [source_table] S
LEFT JOIN [mapping_table] M
ON S.[ID] = M.[ID]

Related

Update table with average [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I have a problem with my query:
UPDATE [DIM_Category]
SET Category 3 = (SELECT AVG(Category 2)
FROM [DIM_Category] GROUP BY Category)
I want to do like in this exemple and I don't know how.
Category
Category 2
Category 3
OCS
10800
20350
OCS
29600
20350
Netflix
24000
23000
Netflix
22000
23000
As much as I dislike the concept you are doing here the query itself needs to be correlated. One way you can do that easily is with a cte. This will work on the example you posted. I included my code to create a table so I had something work with. You would obviously not need that part. ;)
create table DIM_Category
(
Category varchar(20)
, Category2 int
, Category3 int
);
insert DIM_Category
(
Category
, Category2
) values
('OCS', 10800)
,('OCS', 29600)
,('Netflix', 24000)
,('Netflix', 22000);
with CatAvg as
(
select Category
, CatAverage = AVG(Category2)
from DIM_Category
group by Category
)
update c
set Category3 = ca.CatAverage
from DIM_Category c
join CatAvg ca on ca.Category = c.Category;
select *
from DIM_Category;

update column of table 1 according to the similar column of table 2, Sqlite3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two tables in sqlite. Let's say table_1 and table_2. table_1 has two columns named(Barcode, Price) , similarly table_2 has two columns named(Barcodes, Price). I want to update table_1.Price. if table_1.Barcode is present in table_2. Barcodes, then take the price of table_2. Price otherwise keep it table_1.Price.
I hope I am very clear. Let me just write it down in a programmatic sense to be very clear.
if(table_1.Barcode == table_2.Barcodes)
table_1.Price = table_2.Price
else
table_1.Price = table_1.Price
I need sqlite query which does this. Is it possible?
If your version of SQLite is 3.33.0+ you can do it with UPDATE...FROM syntax:
UPDATE table_1 AS t1
SET Price = t2.Price
FROM table_2 t2
WHERE t2.Barcode = t1.Barcode;
For older versions of SQLite you need a corelated subquery which returns the price from table_2:
UPDATE table_1
SET Price = COALESCE(
(SELECT t2.Price FROM table_2 t2 WHERE t2.Barcode = table_1.Barcode),
Price
);

SQL Server: display merge of records by user in one row with condition [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have the following table and need the following result with join based on username
Query
select
iif(count(*)>1, min(ID), max(right([User], 1))) ID,
[User],
iif(count(*)>1, concat('Total: (', cast(count(*) as varchar(12)), ')'), max(replace([Text], 'Text', 'Texto'))) [Text]
from
MyTable
group by
[User]
order by 1;
Results
ID User Text
1 User01 Total: (3)
2 User2 Texto 4
3 User3 Texto 5
6 User04 Total: (2)

SQL Server query for finding some latest records from each ID [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table like this..
ID City
-----------------
1 Jakarta
1 Bogor
2 Bandung
2 Surabaya
3 Solo
3 Null
I want the query would returns:
ID City
---------------
1 Bogor
2 Surabaya
3 Null
Any ideas? I need your help.. I used some methods such as rank , distinct, max.. but the result is not what I expected..
You can use a CTE with the ROW_NUMBER-function:
WITH CTE AS
(
SELECT Id, City, Rn = ROW_NUMBER() OVER (Partition By ID
Order by Id DESC)
FROM dbo.Cities
)
SELECT Id, City
FROM CTE
WHERE RN = 1
Altough i don't know which column you want to use to determine the "latest" record. Use that in Order by Id DESC.

how to select content in database? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I would like ask if this is possible in any database. Lets say there's one column on my database that has a content like this:
-----------------
|columname |
-----------------
| roan,joan,keith|
------------------
If I do a select query, can I just display roan from that field?
Thanks,
MSSQL version of Mark answer:
SELECT SUBSTRING(columnname, 1, CHARINDEX(',', columnname, 1) - 1) FROM testtable;
Oracle solution:
select substr(columnname,1,instr(columnname,',')-1) from testtable;

Resources