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
Related
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')
I'm creating a fixture list for football and would like to create a page "weekend fixtures".
I have managed the following
SELECT * FROM events WHERE DAYOFWEEK(event_time) = 7
or DAYOFWEEK(event_time) = 1
or (DATE_FORMAT(event_time, "%T") > '17:30:00' AND DAYOFWEEK(event_time) = 6)
The above code works in returning fixtures for all weekends. But I need either the current or next.
Any advice would be appreciated.
you can use WEEK function from mysql
This would check id´f event timestamp is in this or the next week
SELECT * FROM events WHERE (DAYOFWEEK(event_time) = 7
or DAYOFWEEK(event_time) = 1
or (DATE_FORMAT(event_time, "%T") > '17:30:00' AND DAYOFWEEK(event_time) = 6))
AND ((WEEK(event_time) = WEEK(Now()) ) OR (WEEK(event_time) = WEEK(Now()) +1 ))
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;
I have a MySQL database with a table named generations the structure of the table is as follows
I want to get value 10 as output when ten_generation have value 1 otherwise it will not return any value, 20 as output if twenty_generation have value 1 otherwise it will not return any value, 30 as output if thirty_generation have value 1 otherwise it will not return any value. If all the three fields has a value 1 output will be 10,20,30 also the task_id will provided as the input.
Its unclear what you intend the output to be when multiple generation columns are 1 but one solution is to use a CASE statement:
SELECT CASE
WHEN ten_generation = 1 THEN 10
WHEN twenty_generation = 1 THEN 20
WHEN thirty_generation = 1 THEN 30
ELSE NULL
END AS value
FROM generations
WHERE id = :your_id
If you want it as multiple columns then:
SELECT CASE
WHEN ten_generation = 1
THEN 10
ELSE NULL
END AS ten_value,
CASE
WHEN twenty_generation = 1
THEN 20
ELSE NULL
END AS twenty_value,
CASE
WHEN thirty_generation = 1
THEN 30
ELSE NULL
END AS thirty_value
FROM generations
WHERE id = :your_id
if only twenty_generation contain value 1 the output is 20 and if twenty_generation and ten_generation contain value 1 output is 10,20
Oracle Query:
SELECT TRIM(
LEADING ',' FROM
CASE WHEN ten_generation = 1 THEN '10' END
|| CASE WHEN twenty_generation = 1 THEN ',20' END
|| CASE WHEN thirty_generation = 1 THEN ',30' END
) AS value
FROM generations
WHERE id = :your_id
For MySQL you'd use CONCAT_WS:
select
concat_ws(',',
case when ten_generation = 1 then '10' end,
case when twenty_generation = 1 then '20' end,
case when thirty_generation = 1 then '30' end
) as result
from mytable
where task_id = 2;
I want to get the month name from a Do Until......Loop and display in a CheckedListBox.
I have two tables.
1. Month_Count
2. Fees_Ledger
My month name function...
Function mnthName(ByVal mnth As Integer)
Dim name As String = String.Empty
name = MonthName(mnth, False)
Return name
End Function
To get the Month Details
"SELECT FromMonth,ToMonth,MonthCount FROM Month_Count WHERE SemesterNumber='1'"
And to get the Paid Count
"SELECT COUNT(*) AS TotMonthPaidCount FROM Fees_Ledger WHERE SemYear='1' AND FeeId='1'"
So...
' Got the values from queries
FromMonth = 7 '(July)
ToMonth = 6 '(June)
MonthCount = 12 '(Loop will rotate 12 times)
TotMonthPaidCount = 1 '(FeeId 1 paid one time)
'Declaring an integer variable
Dim StartM As Integer = 1
FromMonth = FromMonth + TotMonthPaidCount
' For the first month
CheckedListBoxMonth.Items.Clear()
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
' Now the loop to achieve the goal
Do Until StartM = (MonthCount - TotMonthPaidCount)
If FromMonth >= 12 Then
FromMonth = 1
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
Else
FromMonth += 1
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
End If
StartM += 1
Loop
This Subroutine results exactly what I want.
But the problem occurs when the StartMonth = FromMonth (6) + TotMonthPaidCount (7) value >12. As 13 or 14 or 15 has no Month Name, it is showing error.
Argument 'Month' is not a valid value.
I want it like below.
What should I do ?