SQL- how to sum values from table between value to value? - sql

I want to sum rows in select from value to value.
for example:
table T:
value: type:
1 5
2 10
2 8
3 10
3 2
I want to sum the values from the last type 10 (the last two "3" values)
do you have an idea how to do it?
thanks in advance!

Select value,sum(type) as SumofType
From T
Group by value

I think you want this SQL.
Select value,sum(type) as Sum_Type
From Ta where value=3
Group by value

Related

SQL QUERY HELP — Count the frequency of associated values given an input value

[HELP]
Hi everyone,
I have this table with the following columns:
id
textID
value
1
1
Hedonic
2
1
Negative feelings
3
2
Hedonic
4
2
Negative feelings
5
2
Fun
6
2
Motivation
7
3
Enjoy
8
3
Hedonic
9
3
Fun
10
4
Negative feelings
11
4
Pain
And what I want to do is return the frequency of values associated with a given input value.
For example, given the value 'Hedonic', I want to return the count of values associated where 'hedonic' exists.
With the data from the table presented above, it should return the following table, for input 'Hedonic':
value
count
Hedonic
3
Negative feelings
2
Fun
2
Enjoy
1
Motivation
1
Can anyone help me?
I have no idea how to perform the SQL query... I've tried several approaches, but still no success.
Thank you very much
One method uses exists:
select value, count(*)
from t
where exists (select 1
from t t2
where t2.textid = t.textid and t2.value = 'Hedonic'
)
group by value;

How to do the sum for each row of the values of a column?

I have a SQL table containing a column of list of int, like this
Index Column to sum
1 1:5:13:3:6:7:11:2:4:1:2:5
2 1:7:2:1:1
3 19:05:05
4 2:1:1:5:1:4:64:177:86:75:2:83:2:57:1
5 43
6 1:1:1:3:10:6:1
7 2:11:4:3:1
8 1:5:2:3:34:2:2
9 4:3:1:2
10 4:1:1:4
I would like to calcul in SQL the sum for each row of the Column to sum value.
It would give something like this:
1 60
2 12
...
10 10
Any idea?
Thanks
Your first effort should go into fixing your data model, as commented by Gordon Linoff. The numbers should be stored in a separate table, with each value on a separate row.
In Postgres, you can split the values to rows using regexp_split_to_table(), then aggregate:
select t.id, sum(x.val::int) result
from mytable t
cross join lateral regexp_split_to_table(t.column_to_sum, ':') as x(val)
group by t.id

Dividing summed field by another summed field in the same query

How to divide a summed field by another summed field in the same query.
Example: lets have the query "querySummary" which its field have been grouped already
SID SumOfCredits SumOfMarks
1 3 18
2 2 20
3 4 40
Group By Sum Sum
I want to add another field named "FAvg" to the same query that builds up of dividing "SumOfMarks" by SumOfCredits, so the the result should be as following
SID SumOfCredits SumOfMarks FAvg
1 3 18 6
2 2 20 10
3 2 40 20
Any help please ? many Thanks
Replace "Sum" in the "Total" row by "Expression" and in the "Field" row use the expression:
FAvg: Sum(Mark)/Sum(Credit)
You'll get something like this:
(The other Sum columns are not required for the FAvg expression)
The SQL looks like this:
SELECT
Table1.SID,
Sum(Table1.Credit) AS SumOfCredit,
Sum(Table1.Mark) AS SumOfMark,
Sum([Mark])/Sum([Credit]) AS FAvg
FROM
Table1
GROUP BY
Table1.SID;

How to sort the string 'MH/122020/[xx]x' in an Access query?

I am trying to sort the numbers,
MH/122020/101
MH/122020/2
MH/122020/145
MH/122020/12
How can I sort these in an Access query?
I tried format(mid(first(P.PFAccNo),11),"0") but it didn't work.
You need to use expressions in your ORDER BY clause. For test data
ID PFAccNo
-- -------------
1 MH/122020/101
2 MH/122020/2
3 MH/122020/145
4 MH/122020/12
5 MH/122021/1
the query
SELECT PFAccNo, ID
FROM P
ORDER BY
Left(PFAccNo,9),
Val(Mid(PFAccNo,11))
returns
PFAccNo ID
------------- --
MH/122020/2 2
MH/122020/12 4
MH/122020/101 1
MH/122020/145 3
MH/122021/1 5
you have to convert your substring beginning with pos 11 to a number, and the number can be sorted.
How about this ?
SELECT
tmpTbl.yourFieldName
FROM
tmpTbl
ORDER BY
CLng(Mid([tmpTbl].[yourFieldname],InStrRev([tmpTbl].[yourFieldname],"/")+1));
Given the following data in my test_table, column DATETIMESTAMP:
XXX123
YYY000
XXX-1234
my Statement:
SELECT CInt(Mid(datetimestamp,4)) AS Ausdr1
FROM test_data
ORDER BY 1;
sorts my data. please hange 4 to 11 and it will work for you

removing duplicates in ms access

please tell me how to write this query
i have an access table
number
2
2
1
2
2
1
1
3
2
i want a query that gives
number count
2 5
1 3
3 1
any help appreciated
something like...
SELECT number, count(number) AS count
FROM table
GROUP BY number
It's a bad idea to have a column named number since it is a reserved keyword.
You probably want something like
select number_, count(*) as count from ... group by number_