i make sql like this but second select not count rows correctly, and rows duplicated [closed] - sql

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
select * from (
select distinct nationality,gender,regplacetype,count(gender) as total from population
where date_part('year',age(to_date(dateofbirth,'DDMMYYYY'))) BETWEEN 16 and 67 and gender = 'Man'
group by nationality,gender,regplacetype
) as man,
(
select distinct nationality,gender,regplacetype,count(nationality) as total from population
where date_part('year',age(to_date(dateofbirth,'DDMMYYYY'))) BETWEEN 16 and 57 and gender = 'female'
group by xnationality,gender,regplacetype) as female

with man as (
select distinct nationality,gender,regplacetype,count(gender) as total from population
where date_part('year',age(to_date(dateofbirth,'DDMMYYYY'))) BETWEEN 16 and 67 and gender = 'Man'
group by nationality,gender,regplacetype
) select * from man,
union
select distinct nationality,gender,regplacetype,count(nationality) as total from population
where date_part('year',age(to_date(dateofbirth,'DDMMYYYY'))) BETWEEN 16 and 57 and gender = 'female'
group by xnationality,gender,regplacetype

Related

How to find the 3rd largest population of a country by state [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a table where there are three columns zipcode, state, total_population.
ZIPCODE STATE POPULATION
11937 36 15511
11941 36 1822
11940 36 4933
12435 36 280
12063 36 441
64733 29 1251
64734 29 1952
64735 29 13653
I am looking for this
zipcode state third_highest_population
11941 36 1822
4733 29 1251
I cannot think of a way to find the third largest population each state..any help is much appreciated
You can use window functions to help rank zip code populations by state, then keep the third ranked for each state.
SELECT *
FROM (
SELECT zipcode,
state,
total_population,
DENSE_RANK() OVER (PARTITION BY state ORDER BY total_population DESC) as ziprank
FROM yourtable
) r
WHERE ziprank = 3
RANK(), DENSE_RANK() and ROW_NUMBER() may all work here depending on your data. DENSE_RANK() insures you will get at least 1 (but maybe more) ranked at 3. The reason you may get more is if two zip codes share the same population in the state. That is highly unlikely though, so DENSE_RANK() is a good fit.
Something like:
select * from (
select your_table.*, rank() over(partition by state order by totpop) rnk from your_table
) t
where
rnk = 3
Sample Data
DECLARE #Data AS TABLE (zipcode INT,
[state] VARCHAR(10),
[total_population] INT
)
INSERT INTO #Data
SELECT 500010,'Ap',24524540 UNION ALL
SELECT 500020,'KA',47857441 UNION ALL
SELECT 500030,'TN',89456655 UNION ALL
SELECT 500040,'KL',45775475 UNION ALL
SELECT 500050,'UP',47411189
DECLARE #N INT = 3 -- Specify highest total as you required
Using Co related sub query
SELECT * FROM #Data
Go
SELECT zipcode,
[state],
[total_population]
FROM #Data e1
WHERE #N-1 = ( SELECT COUNT(DISTINCT [total_population])
FROM #Data e2
WHERE e2.[total_population] > e1.[total_population] )

I need the right query for the following [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Here is the "Customers" table
ID Name Country
1 A UK
2 B CN
3 C IN
4 D GB
5 E CN
6 F GB
7 H UK
8 I IN
Need a query to show number of customers for each country. GB and UK should be considered as single country.
Use a CASE expression:
SELECT (CASE WHEN country = 'GB' THEN 'UK' ELSE country END) as country,
COUNT(*), Country
FROM Customer
GROUP BY (CASE WHEN country = 'GB' THEN 'UK' ELSE country END);

Is there a way to break out a row if it meets a certain value? [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
For example, let's say there's a table like this:
ID Food
-- ----
1 Fruit
So whenever it says Fruit, I want to break it out into 3 rows:
ID Food
-- ----
1 Apple
1 Banana
1 Orange
Any tips?
Hmmm . . . You could do:
select t.id, coalesce(a.alt, t.food) as food
from t outer apply
(select alt
from (values ('Apple'), ('Banana'), ('Orange')) v(alt)
where t.food = 'Fruit'
) a;
select ID, Food FROM YourTable WHERE food <> 'Fruit'
UNION
select ID, f2 FROM YourTable JOIN
(SELECT'Apple' f2
union
SELECT 'Banana' f2
UNION
SELECT 'ORANGE' f2
) DT
ON food = 'Fruit'

SQL sub query. Dont know how to start [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
The table is called PEOPLE and has the columns: id, fname, lname, state, jobtitle, salary, cat
I want to display the 50 states, the number of people in the state with a Cat value of Y, the number of people in the state with a Cat value of N and the total number of people in the state. The four column headings should be State, Yes, No and Total.
I know that we are supposed to use sub queries but not sure how to begin.
SELECT State
, SUM(CASE WHEN Cat = 'Y' THEN 1 ELSE 0 END) as Yes
, SUM(CASE WHEN Cat = 'N' THEN 1 ELSE 0 END) No
, COUNT(*) as Total
FROM PEOPLE
GROUP BY STATE
Here:
SELECT upper(p.state) State,
(SELECT count(*) from PEOPLE p2 where p2.state = p.state and p2.cat = 'Y') Yes,
(SELECT count(*) from PEOPLE p3 where p3.state = p.state and p3.cat = 'N') No,
count(*) Total
FROM PEOPLE p
GROUP BY upper(state)
If you have any doubt about it, fell free to ask.

Select rows that if I sum their value = 0 from 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
Let's say I have a table like this:
ID. Location. Value.
1. AGF. 10.00
2. VHJ. -20.00
3. AGF. -20.00
4. AGF. 5.00
5. KLZ. 50.00
6. AGF. 10.00
I want to select the rows that have same Location AND whose Values sum to zero.
In this case the result should be:
1
3
6
because those rows are all in Location AGF and they sum to 0 (10 + -20 + 10).
Try:
Select ID from YourTable where Location IN(
Select location from YouTable
Group By Location
Having sum(Value) = 0
)
You need to find all locations with zero sum using grouping and group filters (group by and having clauses respectively). This can be done in a subquery. Then select all IDs with the just selected locations.
select ID
from YOUR_TABLE
where Location in (
select Location
from YOUR_TABLE
group by Location
having sum(Value) = 0
)
You could use GROUP BY and HAVING, like this:
Select ID from tablelocation where Location IN(
Select location from tablelocation
Group By Location
Having sum(Value) = 0
)