I am trying to make a Calculation where I want to update ideal hours based on number of Assistant and Head Coaches based on whether a restaurant is 5 day or 7 day a week. When I make a calculation where I use 10/7 or 20/7, it takes it as 1 and 2 respectively. I am currently using trunc but I have tried using cast :: decimal(10,6) etc and it doesn't work.
select
a.entity,
a.store_name,
a.order_date,
a.daily_ideal_hours,
a.daily_ideal_hours - (case when b.days_open like '%Weekdays%' then ((c.total_acs*(20/5) + c.total_hcs*(10/5)))
when b.days_open like '%All%' then ((c.total_acs * trunc(20/7,10)) + (c.total_hcs * trunc(10/7,10))) end) as updated_value
from scorecards_ideal_labor_hours as a
left join days_store_open as b
on a.entity = b.entity
left join hc_ac_data as c
on a.entity = c.entity_id
and a.order_date = c.report_date
where a.order_date between '2021-12-06' and '2021-12-12'
and a.entity = 66
order by a.order_date desc;
How do I fix this?
You don't need to use trunc function here.
Just typecast both numerator and denominator to float.
This will give the final answer with decimal places, without any rounding:
select (10::float)/(7::float);
This gives answer as:
1.4285714285714286
If you need to round the final answer to few digits only:
select trunc((10::float)/(7::float),2);
Related
I want to add add the Year to date component to this code. I have tried some other ways but I am not getting what I would like to see. Can someone please help me revised this to include the YTD in addition to the Month to date that is already there?
SELECT
COST__DESC,
ST.AD_SRV_MTN AS MONTH_OF_AD,
COUNT(DISTINCT CM.CM_NBR) AS CMS,
MEM_MO AS MBR_MTH,
CMS/MBR_MTH*1000 AS CMS_PER_1000
FROM XTR.FT_CM AS CM
JOIN XTR.FT_ST AS ST ON ST.CM_NBR = CM.CM_NBR
JOIN XTR.DIM_MED_CST AS MC ON ST.CST_CK = MCC.CST_CK
JOIN XTR.DIM_AF AS AFF ON ST.PRO_CK = AFF.AFF_CK
JOIN XTR.DIM_ADJDCTN_STAT AS A_S ON ST.ADJDCTN_STAT_CK = A_S.ADJDCTN_STAT_CK
JOIN XTR.DIM_ADJ_OT AS OT ON ST.ADJ_CK = OT.ADJ_CK
LEFT JOIN
(SELECT
CALENDAR_YEAR_MONTH as YEAR_MO,
SUM(MBR.COUNT_NBR) as MEM_MO
FROM XTR.FT_MBR_MONTHS MBR
INNER JOIN DIM_MBR_C ON MBR.DB_MBR_CK = DIM_MBR_C.DB_MBR_CK
AND MBR.DATE_CK BETWEEN DIM_MBR_C.DB_eff_date_ck
AND DIM_MBR_C.DB_END_DATE_CK
INNER JOIN DIM_DATE DT ON ELI_DATE_CK = DT.DATE_CK
WHERE MBR.F_C_CK = 500058321 AND YEAR_MO >= 201701
GROUP BY 1) MM ON ST.AD_SRV_MTN = MM.YEAR_MO
WHERE ST.F_C_CK = 500058321 AND ST.ST_START_DATE_CK >= 20200101
AND ST.AD_SRV_MTN > 201912 AND MC.MED_DESC IN ('Er', 'IP')
AND ST.AD_SRV_MTN < ((EXTRACT (YEAR FROM CURRENT_DATE) *100) +
EXTRACT (MONTH FROM CURRENT_DATE))
GROUP BY 1,2,4
ORDER BY 1,2
Honestly I don't really get your SQL and what is counted, but: Your can play with dates quite easy in Teradata, as Dates are stored (and can be used) internally as INTEGER. Just keep in mind year 1900 as year 0 and format YYYYMMDD.
So e.g. 16-Apr-2020 is in Format YYYYMMDD 20200416 and if you take 1900 as 0 you'll end up with 1200416 which is the internal format. Just try SELECT CURRENT_DATE (INT); - So if you want compare YearNumers you just have to divide by 10000.
With this your can implement YTD as SUM (CASE WHEN CURRENT_DATE/10000 = <YourDateField>/10000 THEN <YourKPI> else 0 END) as YourKPI_YTD. Counting can be done by SUM...THEN 1 ELSE 0 END....
I am trying to get a percentage of number of times a product is faulty.
I have the faults in one table and the throughput in another. Throughput could never be 0. The report would never be ran if it was 0.
Unfortunately the tables cannot be joined.
I can get the correct result returned in SQL but when I apply the query to manufacturing software with date parameters applied I receive the divide by 0 error.
Query working in SQL:
SELECT
CAST (COUNT (faulttype.faults) AS FLOAT)/
CAST ((SELECT COUNT(throughput.throughput)
FROM [throughput]
WHERE throughput.throughput >= '2017-08-08 00:00:00' and throughput.throughput <= '2018-08-13 23:59:00')
AS FLOAT)
*100 Percentage
FROM faults (nolock)
INNER JOIN faulttype (nolock) ON faults = faults.faults
where fault.procedure = 15
and faults.regtime >= '2017-08-08 00:00:00' and qc_inspections.regtime <= '2018-08-13 23:59:00'
Query with date parameters applied (receives divide by 0 error):
SELECT
CAST (COUNT (faulttype.fault) AS FLOAT)/
CAST ((SELECT COUNT(throughput.throughput)
FROM [throughput]
WHERE CAST(faultype.regtime as date) between #datetimerange_From and #datetimerange_To
AS FLOAT)
*100 Percentage
FROM faults (nolock)
INNER JOIN faulttype (nolock) ON faults.id = faulttype.fault
where fault.procedure = 15
and CAST(faults.regtime as date) between #datetimerange_From and #datetimerange_To
I have tried nullif and also case statement to no avail. Case statement didn't work because of lack of join. I managed to fudge something together so that the case statement searched for an ID count of 1 then show one (that it would never be) else perform my division. I thought it would trick SQL into not seeing a 0 prior to the user applying the date parameters. Buuuut that didn't work and I am sure somebody will tell me why lol.
Its Strange you dont have join on [throughput]
table, but if i take your query like it, try Something like this:
with CountFault as (
select count(throughput) CountFault
from [throughput]
where CAST(throughput as date) between #datetimerange_From and #datetimerange_To
)
select count(f2.fault as decimal) / nullif(f3.CountFault, 0) * 100
from faults f1
INNER JOIN faulttype f2 ON f1.id = f2.fault
cross join CountFault f3
where f1.procedure = 15 and CAST(f1.regtime as date) between #datetimerange_From and #datetimerange_To
I'm trying to create a system that will select the old users that have not logged in for the past 7 days. I have a problem with this query.
The query should select the a.email, p.name, a.name, b.account_id, and I'll explain.
a is accounts
b is billing
p is players
Should check if the b.account_id is equal a.id that can get by p.account_id and after that should check if the p.lastlogin is higher or equal than 7 days then should return the query results.
I tried this, but it isn't working:
SELECT
`p`.`name`,
`a`.`email`,
`a`.`name`,
`b`.`account_id`
FROM
`billing` AS `b` AND `players` AS `p`
LEFT JOIN `accounts` AS `a`
ON `a`.`id` = `p`.`account_id` AND `a`.`name` = `b`.`account_id`
WHERE `p`.`lastlogin` >= UNIX_TIMESTAMP() + (7 * 86400)
AND group_id = 1
ORDER BY lastlogin
DESC
I hope that this is understandable, xD.
Regards,
vankk.
I think the problem is here.
WHERE `p`.`lastlogin` >= UNIX_TIMESTAMP() + (7 * 86400)
It should be -instead of +.
Also, why don't you use 2 JOINs?
You can also use the DATEDIFF function if you are using SQL
where DATEDIFF(d, a.DateValue , DateTimeNow) <7;
https://www.w3schools.com/sql/func_datediff.asp
I have two tables.
Table 1 = My Trades
Table 2 = Market Trades
I want query the market trade 1 minute prior to my trade. If there is no market trade in Table 2 that is 1 minute apart from mine then I want to look back 2 minutes and so on till I have a match.
Right now my query gets me 1 minute apart but I cant figure out how to get 2 minutes apart if NULL or 3 minutes apart if NULL (up to 30 minutes). I think it would best using a variable but im not sure the best way to approach this.
Select
A.Ticker
,a.date_time
,CONVERT(CHAR(16),a.date_time - '00:01',120) AS '1MINCHANGE'
,A.Price
,B.Date_time
,B.Price
FROM
Trade..MyTrade as A
LEFT JOIN Trade..Market as B
on (a.ticker = b.ticker)
and (CONVERT(CHAR(16),a.date_time - '00:01',120) = b.Date_time)
There is no great way to do this in MySQL. But, because your code looks like SQL Server, I'll show that solution here, using APPLY:
select t.Ticker ,
convert(CHAR(16), t.date_time - '00:01', 120) AS '1MINCHANGE',
t.Price,
m.Date_time,
m.Price
from Trade..MyTrade as t outer apply
(select top 1 m.*
from Trade..Market m
where a.ticker = b.ticker and
convert(CHAR(16), t.date_time - '00:01', 120) >= b.Date_time)
order by m.DateTime desc
) m;
I want to know how to get the exact hours between two dates in SQL Server.
I have two queries, but I am not getting the time I want; can you guys help me with this problem? The first one is this:
SELECT p.Id, p.FechaInicio, p.FechaFin,
DATEDIFF(MINUTE, p.FechaInicio, p.FechaFin) / 60 AS 'Duracion'
FROM Programaciones p
INNER JOIN Usuarios u ON u.Id = p.Programacion_Usuario
WHERE u.Nombre = 'User 1'
ORDER BY p.FechaCreacion DESC
This is the result:
And the second one is this:
SELECT p.Id, p.FechaInicio, p.FechaFin,
DATEDIFF(MINUTE, p.FechaInicio, p.FechaFin) / 60.0 as 'Duracion'
FROM Programaciones p
INNER JOIN Usuarios u ON u.Id = p.Programacion_Usuario
WHERE u.Nombre = 'User 1'
ORDER BY p.FechaCreacion DESC
result:
How can I transform 0.500000 to 0.5?
Thanks if you can help me. Have a good day.
You can cast your number as DECIMAL() to define how many decimal places you want to preserve:
CAST(DATEDIFF(MINUTE, p.FechaInicio, p.FechaFin) / 60.0 AS DECIMAL(5,1)) as 'Duracion' from Programaciones
The first parameter in DECIMAL() is the number of digits in total, the second is how many digits after the decimal point, in the case above, 5 digits total, 1 after the decimal point.
You could also wrap it in ROUND().