SQL query to update [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have this table employee with columns
name | salary
-------------
A | 5000
B | 2000
c | 1000
another table
works with columns
name| work
---------
A | w1
A | w2
A | w3
B | w4
B | w5
I want to increase salary of employee by 100 per work and update only if number of works are greater than 1.
Can anyone help me out with this. I need a sql update query for this( no stored proc or trigger or cursor).

Please try using merge statement:
MERGE
INTO employee
USING (
select distinct "name", count(*) over (partition by "name") cnt from works
)x
ON (employee."name" = x."name")
WHEN MATCHED THEN
UPDATE
SET salary = salary+(100*case when cnt=1 then 0 else cnt end);

here is proper query
update employee as a set salary = salary + 100 * NVL((
SELECT count(*)
FROM works as b
where b.name=a.name
group by name
having count(*)>1), 0)

Related

SQL QUERY SQL SSRS [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Hi can anyone help me with sum up first two rows in table and then rest would be same. example is
ID SUM
12 60
0 20
1 30
2 50
3 60
I am expecting
ID SUM
0 80
1 30
2 50
3 60
I am doing this from memory - so if this doesnt work let me know and we can do it another way looking at the row number;
Assuming you have a unique ID to sort it by as you suggested, you could do something like this;
you may want to change the order to be desc if that's how you classify your 'top 2'
SELECT TOP 2 ID,
SUM(VALUE)
FROM [Table]
GROUP BY ID
ORDER BY ID
UNION
SELECT ID,
VALUE
FROM [Table]
WHERE ID NOT IN (SELECT TOP 2 ID
FROM [Table] ORDER BY ID)

OracleSQL, I am stuck with listagg query [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 8 years ago.
Improve this question
I have a list like this:
cities firms
------ -------
["NEWYORK"] 1
["CHICAGO"] 1
["LA"] 1
["DENVER","VIENNA','LONDON'] 2
["TORONTO"] 2
["WASHINGTON",'VIENNA'] 2
I want to replace the list with oracle sql like this:
cities firms
------ -------
NEWYORK,CHICAGO,LA 1
NEWYORK,CHICAGO,LA 1
NEWYORK,CHICAGO,LA 1
DENVER,VIENNA,LONDON,TORONTO,WASHINGTON 2
DENVER,VIENNA,LONDON,TORONTO,WASHINGTON 2
DENVER,VIENNA,LONDON,TORONTO,WASHINGTON 2
Something like that, maybe:
SELECT listagg("city", ',') WITHIN GROUP (ORDER BY "city") "cities",
"firms"
FROM (
SELECT DISTINCT REGEXP_SUBSTR("cities", '[^,]+',1, LEVEL) "city",
"firms"
FROM T
CONNECT BY LEVEL < 5 AND REGEXP_SUBSTR("cities", '[^,]+',1, LEVEL) IS NOT NULL
-- ^
-- :/ Arbitrary maximum depth...
) V
GROUP BY "firms"
Producing:
| CITIES | FIRMS |
|-----------------------------------------|-------|
| CHICAGO,LA,NEWYORK | 1 |
| DENVER,LONDON,TORONTO,VIENNA,WASHINGTON | 2 |
This is rather crude and probably need a lot more improvements. But this should give you some ideas to start from...
I have hard time to understand why this could be helpfull, but if you really need as much as duplicate records as in the original table, a simple JOIN will produce the desired output:
WITH W AS (
the above query
) V
GROUP BY "firms"
)
SELECT W."cities", "firms" FROM W JOIN T USING("firms");
See http://sqlfiddle.com/#!4/403e9/37

Unit record of each type [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
On SQL Server 2008,I need to select the first distinct occurrence of each person in the following table:
ID WeeklyAvg MonthlyAvg
1 8 0
1 7 3
2 9 1
2 6 4
2 6 4
.......................
....................
The output should be:
1 8 0
2 9 1
How do I achieve this?
Even better if I can avoid putting all the 'distinct' columns in the group by clause,just because sql server restricts so.
Appreciate the help.
You can use ROW_NUMBER to get the "first" row:
SELECT ID, WeeklyAvg, MonthlyAvg
FROM
(
SELECT ID, WeeklyAvg, MonthlyAvg,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) RowNum
FROM {table}
) A
WHERE RowNum = 1
Note that the "first" row will be arbitrary unless you specify a particular order.

Having 1 distinct column and a sum value from another column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to have a solution to this question:
I have a datatable here, as shown below
cid amount
1 5
1 10
2 2
3 5
3 7
3 11
Now I need to write a statement that returns a distinct cid, and the sum of the amount column for each cid. The table should look as below:
cid amount
1 15
2 2
3 23
What is the best way to do this? Thanks.
select cid, sum(amount) amount from table group by cid
Try this way:
select cid, sum(amount)
from tab
group by cid
select cid, sum(amount)
from table_name
group by cid;
select cid,sum(amount) from c1 group by cid

Counting frequency of customer [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 8 years ago.
Improve this question
I have this table on sql sever
cstomer |No_Nota
CUS000 | 98342
CUS000 | 98343
CUS000 | 98343
CUS001 | 98355
CUS001 | 98355
I would like to count the frequency of each customer. For similar number of no_nota the value is 1.
I'd like a result like this:
cstomer |Frequent
CUS000 | 2
CUS001 | 1
You want the distinct count of the column no_nota, so that's what you should select...
select customer, count(distinct no_nota) as frequent
from my_table
group by customer
You want to count the result of your select query:
SELECT COUNT(expression)
FROM tables
WHERE predicates;
Example:
SELECT COUNT(No_Nota) FROM your_table WHERE No_Nota > 0;
Sounds like you just want a group by statement to get a count of all individual customers. Something like:
SELECT cstomer, SUM(1) as Frequent FROM table GROUP BY cstomer, No_Nota