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

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.

Related

Create CASE WHEN labels based on DISTINCT values in a particular column [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 2 years ago.
Improve this question
I have data of the following form:
ID
Category
Amount
1
A
100
1
B
200
1
B
150
1
C
500
2
B
20
3
A
100
1
B
100
I wish to GROUP BY the column ID, find out the DISTINCT types of Category present for each ID group and create a new column where I can create the following classification labels for each grouped ID based on the unique or distinct categories present and also calculate the corresponding sum of amount for each grouped ID. So the output should be the follows:
ID
Classification
Sum of Amount
1
ALL
950
1
B only
20
1
A and B only
200
I tried the following SQL code but it doesn't work, most likely because DISTINCT() command within a CASE WHEN statement cannot consider multiple values.
My query:
SELECT
ID,
(CASE WHEN DISTINCT(CATEGORY) IN ("A") then "A Only" WHEN WHEN DISTINCT(CATEGORY) IN ("B") THEN "B only"..........)
SUM(AMOUNT)
FROM Table
GROUP BY 1,2
I have tried multiple ways of using the DISTINCT statement with CASE WHEN but none of them works.
Hmmm . . . How about this?
select id,
(case when min(category) = max(category)
then min(category) || ' only'
when count(distinct category) = 3
then 'All'
when min(category) is NULL
then 'None'
else min(category) || ' and ' || max(category)
end)
from t
group by id;

i make sql like this but second select not count rows correctly, and rows duplicated [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 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

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);

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
)

select only and only specific record in oracle [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a table with truck and locations , each truck can have many records with each location:
ex:
truck | location
----------------
60111 | 1
60111 | 2
60111 | 3
60111 | 4
60222 | 1
60222 | 2
....etc
I want to select the trucks with ONLY locations in (1,2).
in my example the return must be 60222 ONLY
how to do this?
Note: I want a dynamic solution not hard coded.
A solution similar to the one of Patrick Hofman is to move the logic in the HAVING clause
SELECT truck
FROM table
GROUP BY truck
HAVING COUNT(DISTINCT location) = 2
AND SUM(CASE WHEN location IN (1, 2) THEN 0 ELSE 1 END) = 0
The first condition return the truck with only two distinct location, not checking their values, the second condition force those location to be 1 and 2
This will select trucks that have exactly locations 1 and 2:
select t1.truck
from truck_location t1
where t1.location in (1, 2)
group by truck
having count(distinct t1.location) = 2
and count(distinct t1.location) = (select count(*)
from truck_location t2
where t2.truck = t1.truck);
The distinct inside the count() is only necessary if a truck could be assigned to the same location twice. If the combination truck/location is guaranteed to be unique this is not necessary.
SQLFiddle: http://sqlfiddle.com/#!15/b865f/1
select distinct truck
from
my_table
where
truck not in
(
select distinct truck
from my_table
where location not in (1, 2)
)
You can group by the truck, filter on the location and check the result is 2:
select truck
from table
where location in (1, 2)
group
by truck
having count(distinct location) = 2