im query mysql data for mismatch 2 different table data, i already trying getting data on picture bellow. now i need to compare sell_qty and sold_qty allies.
please check out this query
SELECT
purchase_lines.`product_id`,
(SELECT SUM(`quantity`) FROM `purchase_lines`
WHERE transaction_sell_lines.product_id = purchase_lines.product_id GROUP BY product_id LIMIT 1 ) as purchase_qty,
(SELECT SUM(`quantity`) FROM `transaction_sell_lines`
WHERE transaction_sell_lines.product_id = purchase_lines.product_id GROUP BY product_id LIMIT 1 ) as sell_qty,
(SELECT SUM(`quantity_sold`) FROM `purchase_lines`
WHERE transaction_sell_lines.product_id = purchase_lines.product_id GROUP BY product_id LIMIT 1 ) as sold_qty,
variation_location_details.qty_available as avaiable_qty
FROM
`purchase_lines`,
variation_location_details,
transaction_sell_lines
WHERE
purchase_lines.product_id = variation_location_details.product_id
AND
variation_location_details.product_id = transaction_sell_lines.product_id
GROUP BY
variation_location_details.product_id
I need to different between on where clause
sell_qty != sold quantity
Related
In a MYSQL table with those 5 fields: id, user_id, date, type, uid where type can be 1 or 2, I'm looking for a single query where I can fetch 2 results, one for type=1 and another one for type=2 based on date field.
Right now i have the following query which only gives me the last uid without taking care of the type field.
SELECT t.uid
FROM table AS t
WHERE t.user_id = 666
ORDER BY t.date
DESC LIMIT 1
Does anyone know how should modify this query so i can get the last uid for type=1 and the last one for type=2 based on date field? I would like to keep a a single query
Union all is probably the simplest method:
(select t.*
from t
where t.user_id = 666 and t.type = 1
order by date desc
limit 1
) union all
(select t.*
from t
where t.user_id = 666 and t.type = 2
order by date desc
limit 1
)
Finally i updated the query following this "paradigm":
http://dev.mysql.com/doc/refman/5.7/en/example-maximum-column-group-row.html
http://jan.kneschke.de/projects/mysql/groupwise-max/
This is how the query ended up:
SELECT s1.type, s1.uid
FROM t AS s1
LEFT JOIN t AS s2 ON s1.type = s2.type AND s1.date < s2.date
WHERE s2.date IS NULL;
Here's a visual example: http://hastebin.com/ibinidasuw.vhdl
Credits are for snoyes from #sql on Freenode. :)
Is there any other way to write this query ?
I tried doing it in a subquery but it doesn't work because of the multiple columns. It seems like this query only works by itself. Please correct me
Records
PK recordId
dateViewed
CarViewed
I tried this
SELECT R.`dateViewed` FROM Records ,(
SELECT R.CarViewed, COUNT(R.CarViewed) as cnt FROM Records R
GROUP BY R.CarViewed
ORDER BY cnt DESC
LIMIT 1 ) AS favouriteCarOfTheDay
GROUP BY R.`dateViewed
Then I tried this
SELECT R.`dateViewed` ,COUNT(R.CarViewed) as cnt FROM Records ,
(
SELECT R.CarViewed FROM Records R
GROUP BY R.CarViewed
ORDER BY cnt DESC
LIMIT 1 ) AS favouriteCarOfTheDay
GROUP BY R.`dateViewed
Along many other queries I tried, I have no idea how to get it working.
In a nutshell for a specific date, I would like to get the most common cars that were viewed.
Like :
dateViewed favouriteCarOfTheDay
2012-09-22 | Nissan
2012-09-23 | BMW
try this
SELECT R.`dateViewed` ,COUNT(R.CarViewed) as cnt ,R.CarViewed FROM Records R
GROUP BY R.`dateViewed
ORDER BY COUNT(R.CarViewed) DESC
I think the following should work (disclaimer, not all my own work, adapted from an answer at another question)
SELECT DISTINCT
R.dateViewed,
R.CarViewed
FROM Records R
WHERE
R.dateViewed =
(SELECT R2.dateViewed FROM
(
SELECT R1.dateViewed, COUNT(*) AS numViewed
FROM Records R1
WHERE R1.CarViewed = R.CarViewed
GROUP BY R1.dateViewed
ORDER BY R1.numViewed DESC
) AS R2
LIMIT 1
)
ORDER BY r.dateViewed
Such things are really awful to do in MySQL so it might actually by slower than two correlated subquery but at least it returns both the car and it's viewcount:
SELECT counts.`dateViewed`,counts.`CarViewed` as favourite_car, counts.cnt
FROM
(SELECT R.`dateViewed` ,R.`CarViewed`, COUNT(*) as cnt
FROM Records
GROUP BY R.`dateViewed` ,R.`CarViewed`
) as counts JOIN
(SELECT R.`dateViewed`, MAX(cnt) as cnt
FROM
(SELECT R.`dateViewed` ,R.`CarViewed`, COUNT(*) as cnt
FROM Records
GROUP BY R.`dateViewed` ,R.`CarViewed`
) as q
GROUP BY R.`dateViewed`) as maxes
ON counts.cnt=maxes.cnt
I am trying to get multiple columns and show duplicate ItemCodes form a table. Ie. if an ItemCode is repeated more than once.
Using the query:
SELECT ItemCode
,DuplicateCount = COUNT(*)
FROM IQR1
WHERE WhsCode = 01
GROUP BY
ItemCode
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
I get 38 records with duplicate counts of 2 each.
When I try to get another column, ItemName, I get only 37 records returned:
SELECT ItemCode
,ItemName
,DuplicateCount = COUNT(*)
FROM IQR1
WHERE WhsCode = 01
GROUP BY
ItemCode
,ItemName
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
How do I correctly modify the first query to get multiple columns and still return 38 records?
Inner join your first query (by putting it in a derived table) back to the main table, joining on the ItemCode.
SELECT
I.*,
D.DuplicateCount
FROM
IQR1 I
INNER JOIN (
SELECT
ItemCode,
DuplicateCount = COUNT(*)
FROM IQR1
WHERE WhsCode = 01
GROUP BY ItemCode
HAVING COUNT(*) > 1
) D ON I.ItemCode = D.ItemCode
ORDER BY DuplicateCount DESC
Im having trouble with a sql join involving a union. I'm trying to pull a COUNT and a field from 2 tables but getting an error.
The query:
$sql_result7 = mysql_query("(SELECT COUNT (*) as alertcount, date as alertdate FROM alerts WHERE to_id='$id' AND date > '$lastcheck') UNION (SELECT COUNT (*) as mailcount, date maildate FROM mobmail WHERE to_id='$id' AND to_del=0 AND seen = '0')", $db);
$rs7 = mysql_fetch_array($sql_result7);
$alerts = $rs7[alertcount];
$mails = $rs7[mailcount];
$last_alert = $rs7[alertdate];
$last_mail = $rs7[maildate];
Is it something to do with the date as alertdate part?
The error im getting is:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
Besides the space between COUNT and (*), there is another issue. You can't use $rs7[mailcount] nor $rs7[maildate] in your PHP code because your query is equivalent to:
SELECT
COUNT(*) as alertcount
, date as alertdate
FROM alerts
WHERE to_id = '$id'
AND date > '$lastcheck'
UNION
SELECT
COUNT(*) --- No "as mailcount" here
, date --- No "as maildate" either
FROM mobmail
WHERE to_id = '$id'
AND to_del = 0
AND seen = '0'
and will return two rows and only 2 columns:
alertcount | alertdate
-----------|------------
24 | 2012-01-04
73 | 2011-11-11
Two ways to solve this problem:
Either keep the query (changing the UNION to UNION ALL to ensure that you always get 2 rows) and chnage the PHP to use the 2 rows.
Or change the query to:
SELECT alertcount, alertdate, mailcount, maildate
FROM
( SELECT
COUNT(*) AS alertcount
, date AS alertdate
FROM alerts
WHERE to_id = '$id'
AND date > '$lastcheck'
) AS a
CROSS JOIN
( SELECT
COUNT(*) AS mailcount
, date AS maildate
FROM mobmail
WHERE to_id = '$id'
AND to_del = 0
AND seen = '0'
) AS b
I have a table:
id | score | date
bob | 40 | 2010-1-1
bob | 70 | 2010-1-15
sue | 55 | 2010-1-1
sue | 80 | 2010-2-1
I want to query for either the score for a user on a specific date OR, if no score exists for that user on that date,return the score from the most recent date for that user.
Is there a way to do this without a sub-query?
For instance, if I do:
SELECT score
FROM table
WHERE id = '$id'
AND IFNULL(
date = DATE(FROM_UNIXTIME($date)),
MAX(date)
)
I would get no result, as the id does not show up for the most recent date.
Update
Felix reminded me I can't use aggregate functions in the WHERE clause, so now I'm wondering if there is a pseudo-aggregate date function for saying "most recent date" in the where clause, and if so, if I can specify the user when using THAT function?
Update 2
So this is what I have gotten to work, but I still don't know if it's the best way to go (ie, do I need the nested query?):
SELECT score
FROM table
WHERE id = '$id'
AND date = IFNULL(
(SELECT date FROM table
WHERE id = '$id' AND
date = DATE(FROM_UNIXTIME($date))
),
(SELECT MAX(date) FROM table
WHERE id = '$id'
)
)
Not sure of the performance of this one:
SELECT a.*
FROM table a
LEFT JOIN table b ON (
b.date=DATE(FROM_UNIXTIME($date)) AND b.date=a.date AND b.id=a.id
)
WHERE a.id='$id'
ORDER BY b.date DESC, a.date DESC
LIMIT 1
SELECT score
FROM table
WHERE id = '$id'
AND date > '2010-01-01'
ORDER BY date DESC
LIMIT 1
EDIT
See my second comment regarding the interpretations:
Interpretation 1:
SELECT score
FROM table
WHERE id = '$id'
AND date >= '2010-01-01'
ORDER BY date ASC
LIMIT 1
Interpretation 2:
SELECT score,
LEAST(ABS(DATEDIFF('2010-01-01', date)), 1) AS myorder
FROM table
WHERE id = '$id'
AND date >= '2010-01-01'
ORDER BY myorder ASC, date DESC
LIMIT 1
Use this query :
SELECT * FROM test
WHERE date = (
SELECT MAX(date) FROM test
) ;
If you want based on the id , you need to do group by operation on id column