SELECT count(*) rcount
, DATE(create_date) crdate
, fk_account_id accid
, fk_caa_payment_pool_id poolId
FROM caa_payment_confirmation_hdr
WHERE fk_caa_payment_pool_id IN
(
SELECT pk_caa_payment_pool_id
FROM caa_payment_pool
WHERE is_deleted = 0
)
AND create_date >= '2017-08-01'
GROUP BY crdate
, poolId
I want result to display only max value, please help me out
ORDER the table based on count and then use LIMIT
SELECT * FROM
(SELECT count(*) rcount
, DATE(create_date) crdate
, fk_account_id accid
, fk_caa_payment_pool_id poolId
FROM caa_payment_confirmation_hdr
WHERE fk_caa_payment_pool_id IN
(
SELECT pk_caa_payment_pool_id
FROM caa_payment_pool
WHERE is_deleted = 0
)
AND create_date >= '2017-08-01'
GROUP BY crdate
, poolId
) A ORDER BY cnt DESC LIMIT 1
Related
I have written a complex query in MySQL to return a result.
it works perfectly
Until .....
the subquery returns no result
how to write an if or IF null then substitute in date '2020-06-03'
any help greatly appreciated
SELECT
*
FROM
trades
WHERE
stock_code = 'IHVV'
AND acc_id = '4'
AND tx_date >
(SELECT tx_date
FROM
( SELECT *, ( #sum_units := #sum_units + units ) AS sum_units
FROM
trades
JOIN ( SELECT #sum_units := 0 ) params
WHERE
stock_code = 'IHVV'
AND acc_id = '4'
AND tx_date <= '2021-06-30'
AND ( transfer_date IS NULL OR transfer_date <= '2021-06-30' )
ORDER BY
tx_date ASC,
units ASC
) AS query1
WHERE
tx_date < DATE_SUB( '2021-06-30', INTERVAL 1 YEAR )
AND sum_units = 0
ORDER BY
tx_date DESC
LIMIT 1
)
AND tx_date <= '2021-06-30'
AND ( transfer_date IS NULL OR transfer_date <= '2021-06-30' )
ORDER BY
tx_date ASC,
units ASC
clarification
I have written a main query and in one of the where clauses I am using a subquery and this subquery works well, until this subquery does not return a result and my main query stops working, so I would like to on no result in this subquery substitute a value on no result so the main query can function normally
example needed
select * from table where date > (ifnull(subquery, "2002-01-01"))
I get
incorrect parameter count in the call to native function "IFNULL'
AND THE ANSWER IS: ---> :)
thanks guys for all your suggestions
much appreciated
SELECT *
FROM trades
WHERE stock_code = 'IHVV'
AND acc_id = '4'
AND tx_date >
#last 0 date out of 1 year perhaps change the date range by from date and to date in the interval 1 year area
(SELECT COALESCE(
(SELECT tx_date
FROM (SELECT *, (#sum_units := #sum_units + units) AS sum_units
FROM trades
JOIN ( SELECT #sum_units := 0 ) params
WHERE stock_code = 'ANZ'
AND acc_id = '4'
AND tx_date <= '2022-06-30'
AND (transfer_date IS NULL OR transfer_date <= '2022-06-30' ))
ORDER BY tx_date ASC, units ASC) as query1
WHERE tx_date < DATE_SUB('2022-06-30',INTERVAL 1 YEAR)
AND sum_units = 0
ORDER BY tx_date DESC
LIMIT 1), 'TODATE')
AND tx_date <= '2022-06-30'
AND (transfer_date IS NULL OR transfer_date <= '2022-06-30' )
ORDER BY tx_date ASC, units ASC
I don't want to be an order by channel ID.
All I want is order by book_date for the entire channel.
SELECT * FROM
(
SELECT CAST(#rownum := #rownum +1 AS signed integer) AS row_cnt
, id /* index */
, CASE WHEN 0 < (SELECT count(*) FROM cmsDB.g_room_type WHERE name = t1.room_type AND chanel_mgt_id = t1.chanel_mgt_id) THEN '정상' ELSE '비매칭' END AS status
, DATE_FORMAT(book_date,'%Y-%m-%d') AS book_date
, room_type
, (SELECT info.room_name FROM cmsDB.g_room_info info, cmsDB.g_room_type tp WHERE info.id = tp.room_info_id AND tp.name = t1.room_type AND tp.chanel_mgt_id = t1.chanel_mgt_id GROUP BY info.room_name) AS room_name
, DATEDIFF(check_out_date,check_in_date) AS night_cnt
, check_in_date
, check_out_date
, room_num
, book_name
, phone_num
, book_num
, reg_book_num
, deposit_price
, book_status
, client_status
, sub_chanel_sort
, t2.companyNm AS company_name
, t1.remarks
, t1.night_date
, t1.reg_status
, chanel_mgt_id
, (SELECT sort.name FROM cmsDB.g_chanel_sort sort WHERE sort.id = t2.chanelSortId) AS chanelName
FROM
cmsDB.e_cl_book_themoum t1
LEFT JOIN (
SELECT
cm.id AS chanelMgtId
, ci.name AS companyNm, cm.chanel_sort_id AS chanelSortId
FROM cmsDB.g_chanel_mgt cm, cmsDB.g_company_info ci
WHERE cm.company_info_id = ci.id) t2
ON t2.chanelMgtId = t1.chanel_mgt_id
, (SELECT #rownum := 0) r
WHERE 1=1 AND t1.chanel_mgt_id in ( 49 , 50 , 56 , 69 , 70 , 71 )
ORDER BY t1.book_date asc
) tbl ORDER BY 1 desc LIMIT 0,10 ;
If you do this, the results will be...
https://i.stack.imgur.com/8B6Qi.png
This is the result.
But the result I want is the date order for the entire channel_id.
Instead of order by 1 based on column position in select clause (first column on select )
You should use an explicit order by using column name
ORDER BY book_date desc LIMIT 0,10 ;
or
ORDER BY chanel_mgt_id, book_date desc LIMIT 0,10 ;
if you want also the row_cnt then add the column name in the position you need
ORDER BY chanel_mgt_id, book_date, row_cnt desc LIMIT 0,10 ;
Here's my query:
SELECT
FROM_UNIXTIME( date_added, '%m-%d-%Y' ) AS formatted_date,
SUM( tb =1 ) AS sum_users,
SUM( tb =2 ) AS sum_links,
SUM( tb =3 ) AS sum_ads,
SUM( tb =4 ) AS sum_actions
FROM (
SELECT date_added, 1 AS tb
FROM users_list WHERE 1=1
UNION ALL
SELECT date_added, 2
FROM users_links WHERE 1=1
UNION ALL
SELECT date_served, 3
FROM ads_served WHERE 1=1
UNION ALL
SELECT date_served, 4
FROM actions WHERE 1=1
) AS t
GROUP BY formatted_date
ORDER BY formatted_date DESC
Here's my table data:
users_list
id date_added
1 1234567890
2 1334567890
3 1434567890
users_links
id date_added
1 1244567890
2 1354567890
3 1464567890
ads_served
id date_served revenue
1 1234567891 0.01
2 1334567892 0.02
3 1434567893 0.02
actions
id date_served
1 1234561890
2 1334562890
3 1434563890
I am trying to sum the revenue for formatted_date in the ads_served table as a 6th column for the output query. I am lost as to where to start. If I add the sum(revenue) to the union select I get a "column mismatch" error.
Column revenue belongs to ads_served but you are selecting from a sub query where revenue is not present. Add it to the subquery:
SELECT
FROM_UNIXTIME( date_added, '%m-%d-%Y' ) AS formatted_date,
SUM( tb =1 ) AS sum_users,
SUM( tb =2 ) AS sum_links,
SUM( tb =3 ) AS sum_ads,
SUM( tb =4 ) AS sum_actions,
SUM( revenue ) As sum_revenue
FROM (
SELECT date_added, 1 AS tb, 0 As revenue
FROM users_list WHERE 1=1
UNION ALL
SELECT date_added, 2, 0
FROM users_links WHERE 1=1
UNION ALL
SELECT date_served, 3, revenue
FROM ads_served WHERE 1=1
UNION ALL
SELECT date_served, 4, 0
FROM actions WHERE 1=1
) AS t
GROUP BY formatted_date
ORDER BY formatted_date DESC
Try in this way. Why do you use 1=1 ?
SELECT
FROM_UNIXTIME( date_added, '%m-%d-%Y' ) AS formatted_date,
SUM( tb =1 ) AS sum_users,
SUM( tb =2 ) AS sum_links,
SUM( tb =3 ) AS sum_ads,
SUM( tb =4 ) AS sum_actions,
sum(total) as tot_rev
FROM (
SELECT date_added,'' as total, 1 AS tb
FROM users_list WHERE 1=1
UNION ALL
SELECT date_added,'', 2
FROM users_links WHERE 1=1
UNION ALL
SELECT date_served,revenue, 3
FROM ads_served WHERE 1=1
UNION ALL
SELECT date_served,'', 4
FROM actions WHERE 1=1
) AS t
GROUP BY formatted_date
ORDER BY formatted_date DESC
I have the following SQL query. It returns NULL for the 'invoice_date'.
SELECT * , fk_uid AS userId, fk_ivid AS invoice_uid, (
SELECT company
FROM tbl_users
WHERE pk_uid = userId
) AS invoice_customer, (
SELECT DATE_FORMAT( '%d/%m/%y', purchased ) AS invoice_date
FROM _invoices
WHERE invoice_uid = invoice_uid
LIMIT 1
) AS invoice_date
FROM tbl_statement_items
WHERE statement_generated = '1'
AND fk_statementId = '1'
LIMIT 0 , 30
Any help would be appriciated.
Thanks
You have inverted date_format parameters order.
I have this small SQL query.
SELECT a.`id` , a.`title` , a.`date` ,
(
SELECT MAX( grade )
FROM tests
WHERE userid = 41
AND presid = a.`id`
) AS grade
FROM `presentations` a
WHERE a.`visible` = 1
AND `grade` >= 5
ORDER BY `grade` DESC
This gives me the error
1054 - Unknown column 'grade' in 'where clause'
But if i remove the 2nd last line, it works fine. I have tried to do AND a.grade and even give the tests table a name and append that name to grade but still no luck.
How can I use this inline query in a WHERE clause?
I have found that this works, but is it the only way?
SELECT a.`id` , a.`title` , a.`date` ,
(
SELECT MAX( grade )
FROM tests
WHERE userid = 41
AND presid = a.`id`
) AS grade
FROM `presentations` a
WHERE a.`visible` = 1
AND (
SELECT MAX( grade )
FROM tests
WHERE userid = 41
AND presid = a.`id`
) >= 5
ORDER BY `grade` DESC
Sql statements are somewhat evaluated in the following order:
FROM
WHERE
SELECT
GROUP
HAVING
ORDER
So things you define in the SELECT-clause are not available in the WHERE-clause. You would need to put that constraint into a HAVING-clause:
SELECT a.`id` , a.`title` , a.`date` ,
(
SELECT MAX( grade )
FROM tests
WHERE userid = 41
AND presid = a.`id`
) AS grade
FROM `presentations` a
WHERE a.`visible` = 1
HAVING `grade` >= 5
ORDER BY `grade` DESC
SELECT a.`id` , a.`title` , a.`date` ,
(
SELECT MAX( grade )
FROM tests
WHERE userid = 41
AND presid = a.`id`
) AS grade
FROM `presentations` a
WHERE a.`visible` = 1
HAVING `grade` >= 5
ORDER BY
`grade` DESC