Excluding Rows Based on Column Contents - sql

I'm trying to write a query which will exclude rows based on their common value in a particular column if a value in another column exists. Essentially:
SELECT Phone, Name, Age
FROM Customer
WHERE etc etc etc
What I would like to do is: If, for example, one customer's information would be: 111-222-3333, Rick, 45, and for whatever reason 45 is the particular value for which if it exists, I'd like to exclude all subsequent rows which match that corresponding telephone number, how would I do this?
Thanks!

Sounds like a WHERE NOT EXISTS may work here,
SELECT Phone, Name, Age
FROM Customer C
WHERE NOT EXISTS (
SELECT * FROM Customer M
WHERE M.Age = 45 AND M.Phone = C.Phone
)

Related

Return one result for each search value

How can I return a unique value based on a set of non unique values being searched?
For example:
If I wanted to return one phone number for a list of 4 people who can have more than one phone number - but I can only use one phone number for each person. It doesn't matter which phone number I use to reach them because any number that belongs to them will get me to them.
I don't think something like this exists - but if I could use something like the DISTINCT modifier except it would be called FIRST - it would solve my problem:
SELECT FIRST ID
FROM Sample_Table
WHERE ID in ("Bob", "Sam", "Kyle", "Jordan")
In picture - from this
I'd like that (or any) query to return
.
I'm using this type of query in a db where for 200 "ID"s there are millions of "Unique Values", so it is hard to get crafty.
EDIT The Unique value in my db has numbers and letters in each value
There is no such thing as a "first id" in a SQL table. Tables represent unordered sets. You can accomplish what you want (if I understand correctly) using aggregation:
SELECT ID, MIN(UniqueValue)
FROM Sample_Table
WHERE ID in ('Bob', 'Sam', 'Kyle', 'Jordan')
GROUP BY ID;
using group by and max method can help you:
select id
,uniquvalue
,max (typeofvalue)
from Sample_Table
group by
id
,uniquvalue

Determine the number of times a null value occurs in column B for a distinct value in column A, SQL table

I have a SQL table with "name" as one column, date as another, and location as a third. The location column supports null values.
I am trying to write a query to determine the number of times a null value occurs in the location column for each distinct value in the name column.
Can someone please assist?
One method uses conditional aggregation:
select name, sum(case when location is null then 1 else 0 end)
from t
group by name;
Another method that involves slightly less typing is:
select name, count(*) - count(location)
from t
group by name;
use count along with filters, as you only requires Null occurrence
select name, count(*) occurances
from mytable
where location is null
group by name
From your question, you'll want to get a distinct list of all different 'name' rows, and then you would like a count of how many NULLs there are per each name.
The following will achieve this:
SELECT name, count(*) as null_counts
FROM table
WHERE location IS NULL
GROUP BY name
The WHERE clause will only retrieve records where the records have NULL as their location.
The GROUP BY will pivot the data based on NAME.
The SELECT will give you the name, and the COUNT(*) of the number of records, per name.

Create view with except condition

Example i want to create a view name TEST
which i already created a view before that
View name : customer
Inside customer View:
//CUSTOMER
NAME ID ADDRESS AGE SEX TELNO EMAIL
-----------------------------------------------------------------------
CHRIS 1 12321312 21 F 646885 ascs#gmail
JOHN 2 SADASDSA 23 M 5452131 asd#gmail
MAY 3 LKJLKJLKJ 32 F 645643 cxz#gmail
So i want to create a view name TEST that will store all column inside CUSTOMER but EXCEPT TELNO EMAIL.
so i used this query:
CREATE VIEW TEST AS
SELECT * FROM CUSTOMER
EXCEPT
SELECT TELNO,EMAIL FROM CUSTOMER;
But i fail to work, got errors come out. SQL command not properly end and point to EXCEPT, what's wrong?
You have to list all the columns that you want explicitly:
CREATE VIEW TEST AS
SELECT NAME, ID, ADDRESS, AGE, SEX
FROM CUSTOMER;
There is no way to exclude some columns from a * list.
EXCEPT is an operator in SQL Server. The equivalent in Oracle is MINUS. However, this works at the row level, not at the column level.
If you want to get all the columns in the table, except for those two, you can use all_tab_columns:
select column_name
from all_tab_columns
where lower(table_name) = 'customer' and
lower(column_name) not in ('telno', 'email');
You can then paste them into the select clause.

SQL using where contains to return rows based on the content of another table

I need some help:
I have a table called Countries, which has a column named Town and a column named Country.
Then I have table named Stores, which has several columns (it is a very badly set up table) but the ones that are important are the columns named Address1 and Address2.
I want to return all of the rows in Stores where Address1 and Address2 contains the towns in the Countries table.
I have a feeling this is a simple solution but I just can't see it.
It would help if maybe you could use WHERE CONTAINS but in your parameters search in another table's column?
e.g.
SELECT *
FROM Stores
WHERE CONTAINS (Address1, 'Select Towns from Countries')
but obviously that is not possible, is there a simple solution for this?
You're close
SELECT * FROM Stores s
WHERE EXISTS (
SELECT * FROM Countries
WHERE CONTAINS(s.Address1, Town) OR CONTAINS(s.Address2, Town)
)
This would be my first attempt:
select * from stores s
where
exists
(
select 1 from countries c
where s.Address1 + s.Address2 like '%'+c.Town+'%'
)
Edit: Ooops just saw that you want the 'CONTAINS' clause. Then take Paul's solution

Query based on multiple columns

I have a data table with health premium rates that depend on age, gender, and zip code. I want to create a query that gives me just the lowest price premium by a particular plan name/gender/zipcode combo.
Example, what's the lowest price rate?
Zip Gender Age Lowest_price_rate
======================================================
10001 F 90 ?
90210 M 65 ?
I'm assuming your're using some form of SQL and that you have the rates in the table already.
Group the entries by whatever you want and select the minimum one. The query might look something like this:
SELECT Zip,Gender,Age,MIN(rate) AS Lowest_price_rate FROM MyTable GROUP BY Gender,Age,Zip
Of course, you can put a standard WHERE clause in right after the FROM MyTable part if you want to look at only certain criteria. That query is untested, but it should work.
SELECT MIN(Lowest_price_rate) FROM TABLE_NAME WHERE plan_name ='' AND gender = '' AND Age = ''
Use the above query with the necessary values. (Tablename and values for the fields)