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
A table named fruit has the attributes "ID", "Fruit name" and "Basket No":
|ID | Fruit Name | Basket No|
|1 |Apple |1 |
|2 |Banana |2 |
|3 |Orange |1 |
|1 |Apple |2 |
|2 |Banana |3 |
|3 |Orange |2 |
|4 |Mango |2 |
|5 |Grapes |1 |
|1 |Apple |3 |
I was not able to answer these question.
Can someone help me with following questions:
question 1: Find the basket number which have more than 2 fruits.
question 2: Find the basket number which contain orange.
question 3: Find the fruits which are present in more than one basket.
I dont know why people are down voting and closing these question.
question 1:find the basket number which have more than 2 fruits.
SELECT basket_no
FROM baskets
GROUP BY basket_no
HAVING COUNT(*) > 2
question 2:find the basket number which contain orange.
SELECT DISTINCT basket_no
FROM baskets
WHERE fruit_name = 'Orange'
question 3:Find the fruits which are present in more than one basket.
SELECT fruit_name
FROM baskets
GROUP BY fruit_name
HAVING COUNT(*) > 1
While #Mureinik's answer is correct and should be the accepted answer I propose the following extension to Question 3 (which seems designed to catch the unaware out):
question 3: Find the fruits which are present in more than one basket.
For example if there are two Mangoes in basket 2, Mangoes do not meet this requirement and should not be given in the result. I have the following code to take this into account:
select Name
from
(
select BasketId,
Name,
count(id) [Fruit Of This Type Per Basket]
from Fruit
group by BasketId, Name
) as SubQuery
group by Name
having count(1) > 1
This is sql server sql, not sure how standards compliant the subquery is?
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
I have next problem.
There is a table with four columns:
|ID | X | Y | VAL |
|:--|:--:|:--:|----:|
|1 | 1 | 1 | 1110|
|2 | 1 | 2 | 1720|
|3 | 1 | 3 | 2330|
|4 | 1 | 4 | 2940|
|5 | 1 | 5 | 3550|
...
When user enter some value in text field e.g. 2370 i need function to find is there exact match in VAL field and if not to find very first bigger than 2370 (2940) and return ID value.
In some other language I can do it trough dictionaries and so one but in VBA I simply don't have idea.
Any idea or help will be appreciated.
You can use a query to get this answer, using TOP 1 to just return 1 record:
SELECT TOP 1 tblData.ID, tblData.VAL
FROM tblData
WHERE (((tblData.VAL)>=2370))
ORDER BY tblData.VAL ASC;
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 1 year ago.
Improve this question
I have a table of information in sql, I have added a counter field to it, we want to fill this field based on the item field in order. Of course, the item field is variable and must have its own counter based on each type.
|item|no|
|:---|-:|
|110 | |
|120 | |
|110 | |
|150 | |
After the update
|item|no |
|:---|--:|
|110 | 1 |
|120 | 1 |
|110 | 2 |
|150 | 1 |
ok.
You can use row_number(). Assuming that you have a column that specifies ordering:
select t.*,
row_number() over (partition by item order by <ordering col>) as number
from t;
Note: If you don't care about the ordering, you can use order by item. Some databases allow row_number() without the order by as well.
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 think it is very easy, but can't figure out.
I have a table A like this:
Id|A1 |A2
1 |5 |2
2 |5 |3
3 |6 |9
4 |7 |null
and table B like this:
Id|B1
1 |1
2 |2
3 |3
4 |9
I would like to write a select with this output:
R1 | R2
5 | 1
5 | 9
6 | 1
6 | 2
6 | 3
7 | 1
7 | 2
7 | 3
7 | 9
I can get the unmatched values for a given A value:
SELECT * FROM B
WHERE B.id NOT IN (
SELECT A2 FROM A WHERE A2 = 5
)
but can't build the right SQL.
Is there anybody can help me in this subject?
Use a cross join to generate the rows and then filter out the ones that already match:
select a.a1, b.b1
from (select distinct a1 from a) a cross join
b left join
a aa
on aa.a1 = a.a1 and aa.b1 = b.b1
where aa.a1 is null;
This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 6 years ago.
I have a table that looks like this
Invoice |Line # |Item # |Price Per
1 |1 |11 |5.00
1 |2 |22 |10.00
2 |1 |11 |5.00
2 |2 |22 |12.00
3 |1 |11 |5.00
4 |1 |11 |6.00
I am trying to get the last selling price of an item.
How do I run a script that yields the following results?
Invoice |Line # |Item # |Price Per
2 |2 |22 |12.00
4 |1 |11 |6.00
I am using this script to compare to the current selling price.
Thanks
Assuming invoice and line define the ordering, then the traditional method uses row_number():
select t.*
from (select t.*,
row_number() over (partition by item order by invoice desc, line desc) as seqnum
from t
) t
where seqnum = 1;
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
I need to get the maximum score for a particular game_id, for each user:
table 'scores'
----------------------
|uid |game_id |score |
----------------------
|1 |0 |0 |
|2 |0 |0 |
|1 |0 |1 |
|1 |0 |2 |
|2 |1 |0 |
|2 |1 |3 |
|2 |1 |5 |
----------------------
I need this output (for game_id = 1):
-------------
|uid |score |
-------------
|1 | |
|2 |5 |
-------------
As you can see, user 1 doesn't have an entry for game_id 1, but I need him to be returned still...
Any help greatly appreciated!
You can do this with conditional aggregation:
select uid, max(case when game_id = 1 then score end) as game1max
from scores
group by uid;
You can do this with a self-referencing left-join:
SELECT s1.uid, MAX(s2.score) MaxSCore
FROM scores s1
LEFT JOIN scores s2
ON s1.uid = s2.uid
AND game_id = 1
GROUP BY s1.uid
or a subquery:
SELECT DISTINCT
s1.uid,
(SELECT MAX(s2.score)
FROM scores s2
WHERE s1.uid = s2.uid
AND game_id = 1) MaxScore
FROM scores s1