How to remove null value from distinct column of a table in SQL? - sql

I want only distint city from a table but that column contains null value i dont it to come in column when i do distinct city.
select distinct city from billingsystem
the output am getting is
newyork
london
dallas
null
charles
I want output to be as
newyork
london
dallas
charles
can anybody help me out in doing this.

Use this
select distinct city from billingsystem where city is not null

The given query is correct
The Distinct keyword it use to select the distinct values only, In where condition we where mentioned like IS NOT NULL condition so from here you can get the exact answer.
select distinct city
from billingsystem
where city is not null

please try below query
select distinct city from billingsystem where city is not null

Related

How to select the record with two different postcode in ms access with sql query

I'm new in sql script. I'm currently trying to write a sql script to select the record that with two different postcode from a table in ms access with sql query. For some reason this is just not working for me.
Example:
I have the following table resident_postcode :
Postcode Name
1001 Alan
1002 James
1003 Alan
1004 Merry
1001 Merry
I write a sql script to select the name that have 2 different postcode and looking for the output like following:
Name
Alan
Merry
I have tried to run the following script but for some reason this is just not working in MS Acess:
SELECT a.Name
FROM resident_postcode a
WHERE 1 < (SELECT count(b.Postcode) FROM resident_postcode b WHERE b.Name= a.NameGROUP BY b.Name)
Does anyone know what is going wrong with my script?
Try with:
SELECT [Name]
FROM resident_postcode
GROUP BY [Name]
HAVING Count(*) > 1
If there is no repetition of same postcode for any name then you can have names with two post codes as below
SELECT NAME FROM RESIDENT_POSTCODE
GROUP BY NAME
HAVING COUNT(POSTCODE)=2
But if any postcode can be specified more than once (for example 1001 is specified more than once for Alan) then you need to use below query:
select name from (
SELECT distinct NAME, postcode FROM RESIDENT_POSTCODE)
group by name
having count(*)=2
if you want all names having two or more than 2 postcode then use below query:
select name from (
SELECT distinct NAME, postcode FROM RESIDENT_POSTCODE)
group by name
having count(*)>=2
If you want 2 or more different post codes, then you can use:
SELECT NAME
FROM RESIDENT_POSTCODE
GROUP BY NAME
HAVING MIN(POSTCODE) <> MAX(POSTCODE);
If you want exactly two, then you can use:
SELECT NAME
FROM (SELECT DISTINCT NAME, POSTCODE
FROM RESIDENT_POSTCODE
) AS NP
GROUP BY NAME
HAVING COUNT(*) = 2;

sql, select only such rows that share a predefined value

I am trying to select such rows that have the same specific value in one of the columns. For example, in the table below there are airlines that fly to different cities. I need to select only such airlines that fly exclusively to the usa. In the table below that would be only the airline2. The city is basically not important for the moment.
airline country_destination city_destination
airline1 usa washington
airline1 eng london
airline1 fra paris
airline2 usa new york
airline2 usa chicago
airline2 usa washington
airline3 can montreal
airline3 usa new york
airline3 can toronto
My first guess returns all the airlines, because in every of them the usa appears at least once.
select distinct airline from table1 where country_destination = 'usa'
I assume I need a nested 'select' and probably a group by airlines? Somewhere directionof what I have below? But I am stuck at this point. Any help is highly appreciated!
select airline, country_destination
from (select airline, country_destination from table1 where country_destination = 'usa' group by airline)
You do need to aggregate.
This is the simplest way I know of to do it:
select airline
from table1
group by airline
having min(country_destination) = max(country_destination)
and min(country_destination) = 'usa';
One method is to check if the airline's row count matches the conditional count (in this case, the amount of rows where the destination is 'usa').
The CTE aggregates the airline data. In the SELECT-statement you can apply the filter to only include airlines where the total row count equals the row count for destination 'usa'. If the counts between count_all and count_usa differ you know there were other country destinations.
with counts as ( select airline,
count(*) as count_all,
sum(case when country_destination == 'usa' then 1 end) as count_usa
from table1
group by 1 )
select airline
from counts
where count_all = count_usa;
you can use below with inner query-
select * from
(select distinct airline, country_destination from table1 ) t
group by airline
having count(airline) = 1 AND country_destination='usa';

SQLite query to get table based on values of another table

I am not sure what title has to be here to correctly reflect my question, I can only describe what I want.
There is a table with fields:
id, name, city
There are next rows:
1 John London
2 Mary Paris
3 John Paris
4 Samy London
I want to get a such result:
London Paris
Total 2 2
John 1 1
Mary 0 1
Samy 1 0
So, I need to take all unique values of name and find an appropriate quantity for unique values of another field (city)
Also I want to get a total quantity of each city
Simple way to do it is:
1)Get a list of unique names
SELECT DISTINCT name FROM table
2)Get a list of unique cities
SELECT DISTINCT city FROM table
3)Create a query for every name and city
SELECT COUNT(city) FROM table WHERE name = some_name AND city = some_city
4)Get total:
SELECT COUNT(city) FROM table WHERE name = some_name
(I did't test these queries, so maybe there are some errors here but it's only to show the idea)
As there are 3 names and 2 cities -> 3 * 2 = 6 queries to DB
But for a table with 100 cities and 100 names -> 100 * 100 = 10 000 queries to DB
and it may take a lot of time to do.
Also, names and cities may be changed, so, I can't create a query with predefined names or cities as every day it's new ones, so, instead of London and Paris it may be Moscow, Turin and Berlin. The same thing with names.
How to get such table with one-two queries to original table using sqlite?
(sqlite: I do it for android)
You can get the per-name results with conditional aggregation. As for the total, unfortunately SQLite does not support the with rollup clause, that would generate it automatically.
One workaround is union all and an additional column for ordering:
select name, london, paris
from (
select name, sum(city = 'London') london, sum(city = 'Paris') paris, 1 prio
from mytable
group by name
union all
select 'Total', sum(city = 'London'), sum(city = 'Paris'), 0
from mytable
) t
order by prio, name
Actually the subquery might not be necessary:
select name, sum(city = 'London') london, sum(city = 'Paris') paris, 1 prio
from mytable
group by name
union all
select 'Total', sum(city = 'London'), sum(city = 'Paris'), 0
from mytable
order by prio, name
#GMB gave me the idea of using group by, but as I do it for SQLite on Android, so, the answer looks like:
SELECT name,
COUNT(CASE WHEN city = :london THEN 1 END) as countLondon,
COUNT(CASE WHEN city = :paris THEN 1 END) as countParis
FROM table2 GROUP BY name
where :london and :paris are passed params, and countLondon and countParis are fields of the response class

SQL SELECT Percentage of Rows Based on Column Values

I am looking to return 15% of rows based on column values. For example, I have a citizenship column and gender column as well as name email,etc. I want to return 15% of each scenario in those columns. If citizenship = USA I would want 15% of total rows with USA and male another 15% of USA and female and another of 15% of USA and unknown. The same would go for each other citizenship in my result (ie Chinese, Canadian, etc.)
I am able to get 15% of all rows, but not based on column values.
A very stripped down query looks something like this.
SELECT TOP 15 PERCENT FROM (
SELECT name
, email
, citizenship
, gender
FROM bio) a
You want a stratified sample. You can do this using:
select top 15 percent b.*
from bio b
order by row_number() over (partition by citizenship, gender order by (select null));

How to join different columns of same table?

Suppose I have one table with two column, Country and City.
Country
USA
Canada
UK
City
NY
London
I want to join/merge both column records and expect the output like this -
USA
Canada
UK
NY
London
So, what will be the SQL query to merge different columns records of same table?
SELECT Country FROM TABLE
UNION
SELECT City FROM Table
should do it.
Responding to the comment "I am searching for any quick way. Because if I need to merge 10 columns then i have to write 10 Unions! Is there any other way?":
You can use an unpivot, which means you just need to add the column names into a list. Only thing is to watch for data types though. eg:
--CTE for example only
;WITH CTE_Locations as (
select Country = convert(varchar(50),'USA'), City = convert(varchar(50),'NY')
union select Country = 'Canada', City = 'Vancouver'
union select Country = 'UK', City = 'Manchester'
)
--Select a list of values from all columns
select distinct
Place
from
CTE_Locations l
unpivot (Place for PlaceType in ([Country],[City])) u