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 8 days ago.
This post was edited and submitted for review 7 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Have a tabel like below
CustId|Address|OrderId|status
------|-------|-------|------
123 |abc |123 |Received
123 |abc |123 |inprogress
123 |abc |123 |Delivered
234 |abcd |1234 |Delivered
234 |abcd |2344 |Delivered
34 |afg |12345 |inprogress
34 |dgh |22 | Delivered
335 |gh |56 |Delivered
335 |vbv |89 |Delivered
In this above example need output
CustId|Address|OrderId|status
------|-------|-------|------
234 |abcd |1234 |Delivered
234 |abcd |2344 |Delivered
335 |gh |56 |Delivered
335 |vbv |89 |Delivered
That means customer details those order is only in Delivered status if that customer involved in another status we will not consider it.
Need help in SQL queries
Related
Table1:
ID|Days between installments |Number_of_Payments|First_payment_date|Last_payment_date**
001| 7 |5 | 2022-01-01 | 2022-01-29
002| 7 |2 | 2022-10-01 | 2022-10-08
003| 5 |3 | 2022-05-01 | 2022-05-15
004| 3 |1 | 2022-11-01 | 2022-11-01
Table2:
ID |Payment_date|Payment_amount
001|2022-01-01 |100
001|2022-08-01 |50
001|2022-15-01 |20
001|2022-22-01 |50
001|2022-29-01 |20
002|2022-10-01 |500
002|2022-10-08 |400
003|2022-05-01 |350
003|2022-05-10 |250
003|2022-05-15 |200
004|2022-11-01 |900
My final table should look like:
ID|First_payment_date|First_Installment_date|First_Payment_date|First_Payment_Amount|Second_Installment_date|Second_Payment_date|Second_Payment_Amount|Third_Installment_date|third_Payment_date|Third_Payment_Amount|Fourth_Installment_date|Fourth_Payment_date|Fourth_Payment_Amount|Fifth_Installment_date|Fifth_Payment_date|Fifth_Payment_Amount|Last_payment_date**
001|2022-01-01|2022-01-01|100|2022-08-01|2022-08-01|50|2022-15-01|2022-15-01|20|2022-22-01|2022-22-01|50|2022-29-01|2022-29-01|20|2022-29-01
002|2022-10-01|2022-10-01|500|2022-10-08|2022-10-08|400|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|2022-10-08
003|2022-05-01|2022-05-01|350|2022-05-10|2022-05-10|250|2022-05-15|2022-05-15|200|NULL|NULL|NULL|NULL|NULL|NULL|2022-05-15
004|2022-11-01|2022-11-01|900||NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|2022-11-01
Could someone please let me know what is the best way to write this query. Thank you!
efficient
this means you have a method, so show it.
Also while we are anywhere near efficient, there is only efficiency inside a context, show/describe that.
While there are many great SQL coders on this site, reading the mind of people the cannot even be bother describing ANYTHING about their problem, is boring.
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;
I've very minor experience at working with databases( I only know the absolute basics). With that stated I guess my problem is rather easy to solve for more experienced minds.
My question is:
I need a way to be able to search for example "all types of Aluminium and all its sub_materials"?
I can do simple queries like
SELECT *
FROM sub_materials
WHERE category_id = 2;
So what I'm asking for is basically to be able to see the whole branch of Aluminium. I've looked at ltreeand Closure Tables But I'm to much of a Noob to figure it out by my self.
I believe I have to connect the tables somehow as "grandparent, parent, child" or something similar, but I've no idea if there is some other way or?
I'm not even sure if I'm doing this the right way.
Can someone advise me on this?
I've three tables in a database.(below are samples from the tables)
materials_category Holds all the categories for every material in the DB.
materials Holds the Category name and id, material_names and ids and the values for each material.
Some materials has sub_categories. not all but some.
3. sub_materials Holds the category info, material name and id, the sub_material name and id and the sub_material values.
1. `materials_category`
|category_id| category_name|
------------------------------
| 1 | Aggregate |
| 2 | Aluminium |
| 3 | Asphalt |
2. `materials`
|category_id |category_name |material_id |material_name|EE_1|EE_2|EC_1|EC_2
---------------------------------------------------------------------------
| 1 |Aggregate | 1 | General |3 |0 | 52 | 8
| 2 |Aluminium | 2 | General |55 |7 | 9 | 24
| 2 |Aluminium | 3 | Cast Pr |34 |30 | 22 | 28
| 2 |Aluminium | 4 | Extrud. |65 |16 | 8 | 74
| 2 |Aluminium | 5 | Rolled |15 | 0 | 9 | 61
| 3 |Asphalt | 6 |Asphalt, 4% |2 |22 | 6 | 54
| 3 |Asphalt | 7 |Asphalt, 5% |3 |91 | 1 | 4
3. `sub_materials`
|category_id|category_name|material_id|material_name|sub_mar_id|sub_mar_name|EE_1|EE_2|EC_1|EC_2|
|2 |Aluminium |2 |General |1 |Virgin |21 |5 |9 |60 |
|2 |Aluminium |2 |General |2 |Recycled |29 |3 |8 |9 |
Your tables are denormalised. This is usually not a good thing, unless this is a reporting only database.
Anyway, here's a start for what you're after. why don't you run it and give some feedback on whether it's what you want or not.
select *
from materials_category c
left outer join materials m
on c.category_id = m.category_id
left outer join sub_materials s
on s.material_id = m.material_id
where c.category_name = 'Aluminium'
Looking at your data, I'm guessing that 'recycled' could apply to any number of material categories, not just Aluminium? Would you have a whole bunch of 'Recycled' records in your sub_materials to allow for each material_category?
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?
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