I am trying to join two tables based on logic where each record from the first table would pull a value <= to a number in the second table using a join. I am curious if this can be achieved in an efficient manner in SQL. I found some questions focused on dates, but nothing quite similar.
I have two tables. 'Table1' is the primary table
**Table1**
+---------+---------+--------+
| Product | Carrier | Weight |
+---------+---------+--------+
| Z | B | 600 |
+---------+---------+--------+
| Z | B | 350 |
+---------+---------+--------+
| Y | A | 150 |
+---------+---------+--------+
| X | A | 75 |
+---------+---------+--------+
| Y | B | 10 |
+---------+---------+--------+
| X | A | 40 |
+---------+---------+--------+
'Table2' is the lookup table
**Table2**
+---------+--------+------+
| Carrier | Weight | Cost |
+---------+--------+------+
| A | 50 | 2.50 |
+---------+--------+------+
| A | 100 | 2.00 |
+---------+--------+------+
| A | 200 | 1.75 |
+---------+--------+------+
| B | 200 | 1.85 |
+---------+--------+------+
| B | 400 | 1.50 |
+---------+--------+------+
| B | 600 | 1.35 |
+---------+--------+------+
The result would apply the closest cost from table2 for a weight <= Table1
**Result**
+---------+---------+--------+------+
| Product | Carrier | Weight | Cost |
+---------+---------+--------+------+
| Z | B | 600 | 1.35 |
+---------+---------+--------+------+
| Z | B | 350 | 1.50 |
+---------+---------+--------+------+
| Y | A | 150 | 1.75 |
+---------+---------+--------+------+
| X | A | 75 | 2.00 |
+---------+---------+--------+------+
| Y | B | 10 | 1.85 |
+---------+---------+--------+------+
| X | A | 40 | 2.50 |
+---------+---------+--------+------+
Using a traditional join the first row delivers a cost as 600 is a weight listed in 'Table2'
SELECT a.Product
,a.Carrier
,a.Weight
,b.Cost
FROM dbo.table1 a
LEFT JOIN Table2 b ON a.Carrier = b.Carrier AND a.Weight = b.Weight
+---------+---------+--------+------+
| Product | Carrier | Weight | Cost |
+---------+---------+--------+------+
| Z | B | 600 | 1.35 |
+---------+---------+--------+------+
| Z | B | 350 | NULL |
+---------+---------+--------+------+
| Y | A | 150 | NULL |
+---------+---------+--------+------+
| X | A | 75 | NULL |
+---------+---------+--------+------+
| Y | B | 10 | NULL |
+---------+---------+--------+------+
| X | A | 40 | NULL |
+---------+---------+--------+------+
I hope to make changes to the query above to achieved the desired result.
FYI: I am using Microsoft SQL Server 2014.
You should look for the first record that match the conditions:
SELECT a.Product
,a.Carrier
,a.Weight
,(select top 1 Cost from Table2 b
where a.Carrier = b.Carrier AND a.Weight <= b.Weight
ORDER BY b.Carrier, b.Weight ASC) Cost
FROM dbo.table1 a
This should get you started:
with theCost as (
select t1.carrier
, min(t2.weight) thisWeight
from table1 t1 join table2 t2 on t1.carrier = t2.carrier
where whatever
group by t1.carrier
)
select the field you want
from theCost join table2 t2 on theCost.carrier = t2.carrier
and thisWeight = t2.weight
etc
Related
I have this table from a query and I would like to find the sum of the Total Cost (after discount). I have searched for a solution but I can't seem to find the one I'm looking for.
Here are the sample data from my tables
Booking
+-----------+-------------+----------+------------+
| vehicleNo | bookingDay | driverNo | acc_status |
+-----------+-------------+----------+------------+
| 10 | 13/06/2021 | 2 | B |
| 10 | 14/06/2021 | 0 | B |
| 10 | 15/06/2021 | 2 | B |
| 20 | 17/06/2021 | 2 | B |
+-----------+-------------+----------+------------+
Vehicle
+-----------+-------------+----------------+------+
| vehicleNo | vehicleReg | make_model | cost |
+-----------+-------------+----------------+------+
| 10 | IN10NGT | Nissan R34 GTR | 90 |
| 20 | IN10MRX | Mazda RX7 | 70 |
| 30 | IN10TSU | Toyota Supra | 80 |
+-----------+-------------+----------------+------+
Here is the query
SELECT IF(COUNT(Vehicle.vehicleNo) > 1, ROUND(Vehicle.cost,1) * ROUND(COUNT(Vehicle.vehicleNo) * 0.9,1), Vehicle.cost * ROUND(COUNT(Vehicle.vehicleNo),1)) AS 'Total after discount'
FROM Booking
INNER JOIN Vehicle
ON Vehicle.vehicleNo = Booking.vehicleNo
WHERE Booking.driverNo = 2
GROUP BY Vehicle.vehicleNo
ORDER BY Vehicle.vehicleNo;
an here is the result
+----------------------+
| Total after discount |
+----------------------+
| 162 |
| 70 |
+----------------------+
and I am expecting to have a table after calculating the sum like this
+----------------------+
| Overall cost |
|after discount |
+----------------------+
| 232 |
+----------------------+
any help is much appreciated.
i have figured it out. i used this query:
SELECT SUM(Cost) FROM (SELECT IF(COUNT(Vehicle.vehicleNo) > 1, ROUND(Vehicle.cost,1) * ROUND(COUNT(Vehicle.vehicleNo) * 0.9,1), Vehicle.cost * ROUND(COUNT(Vehicle.vehicleNo),1)) AS Cost
FROM Vehicle INNER JOIN Booking
ON Vehicle.vehicleNo = Booking.vehicleNo AND Booking.driverNo = 2
GROUP BY Vehicle.vehicleNo) AS x;
The data looks similar to this:
+----+------+-----------+-------+---------+---------+--------+
| ID | Unit | Floorplan | Sq Ft | Name | Amenity | Charge |
+----+------+-----------+-------+---------+---------+--------+
| 1 | 110 | A1 | 750 | Alan | GARAGE | 50 |
| 2 | | | | | RENT | 850 |
| 3 | | | | | PEST | 2 |
| 4 | | | | | TRASH | 15 |
| 5 | | | | | TOTAL | 20 |
| 6 | 111 | A2 | 760 | Bill | STORAGE | 35 |
| 7 | | | | | GARAGE | 50 |
| 8 | | | | | RENT | 850 |
| 9 | | | | | PEST | 2 |
| 10 | | | | | TOTAL | 15 |
| 11 | 112 | A3 | 770 | Charlie | PETRENT | 20 |
| 12 | | | | | STORAGE | 35 |
| 13 | | | | | GARAGE | 50 |
| 14 | | | | | RENT | 850 |
| 15 | | | | | TOTAL | 2 |
+----+------+-----------+-------+---------+---------+--------+
I am new to SQL and trying my best using Microsoft Access, but I need help.
The data needs to look like this:
My first step is to separate the units from the rest with
SELECT * FROM table WHERE Unit <> NULL;
and after that I've usually just hard-input the rest.
My idea was as follows:
INSERT INTO table
VALUES (NULL,NULL,...,'Pest',$2)
FROM table
WHERE NOT EXIST 'Pest' BETWEEN x AND y
/* where x = Total 1 and y = Total 2*/
Am I on the right track? I probably need a loop or a join, but I'm not at that level yet.
You can use a crosstab query, though a bit convoluted it is:
TRANSFORM
Sum(TableUnit.Charge) AS SumOfCharge
SELECT
S.Unit,
S.Floorplan,
S.SqFt,
S.Name,
S.Amenity
FROM
TableUnit,
(SELECT
Q.Id,
Val(DMax("Id","TableUnit","Id<=" & Q.[Id] & " And Unit Is Not Null")) AS ParentId
FROM TableUnit As Q) AS T,
(SELECT
TableUnit.Id,
TableUnit.Unit,
TableUnit.Floorplan,
TableUnit.SqFt,
TableUnit.Name,
TableUnit.Amenity
FROM
TableUnit
WHERE
TableUnit.Unit Is Not Null) AS S
WHERE
TableUnit.Id=[T].[Id]
AND
T.ParentId)=[S].[Id]
GROUP BY
T.ParentId,
S.Unit,
S.Floorplan,
S.SqFt,
S.Name,
S.Amenity
PIVOT
TableUnit.Amenity In
("Garage","Pest","Trash","PetRent","Storage","Rent");
Your test data differs a little from your expected output, so:
My MSAccess is rather rusty, but something like this should work:
SELECT t0.Unit, t0.Floorplan, t0.[Sq Ft], t0.Name, t0.Amenity
, SUM(IIF(tM.Amenity = 'GARAGE', Charge, 0)) AS [Garage]
, SUM(IIF(tM.Amenity = 'PEST', Charge, 0)) AS [Pest]
FROM (
SELECT t1.id AS id0, MIN(t2.id) AS idN
FROM t AS t1
INNER JOIN t AS t2 ON t1.id < t2.id
WHERE t1.Unit <> '' AND t2.Unit <> ''
) AS groups
INNER JOIN t AS t0 ON t0.id = groups.id0
LEFT JOIN t AS tM ON tM.id > groups.id0 AND tm.id < groups.idN
GROUP BY t0.Unit, t0.Floorplan, t0.[Sq Ft], t0.Name, t0.Amenity
;
Though, if I remember correctly, and it hasn't changed in newer versions; you can't have true subqueries and will need to make groups a separate query you can join to as if it were a table/view.
With tables table1 and table2need select the rows of table1 for those locationIds and hours where tier in table2 is high
table1
+------------+------+---------+----------+
| locationId | hour | metric1 | metric2 |
+------------+------+---------+----------+
| 1111 | 10 | 200 | 40 |
| 1111 | 9 | 300 | -20 |
| 1111 | 11 | 1800 | 300 |
| 1122 | 9 | 600 | 800 |
| 1122 | 11 | 2300 | -10 |
| 1133 | 8 | 10000 | 30 |
+------------+------+---------+----------+
table2
+------------+------+-------+---------+
| locationId | hour | value | tier |
+------------+------+-------+---------+
| 1111 | 10 | 1300 | high |
| 1111 | 9 | 900 | medium |
| 1111 | 11 | 200 | low |
| 1122 | 9 | 100 | low |
| 1122 | 11 | 2300 | high |
| 1133 | 8 | 1400 | high |
+------------+------+-------+---------+
If it was one column - say locationId I could have done something like
select * from table1
where locationId in (select locationId from table2 where tier='high');
How do I do this when the locationId, hour pair need to be compared?
output
+------------+------+---------+----------+
| locationId | hour | metric1 | metric2 |
+------------+------+---------+----------+
| 1111 | 10 | 200 | 40 |
| 1122 | 11 | 2300 | -10 |
| 1133 | 8 | 10000 | 30 |
+------------+------+---------+----------+
I think a simple join should handle this:
SELECT t1.*
FROM table1 t1
INNER JOIN table2 t2
ON t1.locationId = t2.locationId AND
t1.hour = t2.hour
WHERE t2.tier = 'high'
Output:
Demo here:
Rextester
The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.
select * from table1 t1
inner join table2 t2 on t1.locationId =t2.locationId
where t2.tier='high'
Use EXISTS method to get your result :
SELECT *
FROM table1 TBL1 WHERE EXISTS
(
SELECT 1 FROM table2 TBL2 WHERE TBL1.locationId = TBL2.locationId AND
TBL1.hour = TBL2.hour AND TBL2.tier = 'high'
)
Table
id | name | type | x | y | z | refer
-----+------------+---------------+---------------+-------------+------------------+-----------------
1001 | A | 4 | | | | 0
2000 | B | 2 | -1062731776 | | -65536 | 1001
2001 | C | 2 | 167772160 | | -16777216 | 1001
2002 | D | 2 | -1408237568 | | -1048576 | 1001
I need to select columns name,x,y,z if in refer column it refers to id column
and name must be of that id's name. Is it possible with a single query? can anyone please help
here, output should be:
name| x | y | z
----+-----------------+-------------+-----------------
A | -1062731776 | | -65536
A | 167772160 | | -16777216
A | -1408237568 | | -1048576
SELECT t1.name, t2.x, t2.y, t2.z FROM TABLENAME t1
JOIN TABLENAME t2 on t1.id = t2.refer
I have 3 tables shown below in MS Access 2010:
Table: devices
id | device_id | Company | Version | Revision |
-----------------------------------------------
1 | dev_a | Almaras | 1.5.1 | 0.2A |
2 | dev_b | Enigma | 1.5.1 | 0.2A |
3 | dev_c | Almaras | 1.5.1 | 0.2C |
*Field: device_id is Primary Key Unique String
*Field ID is just an auto-number column
Table: activities
id | act_id | act_date | act_type | act_note |
------------------------------------------------
1 | dev_a | 07/22/2013 | usb_axc | ok |
2 | dev_a | 07/23/2013 | usb_axe | ok | (LAST ROW for dev_a)
3 | dev_c | 07/22/2013 | usb_axc | ok | (LAST ROW for dev_c)
4 | dev_b | 07/21/2013 | usb_axc | ok | (LAST ROW for dev_b)
*Field: act_id contains device_id; NOT UNIQUE
*Field ID is just an auto-number column
Table: matrix
id | mat_id | tc | ts | bat | cycles |
-----------------------------------------
1 | dev_a | 2811 | 10 | 99 | 200 |
2 | dev_a | 2911 | 10 | 97 | 400 |
3 | dev_a | 3007 | 10 | 94 | 600 |
4 | dev_a | 3210 | 10 | 92 | 800 | (LAST ROW for dev_d)
5 | dev_b | 1100 | 5 | 98 | 100 |
6 | dev_b | 1300 | 8 | 93 | 200 |
7 | dev_b | 1411 | 11 | 90 | 300 | (LAST ROW for dev_b)
8 | dev_c | 4000 | 27 | 77 | 478 | (LAST ROW for dev_c)
*Field: mat_id contains device_id; NOT UNIQUE
*Field ID is just an auto-number column
Is there any way to query tables to get results as shown below (each device from devices and only last row added [see example output table] from each of the other two tables):
Query Results:
device_id | Company | act_date | act_type | bat | cycles |
------------------------------------------------------------
device_a | Almaras | 07/23/2013 | usb_axe | 92 | 800 |
device_b | Enigma | 07/21/2013 | usb_axc | 90 | 300 |
device_c | Almaras | 07/22/2013 | usb_axc | 77 | 478 |
Any ideas? Thank you in advance for reading and helping me out :)
I think is what you want,
SELECT a.device_id, a.Company,
b.act_date, b.act_type,
c.bat, c.cycles
FROM ((((devices AS a
INNER JOIN activities AS b
ON a.device_id = b.act_id)
INNER JOIN matrix AS c
ON a.device_id = c.mat_id)
INNER JOIN
(
SELECT act_id, MAX(act_date) AS max_date
FROM activities
GROUP BY act_id
) AS d ON b.act_id = d.act_id AND b.act_date = d.max_date)
INNER JOIN
(
SELECT mat_id, MAX(tc) AS max_tc
FROM matrix
GROUP BY mat_id
) AS e ON c.mat_id = e.mat_id AND c.tc = e.max_tc)
The subqueries: d and e separately gets the latest row for every act_id.
Try
SELECT devices.device_id, devices.Company, activities.act_data, activities.act_type, matrix.bat, matrix.cycles
FROM devices
LEFT JOIN activities
ON devices.device_id = activities.act_id
LEFT JOIN matrix
ON devices.device_id = matrix.mat_id;
What do you consider the "last" row in Matrix?
You need to do something like
WHERE act_date in (SELECT max(a.act_date) from activities a where a.mat_id=d.device_id GROUP BY a.mat_id)
and something similar for the join to matrix.