Join/Pivot items with EAV table - sql

I have a table like below which would have the product description
╔════╦══════════════╦══════╗
║ Id ║ name ║ price║
╠════╬══════════════╬══════╣
║ 1 ║ Apple ║ 23 ║
║ 2 ║ shirt ║ 148 ║
║ 3 ║ computer ║ 101 ║
║ 4 ║ printer ║ 959 ║
╚════╩══════════════╩══════╝
and another table which is holding the attributes like linked by the ID
╔════╦══════════════╦══════╗
║ Id ║ attr_name ║ Value║
╠════╬══════════════╬══════╣
║ 1 ║ color ║ red ║
║ 1 ║ size ║ xl ║
║ 1 ║ brand ║ App ║
║ 2 ║ color ║ blue║
║ 2 ║ size ║ l ║
║ 3 ║ color ║ blue║
║ 3 ║ size ║ xxl ║
║ 3 ║ brand ║ HP ║
╚════╩══════════════╩══════╝
Is there any possible way to bring a table like below if I know the attribute name is going to be only color size and brand
╔════╦══════════╦═══════╦═══════╦══════╦══╗
║ id ║ name ║ color ║ brand ║ size ║ ║
╠════╬══════════╬═══════╬═══════╬══════╬══╣
║ 1 ║ apple ║ red ║ app ║ xl ║ ║
║ 2 ║ shirt ║ blue ║ ║ l ║ ║
║ 3 ║ computer ║ blue ║ HP ║ XXL ║ ║
║ 4 ║ printer ║ ║ ║ ║ ║
╚════╩══════════╩═══════╩═══════╩══════╩══╝

You should be able to use an aggregate function with a CASE expression to convert the rows into columns:
select d.id,
d.name,
max(case when a.attr_name = 'color' then a.value end) color,
max(case when a.attr_name = 'brand' then a.value end) brand,
max(case when a.attr_name = 'size' then a.value end) size
from product_description d
inner join product_attributes a
on d.id = a.id
group by d.id, d.name;
See SQL Fiddle with Demo.
Since you are using Oracle 11g, then you can use the PIVOT function to get the result:
select id, name, Color, Brand, "Size"
from
(
select d.id, d.name,
a.attr_name, a.value
from product_description d
inner join product_attributes a
on d.id = a.id
) src
pivot
(
max(value)
for attr_name in ('color' as Color,
'brand' as Brand,
'size' as "Size")
) p;
See SQL Fiddle with Demo

Related

Multiple Full Joins On a User Table in a Single Query

I have a schema of a database where we have a a user table and several fact tables.
The schema is goes something like this:
╔════════════╗
║ USERS ║
╠════════════╣
║ sales_id ║ PK
║ traveler_id║ PK
╚════════════╝
╔════════════╗
║ Sales ║
╠════════════╣
║ sales_id ║ --> FK to User sales_id PK
║ date ║
║ metric_1 ║
║ metric_2 ║
╚════════════╝
╔════════════╗
║ Travels ║
╠════════════╣
║ traveler_id║ --> FK to User traveler_id PK
║ date ║
║ metric_3 ║
║ metric_4 ║
╚════════════╝
I'm trying to join them as a single fact table to get all the metrics and their dates in a inqle query. It would return something like below:
╔════════════╦══════════════╦══════════╦══════════╦══════════╦══════════╦══════════╗
║ sales_id ║ traveler_id ║ date ║ metric_1 ║ metric_2 ║ metric_3 ║ metric_4 ║
╠════════════╬══════════════╬══════════╬══════════╬══════════╬══════════╬══════════╣
║ 1 ║ A ║ Jan ║ x1 ║ a1 ║ null ║ null ║
║ 1 ║ A ║ Feb ║ x2 ║ a2 ║ null ║ null ║
║ 1 ║ A ║ Jan ║ x3 ║ a3 ║ b1 ║ null ║
║ 2 ║ B ║ Mar ║ x4 ║ a4 ║ null ║ c1 ║
╚════════════╩══════════════╩══════════╩══════════╩══════════╩══════════╩══════════╝
Here is the sql I wrote:
select
coalesce(s.date, t.date) as date,
s.sales_id,
t.traveler_id,
max(coalesce(s.metric_1, 0)) as metric_1,
max(coalesce(s.metric_2, 0)) as metric_2,
max(coalesce(t.metric_3, 0)) as metric_3,
max(coalesce(t.metric_4, 0)) as metric_4,
from users as u
full join sales as s on (u.sales_id = s.sales_id)
full join travels as t on (u.traveler_id = t.traveler_id)
group by 1, 2, 3
However, my joining logic ends up returning the same values for the rows that are as null.
╔════════════╦══════════════╦══════════╦══════════╦══════════╦══════════╦══════════╗
║ sales_id ║ traveler_id ║ date ║ metric_1 ║ metric_2 ║ metric_3 ║ metric_4 ║
╠════════════╬══════════════╬══════════╬══════════╬══════════╬══════════╬══════════╣
║ 1 ║ A ║ Jan ║ x1 ║ a1 ║ b1 ║ null ║
║ 1 ║ A ║ Feb ║ x2 ║ a2 ║ b1 ║ null ║
║ 1 ║ A ║ Jan ║ x3 ║ a3 ║ b1 ║ null ║
║ 2 ║ B ║ Mar ║ x4 ║ a4 ║ null ║ null ║
╚════════════╩══════════════╩══════════╩══════════╩══════════╩══════════╩══════════╝
My suspicion is that it's due to the second full join. I would assume I could break these down into two other joins, and match their id, but I'm having a hard time trying to understand how to incorporate the dates, properly.
select
coalesce(s.date, t.date) as date,
s.sales_id,
t.traveler_id,
max(coalesce(s.metric_1, 0)) as metric_1,
max(coalesce(s.metric_2, 0)) as metric_2,
max(coalesce(t.metric_3, 0)) as metric_3,
max(coalesce(t.metric_4, 0)) as metric_4
from users as u
left join sales as s on (u.sales_id = s.sales_id and s.date = coalesce(s.date, t.date))
left join travels as t on (u.traveler_id = t.traveler_id and t.date = coalesce(s.date, t.date))
group by 1, 2, 3
This is what the table looks like:
╔════════════╦══════════════╦══════════╦══════════╦══════════╦══════════╦══════════╗
║ sales_id ║ traveler_id ║ date ║ metric_1 ║ metric_2 ║ metric_3 ║ metric_4 ║
╠════════════╬══════════════╬══════════╬══════════╬══════════╬══════════╬══════════╣
║ 1 ║ A ║ Jan ║ x1 ║ a1 ║ b1 ║ null ║
║ 1 ║ A ║ Feb ║ x2 ║ a2 ║ null ║ null ║
║ 1 ║ A ║ Jan ║ x3 ║ a3 ║ b1 ║ null ║
║ 2 ║ B ║ Mar ║ x4 ║ a4 ║ null ║ c1 ║
╚════════════╩══════════════╩══════════╩══════════╩══════════╩══════════╩══════════╝

How to filter SQL dataset based off value in one column?

I have a table with this structure:
╔══════╦════╦═════════╗
║ Comp ║ ID ║ Desc ║
╠══════╬════╬═════════╣
║ 1 ║ 1 ║ Comp1-1 ║
║ 1 ║ 2 ║ Comp1-2 ║
║ 3 ║ 2 ║ Comp3-2 ║
║ 1 ║ 3 ║ Comp1-3 ║
║ 1 ║ 4 ║ Comp1-4 ║
║ 3 ║ 5 ║ Comp3-5 ║
╚══════╩════╩═════════╝
The dataset I'm creating should have a unique ID.
If an ID exists in Comp1, use that Desc.
If it does not exist in Comp1, use Comp3.
End result should look like this instead:
╔══════╦════╦═════════╗
║ Comp ║ ID ║ Desc ║
╠══════╬════╬═════════╣
║ 1 ║ 1 ║ Comp1-1 ║
║ 1 ║ 2 ║ Comp1-2 ║
║ 1 ║ 3 ║ Comp1-3 ║
║ 1 ║ 4 ║ Comp1-4 ║
║ 3 ║ 5 ║ Comp3-5 ║
╚══════╩════╩═════════╝
I've tried using NOT EXISTS and joining with a subquery but I'm not sure what to Join on.
Using not exists, it looks like:
select t.*
from t
where t.descr like 'Comp1-%' or
not exists (select 1
from t t2
where t2.id = t.id and t2.descr like 'Comp1-%'
);

How to remove count values "0" from result table?

I'm trying to write a query that returns a table with columns name and numberOfClasses. This table includes the name of all students and the amount of classes the student follows. I use the following database tables:
╔══════════════╦═══════════╦══════════╗
║ TakesClasses ║ ║ ║
╠══════════════╬═══════════╬══════════╣
║ id ║ person_id ║ class_id ║
║ 99 ║ 1 ║ 40 ║
║ 98 ║ 1 ║ 41 ║
║ 97 ║ 1 ║ 42 ║
║ 96 ║ 1 ║ 43 ║
║ 95 ║ 2 ║ 44 ║
║ 94 ║ 2 ║ 45 ║
║ 93 ║ 2 ║ 46 ║
╚══════════════╩═══════════╩══════════╝
╔═════════╦═══════╦══╗
║ Persons ║ ║ ║
╠═════════╬═══════╬══╣
║ id ║ name ║ ║
║ 1 ║ Bart ║ ║
║ 2 ║ David ║ ║
║ 3 ║ Dani ║ ║
║ 4 ║ Erik ║ ║
╚═════════╩═══════╩══╝
I used the following query:
SELECT
name,
COUNT(T.person_id) AS numberOfClasses
FROM
Persons P
LEFT OUTER JOIN
TakesClasses T ON P.id = T.person_id
GROUP BY
P.id, P.name
And this is the output:
╔═══════╦═════════════════╗
║ name ║ numberOfClasses ║
╠═══════╬═════════════════╣
║ Bart ║ 4 ║
║ Dani ║ 3 ║
║ David ║ 0 ║
║ Erik ║ 0 ║
╚═══════╩═════════════════╝
How can I remove the entries with 0 from my the results table?
Thanks for the help in advance!
Just use JOIN instead of LEFT JOIN
select name, count(T.person_id) as amountOfClasses
from Persons P join
TakesClasses T
on P.id = T.person_id
group by P.id, P.name
If you want to filter on aggregated data like what comes out of GROUP BY you can use the HAVING clause
SELECT name,COUNT(T.person_id) as amountOfClasses
FROM Persons P LEFT OUTER JOIN TakesClasses T
on P.id = T.person_id
GROUP BY P.id, P.name
HAVING COUNT(T.person_id) > 0;
sqlFiddle: http://sqlfiddle.com/#!9/1108c5/8

Best way to select calculated values from another table in the same query postgres?

Hi i have postgres database and four tables
vehicles -> trips
vehicles -> component_values -> component_types
vehicles:
╔════╦══════════════════════════╦════════════════════════╦════════════════╦═════════╗
║ id ║ slug ║ name ║ manufacturer ║ model ║
╠════╬══════════════════════════╬════════════════════════╬════════════════╬═════════╣
║ 1 ║ manufacturer-x-model-3 ║ Manufacturer X Model 3 ║ Manufacturer X ║ Model 3 ║
║ 2 ║ manufacturer-x-model-1 ║ Manufacturer X Model 1 ║ Manufacturer X ║ Model 1 ║
║ 3 ║ manufacturer-x-model-1-1 ║ Manufacturer X Model 1 ║ Manufacturer X ║ Model 1 ║
╚════╩══════════════════════════╩════════════════════════╩════════════════╩═════════╝
trips:
╔═════╦════════════╦═════════════╦═════════════╦═════════════════╗
║ id ║ vehicle_id ║ name ║ mileage ║ recorded_at ║
╠═════╬════════════╬═════════════╬═════════════╬═════════════════╣
║ 1 ║ 1 ║ 10386735 ║ 386734.997 ║ 2/25/2014 13:56 ║
║ 2 ║ 1 ║ 11771530.14 ║ 771530.14 ║ 3/1/2014 19:41 ║
║ 3 ║ 1 ║ 121112028.4 ║ 1112028.39 ║ 3/5/2014 3:23 ║
║ 4 ║ 1 ║ 131406814.9 ║ 1406814.892 ║ 3/8/2014 20:56 ║
║ 5 ║ 1 ║ 141933528.7 ║ 1933528.711 ║ 3/13/2014 0:19 ║
║ 6 ║ 1 ║ 152256488.6 ║ 2256488.579 ║ 3/16/2014 21:15 ║
╚═════╩════════════╩═════════════╩═════════════╩═════════════════╝
component_values:
╔════╦═══════════════════╦═════════╦════════════╦════════════╦═════════════╦═════════════╗
║ id ║ component_type_id ║ trip_id ║ vehicle_id ║ mileage ║ damage ║ damage_eff ║
╠════╬═══════════════════╬═════════╬════════════╬════════════╬═════════════╬═════════════╣
║ 1 ║ 1 ║ 1 ║ 1 ║ 386734.997 ║ 0.002260565 ║ 0.002225831 ║
║ 2 ║ 2 ║ 1 ║ 1 ║ 386734.997 ║ 0.002260306 ║ 0.002238006 ║
║ 3 ║ 3 ║ 1 ║ 1 ║ 386734.997 ║ 0.002261288 ║ 0.002266295 ║
║ 4 ║ 4 ║ 1 ║ 1 ║ 386734.997 ║ 0.002054489 ║ 0.002060029 ║
║ 5 ║ 5 ║ 1 ║ 1 ║ 386734.997 ║ 0.002052669 ║ 0.002107272 ║
║ 6 ║ 6 ║ 1 ║ 1 ║ 386734.997 ║ NULL ║ NULL ║
║ 7 ║ 7 ║ 1 ║ 1 ║ 386734.997 ║ NULL ║ NULL ║
║ 8 ║ 1 ║ 2 ║ 1 ║ 771530.14 ║ 0.004792952 ║ 0.0048514 ║
║ 9 ║ 2 ║ 2 ║ 1 ║ 771530.14 ║ 0.004792404 ║ 0.004710451 ║
║ 10 ║ 3 ║ 2 ║ 1 ║ 771530.14 ║ 0.004794486 ║ 0.004805461 ║
╚════╩═══════════════════╩═════════╩════════════╩════════════╩═════════════╩═════════════╝
component_types:
╔════╦═════════════════════════════════════╦════════════════╦══════════════════════╗
║ id ║ slug ║ manufacturer ║ name ║
╠════╬═════════════════════════════════════╬════════════════╬══════════════════════╣
║ 6 ║ manufacturer-d-battery ║ Manufacturer D ║ Battery ║
║ 2 ║ manufacturer-b-differential-1 ║ Manufacturer B ║ Differential 1 ║
║ 3 ║ manufacturer-c-driveshaft-1 ║ Manufacturer C ║ Driveshaft 1 ║
║ 5 ║ manufacturer-c-gearbox-output-shaft ║ Manufacturer C ║ Gearbox output shaft ║
║ 1 ║ manufacturer-a-motor-1 ║ Manufacturer A ║ Motor 1 ║
║ 4 ║ manufacturer-c-gearbox-input-shaft ║ Manufacturer C ║ Gearbox input shaft ║
║ 7 ║ usage-profile ║ ║ Usage profile ║
╚════╩═════════════════════════════════════╩════════════════╩══════════════════════╝
and i'm trying to get the vehicles with the latest trip dates and mileage and also the heights and lowest damaged component for each vehicle
so the result will be like:
╔════════════╦══════════════════╦══════════════════════════╦═════════════════════════════════╦════════════════════════════════╦════════════════════════════════╦═══════════════════════════════╗
║ vehicle_id ║ latest_trip_date ║ latest_trip_date_mileage ║ heights_damaged_component_value ║ heights_damaged_component_name ║ lowest_damaged_component_value ║ lowest_damaged_component_name ║
╠════════════╬══════════════════╬══════════════════════════╬═════════════════════════════════╬════════════════════════════════╬════════════════════════════════╬═══════════════════════════════╣
║ 1 ║ 4/19/2014 3:27 ║ 4844305.912 ║ 0.029372972 ║ Gearbox input shaft ║ 0.002052669 ║ Gearbox output shaft ║
║ 2 ║ 5/19/2014 16:13 ║ 5567945.164 ║ 0.029405924 ║ Driveshaft 1 ║ 0.001864137 ║ Gearbox output shaft ║
║ 3 ║ 4/28/2014 12:55 ║ 5286175.763 ║ 0.030745029 ║ Driveshaft 1 ║ 0.001957685 ║ Differential 1 ║
║ 4 ║ 2/25/2014 3:32 ║ 5398006.007 ║ 0.030495792 ║ Driveshaft 1 ║ 0.001814434 ║ Differential 1 ║
║ 5 ║ 4/25/2014 9:51 ║ 5179558.475 ║ 0.032060074 ║ Gearbox input shaft ║ 0.001936431 ║ Differential 1 ║
║ 6 ║ 5/9/2014 7:43 ║ 5234355.804 ║ 0.030576454 ║ Gearbox input shaft ║ 0.002254191 ║ Gearbox output shaft ║
║ 7 ║ 6/21/2014 18:09 ║ 5705722.416 ║ 0.029957374 ║ Driveshaft 1 ║ 0.001653441 ║ Gearbox output shaft ║
║ 8 ║ 4/23/2014 5:25 ║ 5590470.028 ║ 0.031900163 ║ Driveshaft 1 ║ 0.002083733 ║ Gearbox output shaft ║
║ 9 ║ 3/28/2014 20:37 ║ 5598159.883 ║ 0.031208918 ║ Driveshaft 1 ║ 0.00162805 ║ Differential 1 ║
║ 10 ║ 5/24/2014 19:27 ║ 5020795.001 ║ 0.02962505 ║ Gearbox input shaft ║ 0.001729646 ║ Differential 1 ║
╚════════════╩══════════════════╩══════════════════════════╩═════════════════════════════════╩════════════════════════════════╩════════════════════════════════╩═══════════════════════════════╝
i already tried this query
select
vehicles.id as vehicle_id,
latest_trips.recorded_at as latest_trip_date,
latest_trips.mileage as latest_trip_date_mileage,
heights_damaged_components.damage as heights_damaged_component_value,
heights_damaged_components.name as heights_damaged_component_name,
lowest_damaged_components.damage as lowest_damaged_component_value,
lowest_damaged_components.name as lowest_damaged_component_name
from vehicles
left join (
SELECT t.id, t.vehicle_id, t.mileage, t.recorded_at
FROM public.trips t
inner JOIN (SELECT vehicle_id, MAX(recorded_at) as latest_trip_date FROM public.trips GROUP BY vehicle_id)
tm ON t.vehicle_id = tm.vehicle_id AND t.recorded_at = tm.latest_trip_date
)
as latest_trips on latest_trips.vehicle_id = vehicles.id
left join (
select ct.name, hd.component_type_id, hd.vehicle_id, hd.damage
from public.component_values as hd
INNER JOIN (
SELECT vehicle_id,
MAX(damage) as heights_damaged_component
FROM public.component_values
GROUP BY vehicle_id
)
hdm ON hd.vehicle_id = hdm.vehicle_id AND hd.damage = hdm.heights_damaged_component
join public.component_types as ct on ct.id = hd.component_type_id
)
as heights_damaged_components on heights_damaged_components.vehicle_id = vehicles.id
left join (
select ct.name, ld.component_type_id, ld.vehicle_id, ld.damage
from public.component_values as ld
INNER JOIN (
SELECT vehicle_id, MIN(damage) as lowest_damaged_component
FROM public.component_values
GROUP BY vehicle_id
)
ldm ON ld.vehicle_id = ldm.vehicle_id AND ld.damage = ldm.lowest_damaged_component
join public.component_types as ct on ct.id = ld.component_type_id
) as lowest_damaged_components on lowest_damaged_components.vehicle_id = vehicles.id
but i have like 10000 vehicles and big trips and component_values and this query give me the result in like 3 to 6 seconds, is their a batter way to do this with better performance and time?
can i use GENERATED columns in my case and how ?
please any help and many many thanks in advance.

Matching data from two tables with unequal number of rows

I have 2 tables, and I am trying to match up the data but all the answers have suggested right joins or full joins, which are not available on SQLite.
Table 1:
╔═════════╦═════════╦══════╗
║ Company ║ Product ║ Cost ║
╠═════════╬═════════╬══════╣
║ A ║ abc ║ 100 ║
║ B ║ abc ║ 150 ║
║ F ║ xyz ║ 250 ║
║ G ║ xyz ║ 300 ║
╚═════════╩═════════╩══════╝
However I have a list of more companies (with same products)
Table 2:
╔═════════╦═════════╗
║ Product ║ Company ║
╠═════════╬═════════╣
║ abc ║ A ║
║ abc ║ B ║
║ abc ║ C ║
║ abc ║ D ║
║ abc ║ E ║
║ abc ║ F ║
║ abc ║ G ║
║ xyz ║ A ║
║ xyz ║ B ║
║ xyz ║ C ║
║ xyz ║ D ║
║ xyz ║ E ║
║ xyz ║ F ║
║ xyz ║ G ║
╚═════════╩═════════╝
How do I match them up so they look like this?
Table 3:
╔═════════╦═════════╦══════╗
║ Product ║ Company ║ Cost ║
╠═════════╬═════════╬══════╣
║ abc ║ A ║ 100 ║
║ abc ║ B ║ 150 ║
║ abc ║ C ║ null ║
║ abc ║ D ║ null ║
║ abc ║ E ║ null ║
║ abc ║ F ║ null ║
║ abc ║ G ║ null ║
║ xyz ║ A ║ null ║
║ xyz ║ B ║ null ║
║ xyz ║ C ║ null ║
║ xyz ║ D ║ null ║
║ xyz ║ E ║ null ║
║ xyz ║ F ║ 250 ║
║ xyz ║ G ║ 300 ║
╚═════════╩═════════╩══════╝
When I use this code,
SELECT Company, t.Product, Cost
FROM table1 as t INNER JOIN table2 as f ON t.product = f.product
WHERE t.company = f.company
it only returns [Company] with an associated [Product] and [Cost], but does not return [Company] with null values.
When I use
SELECT Company, t.Product, Cost
FROM table1 as t INNER JOIN table2 as f ON t.company = f.company
then my output looks like
╔═══════════╦═══════════╦═════════╗
║ t.Company ║ f.Company ║ Product ║
╠═══════════╬═══════════╬═════════╣
║ A ║ A ║ abc ║
║ B ║ A ║ abc ║
║ F ║ A ║ abc ║
║ G ║ A ║ abc ║
║ A ║ B ║ abc ║
║ B ║ B ║ abc ║
║ F ║ B ║ abc ║
║ G ║ B ║ abc ║
║ A ║ C ║ abc ║
║ B ║ C ║ abc ║
║ F ║ C ║ abc ║
║ G ║ C ║ abc ║
╚═══════════╩═══════════╩═════════╝
Any help will be much appreciated. Thank you!
SQLite does support LEFT OUTER JOIN, which should do the job just fine:
select two.product, two.company, one.cost from two
left outer join one on
((one.company = two.company) and (one.product = two.product));
(where two is your "table 2" and one is your "table 1")
Running this in SQLite with the above data:
abc|A|100
abc|B|150
abc|C|
abc|D|
abc|E|
abc|F|
abc|G|
xyz|A|
xyz|B|
xyz|C|
xyz|D|
xyz|E|
xyz|F|250
xyz|G|300