Use Mysql function as column name in select query in mysql - mysql

I have a mysql table with columns for each day of months. e.g., 1,2,3,4.. are column name. i want to select particular day column based on day present in emp.date.
I tried following query but it is not working.
SELECT emp.id
, emp.DATE_FORMAT(emp.date, "%d")
FROM emp_pattern_mapping epm
WHERE epm.year = 2019
AND epm.month = 8
AND epm.type = 2
AND epm.emp_id = 39;
It is giving column not found error

You have a very poor data format and you should devote your effort to fixing it.
You can do what you want with a giant CASE expression:
SELECT `emp`.`id`,
(CASE WHEN DAY(emp.date) = 1 THEN `1`
WHEN DAY(emp.date) = 2 THEN `2`
. . .
WHEN DAY(emp.date) = 31 THEN `31`
END)
FROM emp_pattern_mapping epm
WHERE epm.year = 2019 AND
epm.month = '08' AND
epm.type = 2 AND
epm.emp_id = 39;

Try this,
SELECT `emp`.`id`,DAY(emp.date) as emp_day
FROM emp_pattern_mapping epm
WHERE epm.year = 2019 AND
epm.month = '08' AND
epm.type = 2 AND
epm.emp_id = 39;

Related

Generating a table of subtraction of two colum count

Hello i want to make a summary table where the filter is the segment and date
The columns will be the subtraction of count of two column in same table so far i have come up to this
select
case
when SEGMENT='champion'THEN SUM(SEGMENTED_DATE = '2022-10-14')-SUM(SEGMENTED_DATE='2022-10-07')
END as total_change_champion,
case
when SEGMENT='Hibernating'THEN SUM(SEGMENTED_DATE = '2022-10-14')-SUM(SEGMENTED_DATE='2022-10-07')
END as total_change_hibernate
from weekly_customer_RFM_TABLE;
This query is giving me null value for columns
select
SUM(SEGMENTED_DATE = '2022-10-14')-
SUM(SEGMENTED_DATE='2022-10-07')as total_changes_in_14th
From weekly_customer_RFM_TABLE
where
SEGMENT ='Hibernating'
This query is actually giving me the result. but when I am going to make a consolidated table using different segment subtraction i am getting null value in my result table.Kindly help me out
my sample data
which is i want to substract totalcount of 2022/10/14 - 2022/10/07 for each segment
image is not loading unfortunately
You must include the CASE expressions inside the aggregate functions:
SELECT COUNT(CASE WHEN SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-14' THEN 1 END) -
COUNT(CASE WHEN SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-07' THEN 1 END) AS total_change_champion,
COUNT(CASE WHEN SEGMENT = 'Hibernating' AND SEGMENTED_DATE = '2022-10-14' THEN 1 END) -
COUNT(CASE WHEN SEGMENT = 'Hibernating' AND SEGMENTED_DATE='2022-10-07' THEN 1 END) AS total_change_hibernate
FROM weekly_customer_RFM_TABLE;
For MySql this can be simplified to:
SELECT SUM(SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-14')-
SUM(SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-07') AS total_change_champion,
SUM(SEGMENT = 'Hibernating' AND SEGMENTED_DATE = '2022-10-14')-
SUM(SEGMENT = 'Hibernating' AND SEGMENTED_DATE='2022-10-07') AS total_change_hibernate
FROM weekly_customer_RFM_TABLE;
You may also use a WHERE clause to filter the table for only the SEGMENTs and SEGMENTED_DATEs that you want:
WHERE SEGMENT IN ('champion', 'Hibernating') AND SEGMENTED_DATE IN ('2022-10-07', '2022-10-14')

MySQL query Count in subquery

Good day,
I have a question.
I have a table called event_booking.
Here we have events and each event has a separate location.
So we have event
event_booking has :
booking_id
booking_name
booking_date
booking_location
booking_comments
The type of structure is :
booking_id = integer
booking_name = varchar
booking_date = datetime
booking_location = integer
booking_comments = text
Now I want to create a Query that lists all event locations and for each location which event has been set there.
So :
I would get a result like :
booking_location = 4 & no_bookings = 256
booking_location = 7 & no_bookings = 34
booking_location = 6 & no_bookings = 128
booking_location = 3 & no_bookings = 24
Now I have fiddled a lit and created the following QUERY.
That's correct in terms of syntax. But totally wrong in terms of output.
SELECT `booking_location`, `booking_location` AS `selector`, (SELECT COUNT(1) FROM `event_booking` WHERE `booking_location` = `selector`) AS `no_bookings` FROM `event_booking` WHERE `booking_location` IN (SELECT `booking_location` FROM `event_booking` GROUP BY `booking_location`)
I know I am missing something. But what I am missing..
Thanks in advance ;-)
Assuming that you really just want to count the booking locations (based on your example of a possible result, and also your query code):
SELECT booking_location,
COUNT(booking_location) AS no_bookings
FROM event_booking
GROUP BY booking_location;

MySQL 5.7.12 Different query result before and after TABLE OPTIMIZE

Working with Amazon Aurora MySQL 5.7.12. And got strange issue.
Have next query
SELECT d.BucketDate,
SUM(d.Value),
d.Key,
d.Code,
d.PlC
FROM MonthlyTable d
JOIN (SELECT #getting list of all available Key with last available BucketDate
d1.Key, MAX(d1.BucketDate) AS BucketDT
FROM MonthlyTable d1
JOIN PCTemp AS pc ON d1.SnapshotDate = 'SOME SNAPSHOT DATE' and pc.PC = d1.PC
JOIN PlCTemp AS p ON d1.SnapshotDate = 'SOME SNAPSHOT DATE' and p.PlC = d1.PlC
WHERE d1.SnapshotDate = 'SOME SNAPSHOT DATE'
AND d1.param1 = 1
AND d1.param2 IN (1 , 3)
GROUP BY d1.Key) dd ON d.SnapshotDate = 'SOME SNAPSHOT DATE' and d.Key = dd.Key
WHERE d.SnapshotDate = 'SOME SNAPSHOT DATE'
AND d.param1 = 1
AND d.param2 IN (3 , 1)
AND (d.BucketDate = dd.BucketDT OR d.BucketDate BETWEEN 'START DATE' AND 'END DATE')
GROUP BY d.Key , d.BucketDate;
The table is getting 6-8 millions of new records each day by SnapshotDate and partitioned by key on the SnapshotDate column.
Yesterday that query failed to return a result.
But:
1 - it started returning the correct result once I've changed d.param2 IN (3 , 1) to d.param2 IN (1 , 3)
2 - it started returning the correct result once I've forced the optimal index without query rewrite.
3 - it started returning the correct result after OPTIMIZE TABLE MonthlyTable
Any ideas on what was that?
How to prevent that happening again?

Laravel eloquent get date format from string

In my table, I have enforce column as string (like: '20170502').
I want to get the week of the day from this string so I try to convert to date format but that not working.
$event = VEventMp::where('event_seq', '=', $mypage_data->event_seq)
->select(
'ENFORCE_PLACE',
'ENFORCE_FROM',
\DB::raw("DATE_FORMAT(STR_TO_DATE(ENFORCE_FROM,'%y/%m/%d'), '%m月%d日')
AS day"),
\DB::raw('(CASE WHEN WEEKDAY(day) = 0 THEN "月"
WHEN WEEKDAY(start_time) = 1 THEN "火"
WHEN WEEKDAY(start_time) = 2 THEN "水"
WHEN WEEKDAY(start_time) = 3 THEN "木"
WHEN WEEKDAY(start_time) = 4 THEN "金"
WHEN WEEKDAY(start_time) = 5 THEN "土"
WHEN WEEKDAY(start_time) = 6 THEN "日" END) AS weekday')
->first();
Where did I make mistake?
Thank you!
You have a couple of issues, firstly if the date format is like 20170502 then you must match that in your call to STR_TO_DATE, so change that to:
STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')
Secondly, you can't use an alias in the SELECT part of the query so you need to change your CASE expression to
(CASE WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 0 THEN "月"
WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 1 THEN "火"
WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 2 THEN "水"
WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 3 THEN "木"
WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 4 THEN "金"
WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 5 THEN "土"
WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 6 THEN "日" END) AS weekday

combining two case queries into one

I want to make a query where where i first check the current date with the date in a column and then base on that I am writing my case. this query is working fine individually but when
i am combining them its not working.
The queires are
SELECT MONTH(CURRENT_DATE)= SUBSTRING(yearmonth,6) FROM dp;
SELECT i,
CASE
WHEN DAY(CURRENT_DATE) =1 THEN `d1_v`
WHEN DAY(CURRENT_DATE) =2 THEN `d1_v`
END VALUE
FROM dp;
combined query..
SELECT i,
CASE
WHEN((MONTH(CURRENT_DATE ))= SUBSTRING(yearmonth,6) THEN
(CASE
WHEN DAY(CURRENT_DATE) = 1 THEN `d1_v`
WHEN DAY(CURRENT_DATE) = 2 THEN `d1_v`
END VALUE)END)Y
FROM dp
please guide me
You've to delete the '(' after the THEN:
SELECT i,
CASE
WHEN ((MONTH(CURRENT_DATE )) = SUBSTRING(yearmonth,6))THEN
CASE
WHEN DAY(CURRENT_DATE) = 1 THEN `day1_value`
WHEN DAY(CURRENT_DATE) = 2 THEN `day1_value`
END
END Y
FROM dp;
The output from the sqlfiddle is 5,null right now.
Hope this work for you.
The sqlfiddle is: http://sqlfiddle.com/#!2/e59c5/10