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
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 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);
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.
Closed 8 years ago.
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.
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
Improve this question
I have the following scenario
pid & month form a composite primary key .
pid month amount
1 1 10
2 2 15
1 2 20
1 3 10
3 3 4
2 3 6
Now the column to be generated with the table will be like this
pid month amount sum
1 1 10 10
2 2 15 15
1 2 20 30
1 3 10 40
3 3 4 4
2 3 6 21
What should be the query ?
This query will do the trick :
SELECT t1.*, sum(t2.amount)
FROM Table1 t1
INNER JOIN Table1 t2 ON t1.pid = t2.pid AND t1.month >= t2.month
GROUP BY t1.pid, t1.month, t1.amount
See SQLFIDDLE : http://www.sqlfiddle.com/#!3/db350/7/0
If using SQL Server 2012:
SELECT *,SUM(amount) OVER(PARTITION BY pid ORDER BY month ROWS UNBOUNDED PRECEDING)'Total'
FROM YourTable
You did not specify what version of SQL Server you are using but you should be able to use the following to get the running total in any version:
select t1.pid,
t1.month,
t1.amount,
(select sum(t2.amount)
from yourtable t2
where t1.pid = t2.pid
and t2.month <= t1.month) total
from yourtable t1;
See SQL Fiddle with Demo