Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
Good Day,
I am trying to apply if or case statement in SQL.
Ex. The marketing plan extended to more stores after a certain date:
First phase starting from Aug 1: only 1 store
Second phase starting from Sept 1: 3 stores
Last phase starting from Oct 1: all stores
So I have everything set except for the WHERE clause where I need to include if or case statement with the following logic:
WHERE
1=1
AND
Pseudo code, something along this line:
IF DATE >=20220801 AND <=20220831, STORE IN (1)
ELIF DATE >=20220901 AND <=20220930, STORE IN (1, 2, 3)
ELIF DATE >= 20221001, all stores
If anyone can point me to the right direction I would much appreciate.
Simply combine your conditions with AND and OR:
... WHERE (DATE >= '01-AUG-22' AND DATE < '01-SEP-22' AND STORE IN (1))
OR (DATE >= '01-SEP-22' AND DATE < '01-OCT-22' AND STORE IN (1, 2, 3))
OR (DATE >= '01-OCT-22')
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have come once more to seek your guidance.
I am not actually sure how possible this is, but I can only hope.
I have an already heavily joined and aggregated query which pulls out results as shown in the attached picture.
As mentioned, this is an already heavily editing query with multiple joins and aggregates to take half hour data and sum it hourly.
I am wondering if it is at all possible to now transpose the data so that there are 24 seperate hourly entries for each date?
So instead of having one single line for 2014-01-01 with each hourly reading in a seperate column, is it at all possible to change it so there are just two columns. One for date/time and one with the hourly total.
So it would look more like:
and then continue down for each hour of each day.
I am using SQL Server 2014 if that helps.
Thank you in advance!
Assuming your time columns are named time0, time1, ..., time23 you could write
SELECT DateAdd(HOUR, 0, date) as DateTime, time0 as total from table
UNION
SELECT DateAdd(HOUR, 1, date) as DateTime, time1 as total from table
UNION
...
SELECT DateAdd(HOUR, 23, date) as DateTime, time23 as total from table
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
My query too much time to execute for fetching data.
SELECT `app`.*, `pat`.`title`, `email_address`, `bad_debtor`, `county`, `date_of_birth`, `dentist_id`, `doctor_id`, `ethnicity`, `first_name`, `middle_name`, `last_name`, `gender`, `pat`.`mobile_phone`, `pat`.`prevent_appointment_booking`, `pat`.`use_email`, `pat`.`use_sms`, `pat`.`recall_method`, `pat`.`status`, `pat`.`pat_id`, `pat`.`active` FROM `patients` `pat` LEFT JOIN `appoiment` `app` ON `pat`.`id` = `app`.`patient_id` WHERE (date(app.start_time) > date(NOW()) - INTERVAL 7 DAY) and `app`.`state` IN ('Completed') and `app`.`patient_id` is not NULL GROUP BY `pat`.`id`
This part
WHERE (date(app.start_time) > date(NOW()) - INTERVAL 7 DAY) and `app`.`state` IN ('Completed') and `app`.`patient_id` is not NULL
the problem is in where clause which takes too much time.
help me to optimize query.
replace app. * with the correct column you need
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an data which includes Processeddate, start time and end time. I need to get minimum starttime and max endtime for each day.
Process date should be the first coulmn
Just use min() and max() function as shown below.
;WITH cte
AS (
SELECT Cast(processedDate AS DATE) AS processedDate
,min(startTime) minStartTime
,max(endTime) maxEndTime
FROM mytable
GROUP BY Cast(processedDate AS DATE)
)
SELECT *
FROM cte
WHERE month(processedDate) = 1
AND year(processedDate) = 2020
ORDER BY processedDate
db<>fiddle
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a mysql table that contains, among others, the columns personName, board (a bool that states if person is part of board or not) and board_date (a date column that contains the date that the person was elected to the board, if she was).
In my club, the people are elected for 5 year terms. I need to create a query that will return the people whose term will expire in the next 6 months. But I have no clue on how to do that math with those dates.
Can someone please give me a hand? I inherited this system from a previous administrator, and the client wants this. I'm not good with SQL
you can use date_add adding 6 months to now()
select *
from my_table
where date_add(board_date, INTERVAL 5 YEAR)
between now() and DATE_ADD(now(), INTERVAL 6 MONTH)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Okay, I have a situation when I need to compare a column from a table with today's date. If the value in a particular row is earlier than today (in other words, the date has already passed, I need to mark a virtual column with the phrase 'lapsed'
Below is the SQL (SQL Server 2012) that I have been using:
SELECT
datediff(day, sysdatetime(), policy_expiration_dt) As 'DayDiff'
,Case When 'DayDiff' < 0 Then 'Lapsed'
FROM TABLE_NAME
The first column that includes the datediff function makes the comparison and the second column marks it as lapsed if the date has passed.
basically the order of how an sql statement is executed does not allow you to use the alias. so you will need to use the datediff() function in the CASE statement.
The order of sql queries execution is:
1. FROM
2. ON
3. OUTER
4. WHERE
5. GROUP BY
6. CUBE | ROLLUP
7. HAVING
8. SELECT
9. DISTINCT
10. ORDER BY
11. TOP
you need to do this:
SELECT DATEDIFF(day, SYSDATETIME(), policy_expiration_dt) AS 'DayDiff'
, CASE
WHEN DATEDIFF(day, SYSDATETIME(), policy_expiration_dt) < 0 THEN 'Lapsed'
END
FROM TABLE_NAME;