SELECT
SQL_CALC_FOUND_ROWS a.* ,
zn.`name` AS zone_name,
c.`name` AS carrier_name,
CASE type
WHEN type=1 THEN 'General day'
ELSE 'Special date' END AS type_changed,
CASE week_day
WHEN week_day = -1 THEN 'notset'
WHEN week_day = 1 THEN 'monday'
WHEN week_day = 2 THEN 'tuesday'
WHEN week_day = 3 THEN 'wednesday'
WHEN week_day = 4 THEN 'thursday'
WHEN week_day = 5 THEN 'friday'
WHEN week_day = 6 THEN 'saturday'
WHEN week_day = 7 THEN 'sunday' END AS week_day_mod ,
IF(date = '0001-01-01 00:00:0', '--', DATE(date)) AS date_mod, IF(is_working_day = 1 ,'working day', 'day off') AS is_working_day_mod
FROM `ps_deliverytime_table` a
LEFT JOIN ps_zone AS zn ON(a.`id_zone` = zn.`id_zone`)
LEFT JOIN ps_carrier AS c ON(a.`id_carrier` = c.`id_carrier`)
WHERE 1 ORDER BY a.`id_time_table` ASC LIMIT 0,50
When week day is equal 1 it works fine but in other case it do not working
When you put column name after CASE, you shouldn't use WHEN column = value, just use WHEN value, because it automatically compares the column to each value in the WHEN clauses.
CASE type
WHEN 1 THEN 'General day'
ELSE 'Special date'
END AS type_changed,
CASE week_day
WHEN -1 THEN 'notset'
WHEN 1 THEN 'monday'
WHEN 2 THEN 'tuesday'
WHEN 3 THEN 'wednesday'
WHEN 4 THEN 'thursday'
WHEN 5 THEN 'friday'
WHEN 6 THEN 'saturday'
WHEN 7 THEN 'sunday'
END AS week_day_mod ,
When you do both, you're testing week_day = (week_day = -1), week_day = (week_day = 1), etc. It works on Monday because 1 = (1 = 1) is equivalent to 1 = 1, which is true; but on Tuesday, it's 2 = (2 = 2), which is equivalent to 2 = 1, which is false.
Related
select
Count(*)
from gl
join TRVMAINDATA msit
on gl.TRANSACTIONID = msit.CHARGETRANSACTIONID
where gl.CREATEDDATETIME >= DATEADD(MONTH,-2,getdate()) AND
datediff(DAY,gl.BUSINESSPROCESSDATE,gl.CREATEDDATETIME)>=4 AND
gl.MARKETCODE In('535','532','056','050','039','036','034','033','030','029','027','023','022','021','018','015','012','011','010','009','007','006','005','002','001' ) and DATEADD(MONTH,DATEDIFF(MONTH,0,gl.CREATEDDATETIME),0) = '2/1/2017 12:00:00 AM'
The above code gives the total for market code mentioned.
But i also want the market code for other code which doesn't belog here. for example
select
Count(*)
from gl
join TRVMAINDATA msit
on gl.TRANSACTIONID = msit.CHARGETRANSACTIONID
where gl.CREATEDDATETIME >= DATEADD(MONTH,-6,getdate()) AND
datediff(DAY,gl.BUSINESSPROCESSDATE,gl.CREATEDDATETIME)>=0
and
DATEADD(MONTH,DATEDIFF(MONTH,0,gl.CREATEDDATETIME),0) = '2/1/2017 12:00:00 AM'
AND
(case
when gl.MARKETCODE In('535','532','056','050','039','036','034','033','030','029','027','023','022','021','018','015','012','011','010','009','007','006','005','002','001') then 'Proprietary'
when gl.MARKETCODE='037' then 'US'
else 'partner'
end )
Is it possible to calculate totals of other market codes in one sql query or do i have to calculate individually?
I think you would like to do count with case when in select statement like this:
SELECT COUNT(CASE
WHEN gl.marketcode IN ( '535', '532', '056', '050',
'039', '036', '034', '033',
'030', '029', '027', '023',
'022', '021', '018', '015',
'012', '011', '010', '009',
'007', '006', '005', '002', '001' ) THEN 1
ELSE NULL
END) AS `Proprietary`,
COUNT(CASE
WHEN gl.marketcode = '037' THEN 1
ELSE NULL
END) AS `US`,
COUNT(CASE
WHEN gl.marketcode NOT IN ( '535', '532', '056', '050',
'039', '036', '034', '033',
'030', '029', '027', '023',
'022', '021', '018', '015',
'012', '011', '010', '009',
'007', '006', '005', '002',
'001', '037' ) THEN 1
ELSE NULL
END) AS `partner`
FROM gl
JOIN trvmaindata msit
ON gl.transactionid = msit.chargetransactionid
WHERE gl.createddatetime >= DATEADD(month, -2, GETDATE())
AND DATEDIFF(day, gl.businessprocessdate, gl.createddatetime) >= 4
AND DATEADD(month, DATEDIFF(month, 0, gl.createddatetime), 0) = '2/1/2017 12:00:00 AM'
I have the below query that utilizes a case statement. I would like to datediff two dates but exclude weekend days.
I have the below that excutes but now I would like to exclude Sat and Sunday from this... AND DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate) <= 2
CASE WHEN
S.Name IN ('Assessment','Survey')
AND A.ALERT_DESC = 'ER'
AND CAST(A.ALERTS_CREATE_DT AS DATE) <= CAST(S.CreatedDate AS DATE)
AND DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate) <= 2 /*EXCLUDE Sat and Sunday from the calculation*/
Full Query
SELECT
CASE WHEN
S.Name IN ('Assessment','Survey')
AND A.ALERT_DESC = 'ER'
AND CAST(A.ALERTS_CREATE_DT AS DATE) <= CAST(S.CreatedDate AS DATE)
AND
( DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate) <= 2 /*Business Days*/
--DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate) + 1
---(DATEDIFF(WK,A.ALERTS_CREATE_DT,S.CreatedDate) * 2)
---(CASE WHEN DATENAME(DW,A.ALERTS_CREATE_DT) = 'SUNDAY' THEN 1 ELSE 0 END)
---(CASE WHEN DATENAME(DW,S.CreatedDate) = 'SATURADAY' THEN 1 ELSE 0 END)
)
THEN 'Y'
WHEN A.ALERT_DESC = 'model' OR S.CreatedDate IS NULL OR S.Name = 'ER'
THEN ''
ELSE 'N'
END 'Count towards Alerts'
FROM A
FULL S ON A.id= S.id
WHERE 1=1
This should give you the required result by excluding the Saturdays and Sundays.
SELECT A.ALERT_DESC,A.ALERTS_CREATE_DT,S.Name,S.CreatedDate,
CASE WHEN
S.Name IN ('Assessment','Survey') AND A.ALERT_DESC = 'ER'
AND CAST(A.ALERTS_CREATE_DT AS DATE) <= CAST(S.CreatedDate AS DATE)
AND
(( DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate)+1
- (datediff(wk,A.ALERTS_CREATE_DT,S.CreatedDate)*2)
- case when datepart(dw,A.ALERTS_CREATE_DT)=1 then 1 else 0 end
- case when datepart(dw,S.CreatedDate)=7 then 1 else 0 end
)) <=2 THEN 'Y'
WHEN A.ALERT_DESC = 'model' OR S.CreatedDate IS NULL OR S.Name = 'ER' THEN ''
ELSE 'N' END 'Count towards Alerts'
FROM A,S
select &enter,
case
when enter=mon then '1'
when enter=yr then '12'
else '0'
end as status
from dual
Based on the original code:
select '&enter',
case
when '&enter'='mon' then '1'
when '&enter'='yr' then '12'
else '0'
end as status
from dual
Suggested code:
select '&enter',
case '&enter'
when 'mon' then 1
when 'yr' then 12
else 0
end as status
from dual
SELECT
ID,
Division,
EffectiveDate,
PM,
case Status
when 0 then 'Dead'
when 1 then 'Active'
when 2 then 'Job'
when 3 then 'Pending'
when 4 then 'Sales Lead'
when 5 then 'Budget'
when 6 then 'Change Order'
end as Status,
Name,
Address,
ProjectType
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND EffectiveDate >= '2015-06-01'
AND Status = 6
ORDER BY EffectiveDate ASC
;
i need to return all the values with a status of 6 OR 2
i tried writing AND Status = 6 or 2 but that doesnt work =/
is this accomplished by joining tables or is there a simpler way?
thanks
SELECT
ID,
Division,
EffectiveDate,
PM,
case Status
when 0 then 'Dead'
when 1 then 'Active'
when 2 then 'Job'
when 3 then 'Pending'
when 4 then 'Sales Lead'
when 5 then 'Budget'
when 6 then 'Change Order'
end as Status,
Name,
Address,
ProjectType
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND EffectiveDate >= '2015-06-01'
AND ( Status = 6 OR Status = 2 )
ORDER BY EffectiveDate ASC
You were close, you just need a OR statement with Status = 2.
SELECT
ID,
Division,
EffectiveDate,
PM,
case Status
when 0 then 'Dead'
when 1 then 'Active'
when 2 then 'Job'
when 3 then 'Pending'
when 4 then 'Sales Lead'
when 5 then 'Budget'
when 6 then 'Change Order'
end as Status,
Name,
Address,
ProjectType
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND EffectiveDate >= '2015-06-01'
AND (Status = 2 OR Status = 6)
ORDER BY EffectiveDate ASC
;
Here is some more info.
You want either IN or OR -
AND Status IN (6, 2)
AND Status = 6 OR Status = 2
You can use IN in your query. Tyr this :
SELECT
ID,
Division,
EffectiveDate,
PM,
case Status
when 0 then 'Dead'
when 1 then 'Active'
when 2 then 'Job'
when 3 then 'Pending'
when 4 then 'Sales Lead'
when 5 then 'Budget'
when 6 then 'Change Order'
end as Status,
Name,
Address,
ProjectType
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND EffectiveDate >= '2015-06-01'
AND Status IN (6,2)
ORDER BY EffectiveDate ASC
At the moment I am grouping the news into month+year but we dont have enough news articles. So instead would like to group them by season: summer, spring, autumn or winter + year
So instead of:
January 2013
March 2013
April 2013
It would be:
Summer 2013
Spring 2013
Monthly archieve
SELECT MONTHNAME(news_date) AS MONTH,
YEAR(news_date) AS YEAR,
COUNT(news_id) AS COUNT
FROM news
WHERE news_type = 'NEWS'
AND news_status = 'ENABLED'
GROUP BY CONCAT(MONTH(news_date), ' ', YEAR(news_date))
ORDER BY news_date DESC
SELECT CASE WHEN MONTH(news_date) IN (12, 1, 2) THEN 'Winter'
WHEN MONTH(news_date) IN (3, 4, 5) THEN 'Spring'
WHEN MONTH(news_date) IN (6, 7, 8) THEN 'Summer'
ELSE 'Fall' END AS Season,
YEAR(news_date) AS YEAR,
COUNT(news_id) AS COUNT
FROM news
WHERE news_type = 'NEWS'
AND news_status = 'ENABLED'
GROUP BY YEAR(news_date), Season
ORDER BY news_date DESC
To get numeric seasons (Winter as 0 through to Autumn as 3), try the more compact:
SELECT quantity, FLOOR((MONTH(date_field) % 12)/3) as season
FROM `tbl`
GROUP BY season
SELECT case when MONTH(news_date) between 3 and 5 then 'Spring',
when MONTH(news_date) between 6 and 8 then 'Summer',
when MONTH(news_date) between 9 and 11 then 'Autum',
when MONTH(news_date) >= 12 and MONTH(news_date) <= 2 then 'Winter'
end AS Period,
YEAR(news_date) AS YEAR,
COUNT(news_id) AS COUNT
FROM news
WHERE news_type = 'NEWS' AND news_status = 'ENABLED'
GROUP BY YEAR(news_date), Period
ORDER BY news_date DESC
use CASE statement like below in your query
CASE WHEN MONTH(news_date) = 1 or MONTH(news_date) = 2 or MONTH(news_date) = 3
THEN CONCAT('SPRING ' , YEAR(news_date) )
WHEN MONTH(news_date) = 4 or MONTH(news_date) = 5 or MONTH(news_date) = 6
THEN CONCAT('SUMMER ' , YEAR(news_date) )
WHEN MONTH(news_date) = 7 or MONTH(news_date) = 8 or MONTH(news_date) = 9
THEN CONCAT('AUTUMN ' , YEAR(news_date) )
WHEN MONTH(news_date) = 10 or MONTH(news_date) = 11 or MONTH(news_date) = 12
THEN CONCAT('WINTER ' , YEAR(news_date) )