Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I wanted to know the best way to solve the below scenario, where I've an ID and a state in one table.
Accepted is the end state, once the state reaches accepted for a particular ID no further updates will occur for that ID.
For example, this is how the table looks
ID | State
-----+----------------
12 | Not Accepted
12 | Not Accepted
12 | Accepted
45 | Not Accepted
67 | Not Accepted
Now, I want to count distinct ids into Accepted and Not Accepted buckets.
If an id reached the accepted state, then it should be counted in accepted bucket (ignoring the fact it has "not accepted" states earlier). Example for id = 12 has 2 non accepted states and 1 accepted state so only the count of accepted should be incremented.
Expected result
Accepted | Not Accepted
---------+--------------
1 | 2
I tried is this SQL statement, but it doesn't give me the correct result
SELECT
SUM(CASE WHEN state = 'Accepted' THEN 1 ELSE 0 END) AS accepted,
SUM(CASE WHEN state != 'Accepted' THEN 1 ELSE 0 END) AS nonaccepted
FROM
SAMPLE;
Results I get:
Accepted | Not Accepted
---------+-------------
1 | 4
Not sure what exactly you are asking for but I think you want this:
select sum(case when min_state = 'Accepted' then 1 else 0 end) accepted_cnt,
sum(case when min_state = 'Not Accepted' then 1 else 0 end) not_accepted_cnt
from (select id, min(state) min_state
from my_table
group by id);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have
id value
1 12
1 15
1 17
1 22
1 22
1 23
And I need like this
id value
1 --
1 3
1 2
1 5
1 0
1 1
Could you tell me, how to achive this?
You can try the below -
select id,
value-max(value) over(order by id rows between 1 preceding and 1 preceding) as value
from tablename
You seem to want lag(), which I'm guessing is per id and based on the ordering of value:
select t.*,
(value - lag(value) over (partition by id order by value)) as diff
from t
order by value;
That said, your sample data has exact duplicates. That is unusual in a SQL table.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Populate sequence by group in sql server:
Input:-
ID data
1 0
1 0
1 0
2 0
2 0
2 0
Output:-
ID data
1 0
1 1
1 2
2 0
2 1
2 2
As it stands, per your sample data, you need to use ROW_NUMBER() along with partitioning.
SELECT ID, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) as DATA
FROM <table>
But because the ID column is not unique, the ORDER BY will not know how to discern between the first row with 1 and the third row with 1.
Which is why, I recommend in the ORDER BY ID part, to also add a unique/primary key column which will give you a deterministic order, so that you can always determine what value a certain row will have, in a fixed set of data.
So, if your table also contains a "PK" (primary key) or unique column:
PK ID data
1 1 0
2 1 1
3 1 2
4 2 0
5 2 1
6 2 2
then your select can turn into:
SELECT ID, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID, PK) as DATA
FROM <table>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
ID Name Dept Project Job Platform
A101 1 0 1 1 0
A103 0 1 1 0 1
B201 1 1 0 0 0
C301 1 0 1 1 1
Any help will be greatly appreciated
EDIT: I have tried these queries:
SELECT count(sittingreading),count(WatchingTV) FROM table_name WHERE sittingreading IS NOT NULL
Select COUNT(*) from table_name where column_value=0
Use a case statement in count() to make SQL Server only count the rows where you have the value you are looking for.
select count(case when T.Name = 0 then 1 end) as NameCount,
count(case when T.Dept = 0 then 1 end) as DeptCount,
count(case when T.Project = 0 then 1 end) as ProjectCount
from YourTable as T
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 have a database table like this:
ID NAMEID ClASSID SEX
---------------------------------
1 1 1 0
2 1 1 1
3 1 1 1
4 2 2 1
5 2 2 0
6 2 2 1
7 2 2 1
Please help me, how to have a result, as follows: (use query in Access)
NameID ClassID SEX MALE FEMALE
--------------------------------------------
1 1 3 2 1
2 2 4 3 1
Thanks so much
I assume that you want to group on the NameId and ClassId fields, and that the Sex field in the result is not sex at all, but the number of persons.
Something like this:
select
NameId,
ClassId,
count(*) as Sex,
sum(Sex) as Male,
count(*) - sum(Sex) as Female
from
TheTable
group by
NameId,
ClassId
Edit:
For Sex values 1 and 2 you can use a case to count them:
sum(case Sex when 1 then 1 else 0 end) as Male,
sum(case Sex when 2 then 1 else 0 end) as Female
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 9 years ago.
Improve this question
eg.
id name age status
1 aaa 10 1
2 bbb 20 0
3 ccc 30 1
Now how to count total status of 1 and 0 from single query.
Ans will be 1 => 2 and 0 => 1
To get one row, which is what I believe you are asking for, use conditional SUMs:
SELECT SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS status1,
SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) AS status0
FROM Table1
To get multiple rows, simply GROUP BY status:
SELECT status, COUNT(1) AS rows
FROM Table1
GROUP BY status
Please try:
SELECT
STATUS,
COUNT(*) Total
FROM
YourTable
GROUP BY STATUS
OR
SELECT DISTINCT
STATUS,
COUNT(*) OVER (PARTITION BY STATUS) Total
FROM
YourTable