Stuck in getting calculations of transaction valuesBest way to get identity of inserted row?Add a column with a default value to an existing table in SQL ServerSQL Server: Database stuck in “Restoring” stateWhy is SQL Server 2008 blocking SELECT's on long transaction INSERT's?how to calculate database transaction count per second and database growthGet size of all tables in databaseSelecting max record for each userAdvanced Grouping - Pulling most recent transaction, but maintaining groupingFind Latest Record With Specific ValuesSQL stored procedure query to clear date field if NULL

How to get cool night-vision without lame drawbacks?

Can we put equal sign after aggregate functions in sql?

Is a single radon-daughter atom in air a solid?

Interaction between Leyline of Anticipation and Teferi, Time Raveler

How much will studying magic in an academy cost?

Folding basket - is there such a thing?

Has there been any indication at all that further negotiation between the UK and EU is possible?

Long term BTC investing

Can someone suggest a path to study Mordell-Weil theorem for someone studying on their own?

Why do some games show lights shine thorugh walls?

First-year PhD giving a talk among well-established researchers in the field

Why is the voltage measurement of this circuit different when the switch is on?

Find the C-factor of a vote

Fedora boot screen shows both Fedora logo and Lenovo logo. Why and How?

3D Crossword, Cryptic, Statue View & Maze

Is my Rep in Stack-Exchange Form?

Why change the size when print a object

expiry or manufactured date?

Why do all the teams that I have worked with always finish a sprint without completion of all the stories?

If you snatch, I trade

MRI maximum field strenght

Can ADFS connect to other SSO services?

Why do even high-end cameras often still include normal (non-cross-type) AF sensors?

When to remove insignificant variables?



Stuck in getting calculations of transaction values


Best way to get identity of inserted row?Add a column with a default value to an existing table in SQL ServerSQL Server: Database stuck in “Restoring” stateWhy is SQL Server 2008 blocking SELECT's on long transaction INSERT's?how to calculate database transaction count per second and database growthGet size of all tables in databaseSelecting max record for each userAdvanced Grouping - Pulling most recent transaction, but maintaining groupingFind Latest Record With Specific ValuesSQL stored procedure query to clear date field if NULL






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have an event table and want to do calculations on the basis on most recent transaction values.



create table evt (evnt integer not null,
value integer not null,
time datetime not null,
)


and query:



SELECT evnt ,value
FROM evt where time IN (select max(time) from events group by evnt )
GROUP BY evnt ,value

insert into evt values (2,5,'2018-05-09 12:42:00')
insert into evt values (4,-42,'2018-05-09 13:19:57')
insert into evt values (2,2,'2018-05-09 14:48:30')
insert into evt values (2,7,'2018-05-09 12:54:39')
insert into evt values (3,16,'2018-05-09 13:19:57')
insert into evt values (3,20,'2018-05-09 15:01:09')


Output



evnt | Value
-------------------
2 | -5
3 | 4


enter image description here










share|improve this question
























  • What Calculations you have to do ? And in the Output how did you get value as -5 ?

    – Srikar mogaliraju
    Mar 25 at 9:47











  • For the evnt 2, the latest value is 2 and the second latest value is 7. so the difference between them is -5.

    – swad
    Mar 25 at 9:58

















0















I have an event table and want to do calculations on the basis on most recent transaction values.



create table evt (evnt integer not null,
value integer not null,
time datetime not null,
)


and query:



SELECT evnt ,value
FROM evt where time IN (select max(time) from events group by evnt )
GROUP BY evnt ,value

insert into evt values (2,5,'2018-05-09 12:42:00')
insert into evt values (4,-42,'2018-05-09 13:19:57')
insert into evt values (2,2,'2018-05-09 14:48:30')
insert into evt values (2,7,'2018-05-09 12:54:39')
insert into evt values (3,16,'2018-05-09 13:19:57')
insert into evt values (3,20,'2018-05-09 15:01:09')


Output



evnt | Value
-------------------
2 | -5
3 | 4


enter image description here










share|improve this question
























  • What Calculations you have to do ? And in the Output how did you get value as -5 ?

    – Srikar mogaliraju
    Mar 25 at 9:47











  • For the evnt 2, the latest value is 2 and the second latest value is 7. so the difference between them is -5.

    – swad
    Mar 25 at 9:58













0












0








0








I have an event table and want to do calculations on the basis on most recent transaction values.



create table evt (evnt integer not null,
value integer not null,
time datetime not null,
)


and query:



SELECT evnt ,value
FROM evt where time IN (select max(time) from events group by evnt )
GROUP BY evnt ,value

insert into evt values (2,5,'2018-05-09 12:42:00')
insert into evt values (4,-42,'2018-05-09 13:19:57')
insert into evt values (2,2,'2018-05-09 14:48:30')
insert into evt values (2,7,'2018-05-09 12:54:39')
insert into evt values (3,16,'2018-05-09 13:19:57')
insert into evt values (3,20,'2018-05-09 15:01:09')


Output



evnt | Value
-------------------
2 | -5
3 | 4


enter image description here










share|improve this question
















I have an event table and want to do calculations on the basis on most recent transaction values.



create table evt (evnt integer not null,
value integer not null,
time datetime not null,
)


and query:



SELECT evnt ,value
FROM evt where time IN (select max(time) from events group by evnt )
GROUP BY evnt ,value

insert into evt values (2,5,'2018-05-09 12:42:00')
insert into evt values (4,-42,'2018-05-09 13:19:57')
insert into evt values (2,2,'2018-05-09 14:48:30')
insert into evt values (2,7,'2018-05-09 12:54:39')
insert into evt values (3,16,'2018-05-09 13:19:57')
insert into evt values (3,20,'2018-05-09 15:01:09')


Output



evnt | Value
-------------------
2 | -5
3 | 4


enter image description here







sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 9:46









StepUp

9,1409 gold badges45 silver badges80 bronze badges




9,1409 gold badges45 silver badges80 bronze badges










asked Mar 25 at 9:35









swadswad

277 bronze badges




277 bronze badges












  • What Calculations you have to do ? And in the Output how did you get value as -5 ?

    – Srikar mogaliraju
    Mar 25 at 9:47











  • For the evnt 2, the latest value is 2 and the second latest value is 7. so the difference between them is -5.

    – swad
    Mar 25 at 9:58

















  • What Calculations you have to do ? And in the Output how did you get value as -5 ?

    – Srikar mogaliraju
    Mar 25 at 9:47











  • For the evnt 2, the latest value is 2 and the second latest value is 7. so the difference between them is -5.

    – swad
    Mar 25 at 9:58
















What Calculations you have to do ? And in the Output how did you get value as -5 ?

– Srikar mogaliraju
Mar 25 at 9:47





What Calculations you have to do ? And in the Output how did you get value as -5 ?

– Srikar mogaliraju
Mar 25 at 9:47













For the evnt 2, the latest value is 2 and the second latest value is 7. so the difference between them is -5.

– swad
Mar 25 at 9:58





For the evnt 2, the latest value is 2 and the second latest value is 7. so the difference between them is -5.

– swad
Mar 25 at 9:58












1 Answer
1






active

oldest

votes


















0














Based on your brief description, i guess this is what you wanted



select evnt, sum(case when rn = 1 then value else -value end)
from
(
select *, rn = row_number() over (partition by evnt order by time desc)
from evt
) d
where d.rn <= 2
group by evnt
having count(*) = 2





share|improve this answer























  • Can we achieve it without row_number()?

    – swad
    Mar 25 at 10:04











  • any particular reason you want to avoid row_number ?

    – Squirrel
    Mar 25 at 10:04











  • these are not allowed in my task..

    – swad
    Mar 25 at 10:06











  • so you are allow to use ?

    – Squirrel
    Mar 25 at 10:21











  • Can't use row_number() .

    – swad
    Mar 25 at 11:14













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55334830%2fstuck-in-getting-calculations-of-transaction-values%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Based on your brief description, i guess this is what you wanted



select evnt, sum(case when rn = 1 then value else -value end)
from
(
select *, rn = row_number() over (partition by evnt order by time desc)
from evt
) d
where d.rn <= 2
group by evnt
having count(*) = 2





share|improve this answer























  • Can we achieve it without row_number()?

    – swad
    Mar 25 at 10:04











  • any particular reason you want to avoid row_number ?

    – Squirrel
    Mar 25 at 10:04











  • these are not allowed in my task..

    – swad
    Mar 25 at 10:06











  • so you are allow to use ?

    – Squirrel
    Mar 25 at 10:21











  • Can't use row_number() .

    – swad
    Mar 25 at 11:14















0














Based on your brief description, i guess this is what you wanted



select evnt, sum(case when rn = 1 then value else -value end)
from
(
select *, rn = row_number() over (partition by evnt order by time desc)
from evt
) d
where d.rn <= 2
group by evnt
having count(*) = 2





share|improve this answer























  • Can we achieve it without row_number()?

    – swad
    Mar 25 at 10:04











  • any particular reason you want to avoid row_number ?

    – Squirrel
    Mar 25 at 10:04











  • these are not allowed in my task..

    – swad
    Mar 25 at 10:06











  • so you are allow to use ?

    – Squirrel
    Mar 25 at 10:21











  • Can't use row_number() .

    – swad
    Mar 25 at 11:14













0












0








0







Based on your brief description, i guess this is what you wanted



select evnt, sum(case when rn = 1 then value else -value end)
from
(
select *, rn = row_number() over (partition by evnt order by time desc)
from evt
) d
where d.rn <= 2
group by evnt
having count(*) = 2





share|improve this answer













Based on your brief description, i guess this is what you wanted



select evnt, sum(case when rn = 1 then value else -value end)
from
(
select *, rn = row_number() over (partition by evnt order by time desc)
from evt
) d
where d.rn <= 2
group by evnt
having count(*) = 2






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 25 at 9:58









SquirrelSquirrel

12.8k2 gold badges23 silver badges28 bronze badges




12.8k2 gold badges23 silver badges28 bronze badges












  • Can we achieve it without row_number()?

    – swad
    Mar 25 at 10:04











  • any particular reason you want to avoid row_number ?

    – Squirrel
    Mar 25 at 10:04











  • these are not allowed in my task..

    – swad
    Mar 25 at 10:06











  • so you are allow to use ?

    – Squirrel
    Mar 25 at 10:21











  • Can't use row_number() .

    – swad
    Mar 25 at 11:14

















  • Can we achieve it without row_number()?

    – swad
    Mar 25 at 10:04











  • any particular reason you want to avoid row_number ?

    – Squirrel
    Mar 25 at 10:04











  • these are not allowed in my task..

    – swad
    Mar 25 at 10:06











  • so you are allow to use ?

    – Squirrel
    Mar 25 at 10:21











  • Can't use row_number() .

    – swad
    Mar 25 at 11:14
















Can we achieve it without row_number()?

– swad
Mar 25 at 10:04





Can we achieve it without row_number()?

– swad
Mar 25 at 10:04













any particular reason you want to avoid row_number ?

– Squirrel
Mar 25 at 10:04





any particular reason you want to avoid row_number ?

– Squirrel
Mar 25 at 10:04













these are not allowed in my task..

– swad
Mar 25 at 10:06





these are not allowed in my task..

– swad
Mar 25 at 10:06













so you are allow to use ?

– Squirrel
Mar 25 at 10:21





so you are allow to use ?

– Squirrel
Mar 25 at 10:21













Can't use row_number() .

– swad
Mar 25 at 11:14





Can't use row_number() .

– swad
Mar 25 at 11:14



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55334830%2fstuck-in-getting-calculations-of-transaction-values%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현