update totalpending and totalaccepted in table - sql-server-2008

Please help to update TotalPending and TotalAccepted in table tblInvitation using tblInvitationEmpStatus
1.tblInvitation
ID Invitation TotalPending TotalAccepted
1 Group Meeting 0 0
2 Project discussion 0 0
3 Skype Call 0 0
tblInvitationEmpStatus
ID EmpID StatusType(1=Pending,2=Accepted)
1 101 1
1 102 1
1 103 2
2 101 2
2 102 2
3 104 1
3 105 2
3 106 2
Output tblInvitation
ID Invitation TotalPending TotalAccepted
1 Group Meeting 2 1
2 Project discussion 0 2
3 Skype Call 1 2

Use Conditional Aggregate to find the count of Pending and Accepted status.
SELECT a.ID,
a.Invitation,
TotalPending = Count(CASE WHEN StatusType = 1 THEN 1 END),
TotalAccepted = Count(CASE WHEN StatusType = 2 THEN 1 END)
FROM tblInvitation a
JOIN tblInvitationEmpStatus b
ON a.ID = b.ID
GROUP BY a.ID,
a.Invitation
SQL FIDDLE DEMO
To Update the tblInvitation table
Update A set
TotalPending = Count(CASE WHEN StatusType = 1 THEN 1 END),
TotalAccepted = Count(CASE WHEN StatusType = 2 THEN 1 END)
FROM tblInvitation a
JOIN tblInvitationEmpStatus b
ON a.ID = b.ID
GROUP BY a.ID,
a.Invitation

Related

SQL convert columns in rows

I want convert this table (Reading):
ID TimeTable_ID reading_Value Sensor_ID
1 1 482 1
2 1 153 2
3 1 152 3
4 1 781 4
5 2 156 1
6 2 842 2
7 2 157 3
8 2 453 4
into this:
TimeTable_ID Sensor_1 Sensor_2 Sensor_3 Sensor_4
1 482 153 152 781
2 156 842 157 453
My try:
SELECT *
FROM (SELECT TimeTable_ID, reading_Value
FROM Reading
) AS BaseData PIVOT
(COUNT(reading_Value) FOR TimeTable_ID IN ([Sensor_1], [Sensor_2], [Sensor_3], [Sensor_4])
) AS PivotTable;
but it does not work.
MySQL does not support the PIVOT operator. But, you may use a standard pivot query instead:
SELECT
TimeTable_ID,
MAX(CASE WHEN Sensor_ID = 1 THEN reading_Value END) AS Sensor_1,
MAX(CASE WHEN Sensor_ID = 2 THEN reading_Value END) AS Sensor_2,
MAX(CASE WHEN Sensor_ID = 3 THEN reading_Value END) AS Sensor_3,
MAX(CASE WHEN Sensor_ID = 4 THEN reading_Value END) AS Sensor_4
FROM Reading
GROUP BY TimeTable_ID;
If you have limited Sensors then you can do :
select TimeTable_ID,
sum(case when Sensor_ID = 1 then reading_Value else 0 end) as Sensor_1,
. . .
sum(case when Sensor_ID = 4 then reading_Value else 0 end) as Sensor_4
from reading r
group by TimeTable_ID;

Get rows with same primary key with different status in reference table

I'm having following table
id task_id stage_id is_completed
1 1 1 1
2 1 2 1
3 1 3 0
4 2 1 1
5 2 2 0
6 2 3 1
7 3 1 1
8 3 2 1
9 3 3 0
How can I get all task_ids where stage_id = 1 and is_completed = 1 and stage_id = 2 and is_completed = 0
You can use the combinations of AND and OR, e.g.:
SELECT task_id
FROM tasks
WHERE (stage_id = 1 AND is_completed = 1)
OR (stage_id = 2 AND is_completed = 2)
GROUP BY task_id;
SELECT * from mt_task_status
where
task_id in ( SELECT task_id FROM mt_task_status WHERE stage_id = '1' and is_completed = '1')
and task_id in ( SELECT task_id FROM mt_task_status WHERE stage_id = '2' and is_completed = '0')
and stage_id in (1,2)
order by task_id
Use union:
select task_id from table where stage_id = 1 and is_completed = 1
union
select task_id from table where stage_id = 2 and is_completed = 0

multiple records per row query

I have a table (in mysql) like this:
TABLE1
Id Name Age
---------------
1 John 22
2 Mary 17
3 Peter 21
4 Agnes 34
5 Steve 14
6 Bart 26
7 Bob 32
8 Vince 18
...
What I am looking for is a SELECT statement, where I can get 4 records in a row. I mean, the result of the select statement would be:
Id1 Name1 Age1 Id2 Name2 Age2 Id3 Name3 Age3 Id4 Name4 Age4
-----------------------------------------------------------
1 John 22 2 Mary 17 3 Peter 21 4 Agnes 34
5 Steve 14 6 Bart 26 7 Bob 32 8 Vince 18
...
I guess it would be like a pivot...
Is this possible? If it is, then how can I achieve it?
I need to populate a report by showing 4 records on a row, so I would like to be able to do it from a datasource that returns this exact structure. So on first band/row there will be
rec1,rec2,rec3,rec4
then on second row:
rec5,rec6,rec7,rec8
and so on.
My first idea was to merge 4 queries that return every 5th record starting with 1,2,3,4 but I'm not exactly sure...
Can you help?
You can do this with arithmetic on the id and group by:
select (case when id % 4 = 1 then id end) as id1,
(case when id % 4 = 1 then name end) as name1,
(case when id % 4 = 1 then age end) as age1,
(case when id % 4 = 2 then id end) as id2,
(case when id % 4 = 2 then name end) as name2,
(case when id % 4 = 2 then age end) as age2,
(case when id % 4 = 3 then id end) as id3,
(case when id % 4 = 3 then name end) as name3,
(case when id % 4 = 3 then age end) as age3,
(case when id % 4 = 0 then id end) as id4,
(case when id % 4 = 0 then name end) as name4,
(case when id % 4 = 0 then age end) as age4
from t
group by floor((id - 1) / 4);
If the id doesn't increment without gaps, then you can generate one using:
from (select t.*, (#rn := #rn + 1) as seqnum
from t cross join
(select #rn := 0) params
order by id
) t

Retrieving the TOP 10 data with arithmetic operation

I want to retrieve the name of the user with the top 10 most voted post.
the formula in calculating the votes should be upvotes-downvotes. I also need to count the votes of that post.
Here's my
table user_info
id lname fname
1 abc1 abc1
2 abc2 abc2
3 abc3 abc3
4 abc4 abc4
5 abc5 abc5
6 abc6 abc6
7 abc7 abc7
table post
post_id post_message user_id
1 sup 1
2 good morning 2
3 hello 5
4 test 3
5 sample 4
table vote
vote_id user_id post_id vote_type
1 1 1 upvote
2 2 1 upvote
3 1 2 downvote
4 1 2 upvote
5 2 2 downvote
6 3 1 upvote
7 3 4 upvote
8 4 4 downvote
here's the query that I tried.
SELECT post_id, fname,lname,COUNT(CASE WHEN vote_type = 'upvote' then 1 ELSE NULL END) as "upvotes", COUNT(CASE WHEN vote_type = 'downvote' then 1 ELSE NULL END) as "downvotes"
FROM user_info
LEFT JOIN post ON
post.user_id = user_info.id
LEFT JOIN vote ON
post.user_id = user_info.id
ORDER BY 'upvotes'- 'downvotes' // is this possible?
LIMIT 10
My desired output is something like this.
post_id lname fname vote
1 abc1 abc1 3 // 3 upvotes
4 abc3 abc3 0 // 1upvote - 1 downvote
3 abc5 abc5 0 // no vote
5 abc4 abc4 0 // no vote
2 abc2 abc2 -1 // 1upvote - 2 downvotes
I don't know where I should put that column "vote" but it should be there in my query.
I created an sql fiddle for this
Fiddle
select p.post_id,u.fname,u.lname,
case when sum(upvotes)+sum(downvotes) is null then 0
else sum(upvotes)+sum(downvotes) end as vote
from post p left join
(
SELECT post_id,
CASE WHEN vote_type = 'upvote' then 1 ELSE 0 END as "upvotes",
CASE WHEN vote_type = 'downvote' then -1 ELSE 0 END as "downvotes"
FROM vote
) t
on p.post_id = t.post_id
left join user_info u on p.user_id = u.id
group by p.post_id
order by vote desc, sum(upvotes) desc, sum(downvotes) desc
limit 10;

How do I count total of value in one column with different data

I'm a bit confuse how to name my question title.
Let say my MySQL database table like this. table_group
UserID UserGroup UserStatus
------------------------------
1 1 1
2 1 1
3 1 2
4 2 3
5 3 2
6 3 1
7 4 3
9 4 4
10 4 1
I want to group it by UserGroup and count the UserStatus.
Let me know what is the correct statement to get the result like this
UserGroup Status_1 Status_2 Status_3 Status_4
-------------------------------------------------
1 2 2 0 0
2 0 0 1 0
3 1 1 0 0
4 1 0 1 1
SELECT UserGroup, count(UserStatus = 1) as Status_1, count(UserStatus = 2) as Status_2,
count(UserStatus = 3) as Status_3, count(UserStatus = 4) as Status_4
FROM table_group GROUP BY UserGroup
You could use case to count rows that match a condition:
select UserGroup
, count(case when UserStatus = 1 then 1 end) as Status_1
, count(case when UserStatus = 2 then 1 end) as Status_2
, count(case when UserStatus = 3 then 1 end) as Status_3
, count(case when UserStatus = 4 then 1 end) as Status_4
from table_group
group by
UserGroup