Match all values in a join view table - mysql

I have the following code where I create a view from two tables. I select few columns from each table. I tried to get the values from inventory.location_x and inventory.location_yfrom all the matches from both tables. However, it only selects one. It should return at least 3 pars of values.
Somebody can help me?
CREATE VIEW summary AS (
SELECT contexts, description, SKU, inventory.location_x, inventory.location_y
FROM inventory
LEFT JOIN product on inventory.epc_hex = product.epc having max(inventory.cycle)
);
Here there is a link to find an example of the two tables and the desired output:
https://www.dropbox.com/sh/k2imnxkqu412mp3/AABeIjgPUflPm8yx9nvr_uida?dl=0

In your query, you are using the "having" clause without specifying any aggregate functions in the select part of the sql.
Try removing the having clause and you will get more records. Try the following:
CREATE VIEW summary AS (
SELECT contexts, description, SKU, inventory.location_x, inventory.location_y
FROM inventory
LEFT JOIN product on inventory.epc_hex = product.epc
);
The following webpage gives examples on how to use the having clause:
http://dev.mysql.com/doc/refman/5.0/en/group-by-handling.html

Related

SQL Temporary Table or Select

I've got a problem with MySQL select statement.
I have a table with different Department and statuses, there are 4 statuses for every department, but for each month there are not always every single status but I would like to show it in the analytics graph that there is '0'.
I have a problem with select statement that it shows only existing statuses ( of course :D ).
Is it possible to create temporary table with all of the Departments , Statuses and amount of statuses as 0, then update it by values from other select?
Select statement and screen how it looks in perfect situation, and how it looks in bad situation :
SELECT utd.Departament,uts.statusDef as statusoforder,Count(uts.statusDef) as Ilosc_Statusow
FROM ur_tasks_details utd
INNER JOIN ur_tasks_status uts on utd.StatusOfOrder = uts.statusNR
WHERE month = 'Sierpien'
GROUP BY uts.statusDef,utd.Departament
Perfect scenario, now bad scenario :
I've tried with "union" statements but i don't know if there is a possibility to take only "the highest value" for every department.
example :
I've also heard about
With CTE tables, but I don't really get how to use it. Would love to get some tips on it!
Thanks for your help.
Use a cross join to generate the rows you want. Then use a left join and aggregation to bring in the data:
select d.Departament, uts.statusDef as statusoforder,
Count(uts.statusDef) as Ilosc_Statusow
from (select distinct utd.Departament
from ur_tasks_details utd
) d cross join
ur_tasks_status uts left join
ur_tasks_details utd
on utd.Departament = d.Departament and
utd.StatusOfOrder = uts.statusNR and
utd.month = 'Sierpien'
group by uts.statusDef, d.Departament;
The first subquery should be your source of all the departments.
I also suspect that month is in the details table, so that should be part of the on clause.

Include rows which don't match with IN() clause

I have a table called log which contains logs sent by several applications. This table has a varchar field called reference.
I have a table panel in Grafana in which I show how many logs we have grouped by reference values. So the user types one or multiple values in a text field on Grafana like 'ref1', 'ref2', 'ref3' and a query like this is fired:
SELECT reference, count(id)
FROM db.log
WHERE reference IN('ref1', 'ref2', 'ref3')
GROUP BY reference
So far so good, it works as intended. What I would like to do is showing a row with count=0 in case a log with given reference doesn't exist. I know I could add arbitrary rows using UNION but I think I can't do it in Grafana dynamically.
Any ideas?
Use a query that returns all the values for which you want results and left join the table to aggregate:
select t.reference, count(l.id)
from (
select 'ref1' reference union all
select 'ref2' union all
select 'ref3'
) t left join db.log l
on l.reference = t.reference
group by t.reference
See a simplified demo.

WHERE clause with INNER JOIN and Sub Query

I have achieved my desired query but I want to know how this one worked. I have multiple tables on my database and my requirements was to take the id from table called product and using this id, I want to retrieve some data from multiple tables and product id is a foreign key to the other tables. The query below works fine (by the way I was just experimenting and luckily got this query).
SELECT ponsfdp.*, product.pName, product.pImage, product.productSizes FROM product
INNER JOIN priceOnSizesForDigitalPrinting AS ponsfdp ON ponsfdp.pId_fk =
(SELECT pId FROM product WHERE pName LIKE "%booklet%")
WHERE pName LIKE "%booklet%";
But when I tried this query,
SELECT ponsfdp.*, product.pName, product.pImage, product.productSizes FROM product
INNER JOIN priceOnSizesForDigitalPrinting AS ponsfdp ON ponsfdp.pId_fk =
(SELECT pId FROM product WHERE pName LIKE "%booklet%");
It contains all the data even with null fields too. Can someone explain to me how it works? My personal opinion is both query should return same data because on the second query, I am using a subquery and it returns only one id, on the other hand, first query has a WHERE clause which generates the same id but by the help of name. How does the first query returns very specific columns and second return all columns even null columns too? I need an explanation for both queries.
Your first query also returning all rows as returned from your second query. But, when you are adding the last filter-
WHERE pName LIKE "%booklet%"
It's just keeping one single row from all rows where pName is like 'booklet'. You can consider the output from your second query as a single table and your logic working as below-
SELECT * FROM (
SELECT ponsfdp.*, product.pName, product.pImage, product.productSizes
FROM product
INNER JOIN priceOnSizesForDigitalPrinting AS ponsfdp
ON ponsfdp.pId_fk = (SELECT pId FROM product WHERE pName LIKE "%booklet%")
)A
WHERE pName LIKE "%booklet%"
Hope this will at least give you some insight of your query.
I don't see any need for a subquery here. You should be using the where condition to select rows from your FROM table, then use the ON clause of your join to find the right record(s) in your joined table for each row of the FROM table:
SELECT ponsfdp.*, product.pName, product.pImage, product.productSizes
FROM product
INNER JOIN priceOnSizesForDigitalPrinting AS ponsfdp
ON ponsfdp.pId_fk = pId
WHERE pName LIKE "%booklet%";

Removing Duplicating Issue in MySQL views

I have two tables called transactions (TransactionID, HotelID, service..) and services (id, userid, HotelID, type) and I need to create view from there. In here 1st table has 15 tuples and 2nd one has 8 tuples.
When I wrote this SQL query:
CREATE VIEW summary
AS
SELECT TransactionID, userid, HotelID, service
FROM transactions, services
WHERE transactions.HotelID = services.name
I got 105 results in summary view. How I get rid of this duplication issue.
As far as i understands your question, i think you need to use group concate and group by using join query.
using group by and group concate duplication of record will be solve.
and using join you can get common data from both table.
try below query.
currently i am not having knowledge of your table structure that's why i take assumption of your query and created my own tables and as per them created query.
Eg:
select
c.country_id,c.country_name,group_concat(s.state_id),group_concat(s.state_name) from country c inner join state s on c.country_id =s.country_id group
by c.country_id;
if you have any query feel free to ask.

inner join with group by function

I am trying to get results matching a variable using a LIKE operator combined with an INNER JOIN and a GROUPING modifier.
The problem I am having with this query:
-the join will not work with the modifier as it is being grouped by the same column that is being used to join the two tables. I get the error '#1052 - Column 'Agency' in field list is ambiguous'
SELECT Agency,Acronym,last,sum(last),current,sum(current),source,url
FROM `budget_table` INNER join budget_table2 on
budget_table.agency=budget_table2.agency WHERE (Agency) LIKE('%$agency%') GROUP BY Agency
I have looked through other answers but am not able to apply what is posted to what I am doing. Assistance would be appreciated. Is there something wrong with my JOIN?
I am trying to select only agencies common to both tables and then match data against the variable from within those results.
If you have the same column name in two tables, you have to reference the table do you want to get the column value. Write the table name before the column name in your select, where and group.
Example:
SELECT budget_table.Agency /* ...your code... */
WHERE budget_table.Agency LIKE ('%$agency%')
GROUP BY budget_table.Agency