Visualize all products of the order with the highest value, to extract the largest amount and orders from it - sql

Desired result
NAME PRICE
Lenovo X1 Carbon 4009.00
Lenovo ThinkVision X1 1549.00
Lenovo YOGA 520 1349.00
Motorola Moto Z2 999.00
Motorola Moto Z 549.00
Motorola Moto E5 179.00
ID CATEGORY NAME PRICE ORDER_ID
1 PC Lenovo ThinkaPad L380 1579 5
2 Mobile Motorola Moto E5 179 1
3 Mobile Motorola Moto Z 549 2
4 Monitor Lenovo ThinkVision X1 1549 4
5 PC Lenovo X1 Carbon 4009 3
6 Mobile Motorola Moto Z2 999 4
7 PC Lenovo Legion Y530 2099 4
8 PC Lenovo YOGA 520 1349 3
9 Monitor Lenovo ThinkVision X1 1549 6
10 PC Lenovo YOGA 520 1349 6
11 Monitor Lenovo ThinkVision X1 1549 3
12 Mobile Motorola Moto Z2 999 3
13 Mobile Motorola Moto E5 179 3
14 Mobile Motorola Moto Z 549 3
I used these 2 queries to extract information from the table in the photo
but i need to find a way to retrieve the information with just one query please help
SELECT TOP 1 NAME , PRICE, ORDER_ID
FROM PRODUCTS
ORDER BY PRICE DESC
SELECT NAME ,PRICE
FROM PRODUCTS
WHERE ORDER_ID like '%3%'
ORDER BY PRICE DESC
Desired result

SELECT NAME ,PRICE
FROM PRODUCTS
WHERE ORDER_ID =
(
SELECT TOP 1 ORDER_ID
FROM PRODUCTS
ORDER BY PRICE DESC
)

-- Get everything from the table
SELECT
*
FROM
Products
-- Where the total order value for the order is the highest
Where
order_id =
(
-- The largest order total
SELECT TOP 1
order_id
FROM
Products
GROUP BY
order_id
ORDER BY
SUM(price) DESC
)
ORDER BY
Price DESC

Related

looking for values from another table where they do not exist in a given group

I have two tables:
SHOPPING
date
id_customer
id_shop
id_fruit
28.03.2018
7423
123
1
13.02.2019
8408
354
1
28.03.2019
7767
123
9
13.02.2020
8543
472
7
28.03.2020
8640
346
9
13.02.2021
7375
323
9
28.03.2021
7474
323
8
13.02.2022
7476
499
1
28.03.2022
7299
123
4
13.02.2023
8879
281
2
28.03.2023
8353
452
1
13.02.2024
8608
499
6
28.03.2024
8867
318
1
13.02.2025
7997
499
6
28.03.2025
7715
499
4
13.02.2026
7673
441
7
FRUITS
id_fruit
name
1
apple
2
pear
3
grape
4
banana
5
plum
6
melon
7
watermelon
8
orange
9
pineapple
I would like to find fruits that have never been bought in a specific id_shop
I tried with this:
SELECT
s.idshop,
s.id_fruit ,
f.name
FROM
shopping s
LEFT JOIN fruit f ON f.id_fruit = s.id_fruit
WHERE NOT EXISTS (
SELECT *
FROM
fruit f1
WHERE f1.id_fruit = s.id_fruit
)
but it does not work...
Yes, you need an OUTER JOIN, but that should be RIGHT JOIN along with NULL values picked from shopping table after join applied, considering your current query such as
SELECT f.*
FROM shopping s
RIGHT JOIN fruit f
ON f.id_fruit = s.id_fruit
WHERE s.id_fruit IS NULL
Demo

SQL Query to get the vehicle ID and his locations to the most recent date(values distrubuted in 3 distinct tables)

This is my first question here.
And I've been struggling for a while and I can't solve it.
So I want to get for all the circuits the location(latitude and longitude) to the most recent date.
The schema that I'm using contains important information so i will give an example:
table Circuit{
NR int Primary Key,
VEHICLE_ID int,
etc...
}
table StopPoints{
NR int Primary Key,
Circuit int Foreign Key Circuit.NR,
Latitude float,
Longitude float,
etc...
}
table StopPoint_Flux{
NR int Primary Key,
StopPoint int Foreign Key StopPoints.NR,
Date DATE,
etc...
}
And an example of the data in it:
Circuit:
NR
VEHICLE_ID
1
100
2
208
3
210
4
1
StopPoints:
NR
Circuit
Latitude
Longitude
1
1
0.3
5.0
2
1
1.7
8.0
3
2
7.0
10.0
4
4
11.0
1.2
5
3
45.0
46.0
6
4
43.1
6.23
7
1
34.42
36.98
8
3
40.48
47.50
StopPoint_Flux:
NR
StopPoint
Date
1
1
21/12/2020
2
2
19/12/2020
3
2
1/12/2020
4
1
5/12/2020
5
3
2/12/2020
6
5
7/12/2020
7
4
30/12/2020
8
5
7/12/2020
9
6
5/12/2020
10
8
1/12/2020
11
7
30/12/2020
12
6
8/12/2020
13
1
21/12/2020
The result that I need:
VEHICLE_ID
Latitude
Longitude
Date
100
34.42
36.98
30/12/2020
208
7.0
10.0
2/12/2020
210
45.0
46.0
7/12/2020
1
11.0
1.2
30/12/2020
If ROW_NUMBER works.
SELECT
q.VEHICLE_ID
, q.Latitude
, q.Longitude
, q.Date
FROM
(
SELECT
circuit.VEHICLE_ID
, stop.Latitude
, stop.Longitude
, flux.Date
, ROW_NUMBER() OVER (PARTITION BY circuit.NR
ORDER BY flux.Date DESC, stop.NR) AS rn
FROM Circuit AS circuit
JOIN StopPoints AS stop
ON stop.circuit = circuit.NR
JOIN StopPoint_Flux AS flux
ON flux.StopPoint = stop.NR
) q
WHERE q.rn = 1
vehicle_id
latitude
longitude
date
100
34.42
36.98
2020-12-30
208
7
10
2020-12-02
210
45
46
2020-12-07
1
11
1.2
2020-12-30
db<>fiddle here

Updating Stock Qunatity based on Orders using SQL

I'm having trouble creating a SQL statement that will update the Inventory table everytime I add an order to the Orders table using a button.
Order Table
id
orderid
variety
weight
quantity
price
1
J1
Native Chicken
1.6
10
120
2
J2
Chicken Broiler
2.3
5
100
3
J3
Duck
1.6
2
250
4
J4
Turkey
1.6
4
350
Inventory Table
id
variety
weight
quantity
price
11
Native Chicken
1.6
20
120
12
Chicken Broiler
2.3
15
100
13
Duck
1.6
5
250
14
Turkey
1.6
9
350
15
Native Chicken
1.9
20
120
16
Chicken Broiler
2.5
15
100
17
Duck
1.7
5
250
18
Turkey
4.7
9
350
Expected Result
id
variety
weight
quantity
price
11
Native Chicken
1.6
10
120
12
Chicken Broiler
2.3
10
100
13
Duck
1.6
3
250
14
Turkey
1.6
5
350
15
Native Chicken
1.9
20
120
16
Chicken Broiler
2.5
15
100
17
Duck
1.7
5
250
18
Turkey
4.7
9
350
this is the code that I'm trying to use using a query in visual studio and every time I run it the quantity keeps on decreasing.
UPDATE Inventory
SET Quantity = TableA.Quantity - TableB.Quantity
FROM Inventory AS TableA JOIN tblOrders AS TableB ON TableA.Variety = TableB.Variety
WHERE
TableA.Variety = TableB.Variety
AND
TableB.Weight = TableB.Weight
In standard SQL, you can use:
update inventory
set quantity = quantity -
(select sum(o.quantity)
from orders o
where o.variety = inventory.quality and
o.weight = inventory.weight
)
where exists (select 1
from orders o
where o.variety = inventory.quality and
o.weight = inventory.weight
);
Many databases have more concise ways to express this logic. But your question has not specified a specific database.
In SQL Server for instance, you can use:
update i
set quantity = i.quantity - o.quantity
from inventory i join
(select o.variety, o.weight, sum(quantity) as quantity
from orders o
group by o.variety, o.weight
) o
on o.variety = inventory.quality and
o.weight = inventory.weight

Complex multi level hierarchical SQL

How can I achieve the below results using a query in SQL Server.
Table: shares_info
Complex multilevel hierarchy:
comp_name investee
APPLE MS
APPLE INTEL
APPLE MRF
APPLE GOOG
MS GOOG
MS MRF
MRF STF
MRF ABC
GOOG INTEL
GOOG TRF
GOOG XYZ
The idea is something like this. APPLE has invested in MS,INTEL,MRF,GOOG. And so on. Now the below input is something like sell my shares but first sell off shares without dependencies first. That is what my output conveys. If I want to sell GOOG shares then based on my below input GOOG has dependency on INTEL/TRF/XYZ and hence before selling GOOG I need to sell (123, XYZ) and (456 INTEL). Next, if I want to sell APPLE it has dependency on MS/INTEL/MRF/GOOG and hence as per below input I need to first sell INTEL/MRF/GOOG to sell off APPLE.
Table: shares_sell_info
Some input
id comp_name
123 APPLE
456 APPLE
123 XYZ
789 GOOG
456 INTEL
243 MRF
432 ABC
The ordering should be like below
123 XYZ (XYZ does not have any dependency and hence should come at the top)
432 ABC (MRF has a dependency on ABC and hence ABC comes on top)
243 MRF (MRF’s dependency is all taken care and hence we have MRF)
456 INTEL (APPLE and GOOGLE has a dependency on INTEL and hence INTEL is on top)
789 GOOG (At this point we can add GOOG because all its dependents are already at top)
123 APPLE (APPLE has a dependency on GOOG and hence GOOG come before APPLE)
456 APPLE
In the above ordering one among XYZ/ABC could have been first and it does not matter because they both do not have any dependency
dbfiddle
WITH
cte_com as (SELECT * FROM (VALUES
(123 ,'APPLE'),
(456 ,'APPLE'),
(123 ,'XYZ'),
(789 ,'GOOG'),
(456 ,'INTEL'),
(243 ,'MRF'),
(432 ,'ABC')) as cte_com(id, comp))
,cte_temp as (SELECT * FROM (VALUES
('APPLE', 'MS'),
('APPLE', 'INTEL' ),
('APPLE', 'MRF' ),
('APPLE', 'GOOG' ),
('MS', 'GOOG' ),
('MS', 'MRF' ),
('MRF', 'STF' ),
('MRF', 'ABC' ),
('GOOG', 'INTEL' ),
('GOOG', 'TRF' ),
('GOOG', 'XYZ')) as cte_temp(one, two))
SELECT id, comp , one
, count(*) as count
from cte_com
left join cte_temp on cte_temp.one=cte_com.comp
group by id, comp, one
order by count(*)
But it's unclear why this solution gives the ordering you want.
What is the difference between 'XYZ' and 'ABC'?
They are both depending on 1 other comp.
output:
id comp one count
123 XYZ 1
432 ABC 1
456 INTEL 1
243 MRF MRF 2
789 GOOG GOOG 3
123 APPLE APPLE 4
456 APPLE APPLE 4
7 rows
I think #Luuk's idea is right with some slight modifications. Here is the query which worked for me.
select * from shares_sell_info as ssi
left join (
select comp_name, count(*) as count
from shares_info si
group by comp_name
UNION
select comp_name, 0 as count
from shares_info
where investee is null
) temp on temp.comp_name = share_info.comp_name
where id in (
)
order by count
Here is the actual answer for my problem that I got from another post.
https://stackoverflow.com/questions/60420380/assign-weight-based-on-hierarchical-depth

Creating a new column based on data from an existing column

Consider a system to track repairs. I have a table with customer data and a common id key. I have a second table with a column to show which type of repair part was used on each id key and how many were used. Their definitions are below:
order_information
order_id | cust_last_name
465 Smith
899 Williams
512 Johnson
345 Fenton
122 Bowles
944 Cooper
parts_usage
order_id | part_type | part_quantity
465 Part 1 5
465 Part 2 4
899 Part 1 2
899 Part 2 8
899 Part 3 6
512 Part 3 1
345 Part 2 4
345 Part 3 5
122 Part 2 3
944 Part 1 2
I'd like to run a query for reporting that will return the part's pieces broken out like so:
order_id | Part 1 | Part 2 | Part 3 | Total
465 5 4 9
899 2 8 6 16
512 1 1
345 4 5 9
122 3 3
944 2 2
Is it possible to do this with a query so that my reports can show how many of each part was used on each repair ticket?
As you can see, each order_id can have multiple part types and unique quantities. I want to break the different part types (I have 3 total) into 3 separate columns with their totals listed by order_id.
select order_id, [Part 1], [Part 2], [Part 3], Total
from
(
select oi.order_id
, part_type
, part_quantity
, Total = sum(part_quantity) over (partition by oi.order_id)
from order_information oi
inner join parts_usage pu on oi.order_id = pu.order_id
) as parts
pivot
(
sum(part_quantity) for part_type in ([Part 1], [Part 2], [Part 3])
) as pvt
order by order_id
This works for me.
I have ordered the resultset by order_id as well; there doesn't appear to be a specific order in your example results but it is mentioned in the question details.
You can see the key is to combine the PIVOT with a SUM aggregate window function.
SQL Fiddle with demo.