selecting min from joined tables - sql

I can't figure this question out by myself so I am wondering if you guys could help me. I tried to find my answer on google, stackoverflow ans several other locations but without any result..
I made a query:
SELECT leverancier.leveranciers_id,
medicijn.artikelnr,
medicijn.naam,
medicijn.in_voorraad,
medicijn.min_voorraad,
min(order_medicijn.inkoopprijs) AS price
FROM leverancier
INNER JOIN voorraadorder
ON leverancier.leveranciers_id = voorraadorder.leverancier_id
INNER JOIN order_medicijn
ON order_medicijn.voorraadorder_id = voorraadorder.voorraadorder_id
INNER JOIN medicijn
ON medicijn.artikelnr = order_medicijn.artikel_id
GROUP BY leverancier.leveranciers_id,
medicijn.artikelnr,
medicijn.naam,
medicijn.in_voorraad,
medicijn.min_voorraad
ORDER BY artikelnr
Which gives the following result:
Leveranciers_ID / artikelnr / naam in_voorraad / min_voorraad / price
2 1 Aspirine 100 50 0.74
1 1 Aspirine 100 50 0.75
2 2 Abacivr 200 180 4.50
4 2 Abacivr 200 180 4.00
4 3 Acetazolamide 100 90 1.20
5 4 Ciclovir 145 120 0.50
3 5 levoceterizine 125 120 2.00
Here comes the question:
What query would I need to get the same result but only with the records where the price is the lowest for each artikelnr. So the result would be like this:
Leveranciers_ID / artikelnr / naam in_voorraad / min_voorraad / price
2 1 Aspirine 100 50 0.74
4 2 Abacivr 200 180 4.00
4 3 Acetazolamide 100 90 1.20
5 4 Ciclovir 145 120 0.50
3 5 levoceterizine 125 120 2.00
If there is any additional information required to answer this question, please ask me.
Thanks in advance

One of the possible solutions: Adding one more inner SQL that computes the min price of each artikelnr and using the HAVING clause, which uses the inner SQL, to filter the result.
Cheers

Related

Finding Max Price and displaying multiple columns SQL

I have a table that looks like this:
customer_id item price cost
1 Shoe 120 36
1 Bag 180 50
1 Shirt 30 9
2 Shoe 150 40
3 Shirt 30 9
4 Shoe 120 36
5 Shorts 65 14
I am trying to find the most expensive item each customer bought along with the cost of item and the item name.
I'm able to do the first part:
SELECT customer_id, max(price)
FROM sales
GROUP BY customer_id;
Which gives me:
customer_id price
1 180
2 150
3 30
4 120
5 65
How do I get this output to also show me the item and it's cost in the output? So output should look like this...
customer_id price item cost
1 180 Bag 50
2 150 Shoe 40
3 30 Shirt 9
4 120 Shoe 36
5 65 Shorts 14
I'm assuming its a Select statement within a Select? I would appreciate the help as I'm fairly new to SQL.
One method that usually has good performance is a correlated subquery:
select s.*
from sales s
where s.price = (select max(s2.price)
from sales s2
where s2.customer_id = s.customer_id
);

Query with sum functions

for a little project(hobby purpose) i am building a C# application with a SQL database behind it.
However I am trying to build a query with a sum function which calculates values from a different table.
Here are the relevant tables and sample data
Hotel table
Id, Name Adress Zipcode Phone
1 Ankunding Group 90 Shelley Terrace 649-6326 86-(672)239-5855
2 Gerlach-Gutmann 50776 Bartillon Road 27109 CEDEX 33-(412)226-8055
3 Breitenberg-Smith 3289 Talisman Avenue 59762 86-(141)636-8780
4 Smitham-Marks 5 Veith Plaza 216282 7-(400)484-7233
5 Beatty LLC 3 Center Pass 940028 212-(310)974-4364
Reservation table
id, customerid, Startdate Enddate Amount of persons
1 163 2016-06-19 2017-04-30 4
2 172 2016-12-02 2016-08-18 5
3 162 2017-01-20 2017-04-08 3
4 66 2017-04-06 2017-01-07 2
5 104 2017-05-07 2016-09-10 2
RoomReservation table
Roomid, reservationid
3 53
3 198
4 178
5 172
5 218
Room table
id, hotelid, Roomnumber, price
1 1 1.01 268.83
2 1 1.02 201.28
3 1 1.03 126.64
4 1 1.04 122.56
5 1 1.05 217.41
Now I am trying to make a query to which gives me an overview off income per hotel. So for each hotel I want to get the reservations, and do amount of persons * the price of the room for each room in the hotel.
I've tried different things without success, I read somewhere that I needed to use a subquery but I have no idea how.
I want it to look like;
Hotelname1; income
Hotelname2; income
Hotelname3; income
Hotelname4; income
Hotelname4; income
Why can't you just do this:
SELECT
Hotel.Name,
SUM(Room.Price*Reservation.Amountofpersons)
FROM
Hotel
JOIN Room
ON Hotel.HotelId=Room.HotelId
JOIN RoomReservation
ON Room.RoomId=RoomReservation.RoomId
JOIN Reservation
ON RoomReservation.ReservationId=Reservation.ReservationId
GROUP BY
Hotel.Name
You can try it whit this query:
select hotel.name,sum(reservation.amount*room.price)
from hotel_table as hotel
inner join room_table as room on (hotel.hotelid=room.hotelid)
inner join roomreservation_table as room_reservation on (room.roomid=room_reservation.roomId)
inner join reservation_table as reservation on (room.reservationId=reservation.reservationid)
group by hotel.hotelid

How to use IF Else in SQL Server

I have to use If Else in Sql Server Query.
Lets Consider the Table below
Product Kg Qty Rate
----------------------------
Mango 1 3 70
Orange 0 2 80
Apple 3 4 90
I need to sum Kg, qty and rate(kgQtyrate) as Total. But when kg is 0 i need to sum only qty*rate.
I need a output like,
Product Kg Qty Rate Total
-----------------------------------
Mango 1 3 70 210
Orange 0 2 80 160
Apple 3 4 90 1080
How can I use If Else here???
You could use CASE:
SELECT
*,
(CASE Kg WHEN 0 THEN 1 ELSE Kg END) * Qty * Rate AS Total
FROM Products
Please have a look here.
you can use this query
select *, case when kg > 0
then kg + qty + rate
else
qty + rate
end as "total"
from table3
look at attached image

Finding double mrp's in SQL

I have table with productcode and mrp like
Pcode MRP
1 30
2 30
2 35
3 100
4 150
4 150
5 45
6 120
6 122
6 125
I want to find which productcodes have more than two mrp.
Thanks in advance.
If you get the count and use the having clause, you should get what you are looking for.
select pcode, count(pcode)
From tab
group by pcode
having count(pcode) > 1

MS Access, Excel, SQL, and New Tables

I'm just starting out with MS Access 2010 and have the following setup. 3 excel files: masterlist.x (which contains every product that I sell), vender1.x (which contains all products from vender1, I only sell some of these products), and vender2.x (again, contains all products from vender2, I only sell some of these products). Here's an example data collection:
masterlist.x
ID NAME PRICE
23 bananas .50
33 apples .75
35 nuts .87
38 raisins .25
vender1.x
ID NAME PRICE
23 bananas .50
25 pears .88
vender2.x
ID NAME PRICE
33 apples .75
35 nuts .87
38 raisins .25
49 kiwis .88
The vender lists get periodically updated with new items for sell and new prices. For example, vender1 raises the price on bananas to $.75, my masterlist.x would need to be updated to reflect this.
Where I'm at now: I know how to import the 3 excel charts into Access. From there, I've been researching if I need to setup relationships, create a macro, or a SQL query to accomplish my goals. Not necessarily looking for a solution, but to be pointed in the right direction would be great!
Also, once the masterlist.x table is updated, what feature would I use to see which line items were affected?
Update: discovered SQL /JOIN/ and have the following:
SELECT * FROM master
LEFT JOIN vender1
ON master.ID = vender1.ID
where master.PRICE <> vender1.PRICE;
This gives me the output (for the above scenario)
ID NAME PRICE ID NAME PRICE
23 bananas .50 23 bananas .75
What feature would instead give me:
masterlist.x
ID NAME PRICE
23 bananas .75
33 apples .75
35 nuts .87
38 raisins .25
Here is a heads up since you were asking for ideas to design. I don't really fancy your current table schema. The following queries are built in SQL Server 2008, the nearest syntax that I could get in sqlfiddle to MS Access SQL.
Please take a look:
SQLFIDDLE DEMO
Proposed table design:
vendor table:
VID VNAME
1 smp farms
2 coles
3 cold str
4 Anvil NSW
product table:
PID VID PNAME PPRICE
203 2 bananas 0.5
205 2 pears 0.88
301 3 bananas 0.78
303 3 apples 0.75
305 3 nuts 0.87
308 3 raisins 0.25
409 4 kiwis 0.88
masterlist:
ID PID MPRICE
1 203 0.5
2 303 0.75
3 305 0.87
4 308 0.25
Join queries can easily update your masterlist now. for e.g.:
When the vendor updates their prices for the fruits they provide you. Or when they stop supply on that product. You may use where clauses to add the conditions to the query as you desire.
Query:
SELECT m.id, p.vid, p.pname, p.pprice
FROM masterlist m
LEFT JOIN product p ON p.pid = m.pid
;
Results:
ID VID PNAME PPRICE
1 2 bananas 0.5
2 3 apples 0.75
3 3 nuts 0.87
4 3 raisins 0.25
Please comment. Happy to help you if have any doubts.