SELECT title AS quiz_namenik, fullname AS profile_name, nik, store_code,
siebel_name AS store_name, MONTHNAME(start_date) AS `month`, score,
start_date, end_date,profil.created_at AS account_created_date,
profil.updated_at AS account_last_update
FROM
(
SELECT users.id, fullname, nik, siebel_name,store_code,
users.created_at, users.updated_at
FROM users
INNER JOIN stores ON users.store_id = stores.id
) AS profil,
(
SELECT kepo_quizs.id, score.user_id, title, score, start_date, end_date
FROM kepo_quizs
LEFT JOIN kepo_user_score AS score ON score.quiz_id = kepo_quizs.id
WHERE `activity_category_id` = '2'
) AS quiz
WHERE quiz.user_id = profil.id
AND (`start_date` >= '2019-01-01' AND `end_date` <= '2019-12-31')
ORDER BY title ASC, fullname ASC
That's my query that i want to change to laravel joins, can help me please
You can use the below code
DB::SELECT("SELECT title AS quiz_namenik, fullname AS profile_name, nik, store_code,
siebel_name AS store_name, MONTHNAME(start_date) AS `month`, score,
start_date, end_date,profil.created_at AS account_created_date,
profil.updated_at AS account_last_update
FROM
(
SELECT users.id, fullname, nik, siebel_name,store_code,
users.created_at, users.updated_at
FROM users
INNER JOIN stores ON users.store_id = stores.id
) AS profil,
(
SELECT kepo_quizs.id, score.user_id, title, score, start_date, end_date
FROM kepo_quizs
LEFT JOIN kepo_user_score AS score ON score.quiz_id = kepo_quizs.id
WHERE `activity_category_id` = '2'
) AS quiz
WHERE quiz.user_id = profil.id
AND (`start_date` >= '2019-01-01' AND `end_date` <= '2019-12-31')
ORDER BY title ASC, fullname ASC");
Related
SELECT
clockin.working_day as cdate,
clockin.,
sch_week.,
user.schedule_id,
user.username,
(
SELECT
GROUP_CONCAT(time separator '==')
from
clockin
WHERE
employee_no = user.employee_no
AND working_day = cdate
GROUP by
working_day
) as time
FROM
user_personal as user
JOIN clockin
inner join schedule_week as sch_week on user.schedule_id = sch_week.schedule_id
and week_day = date_format(working_day, '%a')
where
month(working_day) = month(
current_date()
)
GROUP by
user.employee_no,
clockin.working_day
ORDER by
clockin.working_day desc,
user.employee_no;
with posts as
(
select dpd.user_id as page_id, dpd.tweet_id as post_id, dpd.retweets as shares
from monthly_post_details dpd
where dpd.user_id in ('TheDeshBhakt', 'abhinavxarora', 'dhruv_rathee', 'shamsharmashow', 'TheWaliRahmani', 'AKTKbasics', 'mohakmangal', 'AnOpenLetter001', 'thekumarshyam', 'upword_', 'TheLallantop', 'thequint', 'QuintHindi', 'SatyaHindi', 'newslaundry', 'BrutIndia', 'nitishrajpute', 'ScoopWhoop', 'UFbySamdishh', 'Thepoliticspoi1', 'ndtv', 'aajtak', 'ZeeNews', 'ABPNews', 'kunalkamra88', 'PanickarS', 'SureshChavhanke', 'DaaruBaazMehta', 'ChargingHindi', 'ElvishYadav', 'StringReveals', 'OpIndiaHindi', 'MediaHarshVT', 'nshuklaindia', 'AtriNeeraj', 'atsshow7', 'SatyaSanatan01', 'Thakur312Manish', 'abhiandniyu', 'NationalDastak', 'thenews_intl', 'capitaltvindia', 'ajitanjum', 'dblive15', 'thelivetvnews', 'ppbajpai', 'abhisar_sharma', 'bstvlive', '_pyaraHindustan', 'JaipurDialogues', 'knockingNews', 'SushantBSinha', 'Article19_India', 'ultachasmauc', 'DOpolitics_in', 'sakshijoshii', 'VaadClips', 'TheSatyaShow', 'TV9Bharatvarsh', 'Republic_Bharat', 'indiatvnews', 'ndtvindia', 'news24tvchannel', 'News18India', 'NewsNationTV', 'ZeeHindustan_', 'IndiaNews_itv', 'goodnewstoday', 'DDNewslive', 'WIONews', 'cnnnews18', 'IndiaToday', 'TimesNow', 'theprintindia', 'republic', 'thewire_in', 'MirrorNow', 'NewsX', 'themojostory')
and DATE(dpd.created_time) between '2022-10-01' and '2022-10-31'
),
top_ten as
(
select page_id, shares from posts order by shares desc limit 30
),
top_posts as
(
select page_id, count(*) as num_posts_in_top from top_ten group by page_id
),
median_posts as
(
select page_id, avg(shares) as median_shares
from
(
select page_id, shares,
(select count(*) from posts t2 where t2.page_id = t3.page_id) as ct,
seq,
(select count(*) from posts t2 where t2.page_id < t3.page_id) as delta
from (select page_id, shares, #rownum := #rownum + 1 as seq
from (select * from posts order by page_id, shares) t1
order by page_id, seq
) t3 cross join (select #rownum := 0) x
having (ct%2 = 0 and seq-delta between floor((ct+1)/2) and floor((ct+1)/2) +1)
or (ct%2 <> 0 and seq-delta = (ct+1)/2)
) T
group by page_id
),
metrics as
(
select p.page_id, count(*) as total_posts,
IFNULL(max(p.shares),0) as max_shares,
IFNULL(sum(p.shares), 0) as total_shares,
IFNULL(tp.num_posts_in_top, 0) as num_posts_in_top
from posts p left join top_posts tp
on p.page_id = tp.page_id
group by p.page_id order by total_shares desc
),
top_posts_score as
(
# remove in future: calculation to be done using pd.rank()
select page_id, sum(ran) as num_posts_in_top_score
from (
select page_id,
rank() over(
order by shares) as ran
from top_ten
) tpt
group by page_id
)
select metrics.*, median_posts.median_shares,
IFNULL(tps.num_posts_in_top_score,0) as num_posts_in_top_score
from metrics left join median_posts on metrics.page_id=median_posts.page_id
left join top_posts_score tps on metrics.page_id = tps.page_id;
Query is working when Date range is for 1 day or 1 week not working for 1 month
I know, this question has been asked very often but I know my error, I know how I could fix it, but I canĀ“t find the point where the error is. In my opinion, all the subqueries have different and unique names, I even gave the columns different names then the subqueries. Any help would be appreciated. Where is the point I am missing an alias?
Whenever I am trying to run this query I get the response "Every derived table must have its alias", which is an understandable error message, but I can't figure out where my error is located.
SELECT
mso.entity_id,
GROUP_CONCAT(msh.comment) AS comment,
msoa.lastname,
base_grand_total,
mso.created_at,
mso.status,
marketplace_order_id AS amazon_order_id,
clvData.recurrenceRate,
clvData.avgRepRate
FROM
mag_sales_flat_order AS mso
LEFT JOIN mag_sales_flat_order_status_history AS msh ON mso.entity_id = msh.parent_id
LEFT JOIN mag_sales_flat_order_address AS msoa ON mso.entity_id = msoa.parent_id
left join (
select
cast(((cet.cec - cnt.cnc) / cst.csc) AS decimal(6, 2)) as recurrenceRate,
avg(repRate.countedOrders) AS avgRepRate
from(
Select
*,
(
select
count(customer_email) AS csc
from
mag_sales_flat_order
where
created_at between '2017-01-01'
and '2017-12-31'
) AS cst,
(
select
count(customer_email) AS cec
from
mag_sales_flat_order
where
created_at between '2017-01-01'
and '2020-12-31'
) AS cet,
(
select
count(mso_new.customer_email) AS cnc
from
(
select
*
from
mag_sales_flat_order
where
created_at between '2018-01-01'
and current_date()
) AS mso_new
left join (
select
*
from
mag_sales_flat_order
where
created_at between '2017-01-01'
and '2017-12-31'
) AS mso_old on mso_new.customer_email = mso_old.customer_email
)) AS cnt
join (
select
customer_email,
count(grand_total) as countedOrders,
sum(grand_total) as summedOrders
from
mag_sales_flat_order
group by
customer_email
) AS repRate on cl.customer_email = repRate.customer_email
) AS clvData on mso.customer_email = clvData.customer_email
WHERE
store_id IN({$store['id']})
AND (
mso.status = 'complete'
OR mso.status = 'closed'
OR mso.status = 'processing'
OR mso.status = 'exported'
OR mso.status LIKE 'pending%'
)
AND (
DATE_FORMAT(mso.created_at, '%Y-%m-%d') >= '$begin_date'
)
AND (
DATE_FORMAT(mso.created_at, '%Y-%m-%d') <= '$end_date'
)
GROUP BY
entity_id;
SELECT
mso.entity_id,
GROUP_CONCAT(msh.comment) AS comment,
msoa.lastname,
base_grand_total,
mso.created_at,
mso.status,
marketplace_order_id AS amazon_order_id,
clvData.recurrenceRate,
clvData.avgRepRate
FROM mag_sales_flat_order AS mso
LEFT JOIN mag_sales_flat_order_status_history AS msh ON mso.entity_id = msh.parent_id
LEFT JOIN mag_sales_flat_order_address AS msoa ON mso.entity_id = msoa.parent_id
LEFT JOIN
(
SELECT cast(((cet.cec - cnt.cnc) / cst.csc) AS decimal(6, 2)) as recurrenceRate, avg(repRate.countedOrders) AS avgRepRate
FROM
(
SELECT *,
(
SELECT count(customer_email) AS csc
FROM mag_sales_flat_order
WHERE created_at BETWEEN '2017-01-01' AND '2017-12-31'
) AS cst,
(
SELECT count(customer_email) AS cec
FROM mag_sales_flat_order
WHERE created_at BETWEEN '2017-01-01' AND '2020-12-31'
) AS cet,
(
SELECT count(mso_new.customer_email) AS cnc
FROM
(
SELECT *
FROM mag_sales_flat_order
WHERE created_at BETWEEN '2018-01-01' AND getdate()
) AS mso_new
LEFT JOIN
(
SELECT *
FROM mag_sales_flat_order
WHERE created_at BETWEEN '2017-01-01' AND '2017-12-31'
) AS mso_old on mso_new.customer_email = mso_old.customer_email
) AS cnt
) as cl
JOIN
(
SELECT customer_email, count(grand_total) as countedOrders, sum(grand_total) as summedOrders
FROM mag_sales_flat_order
GROUP BY customer_email
) AS repRate on cl.customer_email = repRate.customer_email
) AS clvData on mso.customer_email = clvData.customer_email
WHERE store_id IN({ $ store ['id'] })
AND
(
mso.status = 'complete'
OR mso.status = 'closed'
OR mso.status = 'processing'
OR mso.status = 'exported'
OR mso.status LIKE 'pending%'
)
AND
(
DATE_FORMAT(mso.created_at, '%Y-%m-%d') >= '$begin_date'
)
AND
(
DATE_FORMAT(mso.created_at, '%Y-%m-%d') <= '$end_date'
)
GROUP BY entity_id;
I'd like to be able to output the following fields from the query below:
AddedById,AddedByName,HoursWorked,CurrentYearlyFlexiAvailable
However where I have WHERE addedby=1, I'd like to replace this with the field name AddedById as I do not want to hard code this value as the overall query should and will return more than one person, I want to get this value from the rpt_timesheet_data view, which is there. The CurrentYearlyFlexiAvailable field should be telling me how much time they have left for the current year to date by doing the calculation between the SELECT SUM(ttl) and from the FROM (SELECT SUM(worked)-420 as ttl
SELECT AddedById,AddedByName,SUM(HoursWorked) AS HoursWorked
,(SELECT SUM(ttl) - (
SELECT SUM(worked)
FROM vwtimesheet
WHERE addedby=AddedById
AND entrydate BETWEEN '2017-01-01' AND '2017-04-13'
AND activityid=3192
GROUP BY addedby ) AS flexihours
FROM (
SELECT SUM(worked)-420 AS ttl
FROM vwtimesheet
WHERE addedby=1 <!--HERE IS THE ISSUE
AND entrydate BETWEEN '2017-01-01' AND '2017-04-13'
AND projectid<>113 AND activityid<>3192
GROUP BY entrydate
HAVING SUM(worked)>420
) AS s) AS CurrentYearlyFlexiAvailable
FROM rpt_timesheet_data
WHERE entrydate BETWEEN '2017-04-02' AND '2017-04-13 23:59:59'
AND ActivityId=3192
GROUP BY AddedById,AddedByName
ORDER BY AddedByName
but I keep getting:
Error Code: 1054. Unknown column 'AddedById' in 'where clause'
Just in that one location. I've tried various queries to sort this, but just cannot figure it out. Sorry not to good at explaining this, can see it in my head what I want to do...
Here is a query that does something very similar in that it returns the results for a single user, where as the one above is meant to loop through all users and give me the results:-
SELECT addedbyname, SUM(ttl) -
(SELECT SUM(worked)
FROM vwtimesheet
WHERE addedby=1
AND entrydate BETWEEN '2017-01-01' AND '2017-04-13'
AND activityid=3192
GROUP BY addedby ) AS CurrentYearlyFlexiAvailable
,(SELECT SUM(worked) FROM vwtimesheet
WHERE addedby=1
AND entrydate BETWEEN '2017-01-01' AND '2017-04-13'
AND activityid=3192
GROUP BY addedby ) AS flexiused
,(SELECT sum(worked) FROM vwtimesheet
WHERE addedby=1
AND entrydate BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND curdate()
AND activityid=3192
GROUP BY addedby ) as fleximonthused
FROM ( SELECT entrydate,addedbyname,SUM(worked)-420 AS ttl FROM vwtimesheet
WHERE addedby=1
AND entrydate BETWEEN '2017-01-01' AND '2017-04-13'
AND projectid<>113
AND activityid<>3192
GROUP BY entrydate,addedbyname
HAVING SUM(worked)>420
) AS s
Please try the following...
SELECT AddedById,
AddedByName,
SUM( HoursWorked ) AS HoursWorked,
SUM( ttl ) - sumWorked AS CurrentYearlyFlexiAvailable
FROM ( SELECT AddedById AS AddedById,
AddedByName AS AddedByName
FROM rpt_timesheet_data
GROUP BY AddedById
) AS AddedByFinder
JOIN ( SELECT addedby AS addedby,
entrydate AS entrydate,
SUM( worked ) - 420 AS ttl
FROM vwtimesheet
WHERE entrydate BETWEEN '2017-01-01' AND '2017-04-13'
AND projectid <> 113
AND activityid <> 3192
GROUP BY addedby,
entrydate
HAVING SUM( worked ) > 420
) AS ttlFinder ON AddedByFinder.AddedById = ttlFinder.addedby
JOIN ( SELECT addedby AS addedby,
SUM( worked ) AS sumWorked
FROM vwtimesheet
WHERE entrydate BETWEEN '2017-01-01' AND '2017-04-13'
AND activityid = 3192
GROUP BY addedby
) sumWorkedFinder ON AddedByFinder.AddedById = sumWorkedFinder.addedby
WHERE entrydate BETWEEN '2017-04-02' AND '2017-04-13 23:59:59'
AND ActivityId = 3192
GROUP BY AddedById,
AddedByName
ORDER BY AddedByName;
(Explanation to follow...)
If you have any questions or comments, then please feel free to post a Comment accordingly.
I am actually stuck in merging the result of this two queries:
first query:
SELECT c.code, c.name, pc.sku, pc.cat_code, pp.title
FROM `cat_parent` cp, cat c, prod_cat pc, products pp
WHERE c.code = cp.cat_code
AND cp.cat_code = pc.cat_code
AND pp.sku = pc.sku
AND cp.parent_code = 01110
AND hide =0
The result I get is:
Second query:
SELECT `sku` , `update_date` , `description` , count( * ) AS total_sold
FROM `orderline`
WHERE `update_date` >= ( DATE_ADD(CURDATE( ) , INTERVAL -14 DAY ) )
AND `update_date` <= ( DATE_ADD(CURDATE( ) , INTERVAL -7 DAY ) )
GROUP BY left( sku, 7 )
ORDER BY total_sold DESC
The result:
The question I want to ask that how can I get the result by filtering the sku available in both tables.
Just bit confused on that part....any ideas will be appreciated.
This is only part of the data. there is heaps of data. Yes, I want to merge the both tables and want to find the common sku available in both tables.
My expected result will be sku, title, total sold.
Thanks, anyway I managed to get around to get the result.
My final query:
SELECT * FROM (
SELECT sku , update_date , description FROM orderline WHERE
update_date >= '2012-03-06' AND update_date <= '2012-03-07' )g
JOIN (
SELECT c.code, c.name, pc.sku, pc.cat_code FROM cat_parent cp, cat
c, prod_cat pc, products pp WHERE c.code = cp.cat_code AND cp.cat_code
= pc.cat_code AND pp.sku = pc.sku AND cp.parent_code =01110 AND hide =0 )p ON left( g.sku, 7 ) = left( p.sku, 7 )
Something like this -
SELECT
`c`.`code`, `c`.`name`, `pc`.`sku`, `pc`.`cat_code`, `pp.title`,
`ol`.`sku`, `ol`.`update_date`, `ol`.`description`, COUNT(*) AS `total_sold`
FROM `cat_parent` `cp`
INNER JOIN `cat` `c`
ON `c`.`code` = `cp`.`cat_code`
INNER JOIN `prod_cat` `pc`
ON `cp`.`cat_code` = `pc`.`cat_code`
INNER JOIN `products` `pp`
ON `pp`.`sku` = `pc`.`sku`
INNER JOIN `orderline` `ol`
ON LEFT(`pc`.`sku`, 7) = LEFT(`ol`.`sku`, 7)
WHERE `cp`.`parent_code` = 01110
AND `hide` = 0
AND `ol`.`update_date` >= ( DATE_ADD(CURDATE( ) , INTERVAL -14 DAY ) )
AND `ol`.`update_date` <= ( DATE_ADD(CURDATE( ) , INTERVAL -7 DAY ) )
GROUP BY left( `ol`.`sku`, 7 )
ORDER BY `total_sold` DESC