SQLite CTE limitations (Correlated Subqueries) - sql

I'm confused why Correlated Sub Queries work fine in SQL, but CTE's that emulate the same logic with dependencies, throw an error that says 'XYZ CTE doesn't exist'.
Error message: Error while executing SQL query on database 'BB_Data': no such table: CTE2
I could of course persist these to a view, but these are temporary transformations hence why I've chosen a CTE.
Correlated Sub Query Approach which works fine
SELECT * ,
arrival_year||"-"||month_transformed||"-"||day_transformed as arrival_date
FROM(
SELECT *,
CASE WHEN length(arrival_month_numeric) =1 THEN "0"||arrival_month_numeric ELSE arrival_month_numeric END as month_transformed,
CASE WHEN length(arrival_date_day_of_month) =1 THEN "0"||arrival_date_day_of_month ELSE arrival_date_day_of_month END as day_transformed
FROM
(
SELECT *,
CASE
WHEN arrival_month = 'January' THEN 1
WHEN arrival_month = 'February' THEN 2
WHEN arrival_month = 'March' THEN 3
WHEN arrival_month = 'April' THEN 4
WHEN arrival_month = 'May' THEN 5
WHEN arrival_month = 'June' THEN 6
WHEN arrival_month = 'July' THEN 7
WHEN arrival_month = 'August' THEN 8
WHEN arrival_month = 'September' THEN 9
WHEN arrival_month = 'October' THEN 10
WHEN arrival_month = 'November' THEN 11
WHEN arrival_month = 'December' THEN 12
ELSE 0 END as arrival_month_numeric
FROM
booking_table))
CTE Approach
WITH CTE1 AS(
SELECT *,
CASE
WHEN arrival_month = 'January' THEN 1
WHEN arrival_month = 'February' THEN 2
WHEN arrival_month = 'March' THEN 3
WHEN arrival_month = 'April' THEN 4
WHEN arrival_month = 'May' THEN 5
WHEN arrival_month = 'June' THEN 6
WHEN arrival_month = 'July' THEN 7
WHEN arrival_month = 'August' THEN 8
WHEN arrival_month = 'September' THEN 9
WHEN arrival_month = 'October' THEN 10
WHEN arrival_month = 'November' THEN 11
WHEN arrival_month = 'December' THEN 12
ELSE 0 END as arrival_month_numeric
FROM
booking_table),
CTE2 AS(
SELECT *,
CASE WHEN length(arrival_month_numeric) =1 THEN "0"||arrival_month_numeric ELSE arrival_month_numeric END as month_transformed,
CASE WHEN length(arrival_date_day_of_month) =1 THEN "0"||arrival_date_day_of_month ELSE arrival_date_day_of_month END as day_transformed
FROM CTE1)
SELECT * FROM CTE2;

Related

SQL Server AVG returning 0 for every row

I'm trying to make a somewhat complex query on SQL Server. Had to do a select from a select in order to group by aliases. The problem is that the sum and the count return correct values, but the average returns always zero. if I calculate it as sum(quer.score)/count(quer.score) it also returns all zeros.
What am I doing wrong?
Thanks in advance!
SELECT quer.month, quer.item_type, SUM(quer.score) AS sum_values,COUNT(quer.score) as count_values, avg(quer.score) as final_value FROM (
SELECT
CASE
WHEN MONTH(inte.date) = 1 THEN 'January'
WHEN MONTH(inte.date) = 2 THEN 'February'
WHEN MONTH(inte.date) = 3 THEN 'March'
WHEN MONTH(inte.date) = 4 THEN 'April'
WHEN MONTH(inte.date) = 5 THEN 'May'
WHEN MONTH(inte.date) = 6 THEN 'June'
WHEN MONTH(inte.date) = 7 THEN 'July'
WHEN MONTH(inte.date) = 8 THEN 'Agosto'
WHEN MONTH(inte.date) = 9 THEN 'September'
WHEN MONTH(inte.date) = 10 THEN 'October'
WHEN MONTH(inte.date) = 11 THEN 'November'
WHEN MONTH(inte.date) = 12 THEN 'December'
END AS month,
CASE
WHEN inte.item_id in (SELECT distinct item_id from Items where item_type = 'electronic') THEN 'electronic'
ELSE 'not electronic'
END AS item_type,
CASE WHEN scores.score <7 THEN -1
WHEN scores.score >8 THEN 1
ELSE 0
END AS score
FROM internal_items inte inner join scores_data scores on inte.item_id = scores.item_id
WHERE inte.internal_type = 'dba'
) quer
GROUP BY quer.month, quer.item_type
Result:
month item_type sum count avg
January electronic 0 3 0
January not electronic -2 7 0
February electronic -4 6 0
February not electronic -6 8 0
March electronic -4 5 0
March not electronic -3 6 0
You should use decimals or floats, not integers as data type, e. g. use
CASE WHEN scores.score <7 THEN -1.0
WHEN scores.score >8 THEN 1.0
ELSE 0.0
END AS score
(note the .0 added).
SQL Server uses the same data type as the base column for the aggregate result, and thus, the average would be calculated as int when the input values are ints.

Display the month and how many were hired in that month

I'm trying to list how many were hired in each month. I'm not sure where I went wrong but I get 23 rows and get duplicate months.
SELECT DISTINCT COUNT(E.BusinessEntityID) AS NumberOfEmployees,
CASE
WHEN DATEPART(MONTH,E.HireDate) = 1 THEN 'Janurary'
WHEN DATEPART(MONTH,E.HireDate) = 2 THEN 'Feburary'
WHEN DATEPART(MONTH,E.HireDate) = 3 THEN 'March'
WHEN DATEPART(MONTH,E.HireDate) = 4 THEN 'April'
WHEN DATEPART(MONTH,E.HireDate) = 5 THEN 'May'
WHEN DATEPART(MONTH,E.HireDate) = 6 THEN 'June'
WHEN DATEPART(MONTH,E.HireDate) = 7 THEN 'July'
WHEN DATEPART(MONTH,E.HireDate) = 8 THEN 'August'
WHEN DATEPART(MONTH,E.HireDate) = 9 THEN 'September'
WHEN DATEPART(MONTH,E.HireDate) = 10 THEN 'October'
WHEN DATEPART(MONTH,E.HireDate) = 11 THEN 'November'
WHEN DATEPART(MONTH,E.HireDate) = 12 THEN 'December'
ELSE 'Unknown'
END AS Month
FROM HumanResources.Employee AS E
GROUP BY
E.HireDate;
use DATENAME function for month name and group by for month-wise count
SELECT COUNT(E.BusinessEntityID) AS NumberOfEmployees,
DATENAME(mm,E.HireDate) from t
group by DATENAME(mm,E.HireDate)
But I'm not sure you need year or not ,cause if you need year then it will also come to selection and group by
have you tried to_char(e.HireDate, month) as hire_month
select count(e.businessentityid) empcount, to_char(e.HireDate,'Month') monthname from employee e group by to_char(e.HireDate,'Month')
order by to_date( monthname, 'Month' )

SQL: Add Column from table to another

I have two tables that count records and displays the count by month.
I want to show both counts on the same table, but I'm not sure how to combine them.
I just want to add the "IM Count" column to the first table, to the right of "CR Count."
Below is the code for the first table. The second table is similar code, but it draws its data from another table. My main issue is that I don't have a column that matches between tables. (The Year and Month columns technically do match, but only because they are both counts per month.)
Use sm
select
year(planned_start) Year,
Case
When month(planned_start) = 1 then 'January'
When month(planned_start) = 2 then 'February'
When month(planned_start) = 3 then 'March'
When month(planned_start) = 4 then 'April'
When month(planned_start) = 5 then 'May'
When month(planned_start) = 6 then 'June'
When month(planned_start) = 7 then 'July'
When month(planned_start) = 8 then 'August'
When month(planned_start) = 9 then 'September'
When month(planned_start) = 10 then 'October'
When month(planned_start) = 11 then 'November'
When month(planned_start) = 12 then 'December'
end as Month,
count(*) 'CR Count'
from dbo.cm3rm1
where planned_start between dateadd(Year,-1,getdate()) and getdate()
and
category !='OAS Normal'
group by year(planned_start), month(planned_start)
order by year(planned_start), month(planned_start)
Well, for the "idea" :
SELECT tmp_CR.Year,
Case
When tmp_CR.month = 1 then 'January'
When tmp_CR.month = 2 then 'February'
When tmp_CR.month = 3 then 'March'
When tmp_CR.month = 4 then 'April'
When tmp_CR.month = 5 then 'May'
When tmp_CR.month = 6 then 'June'
When tmp_CR.month = 7 then 'July'
When tmp_CR.month = 8 then 'August'
When tmp_CR.month = 9 then 'September'
When tmp_CR.month = 10 then 'October'
When tmp_CR.month = 11 then 'November'
When tmp_CR.month = 12 then 'December' end as month,
tmp_CR.CR_Count,
tmp_IM.IM_Count
FROM (
select
year(planned_start) Year,
month(planned_start) as Month,
count(*) as CR_Count
from dbo.TABLE_1
where planned_start between dateadd(Year,-1,getdate()) and getdate()
and
category !='OAS Normal'
group by year(planned_start), month(planned_start)
) tmp_CR
INNER JOIN (
select
year(planned_start) Year,
month(planned_start) as Month, as Month,
count(*) as IM_Count
from dbo.TABLE_2
where planned_start between dateadd(Year,-1,getdate()) and getdate()
and
category !='OAS Normal'
group by year(planned_start), month(planned_start)
) tmp_IM ON tmp_CR.year = tmp_IM.year and tmp_CR.month = tmp_IM.month
ORDER BY tmp_CR.Year, tmp_CR.month

converting number to string with specified order in sql

I need the records order by month on school year, ex
August
September
October
November
December
January
February
March
April
May
June
To get that order i found this order by but is returning the records with numbers and i need the name of the month. How can i get the name of the month without affecting the order?
SELECT CASE
When G.MO = 8 then 'August'
When G.MO = 9 then 'September'
When G.MO = 10 then 'October'
When G.MO = 11 then 'November'
When G.MO = 12 then 'December'
When G.MO = 1 then 'January'
When G.MO = 2 then 'February'
When G.MO = 3 then 'March'
When G.MO = 4 then 'April'
When G.MO = 5 then 'May'
When G.MO = 6 then 'June'
When G.MO = 7 then 'July'
end as [Month]
,...
from TABLE
group by [Month]
order by convert(nvarchar,(CASE WHEN CAST(LEFT(G.MO, 2) AS int) >= 8 THEN CAST(LEFT(G.MO, 2) AS int) - 8 ELSE CAST(LEFT(G.MO, 2) AS int) + 4 END
Usual way to rotate a sequence is mod function.
ordre by (g.mod+4)%12
--(8+4)%12=0
--(9+4)%12=1
--...
--(7+4)%12=11
Just use a simple comparison to put the months after august first:
order by (case when g.mo >= 8 then 1 else 2 end),
g.mo
Also, you can simplify the naming by doing:
select datename(month, cast(replace('2000-#mon-01', '#mon', g.mo) as date))
The cast() is actually not necessary, but I much prefer explicit conversion to implicit conversion.

SQL MULTIPLE COUNTS, GROUP BY MONTHS

I have table:
STATUS DEFECT DATE
CLOSED IP 01.01.2012
CLOSED TV 03.03.2012
CLOSED ADSL 05.05.2012
CLOSED ADSL 11.01.2012
CLOSED TV 15.01.2012
NEW TV
NEW TV
I want to group this by months with count for each specific DEFECT. Status which is considered is CLOSED
Resulting table I would like to be:
MONTH TV ADSL IP
January 1 1 1
March 1 0 0
May 0 1 0
I am using db2 database so the part for displaying months which works is:
select case month(timestamp_iso(DATE))
when 1 then 'January'
when 2 then 'February'
when 3 then 'March'
when 4 then 'April'
when 5 then 'May'
when 6 then 'Jun'
when 7 then 'July'
when 8 then 'August'
when 9 then 'September'
when 10 then 'October'
when 11 then 'November'
when 12 then 'December'
end as Month
from TABLE
where STATUS='CLOSED'
group by month(timestamp_iso(DATE))
order by month(timestamp_iso(DATE))
So I just need to add this part for counting.Thanks
You might count defect categories using another case statement that would restrict count to defects in one category:
select case month(timestamp_iso(DATE))
when 1 then 'January'
when 2 then 'February'
when 3 then 'March'
when 4 then 'April'
when 5 then 'May'
when 6 then 'Jun'
when 7 then 'July'
when 8 then 'August'
when 9 then 'September'
when 10 then 'October'
when 11 then 'November'
when 12 then 'December'
end as Month,
count (case when defect = 'TV' then 1 end) TV,
count (case when defect = 'ADSL' then 1 end) ADSL,
count (case when defect = 'IP' then 1 end) IP
from TABLE
where STATUS='CLOSED'
group by month(timestamp_iso(DATE))
order by month(timestamp_iso(DATE))