Replace for-loop in SQL - sql

I have a table in a database. For example table of user IDs and right IDs:
UserId RightId
---------------
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
4 1
4 2
5 1
6 1
6 2
What is the best way to insert for each userId new rightId 4?
I heard that using while or for loops is not the best way to do such thing.
Can you please show me an example how to solve such problem with JOINs and SETs for example?

How about something like
INSERT INTO MyTable (UserID, RightID)
SELECT DISTINCT
UserID,
4
FROM MyTable
SQL Fiddle DEMO

If you simply want to change every entry in the RightId column you could try something like this:
UPDATE <table_name> SET RightId=4;

Related

SQL How to SUM rows in second column if first column contain

View of a table
ID
kWh
1
3
1
10
1
8
1
11
2
12
2
4
2
7
2
8
3
3
3
4
3
5
I want to recive
ID
kWh
1
32
2
31
3
12
The table itself is more complex and larger. But the point is this. How can this be done? And I can't know in advance the ID numbers of the first column.
SELECT T.ID,SUM(T.KWH)SUM_KWH
FROM YOUR_TABLE T
GROUP BY T.ID
Do you need this one?
Let's assume your database name is 'testdb' and table name is 'table1'.
SELECT * FROM testdb.table1;
SELECT id, SUM(kwh) AS "kwh2"
FROM stack.table1
WHERE id = 1
keep running the query will all (ids). you will get output.
By following this query you will get desired output.
Hope this helps.

distinct value row from the table in SQL

There is a table with values as below,
Id Value
1 1
2 1
3 2
4 2
5 3
6 4
7 4
now need to write a query to retrieve value from the table and output should look as
ID Value
1 1
3 2
5 3
6 4
any suggestion ?
The query you want is nothing to do with being distinct, it's a simple aggregation of value with the minimum ID for each:
select Min(id) Id, value
from table
group by value

Combining values from one column by the key value from another column

I need to combine all values by one column depends on the key from another column. Can someone help me to get out of this problem please?
here is the short example of my problem.
CUST_ID CUST_REL_ID
100 1
100 2
100 3
100 4
200 5
200 6
200 7
CUST_ID CUST_REL_ID
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
...
5 5
5 6
5 7
I think you just want a self-join:
select t1.cust_rel_id, t2.cust_rel_id
from t t1 join
t t2
on t1.cust_id = t2.cust_id
order by t1.cust_rel_id, t2.cust_rel_id;
I don't understand your naming conventions. The column called cust_id in the result set looks nothing like the column called cust_id in the source data. But this appears to be what you want to do.

PSQL get duplicate row

I have table like this-
id object_id product_id
1 1 1
2 1 1
4 2 2
6 3 2
7 3 2
8 1 2
9 1 1
I want to delete all rows except these-
1 1 1
4 2 2
6 3 2
9 1 2
Basically there are duplicates and I want to remove them but keep one copy intact.
what would be the most efficient way for this?
If this is a one-off then you can simply identify the records you want to keep like so:
SELECT MIN(id) AS id
FROM yourtable
GROUP BY object_id, product_id;
You want to check that this works before you do the next thing and actually throw records out. To actually delete those duplicate records you do:
DELETE FROM yourtable WHERE id NOT IN (
SELECT MIN(id) AS id
FROM yourtable
GROUP BY object_id, product_id
);
The MIN(id) obviously always returns the record with the lowest id for a set of (object_id, product_id). Change as desired.

Rows in columns

I have this table:
Id Kind
1 MODEL
1 MOTOR
2 MODEL
2 MOTOR
3 MOTOR
4 MODEL
And I want to insert into anothe table:
IdModel IdMotor
1 1
1 2
1 3
2 1
2 2
2 3
4 1
4 2
4 3
I know how to do it with cursors, but it's indeed very slow. I've tried with union but it looks like today is not my best day!
I also know this can be done in SQL 2005 with pivot, but I have to do it with SQL Server 2000.
Any Transact-SQL guru out there with a good and quick query?
Thanks in advance!
Looks like this will work:
INSERT Table2
SELECT model.id, motor.id
FROM
Table model,
Table motor
WHERE
model.Kind = 'MODEL'
and motor.Kind = 'MOTOR'
INSERT INTO AnotherTable
SELECT [IdModel]
, [IdMotor]
FROM (
SELECT [IdModel] = ID
FROM ATable
WHERE Kind = 'MODEL'
) md
CROSS APPLY
(
SELECT [IdMotor] = ID
FROM ATable
WHERE Kind = 'MOTOR'
) mt