How get records by last date with specific query - php

I have table currencies and table this courses data . How i can get one course data for each currency by last date
This is my code where i get courses for each currency by date:
SELECT
`coll_currency`.`collectionAlias`,
`coll_currency`.`currency_abbr`,
`coll_currency`.`currency_image`,
`coll_currency`.`currency_name`,
`coll_currency`.`currency_amount`,
`coll_currency`.`id`,
`pars_table`.`course` AS `currency_course`,
`pars_table`.`publishdate` AS `currency_publishdate`,
`pars_table`.`currency_id`
FROM
`coll_currency`
INNER JOIN `pars_currency_nbu` AS `pars_table`
ON coll_currency.id = pars_table.currency_id
WHERE
(
(`currency_type` = '3670924')
AND (`currency_state` = '3583429')
AND (
DATE_FORMAT(
`pars_table`.`publishdate`,
'%Y-%m-%d'
) = '2017-08-09'
)
)
ORDER BY
`currency_publishdate` DESC

Try something like this:
SELECT `c`.`collectionAlias`,
`c`.`currency_abbr`,
`c`.`currency_image`,
`c`.`currency_name`,
`c`.`currency_amount`,
`c`.`id`,
`p`.`course` AS `currency_course`,
`p`.`publishdate` AS `currency_publishdate`,
`p`.`currency_id` -- this has the same value as `c`.`id`
FROM `coll_currency` AS `c`
JOIN `pars_currency_nbu` AS `p` ON `c`.`id` = `p`.`currency_id`
JOIN ( SELECT `cc`.`id`, MAX(`pp`.`publishdate`) AS `maxdate`
FROM `coll_currency` AS `cc`
JOIN `pars_currency_nbu` AS `pp` ON `cc`.`id` = `pp`.`currency_id`
GROUP BY `cc`.`id`
) AS `q` ON `q`.id` = `c`.`id` AND `q`.`maxdate` = `p`.`publishdate`

Related

Duplicate MethodID in query

I have a query that is using a subquery, and I can't seem to figure out why it is telling me I have duplicate methodID's. The query is supposed to take the data and order by and group by for showing only the latest single result for a given studentID where there could be multiple results with different timestamps but same studentID
SELECT a.*
FROM
( SELECT *
, o.methodName oldName
, n.methodName newName
, s.firstName fName
, s.lastName lName
FROM changeReport r
LEFT
JOIN methodLookup o
ON o.methodID = r.oldMethod
LEFT
JOIN methodLookup n
ON n.methodID = r.newMethod
JOIN students s
ON s.studentID = r.studentID
LEFT
JOIN staffaccounts a
ON r.staffID = a.staffID
WHERE 31 IN (newSubMethod,oldSubMethod)
AND date(timestamp) = CURRENT_DATE
) a
JOIN
( SELECT students.studentID
, MAX(timestamp) timestamp
FROM changeReport r
LEFT
JOIN methodLookup o
ON o.methodID = r.oldMethod
LEFT
JOIN methodLookup n
ON n.methodID = r.newMethod
JOIN students s
ON s.studentID = r.studentID
LEFT
JOIN staffaccounts a
ON r.staffID = a.staffID
WHERE 31 IN (newSubMethod,oldSubMethod)
AND date(timestamp) = CURRENT_DATE
) b
ON b.studentID = a.studentID
AND b.timestamp = a.timestamp;
Any ideas on how this could be?

Select INNER JOIN mysql DataBase

I want to select database with condition but not working.
I have this query :
SELECT c.*
,s.*
,f.*
,count(c.id) as instock_count
,totals.orders_total
,GROUP_CONCAT(c.sku SEPARATOR '<br/>') AS sku
FROM produse_com c
INNER JOIN (SELECT * , SUM(stoc) as stoc_sum
FROM stocuri_mentor
GROUP BY sku ) s
ON c.sku = s.sku
INNER JOIN (SELECT count(id) as orders_total, id_comanda
FROM produse_com
WHERE NOT ridicat = 'Da'
GROUP BY id_comanda ) totals
ON totals.id_comanda = c.id_comanda
INNER JOIN (SELECT data_add, status, id_comanda
FROM comenzi
WHERE NOT (status = 'Impachetat' OR status = 'Stornat' OR status = 'Anulat')
) f
ON f.id_comanda = c.id_comanda
WHERE stoc_sum >= c.qty
GROUP BY c.id_comanda
HAVING instock_count = orders_total
The issue is I have a condition WHERE NOT ridicat = 'Da'
But the result contain all result with ridicat = 'Da'
EDIT :
If I remove this query INNER JOIN (SELECT * ,SUM(stoc) as stoc_sum FROM stocuri_mentor GROUP BY sku ) s ON c.nume_produs = s.sku AND WHERE stoc_sum >= c.qty the result is OK
EDIT 2 : SOLUTION = add 1 more condition WHERE stoc_sum >= c.qty AND ridicat <> 'Da'
You can try this query.
SELECT count(id) as orders_total, id_comanda, ridicat
FROM produse_com
WHERE ridicat != 'Da'
GROUP BY id_comanda;

Arithmetic operators with SELECT, LEFT JOIN and WHERE clause

I need help. I have the code below which works fine but its not achieving my objective. I want a column "account_title" with each of its calculated values by the side before the subtraction which should eventually give me the difference of the subtracted value. Thanks in anticipation.
SELECT
(
SELECT SUM(amount)
FROM mainaccount_a_2017
LEFT JOIN chart_of_account ON (chart_of_account.joint_account_numbers = mainaccount_a_2017.joint_account_number)
LEFT JOIN asset_liability_income_expenditure_tbl ON (asset_liability_income_expenditure_tbl.a_l_code = chart_of_account.account_type)
WHERE a_l_code = 'FA' AND dr_cr_action = 'DR'
)
-
(
SELECT SUM(amount)
FROM mainaccount_b_2017
LEFT JOIN chart_of_account ON (chart_of_account.joint_account_numbers = mainaccount_b_2017.joint_account_number)
LEFT JOIN asset_liability_income_expenditure_tbl ON (asset_liability_income_expenditure_tbl.a_l_code = chart_of_account.account_type)
WHERE a_l_code = 'FA' AND dr_cr_action = 'CR'
)
Change the subqueries to be grouped by account_title, and then join them.
SELECT debits.account_title, debits.total - credits.total AS net_reduction
FROM (
SELECT mainaccount_a_2017.account_title, SUM(amount) AS total
FROM mainaccount_a_2017
LEFT JOIN chart_of_account ON (chart_of_account.joint_account_numbers = mainaccount_a_2017.joint_account_number)
LEFT JOIN asset_liability_income_expenditure_tbl ON (asset_liability_income_expenditure_tbl.a_l_code = chart_of_account.account_type)
WHERE a_l_code = 'FA' AND dr_cr_action = 'DR'
GROUP BY mainaccount_a_2017.account_title
) AS debits
JOIN (
SELECT mainaccount_b_2017.account_title, SUM(amount) AS total
FROM mainaccount_b_2017
LEFT JOIN chart_of_account ON (chart_of_account.joint_account_numbers = mainaccount_b_2017.joint_account_number)
LEFT JOIN asset_liability_income_expenditure_tbl ON (asset_liability_income_expenditure_tbl.a_l_code = chart_of_account.account_type)
WHERE a_l_code = 'FA' AND dr_cr_action = 'CR'
GROUP BY mainaccount_b_2017.account_title
) AS credits ON debits.account_title = credits.account_title
DEMO

multiple subquery need to change as join query

Hi all I need a solution for this question here is my query look like it works.But takes too much time because of sub queries.Give an alternate query to this query
SELECT *
FROM `room_types`
WHERE id
IN (SELECT capacity
FROM rooms
WHERE id
IN (
SELECT DISTINCT room_id
FROM `reservations`
WHERE DATE(
START ) >= '2016-01-10'
AND DATE(
END ) <= '2016-01-15'
AND STATUS = 'CheckedOut'
AND id
IN (
SELECT op_no
FROM `bills`
WHERE billed = 'Yes'
)
)
)
As of your provided query, Try this:
SELECT
`room_types`.*
FROM
`room_types`
INNER JOIN `rooms` ON (`room_types`.`id` = `rooms`.`id`)
INNER JOIN `reservations` ON (`rooms`.`id` = `reservations`.`room_id`)
INNER JOIN `bills` ON (`reservations`.`id` = `bills`.`op_no`)
WHERE
DATE(`reservations`.`START`) BETWEEN '2016-01-10' AND '2016-01-15'
AND `reservations`.`STATUS` = 'CheckedOut'
AND `bills`.`billed` = 'Yes'
Also you can index columns which will make it more faster.
So.. First try without Database Schema !
SELECT *
FROM room_types rt
JOIN rooms r ON r.capacity = rt.id
JOIN reservations resa ON r.id = resa.room_id
AND DATE(resa.start ) >= '2016-01-10'
AND DATE(resa.end ) <= '2016-01-15'
AND resa.status LIKE "CheckedOut"
JOIN bills b ON resa.id = b.resa_id AND b.billed LIKE "Yes"
Notice that "rt", "r", "resa" and "b" are aliases for your tables

How Can i Convert this multiple sub query in zend query?

i have the following query which gives desired output in mysql , now i want to implement it in zend query language,
which has different approach to implement the query..
SELECT A.NAME , B.PAYMENT , C.TOTALPROJ , D.TOTALTASK , T.ACTIVETASK , H.HOUR
FROM USERMASTER AS A
LEFT OUTER JOIN
(
SELECT A.U_ID , SUM(A.TOTALTIME * B.RATE) AS PAYMENT
FROM
(
SELECT U_ID , PROJECT_ID ,
SUM(TIME_TO_SEC(CASE WHEN endtime is null then timediff (starttime,starttime)
ELSE timediff (endtime,starttime) END )) / 3600 AS TOTALTIME
FROM LOGMASTER AS A
WHERE PROJECT_ID IS NOT NULL
GROUP BY U_ID , PROJECT_ID
) AS A
INNER JOIN PROJECTTOUSER AS B ON A.PROJECT_ID = B.PROJECT_ID AND A.U_ID = B.U_ID
GROUP BY B.U_ID
) AS B ON A.ID = B.U_ID
LEFT OUTER JOIN
(
SELECT U_ID , COUNT(*) AS TOTALPROJ FROM PROJECTTOUSER GROUP BY U_ID
) AS C ON A.ID = C.U_ID
LEFT OUTER JOIN
(
SELECT ASSIGNED_TO , COUNT(*) AS TOTALTASK FROM TASKTOTARGET GROUP BY ASSIGNED_TO
) AS D ON A.ID = D.ASSIGNED_TO
LEFT OUTER JOIN
(
SELECT ASSIGNED_TO,COUNT(*) AS ACTIVETASK FROM TASKTOTARGET WHERE
IS_ACTIVE = 0 GROUP BY ASSIGNED_TO
) AS T ON A.ID = T.ASSIGNED_TO
LEFT OUTER JOIN
(
SELECT U_ID, SEC_TO_TIME(SUM(TIME_TO_SEC(CASE WHEN endtime is null then
timediff (starttime,starttime) ELSE timediff (endtime,starttime) END ))) AS HOUR
FROM LOGMASTER WHERE INSERT_DATE >= '2013-08-20' AND INSERT_DATE <='2013-08-31'
GROUP BY U_ID
) AS H ON A.ID = H.U_ID
so if any one can guide me in how to create this query in zend then it will be very helpful, and appreciated
Each of your subqueries becomes an new Zend_Query that you can then use just like a table and pass in to the main query.
For example:
$h = new Zend_Db_Select()
->from('LOGMASTER', array('U_ID', 'HOUR' => new Zend_Db_Expr('SEC_TO_TIME(SUM(TIME_TO_SEC(CASE WHEN endtime is null then
timediff (starttime,starttime) ELSE timediff (endtime,starttime) END ))))')
->where("INSERT_DATE >= '2013-08-20'")
->where("INSERT_DATE <= '2013-08-31'")
->group('U_ID');
$mainQuery = new Zend_Db_Select()
->from('a' => 'USERMASTER', array('NAME'))
->joinLeft($h, 'A.ID = H.U_IS', array('HOUR'));
You would create each of your subqueries as its own object and then you can join them into your main query. The last argument of the join function is which columns from the subquery should be added to the main query.
With ZF's fluid interface you can keep joining tables/queries until you have built your entire query.
http://framework.zend.com/manual/1.12/en/zend.db.select.html

Categories