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 some tables following
Room(CodeR, NameR)
Title(CodeT,NameT)
Member(ID,Name, Salary, Sex, CodeR, CodeT)
I want write the sql to select Roooms that member less than 5 and print all member of this room.
Please help me solve this problem
pls help me remove this topic. i can not ask any question.
select R.CodeR, NameR, m.Id, m.Name FROM
Room R INNER JOIN Member m on
R.CodeR = m.CodeR
WHERE R.CodeR in
(SELECT iM.CodeR FROM Member iM GROUP BY iM.CodeR HAVING COUNT(*) <5)
First of all pull all the rooms where no of members are less than 5
SELECT iM.CodeR FROM Member iM GROUP BY iM.CodeR HAVING COUNT(*) <5
Then pull out room and member information as required using above result.
select R.CodeR, NameR, m.Id, m.Name FROM
Room R INNER JOIN Member m on
R.CodeR = m.CodeR
WHERE R.CodeR in (X, Y, Z) //X, Y, Z will be replaced by subquery
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 3 years ago.
Improve this question
I have a table that contains a list of debts owed by customers. I need to write a SQL query to find which customers have an increasing amount of debt each year. Basically, I am trying to find the "bad apples", the people who continually get debts, in an increasing amount, every year. I suppose I am looking for a trend?
I have created a SQL Fiddle with some sample data. In the example, I don't care about customerId 2174 as they have only had two debts, many years ago, however, customerId 5218 has had an increasing amount of debts over the last 3-4 years.
Ideally, I'd like a list of customerIds that show a "trend" of increasing quantity of debts. i.e. they have 1 in 2015, 5 in 2016, 30 in 2017 etc.
You may try this.
; with cte as
(SELECT customerId
,YEAR(debtDate) AS debtYear
,COUNT(*) AS debtCount
FROM dbo.Debts
----- apply year filter over there if want to select record for particular year range
GROUP BY YEAR(debtDate)
,customerId
)
, CT AS
(
SELECT C.* FROM CTE AS C
CROSS APPLY (
SELECT * FROM CTE AS CC
WHERE C.customerId=CC.customerId
AND C.debtYear>CC.debtYear
AND C.debtCount>=CC.debtCount
) AS D
)
SELECT DISTINCT customerId FROM CT --- IF NEED JUST CUSTOMER NAME
------- OR IF NEED ALL DETAILS ADD debtYear, debtCount COLUMN
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 9 years ago.
Improve this question
Given the table Stuff
IdStuff StuffName IdLanguage
------- --------- ----------
1 Stuff 1 1
1 Stuff 2 2
and the view: vwLanguages
IdLanguage Name
---------- ----
1 Language 1
2 Language 2
3 Language 3
I would like to obtain:
IdStuff StuffName IdLanguage
------- --------- ----------
1 Stuff 1 1
1 Stuff 2 2
1 NULL 3
I've tried with RIGHT JOIN, but I did't succeed...
SELECT
S.IdStuff, S.StuffName, vwLanguages.IdLanguage
FROM
Stuff S
RIGHT JOIN vwLanguages ON vwLanguages.IdLanguage = S.IdLanguage
The problem is that only 2 rows are shown, for language 1 and language 2...
RIGHT JOIN is almost never the correct approach. I think what you're looking for is a CROSS JOIN between distinct stuff IDs and language rows, and then a LEFT JOIN to the stuff details if available.:
SELECT
subStuff.IdStuff,
s.StuffName,
l.IdLanguage
FROM
(
SELECT DISTINCT IdStuff
FROM Stuff
) subStuff
CROSS JOIN vwLanguages l
LEFT JOIN Stuff s
ON s.IdStuff = subStuff.IdStuff
AND s.IdLanguage = l.IdLanguage
SQL Fiddle
The reason that you need the CROSS JOIN is that you're basically wanting each distinct ID from stuff to be matched up with each record from language, even if there's no matching IdLanguage. Once you have this Cartesian product of these two sets, you then want to get the StuffName if it happens to have a corresponding stuff ID and language ID.
The implementation is a little hard to explain because the requirements are a little weird, and don't really match up with your table structure.
For example, why is IdStuff not unique in the stuff table?
I'm assuming this is a contrived example, but it raises a lot of other questions.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
if there are tables customer, order, and product then (online shopping)
1) all customers who have purchased more than one products in same order
2) top 10 customers who have spent most money
if there are doctors, patients and appointments then (hospital management)
1) all patients who have been diagnosed by more than one doctors
2) top 10 patients who have most appointments
if there are students, teachers and courses (school management system)
1) all students who have taken more than one course from same teacher
2) top 10 students with respect to scores
i'll do first one :
schema should be something like this customer--> orders-->products
1. select *,count(*) from customers c join order o on c.order_id=o.orders_id join products p on
p.product_id=c.product_id where count(*)>1 group by c.order_id,p.product_id
2.select top 10 from customers order by spent_money desc
SELECT patients, doctors FROM hospital_management GROUP BY patients,doctors HAVING COUNT(doctors) > 1
SELECT TOP 10 FROM hospital_management ORDER BY appointments DESC
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Here is pics of Relationship and Pasenger_detail form and i wrote query to select seat_no but its not working properly. it is only showing all seats as per bus reserved id. but requirement were to show only those which are not yet selected. here is query
"Select seat_no.seat_no FROM Seat_No Where seat_no.seat_no <= (select br_info.Seats_Reserved from br_info where ((Br_info.br_id)=[forms]![pasenger_detail]![br_id]) AND (Seat_No.seat_no) NOT IN (SELECT Pasenger_Detail.Seat_No FROM Pasenger_Detail WHERE (((Pasenger_Detail.Group_ID)=[forms]![Pasenger_Detail]![Group_ID]) AND ((Pasenger_Detail.BR_ID)=[forms]![Pasenger_Detail]![BR_ID]))));"
SELECT S.Seat_No
FROM Seat_No AS S
WHERE S.Seat_No Not In
(
SELECT P.Seat_No
FROM BR_Info AS B INNER JOIN Pasenger_Detail AS P ON B.BR_ID = P.BR_ID
WHERE B.BR_ID = Forms!pasenger_detail!BR_ID
AND B.Group_ID = Forms!Pasenger_Detail!Group_ID
)
In the above query I am joining BR_Info and Passenger_Detail together. It is an INNER JOIN which means that the record has to exist in both BR_Info and Passenger_Detail in order to be considered. Then I am using Not In to get all the Seat_No that are not contained in the sub query.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want a simple query to get some output. Records are
ID Count
215 1
215 1
215 1
215 1
Output must be
ID Count
215 4
SELECT ID, COUNT(*) AS Count
FROM tablename
GROUP BY ID
Very simple,
SELECT ID ,COUNT(*) AS CNT
FROM TABLE_NAME
GROUP BY ID
SELECT ID, sum(Count) AS Count
FROM tablename
GROUP BY ID