Get count of two column in access 2007 [closed] - sql

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

Related

Comma separated values to results

I have 3 tables namely Question, Feedback and FeedbackResult. I wish to generate a report where I need to find the rating average against each question.
Questions
---------
QuestionId Question
1 Question 1
2 Question 2
3 Question 3
4 Question 4
5 Question 5
Feedback
--------
FeedbackId Questions QuestionCount
2 1,2,3,4,5 5 -- Questions column has questionid from Question table
FeedbackResults
---------------
FeedbackResId FeedbackId Answers
1 2 4,3,5,2,3 -- these answers are ratings(1 to 5) against each question
2 2 4,2,3,4,5 -- which means 4 is the rating for QuestionId 1, 2 is for QuestionId 2 etc
3 2 5,3,4,3,2
4 2 4,1,1,1,2
I wish to get result as average rating against each question
Question Rating
Question 1 3.5
Question 2 4
Question 3 5
Question 4 2
Question 5 4.5
Edit
Should I redesign the database table as
FeedbackResults
---------------
FeedbackResId FeedbackId QuetionID Answers UserId
1 2 4 4 1
2 2 1 3 1
3 2 5 3 1
4 2 1 2 2

Change columns every 5 rows in a SQL query [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a table of data and have some records.
I want to change every fifth row of data and update the column ParentId to be equal of first row Id of five row group - like this:
Id ParentId ....
-------------------------------
1 1
2 3
3 10
4 5
5 5
6 5
7 2
8 2
9 2
10 2
11 2
12 7
13 7
and result I need in SQL Server should be like this:
Id ParentId ....
-------------------------------
1 1
2 1
3 1
4 1
5 1
6 6
7 6
8 6
9 6
10 6
11 11
12 11
13 11
...
You can use this code if you want update your table:
Update YourTable set ParentId= (Ceiling(Id*1.0/5)-1)*5 + 1
and if you want just select rows use this :
select Id, (Ceiling(Id*1.0/5)-1)*5 + 1 as ParentId from yourtable
update myTable
set ParentId = 5 * (Id / 5) + 1
where Id % 5 != 0
update myTable
set ParentId = 5 * ((Id /5) - 1) + 1
where Id % 5 = 0

Select DISTINCT IDs with SUM Function in SQL [closed]

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

Error while fetching nested loop output [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I want to generate a number triangle in PL/SQL like this,
If you pass 9 then it has to generate the given output.
123456789
12345678
1234567
123456
12345
1234
123
12
1
If you pass 10 then it has to generate the given output.
12345678910
123456789
12345678
1234567
123456
12345
1234
123
12
1
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
DECLARE
V VARCHAR2(20);
BEGIN
FOR I IN REVERSE 1..5
LOOP
FOR J IN 1..I
LOOP
V:=V||' '||J;
END LOOP;
DBMS_OUTPUT.PUT_LINE(V);
V:=NULL;
END LOOP;
END;
/

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.