I'm trying to make a query which shows the name of the guys(Angajat.nume and Angajat.prenume) and the salary (Angajat.salar) working at a restaurant (Restau.nume) and instead of writing it for 5 times, it writes for 25 times. I have 5 workers(id_a) and 1 restaurant(id_r) and 5 menu(id_m). How can I make it to show only for 5 times and keep the Meniu table in the query?
My Relations and Data Base
My Query
SELECT
Restau.nume, Angajat.nume AS Expr1, Angajat.prenume
FROM
Restau
INNER JOIN
Meniu ON Restau.id_r = Meniu.id_r
INNER JOIN
Angajat ON Restau.id_r = Angajat.id_r
You are using an unuseful join
SELECT
Restau.nume
, Angajat.nume AS Expr1
, Angajat.prenume
FROM Restau
INNER JOIN Angajat ON Restau.id_r = Angajat.id_r
add distinct if you have duplicated result
SELECT DISTINCT
Restau.nume
, Angajat.nume AS Expr1
, Angajat.prenume
FROM Restau
INNER JOIN Angajat ON Restau.id_r = Angajat.id_r
Related
I have made two SQL queries both bring back the information i want which is great, i was wondering if i could combine the result into one instead of 2 separate results , i don't know if this can be done but either show without headings or new custom headings.
the SQL code i am using is below
SELECT T_PRODUCT_NUTRITIONALINFORMATION.C_ENERGY_KJ ,T_PRODUCT_NUTRITIONALINFORMATION.C_PROTEIN, T_PRODUCT_SELLING_PRICEDEFINITION.C_NETPRICE
FROM ((T_PRODUCT
INNER JOIN T_PRODUCT_NUTRITIONALINFORMATION ON T_PRODUCT.C_NUTRITIONALINFORMATION = T_PRODUCT_NUTRITIONALINFORMATION.C_ID)
INNER JOIN T_PRODUCT_SELLING_PRICEDEFINITION ON T_PRODUCT.C_SELLING = T_PRODUCT_SELLING_PRICEDEFINITION.C__OWNER_)
WHERE C_CODE LIKE 'STK000832';
SELECT T_PRODUCT_NUTRITIONALINFORMATION.C_ENERGY_KJ ,T_PRODUCT_NUTRITIONALINFORMATION.C_CARBOHYDRATESOFWHICHARESUGAR, T_PRODUCT_SELLING_PRICEDEFINITION.C_NETPRICE
FROM ((T_PRODUCT
INNER JOIN T_PRODUCT_NUTRITIONALINFORMATION ON T_PRODUCT.C_NUTRITIONALINFORMATION = T_PRODUCT_NUTRITIONALINFORMATION.C_ID)
INNER JOIN T_PRODUCT_SELLING_PRICEDEFINITION ON T_PRODUCT.C_SELLING = T_PRODUCT_SELLING_PRICEDEFINITION.C__OWNER_)
WHERE C_CODE LIKE 'STK000832';
the result is below
below is the result i am trying to achieve
Sure, you can use UNION ALL
SELECT T_PRODUCT_NUTRITIONALINFORMATION.C_ENERGY_KJ AS NewHeading1,
T_PRODUCT_NUTRITIONALINFORMATION.C_PROTEIN AS NewHeading2,
T_PRODUCT_SELLING_PRICEDEFINITION.C_NETPRICE AS NewHeading3
FROM ((T_PRODUCT
INNER JOIN T_PRODUCT_NUTRITIONALINFORMATION ON T_PRODUCT.C_NUTRITIONALINFORMATION = T_PRODUCT_NUTRITIONALINFORMATION.C_ID)
INNER JOIN T_PRODUCT_SELLING_PRICEDEFINITION ON T_PRODUCT.C_SELLING = T_PRODUCT_SELLING_PRICEDEFINITION.C__OWNER_)
WHERE C_CODE LIKE 'STK000832'
UNION ALL
SELECT T_PRODUCT_NUTRITIONALINFORMATION.C_ENERGY_KJ AS NewHeading1,
T_PRODUCT_NUTRITIONALINFORMATION.C_CARBOHYDRATESOFWHICHARESUGAR AS NewHeading2,
T_PRODUCT_SELLING_PRICEDEFINITION.C_NETPRICE AS NewHeading3
FROM ((T_PRODUCT
INNER JOIN T_PRODUCT_NUTRITIONALINFORMATION ON T_PRODUCT.C_NUTRITIONALINFORMATION = T_PRODUCT_NUTRITIONALINFORMATION.C_ID)
INNER JOIN T_PRODUCT_SELLING_PRICEDEFINITION ON T_PRODUCT.C_SELLING = T_PRODUCT_SELLING_PRICEDEFINITION.C__OWNER_)
WHERE C_CODE LIKE 'STK000832';
I have two sets of queries:
SELECT
t.series_name,
ti.num_views_per_telecast
FROM
(
SELECT
ti.telecast_id,
ti.network_id,
count(*) as num_views_per_telecast
FROM
tunein AS ti
INNER JOIN affiliates AS a ON ti.network_id = a.network_id
WHERE
ti.dvr_time_shift = 'L+SD'
and a.network_name = 'ABC'
group by
ti.telecast_id,
ti.network_id
) ti
inner join telecast AS t On t.telecast_id = ti.telecast_id
ORDER BY
ti.num_views_per_telecast DESC
And
select
distinct *
from
telecast
where
episode_name = 'friday night dinner'
and series_name = 'A Million Little Things'
and date(program_start_local) = '2018-10-17'
I want to be able to combine the two so that I can get the num_views_per_telecast for the episodes in the bottom query. Not quite sure how I would inner join these though so I could keep the results from the first set of queries.
How the tables are connected are below:
How would I combine these???
At a guess:
SELECT
t.*,
ti.num_views_per_telecast
FROM
(
SELECT
ti.telecast_id,
ti.network_id,
count(*) as num_views_per_telecast
FROM
tunein AS ti
INNER JOIN
affiliates AS a
ON
ti.network_id = a.network_id
WHERE
ti.dvr_time_shift = 'L+SD' and
a.network_name = 'ABC'
group by
ti.telecast_id,
ti.network_id
)ti
inner join telecast AS t
On
t.telecast_id = ti.telecast_id
--from query2
where
t.episode_name = 'friday night dinner'
and t.series_name = 'A Million Little Things'
and t.date(program_start_local) = '2018-10-17'
ORDER BY ti.num_views_per_telecast DESC
For reasons given in the comment, the DISTINCT is redundant so you seem to want all rows from telecast that match some criteria. Given that your first query contains telecast but without any criteria and you only select one column from it, merging the two is a case of boosting the number of columns selected to (all from telecast) plus anything else, and adding the where clause from query 2 to restrict the rows in query 1
I am facing a complex SQL query in some code, which is suppose to return products without duplicates (by the use of DISTINCT keywork at the beginning), here is the query:
SELECT DISTINCT p.`id_product`, p.*, product_shop.*, pl.* , m.`name` AS manufacturer_name, x.`id_feature` , x.`id_feature_value` , s.`name` AS supplier_name
FROM `ps_product` p
INNER JOIN ps_product_shop product_shop
ON (product_shop.id_product = p.id_product AND product_shop.id_shop = 1)
LEFT JOIN `ps_product_attribute` y ON (y.`id_product` = p.`id_product`)
LEFT JOIN `ps_product_attribute_combination` ac ON (y.`id_product_attribute` = ac.`id_product_attribute`)
LEFT JOIN `ps_product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.id_shop = 1 )
LEFT JOIN `ps_manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)
LEFT JOIN `ps_feature_product` x ON (x.`id_product` = p.`id_product`)
LEFT JOIN `ps_supplier` s ON (s.`id_supplier` = p.`id_supplier`)
LEFT JOIN `ps_category_product` c ON (c.`id_product` = p.`id_product`)
WHERE pl.`id_lang` = 1 AND c.`id_category` = 18 AND p.`price` between 0 and 1000
AND product_shop.`visibility` IN ("both", "catalog") AND product_shop.`active` = 1
ORDER BY p.`id_product` ASC LIMIT 1,4
But it returns 4 product with 2 products with same "id_product" (11941)
What I need is to return 4 products but of different ids each.
Anyone ?
Thanks a lot
Aymeric
[EDIT]
The result of this query shows 4 rows, with 2 having the same exact columns values EXCEPT for the id_feature_value column which 36 for one and 38 for the other.
SELECT DISTINCT gets all the distinct combinations of all selected fields in your query, not just the first field.
Now, you could solve that by using GROUP BY to select only distinct values of id_product specifically, like:
SELECT p.`id_product`, p.*, product_shop.*, pl.* , m.`name` AS manufacturer_name, x.`id_feature` , x.`id_feature_value` , s.`name` AS supplier_name
FROM `ps_product` p
INNER JOIN ps_product_shop product_shop
ON (product_shop.id_product = p.id_product AND product_shop.id_shop = 1)
LEFT JOIN `ps_product_attribute` y ON (y.`id_product` = p.`id_product`)
LEFT JOIN `ps_product_attribute_combination` ac ON (y.`id_product_attribute` = ac.`id_product_attribute`)
LEFT JOIN `ps_product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.id_shop = 1 )
LEFT JOIN `ps_manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)
LEFT JOIN `ps_feature_product` x ON (x.`id_product` = p.`id_product`)
LEFT JOIN `ps_supplier` s ON (s.`id_supplier` = p.`id_supplier`)
LEFT JOIN `ps_category_product` c ON (c.`id_product` = p.`id_product`)
WHERE pl.`id_lang` = 1 AND c.`id_category` = 18 AND p.`price` between 0 and 1000
AND product_shop.`visibility` IN ("both", "catalog") AND product_shop.`active` = 1
GROUP BY p.`id_product`
ORDER BY p.`id_product` ASC LIMIT 1,4
However, the problem now is that your query has multiple different values of all the other fields you are selected to choose from, and no deterministic way to pick from them. Even though the id_product is unique in it's table, it's not unique in the result set because in at least one of your JOINs there is a one-to-many relationship, meaning there are several rows that match the JOIN conditions.
On older versions of MySQL, it will just pick the first value it finds in this case, but on SQL Server it will actually error out and tell you that the remaining fields either have to be mentioned in the GROUP BY clause, or they have to be aggregated. So, you've got a few ways you can go from here:
You are on an old version of MySQL and you don't particularly care which values are returned for the rest of the fields, so leave the query as I've posted and use that. I wouldn't recommend this, as it's undefined behaviour so in theory it could change at MySQL's whim. All the values returned will be from the same result row though.
Add aggregate functions, such as MIN() or MAX() to the rest of the remaining fields in the select clause. This will reduce the possible values for the fields down to one, but you will probably end up with a mixture of values from different rows.
Remove any one-to-many JOINs from your query so that you only ever get one row back in the result set for each individual id_product. Then, fetch the remaining data you need in a separate query.
There may be other alternative solutions, but it depends a lot on which values you want returned for the rest of the rows and what RDBMS you are using. For example, on SQL Server you could potentially make use of PARTITION BY to select the first row for each distinct id_product deterministically.
When I execute this query it gives a 10 result set .
select * from OA_SERVICE_REQUESTS WHERE
OA_SERVICE_REQUESTS.CUSREG_ID=4
But when I join with other table with to get more information, I use 2 inner join because this is 2 foreign key from ELVM_SMUNT_CUS table it gives me 120 results
select * from OA_SERVICE_REQUESTS
inner join ELVM_SMUNT_CUS T1 on OA_SERVICE_REQUESTS.DIVCOD = T1.DIVCOD
inner join ELVM_SMUNT_CUS T2 on OA_SERVICE_REQUESTS.UNTNUM = T2.UNTNUM
WHERE OA_SERVICE_REQUESTS.CUSREG_ID=4
Try to combine them together :
select * from OA_SERVICE_REQUESTS R
inner join ELVM_SMUNT_CUS T1 on ( R.DIVCOD = T1.DIVCOD
and R.UNTNUM = T1.UNTNUM )
where R.CUSREG_ID=4;
for your query not to produce cross-product results.
Probably, you have 12 matching records for R.DIVCOD = T1.DIVCOD, and 10 matching records for R.UNTNUM = T1.UNTNUM for R.CUSREG_ID=4, by combining the result set by an and you can have 10results at the same time, but may yield 120 occurences by 12 times 10, if conditions are taken apart by more joins.
I have 3 tables:
CP_carthead (idOrder)
CP_cartrows (idOrder, idCartRow)
CP_shipping (idCartRow, idShipping, dateShipped)
There can be multiple idCartRows per idOrder.
I want to get all orders where all its idCartRows exist in CP_shipping. This seems like it should be simple, but I haven't found much on the web.
Here's my query now:
SELECT
s.idOrder
, s.LatestDateShipped
FROM
CP_carthead o
LEFT OUTER JOIN (
SELECT
MAX(s.dateShipped) [LatestDateShipped]
, r.idOrder
FROM
CP_shipping s
LEFT OUTER JOIN CP_cartrows r ON s.idCartRow = r.idCartRow
GROUP BY
r.idOrder
) s ON o.idOrder = s.idOrder
Your query is returning rows from "s" and not the orders. Based on your question, I came up with this query:
select o.*
from CP_Carthead o
where o.orderId in (select cr.idOrder
from cp_cartrows cr left outer join
cp_shipping s
on cr.idCartRow = s.IdCartrow
group by cr.idOrder
having count(s.idCartRow) = COUNT(*)
)
The subquery in the in statement is getting orders all of whose cartrows are in shipping.