For example
id staff_id skill_id mainskill
1 1 24 1
2 1 24 0
3 1 7 0
4 4 24 0
5 4 18 0
6 6 8 0
7 6 18 1
I would like the result to contain only the tuples with a skill_id that is present only once in all the data. In other words I want to retrieve the tuples containing the skill_ids that are only possessed by a single staff member.
And so the desired output is:
id staff_id skill_id mainskill
3 1 7 0
6 6 8 0
Thanks in advance :).
You can do it with GROUP BY and HAVING, like this:
SELECT
MAX(id) as id,
MAX(staff_id) as staff_id,
skill_id,
MAX(mainskill) as mainskill
FROM MyTable
GROUP BY skill_id
HAVING COUNT(1)=1
Related
I have 1 table called itemmovement : It has Item Id , Quantity In , Quantity Out , Invoice Id, Date. I need to make in one query to show how many pieces are sold and beside the sold column there will be the current on hand quantity .
itemmovement
Id itemid qtyin qtyout invid purchasereturnid date
1 1 10 2019-01-04
2 2 8 2019-01-06
3 2 2 1 2019-01-08
4 1 3 2 2019-01-12
5 2 1 2019-02-04
6 3 4 2019-03-04
7 1 1 3 2019-04-04
8 1 1 1 2019-04-14
9 3 1 2 2019-04-24
I need the query to show this result
Id itemid Sold Quantity OnHandQty
1 1 4 5
2 2 2 7
3 3 0 3
I'm Trying to use this query but not working
SELECT *
FROM
(SELECT itmv.itemid,
sum(itmv.qtyout)-sum(itmv.qtyin)
FROM itemmovement itmv
WHERE (itmv.systemdate BETWEEN '2019-01-01' AND '2019-06-01')
AND invid>0
GROUP BY itmv.itemid) AS result1,
(SELECT sum(itmv2.qtyin)-sum(itmv2.qtyout)
FROM itemmovement itmv2
WHERE itmv.itemid=itmv2.itemid
GROUP BY itmv2.itemid) AS result2
ORDER BY sum(itmv.qtyin)-sum(itmv.qtyout)
I'm getting :
Unknown column 'itmv.itemid' in 'where clause it for this syntax :
where itmv.itemid = itmv2.itemid
Here's your query.
select itemid
, sum(case when COALESCE(invid,0) > 0 then qtyout else 0 end) as Sold_Qantity
, sum(qtyin)-sum(qtyout) as OnHandQty
from itemmovement
group by itemid
I need to select user_id & quiz_id, for users which their count of questions in their quiz = sum of correct, this mean they answer 100% correct
answers table:
quiz_id question_id user_id answer_id correct
1 1 1 1 1
1 2 1 6 0
1 3 1 9 1
2 1 2 1 1
2 2 2 5 1
3 4 1 17 1
3 5 1 21 1
3 6 1 25 1
4 1 3 1 1
5 4 4 18 0
6 1 5 1 1
6 2 5 5 1
7 1 3 2 0
7 2 3 7 0
ex 1:
user 1 took "quiz_id" = 1
count of questions in "quiz_id = 1" = 3
sum of correct = 2
so it's not 100%
user_id = 1 in quiz_id = 1 => will not selected
but user_id = 1 will be selected with quiz_id = 3 cause he got 100%
expected results:
quiz_id user_id
2 2
3 1
4 3
6 5
notes:
quiz could be taken with different users with different number of
questions
quiz_id, user_id unique together (user can not take same quiz twice)
thanks,
You should use an aggregate query with HAVING clause:
SELECT quiz_id, user_id
FROM quiz_answer -- or whatever the name is
GROUP BY quiz_id, user_id
HAVING COUNT(question_id) = SUM(correct)
here you must use HAVING instead of WHERE because
The HAVING clause can refer to aggregate functions, which the WHERE
clause cannot
as specified in the docs.
I need to sum all time from "user_answers.time" and update "question.total_time", for all records
note: (user_answers.qid foreign key refer to question.id)
tables structure:
"question" table:
id total_time
1 0
2 0
3 0
4 0
5 0
"user_answers" table:
id qid time
1 1 3
2 1 44
3 2 3.2
4 3 2
5 1 5
6 4 1
I need to finish with "question" table like this:
id total_time
1 52
2 3.2
3 2
4 1
5 0
thanks,
Try this:
UPDATE question
SET total_time = (SELECT SUM(time) FROM user_answers WHERE question.id = user_answers.qid)
I have two tables that aren't really associated, but need to be combined. So I'm using union all on the two tables. The unioned tables are ordered by date, so rows from one table are dispersed among rows from the other table. What I need to do is get a running count of a column so I can group elements.
To explain further, table A holds dates of when a container is emptied, while table B holds daily entries for content of the container. I need to union the two tables so I have one table where I can get the sum of the information for a container before the container is emptied.
So I need something like this:
Table A:
Location_ID Empty Date
123 3/2/13
123 3/10/13
123 4/1/13
Table B:
PSI Entry Date Location_ID
120 2/28/13 123 (same for all)
130 3/1/13
100 3/8/13
110 3/9/13
200 3/18/13
180 3/20/13
So the unioned table after some magic would look like:
Table C...:
Location_ID Date PSI Emptied
123 2/28/13 120 0
123 3/1/13 130 0
123 3/2/13 null 1
123 3/8/13 100 0
123 3/9/13 110 0
123 3/10/13 null 1
123 3/18/13 200 0
123 3/20/13 180 0
123 4/1/13 null 1
What I need to do is have a grouping such that I can have a table like this
Table C_b
Location_ID Date PSI Emptied Group
123 2/28/13 120 0 1
123 3/1/13 130 0 1
123 3/2/13 null 1 1
123 3/8/13 100 0 2
123 3/9/13 110 0 2
123 3/10/13 null 1 2
123 3/18/13 200 0 3
123 3/20/13 180 0 3
123 4/1/13 null 1 3
How can I get that grouping in that way? I have to make it work in SQL Server 2008. I have tried using Count, and Rank, and Row_Number. But the problem with those is that it won't do a running count, it will just say the total count in each row.
Try this query:
DECLARE #MyTable TABLE(
EntryDate DATE NOT NULL,
Emptied BIT NOT NULL
);
INSERT INTO #MyTable (EntryDate,Emptied)
VALUES
('2013-01-01',0),
('2013-01-02',0),
('2013-01-03',1),
('2013-01-04',0),
('2013-01-05',0),
('2013-01-06',1),
('2013-01-07',0),
('2013-01-08',0),
('2013-01-09',1);
DECLARE #TableWithRowNum TABLE(
EntryDate DATE NOT NULL,
Emptied BIT NOT NULL,
RowNum INT PRIMARY KEY
);
INSERT INTO #TableWithRowNum (EntryDate,Emptied,RowNum)
SELECT crt.*,ROW_NUMBER() OVER(ORDER BY crt.EntryDate) AS RowNum
FROM #MyTable crt;
WITH RecCTE
AS(
SELECT
crt.EntryDate,
crt.Emptied,
crt.RowNum,
1 AS Grp
FROM #TableWithRowNum crt
WHERE crt.RowNum=1
UNION ALL
SELECT
crt.EntryDate,
crt.Emptied,
crt.RowNum,
CASE WHEN prev.Emptied=1 THEN prev.Grp+1 ELSE prev.Grp END
FROM #TableWithRowNum crt INNER JOIN RecCTE prev ON crt.RowNum=prev.RowNum+1
)
SELECT * FROM RecCTE
OPTION(MAXRECURSION 0); -- Default value for MAXRECURSION is 100
GO
Results:
EntryDate Emptied RowNum Grp
---------- ------- ------ ---
2013-01-01 0 1 1
2013-01-02 0 2 1
2013-01-03 1 3 1
2013-01-04 0 4 2
2013-01-05 0 5 2
2013-01-06 1 6 2
2013-01-07 0 7 3
2013-01-08 0 8 3
2013-01-09 1 9 3
I have a table like this
ID Name Score_1 Score_2 Score_3
1 Abcd 4 5 5
2 Bdc 8 7 0
3 dcd 0 0 3
4 cdded 0 0 0
I need another column in the end which can count the number of non zero columns.Result should be like this
ID Name Score_1 Score_2 Score_3 Count
1 Abcd 4 5 5 3
2 Bdc 8 7 0 2
3 dcd 0 0 3 1
4 cdded 0 0 0 0
Thank you
select *,
if(score_1<>0,1,0)+if(score_2<>0,1,0)+if(score_3<>0,1,0) as `count`
from table
Use a select like this when you need instead of storing a calculated field.