COUNT MANY FIELDS IN ACCESS [closed] - sql

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

Related

Populate sequence by group in sql server [closed]

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>

Postgres : get min and max rows count in many to many relation table [closed]

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 have mapping table for RFQ(request for quotation) and Vendor's bid amount.
rfq_vendor_mapping :
id rfq_id(FK) vendor_id(FK) amount
---------------------------------------
1 1 1 100
2 1 2 50
3 2 1 200
4 2 3 300
5 2 2 40
6 3 4 70
7 3 1 90
8 3 2 250
9 4 3 30
10 5 1 500
In above table, I want analysis for how many times vendor has submitted minimum and maximum bid for each RFQ.
Expected Output :
vendor_id min_bid_count max_bid_count
-----------------------------------------
1 1 2
2 2 1
3 1 2
4 1 0
http://sqlfiddle.com/#!15/60198/1
Compare the vendor's amount with min and max from a window function and run a conditional count on outer query level:
SELECT vendor_id
, count(min_bid OR NULL) AS min_bid_count
, count(max_bid OR NULL) AS max_bid_count
FROM (
SELECT vendor_id
, amount = min(amount) OVER w AS min_bid
, amount = max(amount) OVER w AS max_bid
FROM rfq_vendor_mapping
WINDOW w AS (PARTITION BY rfq_id)
) sub
GROUP BY 1
ORDER BY 1;
SQL Fiddle.

SQL Access " Count "? [closed]

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
table
Name tap
aa 1
aa 1
aa 2
bb 2
aa 3
cc 1
bb 2
bb 3
cc 2
Results ?????
name Count(1) Count(2) Count(3)
aa 2 1 1
bb 0 2 1
cc 1 1 0
??????
You can't use Count as it will do just that - count the number of records for each Name ignoring the value of tap.
But you check for the values of tap and sum these matches. However, while SQL Server returns 1 for a match, Access returns -1, thus - for a universal solution - you can apply Abs to always have a positive sum.
Then you get a query like this:
Select
[Name],
Abs(Sum(tap=1)) As Count1,
Abs(Sum(tap=2)) As Count2,
Abs(Sum(tap=3)) As Count3
From
YourTable
Group By
[Name]
Output will be:
Name Count1 Count2 Count3
aa 2 1 1
bb 0 2 1
cc 1 1 0

Find all Columns and its count of occurrence which have value 1 in SQL Server table [closed]

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

How to count total enable and disable row from one query? [closed]

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