SQL Server: increment row number only one value - sql-server

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)
...

Related

Get count of lDs where all status is greater than equal to 1

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

How to check values of different rows of a table

I have below sample input table. In real it has lots of records.
Input:
ID
Classification
123
1
123
2
123
3
123
4
657
1
657
3
657
4
For a 'ID', I want it's records should have 'Classification' column contains all the values 1, 2, 3 and 4. If any of these values are not present then that ID's records should be considered as an exception. The output should be as below.
ID
Classification
Flag
123
1
0
123
2
0
123
3
0
123
4
0
657
1
1
657
3
1
657
4
1
Can someone please help me with how can this can be achieved in sql server.
Thanks.
There are a couple of options here, which is more performant is up to you to test, not me (especially when I don't know what indexes you have). One uses conditional aggregation, to check that all the values are there, and the other uses a subquery and counts the DISTINCT values (as I don't know if there could be duplicate classifications):
SELECT *
INTO dbo.YourTable
FROM (VALUES(123,1),
(123,2),
(123,3),
(123,4),
(657,1),
(657,3),
(657,4))V(ID,Classification);
GO
CREATE CLUSTERED INDEX CI_YourIndex ON dbo.YourTable (ID,Classification);
GO
SELECT ID,
Classification,
CASE WHEN COUNT(CASE YT.Classification WHEN 1 THEN 1 END) OVER (PARTITION BY ID) > 0
AND COUNT(CASE YT.Classification WHEN 2 THEN 1 END) OVER (PARTITION BY ID) > 0
AND COUNT(CASE YT.Classification WHEN 3 THEN 1 END) OVER (PARTITION BY ID) > 0
AND COUNT(CASE YT.Classification WHEN 4 THEN 1 END) OVER (PARTITION BY ID) > 0 THEN 1 ELSE 0
END AS Flag
FROM dbo.YourTable YT;
GO
SELECT ID,
Classification,
CASE (SELECT COUNT(DISTINCT sq.Classification)
FROM dbo.YourTable sq
WHERE sq.ID = YT.ID
AND sq.Classification IN (1,2,3,4)) WHEN 4 THEN 1 ELSE 0
END AS Flag
FROM dbo.YourTable YT;
GO
DROP TABLE dbo.YourTable;

I want o get one case per account

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

SQL Server : getting data from multiple rows into one row

I have the following table and I want to get one row per user (userId) with the Answer column value when initial is set to both true and false. So the following tables
UserId QuestionId Answer Initial
----------------------------------------------------------------
027D76AC-DFBC-4BD2-9B88DD7B2456338E 1 5 False
027D76AC-DFBC-4BD2-9B88DD7B2456338E 1 4 True
06B1713D-2E47-4454-8949C950C58753DC 1 4 True
216F33EF-1ACD-4D1F-86D2932AF598326E 1 5 False
216F33EF-1ACD-4D1F-86D2932AF598326E 1 4 True
23A950EB-3C68-4FE7-B719B86DC299343D 1 4 True
23A950EB-3C68-4FE7-B719B86DC299343D 1 4 False
Would return the following results
UserId QuestionId trueAnswer FalseAnswer
-----------------------------------------------------------------------
027D76AC-DFBC-4BD2-9B88DD7B2456338E 1 5 4
216F33EF-1ACD-4D1F-86D2932AF598326E 1 5 4
23A950EB-3C68-4FE7-B719B86DC299343D 1 4 4
Is this something that can be done with sub selects?
I think below can be a solution (top portion is just creating a temp table to test it). I am always an advocate of avoiding GROUP BY and plus I think OUTER/CROSS APPLY are really cool. Notice though that the result is opposite your result. For example, your top one shows the FalseAnswer to be 4. According to the data it is 5. Unless I am missing something.
-- creating sample set
IF object_id('tempdb..#YOUR_TABLE') is not null drop table #YOUR_TABLE
CREATE TABLE #YOUR_TABLE (UserID VARCHAR(200), QuestionID INT, Answer INT, Initial BIT)
INSERT INTO #YOUR_TABLE(UserID, QuestionID, Answer, Initial)
Values
('027D76AC-DFBC-4BD2-9B88DD7B2456338E', 1, 5, 'false'),
('027D76AC-DFBC-4BD2-9B88DD7B2456338E', 1, 4, 'true'),
('06B1713D-2E47-4454-8949C950C58753DC', 1, 4, 'true'),
('216F33EF-1ACD-4D1F-86D2932AF598326E', 1, 5, 'false'),
('216F33EF-1ACD-4D1F-86D2932AF598326E', 1, 4, 'true'),
('23A950EB-3C68-4FE7-B719B86DC299343D', 1, 4, 'true'),
('23A950EB-3C68-4FE7-B719B86DC299343D', 1, 4, 'false')
-- solution
SELECT a.UserID,
a.QuestionID,
a.Answer,
b.FalseAnswer
FROM #YOUR_TABLE AS a
OUTER APPLY
(
SELECT y.Answer AS FalseAnswer
FROM #YOUR_TABLE AS y
WHERE y.Initial='false' AND a.UserID=y.UserID
) AS b
WHERE a.Initial='true' AND b.FalseAnswer IS NOT null
output
UserID QuestionID Answer FalseAnswer
027D76AC-DFBC-4BD2-9B88DD7B2456338E 1 4 5
216F33EF-1ACD-4D1F-86D2932AF598326E 1 4 5
23A950EB-3C68-4FE7-B719B86DC299343D 1 4 4
Try this:
select UserID,
max(QuestionID),
max(case when Initial = 'True' then Answer end) [trueAnswer],
max(case when Initial = 'False' then Answer end) [falseAnswer]
from TABLE_NAME
group by UserID
having count(distinct Initial) = 2

Get Count of Records in Separate Column [duplicate]

This question already has answers here:
Different value counts on same column
(3 answers)
Closed 8 years ago.
Table : STATUS_TABLE
id | Status |
=================
1 true
2 false
3 false
4 true
How can I get count of both True and False in separate column using Oracle??
I must show only those record whose status is true. So I put where condition.
But I must display count of Both.
Like
MARKED_RECORD UNMARKED_RECORD
2 2
How about something like
SELECT SUM(CASE WHEN Status = 'true' THEN 1 ELSE 0 END) MARKED_RECORD,
SUM(CASE WHEN Status = 'false' THEN 1 ELSE 0 END) UNMARKED_RECORD
FROM STATUS_TABLE
WITH src1 AS
(
SELECT 1 id, 'TRUE' status FROM dual UNION ALL
SELECT 2 id, 'TRUE' status FROM dual UNION ALL
SELECT 3 id, 'FLASE' status FROM dual UNION ALL
SELECT 4 id, 'TRUE' status FROM dual UNION ALL
SELECT 5 id, 'FLASE' status FROM dual UNION ALL
SELECT 6 id, 'TRUE' status FROM dual UNION ALL
SELECT 7 id, 'FLASE' status FROM dual
)
, src AS
(
SELECT id
, status
, SUM(DECODE(status, 'TRUE', 1, NULL)) OVER (ORDER BY id) AS is_true
, SUM(DECODE(status, 'FLASE', 1, NULL)) OVER (ORDER BY id) AS is_false
FROM src1
)
SELECT s1.id
, s1.status
, s2.m_is_true
, s2.m_is_false
FROM src s1
CROSS JOIN
(
SELECT MAX(is_true) AS m_is_true
, MAX(is_false) AS m_is_false
FROM src
) s2
WHERE s1.status = 'TRUE'
;
Result:
ID STATUS M_IS_TRUE M_IS_FALSE
1 TRUE 4 3
2 TRUE 4 3
4 TRUE 4 3
6 TRUE 4 3
Like this
select decode(status,'true','Marked','false','Unmarked')status,
count(status) Count
from sample_table
code layoutgroup by status

Resources