How Do I Exclude Data Based on Column Values in SQL? - sql

I have a table which is set up like this:
Name QuestionCd Response
John Smith 837987 3
John Smith 837988 NULL
John Smith 837991 3
John Smith 837996 3
John Smith 838003 NULL
Mary Smith 837987 1
Mary Smith 837988 1
Mary Smith 837991 3
Mary Smith 837996 1
Mary Smith 838003 5
I need to bring in the customer who has a response for all 5 questions, which would be Mary Smith. I've tried to use a case statement to flag the rows that have a response but that would be incorrect since John Smith did not answer all of the questions.
I've tried the following:
-- Case when QuestionCd between '837987' and '838069' and response is null then '' else 'X' end
-- Case when QuestionCd between '837987' and '838069' and Response is null then ''
when QuestionCd between '837987' and '838069' and Response is not null then 'x' end
Any ideas or tips on how to accomplish this? Thank you kindly for your help.

This is how I would do it.
SELECT Name
FROM
(
SELECT Name, QuestionCd
FROM <YourTable>
WHERE NOT Response IS NULL
GROUP BY Name, QuestionCd
) SQ
GROUP BY Name
HAVING COUNT(QuestionCd)=5
The sub query is to have a distinct list of question answered since Mary Smith or John Smith may have answered the question more than one.

If the question ID's are fixed you can just return the name of the person who has answered 5 unique questions
SELECT Name FROM tbl
GROUP BY Name
HAVING count(QuestionCD) = 5
Edit - fixed SQL

Related

How to return a count of duplicates and unique values into a column in Access

I currently have this table:
First_Name
Last_Name
Jane
Doe
John
Smith
Bob
Smith
Alice
Smith
And I'm looking to get the table to look for duplicates in the last name and return a value into a new column and exclude any null/unique values like the table below, or return a Yes/No into the third column.
First_Name
Last_Name
Duplicates
Jane
Doe
0
John
Smith
3
Bob
Smith
3
Alice
Smith
3
OR
First_Name
Last_Name
Duplicates
Jane
Doe
No
John
Smith
Yes
Bob
Smith
Yes
Alice
Smith
Yes
When I'm trying to enter the query into the Access Database, I keep getting the run-time 3141 error.
The code that I tried in order to get the first option is:
SELECT first_name, last_name, COUNT (last_name) AS Duplicates
FROM table
GROUP BY last_name, first_name
HAVING COUNT(last_name)=>0
You can use a subquery. But I would recommend 1 instead of 0:
select t.*,
(select count(*)
from t as t2
where t2.last_name = t.last_name
)
from t;
If you really want zero instead of 1, then one method is:
select t.*,
(select iif(count(*) = 1, 0, count(*))
from t as t2
where t2.last_name = t.last_name
)
from t;

SQL Server group by? [duplicate]

This question already has answers here:
Retrieving last record in each group from database - SQL Server 2005/2008
(2 answers)
Closed 4 years ago.
I'm not sure how to word my question so perhaps an example would be best. I'm looking for a function or statement that would produce the following result from a single table. For each name, return the row with largest id.
ID NAME ADDRESS
1 JOHN DOE 123 FAKE ST.
2 JOHN DOE 321 MAIN ST.
3 JOHN DOE 333 2ND AVE.
4 MARY JANE 222 1ST. AVE
5 MARY JANE 444 POPLAR ST.
6 SUZY JO 999 8TH AVE.
DESIRED RESULT
3 JOHN DOE 333 2ND AVE.
5 MARY JANE 444 POPLAR ST.
6 SUZY JO 999 8TH AVE.
One option is to use the row_number window function. This allows you to establish a row number to the result set. Then you can define the grouping and ordering within the over clause, in this case you want to partition by (group) the name field and order by the id field descending. Finally you filter those results where rn = 1 which returns the max result for each grouping.
select *
from (
select *, row_number() over (partition by name order by id desc) rn
from yourtable
) t
where rn = 1

Sorting Middle string

I have the following values in a column
Name
Smith
Marry
Tom
Robert
Albert
I have to display in the following order. I want MARRY on top. For the rest of the values ordering is not matter.
Name
Marry
Smith
Tom
Robert
Albert
How can I achieve this?
You can use case in order by:
order by (case when name = 'Marry' then 1 else 2 end)

Rollup a table eliminating null values

I'm pretty sure this should be easy, I just don't know what function or whatever I need to get it done. I've got a table that has the number of hours worked in a day, # of documents reviewed, and the date they were reviewed.
I've been able to combine and pivot this out into a nice report of the person and the # of hours or docs. However, it's got null values throughout.
First Last 11/30/2013 12/1/2013 12/2/2013
John Doe 11 NULL NULL
John Doe NULL 12 NULL
John Doe NULL NULL 8
John Doe NULL NULL NULL
I need it to look like this
FirstName LastName 11/30/2013 12/1/2013 12/2/2013
John Doe 11 12 8
Jane Smith 13 5 8
Figured it out, why it took me so long... I'm going to blame it on it being late...
Select Employee_ID
,SUM(#Temp3.[1])
,SUM(#Temp3.[2])
,SUM(#Temp3.[4])
,SUM(#Temp3.[5])
,SUM(#Temp3.[6])
,SUM(#Temp3.[7])
From #Temp3
Group By Employee_ID

SQL Query that leaves duplicate fields blank?

What I would like is if my data is this:
ID Name
0 Jim
1 Dave
1 Bob
1 John
2 Ann
My query returns this:
ID Name
0 Jim
1 Dave
Bob
John
2 Ann
Is there a simple way to do this?
Edit:
The query which returns the original 2 columns of data would be something like this:
SELECT ID, NAME
FROM TestTable
ORDER BY ID