I have 3 different tables: table1, table2, table3
Table 1 contains all the different orders that were purchased
Table 2 contains the detail of every order (i mean, it contains a
column called ORDER_DETAIL and the number represent an item of
that order -a unique value)
Table 3 contains the workflow.. some numbers that were inside
ORDER_DETAIL from Table 2 will appear here because this item must be
approved to be delivered
I want to obtain all the different orders whose items did not appear in Table 3.
This picture explains everything:
This is my SQLFIDDLE: http://sqlfiddle.com/#!9/5bfc22/2
I did this query but i am not getting what i want:
select * from table1 kio
inner join table2 jio on kio.ORDER_NUMBER = jio.ORDER_NUMBER
where jio.CANCELLED = 0
and not exists (select 1 from table3 gio where jio.ORDER_DETAIL = gio.ORDER_DETAIL)
Also, how can i obtain those orders whose ORDER_DETAILs only appear on TABLE 2 AND those orders whose order_details appear in table 3 with PROCESSED = 1 and APPROVED = 1? All in the same query.
You can use aggregation: join table1 with table2, then left join table3, aggregate by order_number and filter on groups that have no match in table3.
select t1.id, t1.order_number
from table1 t1
inner join table2 t2 on t2.order_number = t1.order_number
left join table3 t3 on t3.order_detail = t2.order_detail
group by t1.id, t1.order_number
having count(t3.order_detail) = 0
In your DB Fiddle, this produces:
id order_number
3 46646
Also, how can i obtain those orders whose ORDER_DETAILs only appear on TABLE 2 AND those orders whose order_details appear in table 3 with PROCESSED = 1 and APPROVED = 1? All in the same query
For this, you can just add another pair of conditions in the having clause:
having
count(t3.order_detail) = 0
or (max(t3.processed) = 1 and max(t3.approved) = 1)
Yields:
id order_number
1 78945
3 46646
I want to obtain all the different orders whose items did not appear in Table 3.
This seems like a reasonable interpretation of the question, although you add more questions later.
If so, then you don't seem to need table1:
select t2.order_number
from table2 t2 left join
table3 t3
on t2.ORDER_DETAIL = t3.ORDER_DETAIL
group by t2.order_number
having count(t3.ORDER_DETAIL) = 0;
Related
I have two tables (table 1 and table 2). Table 1 consists the options and table 2 consists the result for each options.
**table1** table2
id id
optionvalue optionvalueid
time (unixtime)
So when the data is inserted it will be stored in table2. There are 5 optionvalue in table 1 and when data is inserted, then in the table2 it will insert the optionvalueid of table 1 and timestamp in unixtimestamp. Eaach month, I want to count the number of values for each optionvalue. Evene if there is no value for an optionvalue, I still want to see count as zero.
I did the following query but only return the value with rows with data only.
SELECT
po.id,po.optionvalue, COUNT(pr.optionvalueid) as votes,
FROM_UNIXTIME(`time`, '%m-%Y') as ndate
FROM table 1
LEFT JOIN table 2 pr ON po.id=pr.optionvalueid
GROUP BY ndate, po.optionvalue ORDER BY ndate ASC
Is there any other ways to make the query so that it will return all the options even if there is no value.
You can CROSS join table1 to the distinct months of table2 and then LEFT join to table2 to aggregate:
SELECT t.ndate, t1.id, t1.optionvalue, COUNT(t2.optionvalueid) votes
FROM table1 t1
CROSS JOIN (SELECT DISTINCT FROM_UNIXTIME(`time`, '%m-%Y') ndate FROM table2) t
LEFT JOIN table2 t2 ON t1.id = t2.optionvalueid AND t.ndate = FROM_UNIXTIME(t2.`time`, '%m-%Y')
GROUP BY t.ndate, t1.id, t1.optionvalue
ORDER BY t.ndate ASC
I have two tables I'm trying to join to produce a unique set of data for a third table, but having trouble doing this properly.
The left table has an id field, as well as a common join field (a).
The right table has the common join field (a), and another distinct field (b)
I'm trying to extract a result-set of id and b, where neither id nor b are duplicated.
I have an SQL fiddle set up: http://www.sqlfiddle.com/#!9/208de/3/0
The ideal results should be:
id | b
---+---
1 | 1
2 | 2
3 | 3
Each id and b value appears only once (it's only coincidence they match here, that can't be assumed always).
Thanks
What about a CTE along with a DISTINCT, Would that work?
WITH
cte1 (ID, B)
AS
(
SELECT DISTINCT Table1.ID
FROM Table1
WHERE Table1.ID IS NOT NULL
GROUP BY Table1.ID
)
SELECT DISTINCT
Table2.b
FROM Table2 AS sp
INNER JOIN cte1 AS ts
ON sp.b <> ts.ID
ORDER BY ts.ID DESC
I have 2 table lets say table1 and table2 table 1 is the master data and table 2 is child. I have to find out records from both table corresponding to the given table1.id. But there are more than 1 record in table2 corresponding to table1.id. So I would like to filter the latest record first from table2 corresponding to table1.id then apply the join on filtered data from table2 and table1.
table1= location_grids
table2= local_ads
Following what I am doing ?
SELECT * FROM `location_grids`as l
LEFT JOIN `local_ads` la ON `la`.`location_grid_id` = `l`.`id`
Inner join (SELECT id, location_grid_id, max(booked_till) as maxbooked
from local_ads group by location_grid_id ) as b
on la. location_grid_id = b.location_grid_id
and la.booked_till = b.maxbooked
WHERE `l`.`location_id` = '1'
AND `l`.`flag` = 1
The query is returning me unfiltered data. How can I filter then apply join on data. Any help will be appreciated.
table1:- location-grid
table2:- local_ad
Finally got the solution :- I was searching for following query.
SELECT location_grid_id, user_id, booked_from, booked_till, purpose, category, duration, price, MAX( created )
FROM local_ads
GROUP BY location_grid_id
Here I wanted to run join with latest record of the particular id as many record must be exist in local_ads
I'm trying to sum certain rows from a hash table using two elements: a select group of IDs and a particular key.
Here's the setup:
Table 1:
ID KEY VALUE
1 name John Doe
1 amount 10
2 name Jane Doe
2 amount 15
3 name Mike Lowry
3 amount 5
Table 2:
ORDERID TYPE TRANSACTIONID
1001 Purchase 1
1002 Donation 2
1003 Purchase 3
I'm trying to get a sum of all the amounts where the type is "Purchase." Here's the query I'm using:
SELECT SUM(Table1.value) as balance
FROM Table1
LEFT JOIN (SELECT Table2.TRANSACTIONID as TID FROM Table2 WHERE Table2.TYPE = "Purchase" ) as ids
ON Table1.ID = ids.TID
WHERE Table1.key = "amount"
Tweaking that, I've managed to get 0 and the total of all the rows, but not just the one result. Ideas?
The problem is that your query makes an outer join between Table1 and Table2, such that all records of Table1 are preserved irrespective of whether a matching record is found from Table2. Learn about SQL joins.
You want to make an inner join instead:
SELECT SUM(VALUE)
FROM Table1 JOIN Table2 ON Table1.ID = Table2.TRANSACTIONID
WHERE Table1.KEY = 'amount' AND Table2.TYPE = 'Purchase'
See it on sqlfiddle.
At first I thought this may work as a join but I'm not sure if this is really a union command or if even possible. Below is an example of the two tables and each has about 20 more columns of various different data.
Table 1
> id assembly user1 user2 containerID productID packageID
1 line2 Billy John 3794 4892 4589
2 line4 John Doug 7794 6201 7864
Table 2
> item_id name width height weight flag1 flag2
3794 Box 10 10 10 0 1
4892 Lamp 4 6 2 1 1
7864 BigBox 200 200 300 4 5
What I am trying to do is show all of Table 1 but replace the containerID, productID, and packageID with their name from Table 2 using the matching item_id. Tried doing this outside of mysql with foreach but with Table 2 having 30k rows it lags up "just a bit" when trying to display the hundreds of rows from Table 1 and replacing each id with its name equivalent.
To see all the table_1 records, use:
SELECT t1.id, t1.assembly, t1.user1, t1.user2,
t2c.name, t2pr.name, t2pk.name
FROM TABLE_1 t1
LEFT JOIN TABLE_2 t2c ON t2c.item_id = t1.containerid
LEFT JOIN TABLE_2 t2pr ON t2pr.item_id = t1.productid
LEFT JOIN TABLE_2 t2pk ON t2pk.item_id = t1.packageid
You can change those to INNER JOINs, but any row that doesn't match all three will not be in the resultset.
My query uses table aliases, because you have to join to the appropriate TABLE_2 column for each name you want to look up.
try to use something like this:
SELECT id, assembly, user1, user2,
(SELECT name from table2 where item_id = table1.containerID),
(SELECT name from table2 where item_id = table1.productID),
(SELECT name from table2 where item_id = table1.packageID)
FROM table1
ORDER BY id;
You'll need to join each row three times for each item_id
SELECT t1.*, t21.name,t22.name,t23.name FROM table_1 t1
INNER JOIN table_2 t21 ON t1.containerID = t21.itemid
INNER JOIN table_2 t22 ON t1.productId = t22.itemid
INNER JOIN table_2 t23 ON t1.packageID = t23.itemid
Make sure there's an index on table_2's itemid