I have that query:
SELECT Id,AccountId,IsEscalated FROM Case WHERE AccountId != null AND IsClosed = FALSE ORDER BY AccountId,IsEscalated DESC
That return this data
Id AccountId IsEscalated
1 1 true
2 1 false
3 1 false
4 2 false
5 2 false
6 3 true
But I want recive this
Id AccountId IsEscalated
1 1 true
4 2 false
6 3 true
Related
I want to select count of ids where all status is greater than equal to 1
I want something like this
SELECT count(ID)FROM table where all status >= 1
ID
status
1
1
1
2
1
1
1
1
1
1
1
1
2
0
2
1
2
0
2
1
1
1
3
1
3
1
3
1
3
2
3
2
3
2
As you can see in the table where ID = 1,3 has all status greater than equal to 1.
I want to select the count of those IDs.
For example, I want count 2 from the above table.
How can I get those ids count where all status is >=1
Edited:Edited table
To get the ID where all status are >=1 you can use
SELECT ID
FROM Table1
GROUP BY ID
HAVING MIN([status]) >= 1
to count them you can use
SELECT COUNT(*)
FROM (SELECT ID
FROM Table1
GROUP BY ID
HAVING MIN([status]) >= 1) T
I have this table:
Id status
-----------
1 true
2 false
3 true
4 true
5 false
I need output like this:
Id status count
-----------------
1 true 1
2 false 1
3 true 2
4 true 3
5 false 3
So I need to increment only true and but not false
You can use the window functions ( well worth your time to get familiar with)
Example
Declare #YourTable Table ([Id] int,[status] varchar(50)) Insert Into #YourTable Values
(1,'true')
,(2,'false')
,(3,'true')
,(4,'true')
,(5,'false')
Select *
,count = sum(case when status='true' then 1 else 0 end) over (order by id)
From #YourTable
Returns
Id status count
1 true 1
2 false 1
3 true 2
4 true 3
5 false 3
Edit... If [status] is a bit
...
,count = sum(convert(int,status)) over (order by id)
...
I have this table . .
ID item_ID status
----------------------------
1 1 'available'
2 1 'available'
3 1 'available'
4 1 'reserved'
5 2 'available'
6 2 'available'
7 3 'reserved'
8 3 'reserved'
9 3 'reserved'
I want my SQL query to return this result:
item_ID quantity
------------------
1 3
2 2
3 0
Please help with this. I'm not really good with T-SQL.
You could use aggregation, but first you need to filter only "available" products:
SELECT item_id, COUNT(*) as quantity
FROM table
WHERE status = 'available'
GROUP BY item_id
ORDER BY item_id;
EDIT:
To get 0 you could use:
SELECT s.item_id, COUNT(t.item_ID) AS quantity
FROM (SELECT DISTINCT item_ID FROM table) s
LEFT JOIN table t
ON s.item_ID = t.item_id
AND t.status = 'available'
GROUP BY s.item_id
ORDER BY s.item_id;
one more way is to use Sum with case
select item_id,
sum(Case when status='available'
then 1 else 0 end) as 'quantity
from
table
group by item_id
I have 2 tables
Emp
Empid postid postcodeid
1 1 null
2 1 null
3 1 null
2nd table
Postcode
Postcodeid postid
1 1
2 1
3 1
Now i want to update postcodeid with 2nd table ids.
Like the below table
Empid postid postcodeid
1 1 1
2 1 2
3 1 3
Run an Update query:
UPDATE table1
SET table1.postcodeid=table2.postcodeid
WHERE table1.empid= table2.empid
I have this kind of query in my ms sql.
SELECT
OdeaMainDataBase.dbo.ItemChoicesTable.*,
CASE
WHEN EXISTS(SELECT * FROM StudentTestTablefromRemark
WHERE StudID = #studId AND ExamID = #examId
AND ItemBankID = #itemBankId
AND ChoiceID = OdeaMainDataBase.dbo.ItemChoicesTable.ChoiceID)
THEN 'True'
ELSE 'False'
END AS isAnswered
FROM
OdeaMainDataBase.dbo.ItemChoicesTable
WHERE
ItemBankID = #itemBankId
I observe that the CASE statement I made causes my query to run slow. When I remove that CASE, it run fast but I need that case for my output.
Here is the structure I want to display
Item Choice Table
ItemBankID ChoiceID ChoiceLetter
--------------------------------------
1 1 A
1 2 B
1 3 C
2 4 A
2 5 B
2 6 C
Student Answers Table
ItemBankID ChoiceID ChoiceLetter StudentId
-------------------------------------------------
1 2 B 123
2 6 C 123
Desired Output
ItemBankID ChoiceID ChoiceLetter IsAnswered
----------------------------------------------------
1 1 A False
1 2 B True
1 3 C False
2 4 A False
2 5 B False
2 6 C True
I want to achieve that output without slowing my program.
Any idea will do to me.
Thank you so much
Try this
SELECT i.*,
CASE
WHEN s.ChoiceID IS NULL THEN 'False'
ELSE 'True'
END
FROM OdeaMainDataBase.dbo.ItemChoicesTable i
LEFT OUTER JOIN StudentTestTablefromRemark s
ON s.ChoiceID = i.ChoiceID
AND s.ItemBankID = i.ItemBankID
AND s.StudID = #studId
AND s.ExamID = #examId
WHERE i.ItemBankID = #itemBankId
Try this query
SELECT OdeaMainDataBase.dbo.ItemChoicesTable.*,
ISNULL(a,'False') as isAnswered
FROM OdeaMainDataBase.dbo.ItemChoicesTable
left outer join(Select 'True' as a,ChoiceID FROM StudentTestTablefromRemark
Where StudID=#studId AND ExamID=#examId AND ItemBankID=#itemBankId )b on b.ChoiceID=OdeaMainDataBase.dbo.ItemChoicesTable.ChoiceID
Where ItemBankID = #itemBankId