getting one to one result from one to many relationship [closed] - sql

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 9 years ago.
Improve this question
my situation is that i have one to many relation ,like order and orderdetails ,i need to get order which has single order details.

How about:
select *
from order
where order_number in
(select order_number
from order_details
group by order_number
having count(*) = 1)

SELECT O1.order_number
FROM Orders AS O1
WHERE 1 = (
SELECT COUNT(*)
FROM OrderDetails AS D1
WHERE O1.order_number = D1.order_number
);

Related

Select the most recent date from the 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 2 years ago.
Improve this question
I have a purchase order table that has a list of order numbers and associated order lines with different dates. I want to a query to fetch the orders with orderlines that has the most recent date.
I want the fetch to result in something like below
Here is a way to do this using row_number
select * from (
select t.*
,row_number() over(partition by ordernumber,orderlinenumber order by date desc) as rnk
from <your_table> t
)x
where x.rnk=1

SQL: How do I list all of customer ID that had their first 2 orders exactly in the sequence of: first order being hat, second order being sunglasses? [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
I am new to SQL and I am trying to figure out the query:
How do I list all of customer ID that had their first 2 orders exactly in the sequence of: first order being hat, second order being sunglasses?
Thanks!
Well, in BigQuery you can do something like this:
select o.customerId
from orders o
group by o.customerId
having array_agg(o.product order by o.order_date asc limit 2) = array['hat', 'sunglasses'];
You don't specify what the data looks like, but this should give you the idea of an approach.
you try like this
SELECT CustomerID, Product
FROM Customers
WHERE Productin ('Hat', 'sunglasses')
order by Product Asc

sql in ms-access [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 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])
);

I need help in finding an average rating of all better products [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 4 years ago.
Improve this question
id year category_id rating avg_better
need help in finding the average rating of all better products
select *,
( select avg(rating)
from mytab as t2
where t2.year = t1.year -- same year
and t2.rating > t1.rating -- better rating
) as avg_better
from mytab as t1

How to find group which is having Status Active? [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 4 years ago.
Improve this question
I have a id which is having more than one Accountid in a table with status Active . I need to find group of active for the particular ID .
Please find my below query .is it correct or something need to modify to get better optimized result .
select id,count(Accountid) from CustomerAccount
where status='Active'
group by id
having count(*)>( select min(maxxount) from(
select id,count(accountid) as maxxount
from CustomerAccount
group by id)A)
Use this query:
select id,count(Accountid) from CustomerAccount
where status='Active'
group by id
having count(*) >1