Sum column in last row [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 5 years ago.
Improve this question
I need to get the sum of the column Value in column Total grouped by Year, Month and Day, but the sum must be in the "last" row of the group
Year Month Day Value Total
2017 5 10 10 0
2017 5 21 5 0
2017 5 28 3 18
2017 6 15 8 8
2017 7 14 9 0
2017 7 18 2 11
How can I do this ?

You seem to want the total on the last record for each month. You can do this using ANSI standard window functions:
select t.*,
(case when row_number() over (partition by year, month order by day desc) = 1
then sum(value) over (partition by year, month)
else 0
end) as total
from t;

Related

how to get diff between subsequent rows without lag [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
id value
1 12
1 15
1 17
1 22
1 22
1 23
And I need like this
id value
1 --
1 3
1 2
1 5
1 0
1 1
Could you tell me, how to achive this?
You can try the below -
select id,
value-max(value) over(order by id rows between 1 preceding and 1 preceding) as value
from tablename
You seem to want lag(), which I'm guessing is per id and based on the ordering of value:
select t.*,
(value - lag(value) over (partition by id order by value)) as diff
from t
order by value;
That said, your sample data has exact duplicates. That is unusual in a SQL table.

how do sum every Column in Sql Sevrer? [duplicate]

This question already has answers here:
Calculate a Running Total in SQL Server
(15 answers)
Closed 4 years ago.
Every record amount sum into Total column
id Amount total
--------------------------------------------
1 100 100
1 50 150
1 60 210
1 10 220
2 70 290
2 10 300
Any suggestion would be very much appreciated.
You want window function :
select t.*,
sum(amount) over (order by id rows between unbounded preceding and current row) as total
from table t;

How to fill a new row in oracle with previous row values [duplicate]

This question already has answers here:
lag to get first non null value since the previous null value
(3 answers)
Closed 4 years ago.
I have a table A with below values
year quarter price source
2017 1 10 s
2017 3 12 t
2018 1 10 m
2018 2 15 r
I would like to see the table as below. Yes, I would like to fill the non specified quarters with previous quarter values.
I thought of creating a temp table B with all quarters and do a join with this table A but how can I fill the price and source fields from previous quarter.
year quarter price source
2017 1 10 s
2017 2 10 s
2017 3 12 t
2017 4 12 t
2018 1 10 m
2018 2 15 r
For this, oracle has the LAG window function, which allows you to access previous rows. I think, it is anyways not required to fill the data to the next row as you can easily gather it when you select it.
E.g.
SELECT year,
quarter,
price,
source,
price,
LAG(price, 1, 0) OVER (ORDER BY year, quarter) AS price_previous FROM A;

Postgres : get min and max rows count in many to many relation table [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 6 years ago.
Improve this question
I have mapping table for RFQ(request for quotation) and Vendor's bid amount.
rfq_vendor_mapping :
id rfq_id(FK) vendor_id(FK) amount
---------------------------------------
1 1 1 100
2 1 2 50
3 2 1 200
4 2 3 300
5 2 2 40
6 3 4 70
7 3 1 90
8 3 2 250
9 4 3 30
10 5 1 500
In above table, I want analysis for how many times vendor has submitted minimum and maximum bid for each RFQ.
Expected Output :
vendor_id min_bid_count max_bid_count
-----------------------------------------
1 1 2
2 2 1
3 1 2
4 1 0
http://sqlfiddle.com/#!15/60198/1
Compare the vendor's amount with min and max from a window function and run a conditional count on outer query level:
SELECT vendor_id
, count(min_bid OR NULL) AS min_bid_count
, count(max_bid OR NULL) AS max_bid_count
FROM (
SELECT vendor_id
, amount = min(amount) OVER w AS min_bid
, amount = max(amount) OVER w AS max_bid
FROM rfq_vendor_mapping
WINDOW w AS (PARTITION BY rfq_id)
) sub
GROUP BY 1
ORDER BY 1;
SQL Fiddle.

How to arrange sequence range in group by [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
Hi all
i am facing a problem and need a query for that ,
I have a table and data like
----------
seq_id run_id mark_Flag
1 10 A
2 11 A
3 12 A
4 13 Z
5 14 A
6 15 A
7 16 Z
8 17 Z
9 18 A
10 19 A
11 20 Z
----------
Now i required the output like
seq runidFrom runidTo mark_Flag
1 10 12 A
2 13 13 Z
3 14 15 A
4 16 17 Z
5 18 19 A
6 20 20 Z
Thanks in advance ....
Try this query the main idea is to group by count of previous Mark_flag not equal to current
SELECT min(run_id),max(run_id),max(mark_Flag)
FROM T as T1
GROUP BY mark_flag,
(
SELECT COUNT(*) FROM T
WHERE seq_id<T1.seq_id
and
mark_flag<>T1.Mark_flag
)
ORDER BY MIN(seq_id)
SQLFiddle demo
This query have to work on any database system but when you post a question please add a tag with your RDBMS (not just SQL) so a query can be optimized for your data base system.
UPD: Here is the MS SQL version:
SELECT min(run_id),max(run_id),max(mark_Flag)
FROM
(
Select run_id, mark_flag,seq_id,
(
SELECT COUNT(*) FROM T
WHERE seq_id<T1.seq_id
and mark_flag<>T1.Mark_flag
) as group_id
From t as T1
) as T2
GROUP BY mark_flag,group_id
ORDER BY MIN(seq_id)
SQLFiddle demo