Sum of row Depending on data in sql server [closed] - sql-server

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 table which store Customerid, amount and vouchartype this is a transaction table which store multiple entry for a single customer. vouchartype is 'D' for debit and 'C' for Credit and all amount have positive value. Now I want to sum of Individual customer debit value should be subtract from Credit value.
Please help me

Here is one solution. Everything other than 'C' is considered positive. You might need to reverse the sign depending on what you need.
SELECT
Customerid,
sum(case when vouchartype = 'C' THEN -amount ELSE amount END) As AMount
FROM YOurTable
GROUP BY Customerid

Related

Row numbers for consecutive dates for a given activity [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 days ago.
Improve this question
I need to return row numbers for consecutive dates for each activity.
enter image description here
I have tried row_number() over(partition by) but cannot seem to get the right row numbers against the consecutive dates for the activity type. This is what I am trying to return:
enter image description here

How to sort an array in a way that if a condition is met the contents get sorted first? [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 last month.
Improve this question
I need to sort an array of orders so that the ones already on delivery get put at the end of the list so to put the unfinished orders on the top.
The variable state is what i wanted to sort on. If the variable is 0 the contents should have a higher priority than where the state is 1.
Here is a simplification of what i need:
input array:
{{state=1,...},{state=0,...},{state=0,...},{state=0,...},{state=1,...}}
=>
output array:
{{state=0,...},{state=0,...},{state=0,...},{state=1,...},{state=1,...}}
I tried table.sort and some other methods but it didn't fit what i needed or didn't do what i wanted it to do.
table.sort(arr, function(a, b) return a.state < b.state end) works fine to sort arr in non-descending order based on the state field of each entry. How did you try to use table.sort?

Create a random number between the max and min ID [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 need to create three random id between the max ID of the table and the min ID of the table. How can I do that in Sql?
You need to use two variables to store Min and Max primary keys. let's say #a and #b.
then using below query you can get your random number
declare #a int,#b int
select #b=max(id),#a=min(id)
from mytbl
SELECT FLOOR(RAND()*(#b-#a)+1)

How to convert this MS SQL numeric value to a date [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
Should be an easy convert, but cast and convert are failing me.
The value I am trying to convert is: 1509180600
Other examples are 1572256200, 5150938920
I have a very large number of records and many fields where dates have been stored like this. The system is being replaced, which means thankfully, this problem will go. But need to know what the values are before wiping them!
This is probably a Unix timestamp. You can convert it by adding seconds to 1970-01-01:
select dateadd(second, col, '1970-01-01')
. . .

How to calculate birth year in SQL Server [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 7 years ago.
Improve this question
In TSQL, how can the year of birth be calculated?
I have tried datediff options but to no avail.
Thanks in advance for any thoughts.
If you only have age, you have already lost information. you can never know for sure the year of birth. This will approximate the YOB
dateadd(yy,-Age,CURRENT_TIMESTAMP)
You could slightly improve the accuracy of this guess by trying to adjust for the probability of whether the individual has had a birthday this year.
dateadd(yy,case when month(current_timestamp) <= 6 then 0 else 1 end-Age,CURRENT_TIMESTAMP)
You will want to substitute CURRENT_TIMESTAMP for the time that the age metric was acquired if you are retro-applying this to an existing dataset (assuming that data is available).
Above all, I would recommend changing your process to capture date of birth upfront if possible.

Resources