473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Need SQL help - Select Distinct, Count, Group By

Hello all,

I'm having some trouble setting up a query.

Background: The table TBLSCREEN stores data about screenings of
patients/subjects for eligibility to participate in a health study. A
subject can be screened multiple times.

The simplified table structure is

SITE - location of subject
SUBJECTID - ID of subject
SCREENNUM - the number of the screening (1 represents first screening,
etc)
SCREENDATE - the date the subject was screened

A subject is uniquely identified by the combination of SITE and
SUBJECTID. A screening is uniquely identified by the combination of
SITE, SUBJECTID, and SCREENNUM.

I need to create a query that shows the number of unique subjects
screened per month.

If a subject is screened twice in two different months, only the first
screening should count.

I've tried variations of something like this, but nothing has worked
so far:

SELECT COUNT(*) AS NumSubj FROM
(SELECT DISTINCT SITE, SUBJECTID
FROM TBLSCREEN)
GROUP BY Format(SCREENDATE,"yyyy-mm");

Thanks in advance for any help you may be able to provide,
Chris
Nov 13 '05 #1
2 9493
"Chris" <ch*****@mailinator.com> wrote in message
news:a0**************************@posting.google.c om...

I need to create a query that shows the number of unique subjects
screened per month.

If a subject is screened twice in two different months, only the first
screening should count.


When you work with groupings of temporal data it's often better to build and
populate a calendar table to hold the individual date groups, and then join
to this table in your query. I don't have time now but see if this puts you
on the right track, (do a search in google for calendar table) - if you
can't solve it post back and i'll write the sql.
Nov 13 '05 #2
> A subject is uniquely identified by the combination of SITE and
SUBJECTID. A screening is uniquely identified by the combination of
SITE, SUBJECTID, and SCREENNUM.

SELECT COUNT(*) AS NumSubj FROM
(SELECT DISTINCT SITE, SUBJECTID
FROM TBLSCREEN)
GROUP BY Format(SCREENDATE,"yyyy-mm");

Is this what you are looking for?

SELECT COUNT(*) AS NumSubj, site
FROM
(SELECT COUNT(subjectid), site, screennum
FROM site
GROUP BY site, screennum) as subquery
GROUP BY site
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: soni29 | last post by:
hi, i need some help with a query, also to find out if this is even possible with sql. currently i have a table with the following data: CustomerNumber CustomerBranch 123 NULL 123 1...
5
by: Ross Presser | last post by:
The purpose, as you can probably guess, is to produce a set of sample documents from a large document run. The data row has a CLUB column and an IFC column; I want a set of samples that contains...
6
by: RCS | last post by:
I've been running into more and more complexity with an application, because as time goes on - we need more and more high-level, rolled-up information. And so I've created views, and views that use...
9
by: Kelvin | last post by:
Okay so this is baking my noodle. I want to select all the attritbutes/fields from a table but then to excluded any row in which a single attributes data has been duplicated. I.E. Here's my...
1
by: mianiro | last post by:
I want to use a distinct and a top in a select statement, however I want the distinct to only apply to one of the columns. For example SELECT DISTINCT TOP 15 col1,col2,col3,col4 from table1 I...
21
by: tizmagik | last post by:
Database consists of the following 4 tables with respective attributes: CUSTOMER(C#, CUSTOMER NAME, ADDRESS), the key is ITEM(I#, ITEM NAME, MANUFACTURER, YEAR), the key is BOUGHT(C#, I#,...
1
by: zafm86 | last post by:
Hi everyone! I'm sure my problem is pretty easy to solve but I've been working on it for a long and my my brain is not working correctly anymore. I'm working with an AS400 and I mhave to do an...
1
by: aknoch | last post by:
My basic situation is this - I ONLY want duplicates, so the opposite of DISTINCT: I have two tables. Ordinarily, Table1ColumnA corresponds in a one to one ratio with Table2ColumnB through a...
5
by: Radu | last post by:
Hi. I'm looking at a problem and I can't find any solution short of coding. I have a pool of, say, 1000 PINS. I have 7 tables (buckets). Any of the 1000 PINS can be in 0, 1, 2, or... or all 7...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.