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 4 years ago.
Improve this question
I have a table of Neighborhood that has primary key - NID and a table of Apartment that has a foreign key NeighborhoodID.
How do I create a view that show in each neighborhood how much apartments there.
Thank you!
I'd join the tables on the neighbourhood id, group by it, and count the rows:
CREATE OR REPLACE VIEW neighbourhood_apartments AS
SELECT n.name, COUNT(*)
FROM neighbourhood n
JOIN apartment a ON n.nid = a.neightbourhoodid
GROUP BY n.name
For each id in neighborhood count the rows in apartment that match the neighborhoodid:
SELECT
neighborhood.id,
neighborhood.name,
(SELECT COUNT(*) FROM apartment AS a WHERE a.neighborhoodid = n.id) AS counter
FROM
neighborhood AS n
Related
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 7 months ago.
Improve this question
SELECT DISTINCT CITY FROM STATION WHERE MOD(ID,2)=0 ORDER BY CITY ASC;
SELECT DISTINCT CITY FROM STATION
Primary command to select data from the database. It is asking to select CITY from the table STATION whose data is unique. Thus, no duplicates are produced in result.
WHERE MOD(ID, 2) = 0
Only select those that have an even number ID.
ORDER BY CITY
Sort the results with respect to CITY names.
ASC
Sort in ascending order; which means that cities with names starting with A will come before those that have Z as first letter.
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
Permission to ask, why is this the error? I try
select user_idea.idea_id,user_idea.title, idea_events.events, idea_categories.category_name AS category
from user_idea
LEFT join idea_events
on user_idea.idea_id=idea_events.idea_id
LEFT JOIN idea_categories
ON user_idea.idea_categories=idea_categories.category_id;
The result is sqliteonline like this:
I want to combine Dewasa and Anak events because of the same idea. For category why is the result NULL for values 1;2 not read in the idea_category table which should be Mudah; Sedang.
For table:
Output expected:
select user_idea.idea_id,user_idea.title,
(SELECT GROUP_CONCAT(events,';') FROM idea_events Where user_idea.idea_id=idea_events.idea_id) as events,
(SELECT GROUP_CONCAT(category_name ,';') FROM idea_categories Where user_idea.idea_categories=idea_categories.category_id) AS category
from user_idea;
1:2 idea_categories in user_idea is not considered as relevant to join so when joined it cannot find the data in other table here so data is null
reminder
before doing join just analyse the data anomalies like not having common data and properly formatted
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 this table:
BANK - BRANCH_NAME - ADDRESS - DISTRICT - CITY - PHONE - FAX - OPENNING DATE
How do I bring branches on a city basis?
What I expect is a city knows how many branches it has.
From the comments:
what I expect is a city knows how many branches it has
You can use aggregation:
select city, count(*) no_branches from mytable group by city
If (city, branch) tuples are not unique in the table, then:
select city, count(distinct branch) no_branches from mytable group by city
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 4 years ago.
Improve this question
I have a table name customers with two columns, case id and owner. I need to write a query to select random 5 caseids for every name in owner column.please help
For a start, you need something like:
SELECT TOP 5
ID,
[Case ID],
[Owner],
Rnd(-Timer()*[ID]) AS RandomRecord
FROM
[Cases]
ORDER BY
Rnd(-Timer()*[ID]);
to be used as a subquery filtered on OwnerID of your Owners' table.
I once posted an article on this with a lot more details:
Random Rows in Microsoft Access
You can use in:
select t.*
from t
where t.id in (select top 5 id
from t as t2
where t2.name = t.name
order by Rnd(-Timer()*[ID])
);
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 8 years ago.
Improve this question
have a table EMP having two columns ID, Name
EMP contains lots of entries, obviously, repeated entries.
I need to print those entries which are being repeated, but, i don't have to look on NAME column, on the basis of id only i want to print the repeated entries.
Please help me out. Thanks in advance.
To display all the repeated ids:
SELECT ID
FROM EMP
GROUP BY ID
HAVING COUNT(*) > 1
To also print the names:
SELECT ID, NAME
FROM EMP
WHERE ID IN
(
SELECT ID
FROM EMP
GROUP BY ID
HAVING COUNT(*) > 1
)