Select DISTINCT IDs with SUM Function in SQL [closed] - sql

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 7 years ago.
Improve this question
I need help with this because I Can't think of a way to do it.
I have this Table:
Score Table:
UserID: 1 1 2 1 2 3
Score: 100 -10 100 -10 -20 100
So what I need to do is create a view using a SUM function to get the current score of each user, something like this...
View:
UserID: 1 2 3
Score: 80 80 100
Any ideas how can I achieve that?
Thx for the help, but I'm having another issue.
Now I have another Column in my table (rID) for instance.
User ID : 1 2 1 1 2 2
rID: NULL Null 1 2 1 4
Score: 100 100 -20 -30 -20 -40
I want to group by UserID and rID taking diffrents scores for each one of the rID, I want this result:
UserID: 1 1 2 2
rID: 1 2 1 4
Score: 80 70 80 60
Any help would be appreciated!

Use GROUP BY:
SELECT UserID, SUM(Score)
FROM Table
GROUP BY UserID

Related

Sort data by numeric and alphabetical in sqlite3 [duplicate]

This question already has an answer here:
Sqlite query - order results with numbers last
(1 answer)
Closed 7 years ago.
I have table data like this:
ID name
----------
1 30
2 aaa
3 zzz
4 20
5 40
6 10
My result should be sorted data
ID name
5 40
1 30
4 20
6 10
3 zzz
2 aaa
Means numeric and alphabetic should be separate and it should be sorted in DESC or ASC.
If anyone have an idea to perform this operation in sqlite3 help me
SELECT * FROM yourTableName ORDER BY ID ASC;
If you want something more detailed than that, it would help to know what you are currently running or what you want the output to be.

sql HAVING & averages [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 8 years ago.
Improve this question
Learning the having clause, and averages (did AVG before), and I have to admit, I'm confused.
I'll post a problem here, one of the simpler ones on this weeks' assignment, and ask someone to explain it to me in 'real world' terminology (not the textbook language, which is sparse at best).I kinda see what I need to do, but averaging multiple values within same column has me somewhat confused, as does using 'Having' over 'Where'.
SQL> desc inventory
Name
----------------------------
BOOK_CODE
BRANCH_NUM
ON_HAND
SQL> SELECT * FROM INVENTORY;
BOOK BRANCH_NUM ON_HAND
---- ---------- ----------
0180 1 2
0189 2 2
0200 1 1
0200 2 3
0378 3 2
079X 2 1
079X 3 2
079X 4 3
0808 2 1
1351 2 4
1351 3 2
1382 2 1
138X 2 3
2226 1 3
2226 3 2
2226 4 1
2281 4 3
2766 3 2
2908 1 3
2908 4 1
3350 1 2
3743 2 1
3906 2 1
3906 3 2
5163 1 1
5790 4 2
6128 2 4
6128 3 3
6328 2 2
669X 1 1
6908 2 2
7405 3 2
7443 4 1
7559 2 2
8092 3 1
8720 1 3
9611 1 2
9627 3 5
9627 4 2
9701 1 2
9701 2 1
9701 3 3
9701 4 2
9882 3 3
9883 2 3
9883 4 2
9931 1 2
Maybe this helps:
You know how SELECT determines which COLUMNS to return? And you use WHERE in order to choose which ROWS will be returned? Obvious so far, right. But when you aggregate values from multiple rows together into one returned value, then you have to use HAVING if you want to perform some filtering on those.
For example:
SELECT author, title
FROM books
WHERE author = 'Twain, Mark'
would give you the list of all the books Mark Twain wrote.
And
SELECT author, count(*)
FROM books
GROUP BY author
would give you the count of books written by each author.
Finally, if you wanted only those authors who had written 3 or more books:
SELECT author, count(*)
FROM books
GROUP BY author
HAVING count(*) >= 3
I intentionally simplified the data here, but you can probably see how it applies. Another example could be to show all the authors whose books average less than $10:
SELECT author, average(price)
FROM books
GROUP BY author
HAVING average(price) < 10
If you tried to use WHERE here, all you could do is filter out rows in which the price of a single book was less:
SELECT author, average(price)
FROM books
GROUP BY author
WHERE price < 10
That would be the wrong answer of course, mostly because it would fail. But even if it returned a result, it would be the average price of all books less than $10 -- not the same at all.

Get count of two column in access 2007 [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
TblAquestion:
AQcode(pk)|AQdescribe
---------------------------------
1 do you want to continue?
2 what ..........?
3 this is a ..........
4 my name is a ali?
TblUserResult:
ID(pk)|AQcode_fk|Result
-----------------------
100 1 1
101 1 0
102 1 0
103 1 0
104 1 1
105 2 1
106 2 0
107 3 1
108 3 1
109 3 1
110 3 0
111 4 1
OutPut:
ResultYes is count of result where '1'
ResultNo is count of result where '0'
AQcode|AQdescribe |CountResultYes|CountResultNo
---------------------------------------------------------------
1 do you want to continue? 2 3
2 what ..........? 1 1
3 this is a .......... 3 1
4 my name is a ali? 1 0
SQL command?
select q.AQcode, q.AQdescribe,
sum(IIF(r.Result = 1, 1, 0)) as CountResultYes,
sum(IIF(r.Result = 0, 1, 0)) as CountResultNo
from TblAquestion q
left join TblUserResult r on r.AQcode_fk = q.AQcode
group by q.AQcode, q.AQdescribe
select aqcode,aqdescribe,sum(Cno),sum(Cyes) from
(
select aqcode,aqdescribe,'0' as Cno,count(result) as Cyes
from TblAquestion aq,TblUserResult r
where aq.aqcode=r.aqcode and r.result='1'
group by aqcode,aqdescribe
Union
select aqcode,aqdescribe,count(result) as Cno,'0' as Cyes
from TblAquestion aq,TblUserResult r
where aq.aqcode=r.aqcode and r.result='0'
group by aqcode,aqdescribe
)
group by aqcode,aqdescribe

Row aggregation of count-distinct measure

I have a fairly simple project set up to demonstrate what I want here. Here's the data:
Group
ID Name
1 Group 1
2 Group 2
3 Group 3
Person
ID GroupID Age Name
1 1 18 John
2 1 21 Stephen
3 1 18 Kate
4 2 18 Mary
5 2 19 Joseph
6 2 19 Michael
7 3 21 David
8 3 22 Kevin
9 3 21 Julian
I have 1 measure in my cube called Person Count which is a Distinct count on Person ID
I have set up each non-ID column in the dimensions as attributes (Age, Person Name, Group).
When I process and browse the cube in Business Intelligence Development Studio, I get the following result set:
But what I actually want here are the rows for Age to aggregate up the count of the Person Count together, so here it should show 2 and only one row for 18.
Is this possible (and how)?
Turns out this was a problem with the way I set up the Age attribute for the dimension.
I had:
KeyColumns = Person.ID
ValueColumn = Person.Age.
I don't know why I did this, but the solution is to delete the content of ValueColumn and set the KeyColumns to Person.Age again.
I now get the following result:
Everything else is the same for the project; this was the only change and is exactly what I wanted. If I get any issues with it I will keep this post updated for anyone else who may run into this in the future.

Oracle SQL to roll up counts into ranges [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Oracle: how to “group by” over a range?
Let's say I have data that looks like this:
Item Count
======== ========
1 123
2 1
3 47
4 117
5 18
6 466
7 202
I want to create a query that gives me this:
Count Start Count End Occurrences
=========== =========== ===========
0 100 3
101 200 2
201 300 1
301 400 0
401 500 1
Basically, I want to take a bunch of counts and group them into ranges for statistical rollups. I don't think I'm using the right keywords to find the answer to this. I am going against Oracle, though if there is an ANSI SQL answer I'd love to have it.
select
a.mini,
a.maxi,
count(a.item)
from
(
select
table.item,
case (table.counter)
when counter>=0 and counter<=100 then 0
when counter>100 and counter<200 then 101
when ....
end as mini
table.item,
case (table.counter)
when counter>=0 and counter<=100 then 100
when counter>100 and counter<200 then 201
when ....
end as maxi
from
table
) a
group by
a.mini,
a.maxi
One way is using CASE statements. If you want it to be scalable, try having the range in a separate table and use JOIN to count the occurence.