Create unique ID for SQL Server VIew [closed] - sql

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 8 years ago.
Improve this question
Is it possible to create an ID column for a grouped view? For instance I'm trying to also create a unique clustered index for the view, hence I need a unique column that does not contain duplicates.
Much Appreciated!!
Jonathan

There is no 'out-of-the-box' solution however you can workaround by using ROW_NUMBER over columns that do not have unique records.
SELECT ROW_NUMBER() OVER (ORDER BY Col1, Col2), Col1, Col2, ... FROM
(SELECT X AS Col1 FROM [Table]
UNION ALL SELECT Y AS Col2 FROM [Table2])

Related

SQL Adding quantity of another column [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 11 months ago.
Improve this question
I need to compute the sum of a column (B) in another column (C), but adding the next value and changing taking into account another column criteria (A), something like this:
Is this possible with a simple SELECT?
In SQL Server, the window function sum() over() should do the trick.
Note the order by ColB ... This is just a placeholder, I suspect you have another column which would have the proper sequence
Example
Select ColA
,ColB
,ColC = sum(ColB) over (partition by ColA order by ColB rows unbounded preceding )
From YourTable

How can I do 'insert if not exists' in Ms sql server? [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 1 year ago.
Improve this question
I want to move data from table1 to table2 (both the tables have same structure)with condition of comparing their emaild (unquie)in both the tables...if the data of emaild exits in table 2 I should not load that data otherwise I have to insert the record in table 2..these two tables are in two different databases of same sql server instance... Could you please provide me with the syntax.. Thanks in advance
INSERT db2.dbo.table2(key, other cols)
SELECT key, other cols
FROM db1.dbo.table1 AS t1
WHERE NOT EXISTS
(
SELECT 1
FROM db2.dbo.table2 AS t2
WHERE t2.key = t1.key
);
You can double-check you're getting the right rows by commenting out the first line.
Here's an example with a procedure on db<>fiddle.

How to get data from a table with a certain prefix? [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 a table where the name is like "id,name" and I know the id portion and I need to get all the rows from the table by giving that id. The ID is always unique for the table. I've tried selecting the table out from information_schema.tables and I can get the table selected but then can't figure out how to get the data from it.
You're looking to use AS (alias):
SELECT test1.id AS id_test1, t2.id AS id_test2
FROM table1 AS test1
INNER JOIN table2 AS t2 ON (...)

Remove rows with duplicate value for a column in SQL [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 years ago.
Improve this question
From the above table, I want to remove the rows that have the duplicate CODE column. Out of the duplicate rows, the row with the smallest ID_CHECK should be retained.
Output:
If I understand correctly, this should be your SQL query:
Select Names, Code, ID_CHECK
FROM
(
Select Names, Code, ID_CHECK,
ROW_NUMBER() OVER(Partition by Name,Code Order By ID_CHECK) as rnk
FROM table
)tmp
WHERE tmp.rnk=1;
Let me know if this works.

The difference between SELECT * and SELECT A.* [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 5 years ago.
Improve this question
I am new to SQL can someone help me understand why do we use Select .* in sql server ? And why is it different from just select * from table name
In some cases we use statements like select .*, firstname , lastname from employee. Just a bit confused.
.* means, select all columns of a table
SELECT A.*,B.Id from A,B select all columns of A and only Id column of B.