Related
I want to replace the 1st and 2nd parameter in DATEADD function depending on the result of cteTable. Is this something possible? Do I need to create a parameter? Thank you in advance. Happy new year!
;with cteDP as(
SELECT CODE, MEASSUREMENT, DEFEXPIRYDATEDAYS,
CASE WHEN MEASSUREMENT = 0 THEN 'DAY'
WHEN MEASSUREMENT = 1 THEN 'WEEK'
WHEN MEASSUREMENT = 2 THEN 'MONTH'
WHEN MEASSUREMENT = 3 THEN 'YEARS'
ELSE 'SECOND'
END AS MEASUREMENTINWORD
FROM PW001C06 C06 WHERE CODE IN ('ADC','MMC')
)
select * from cteDP
UPDATE P05
SET P05.EXPIRYDATE = CASE
WHEN P05.CODE = 'ADC' THEN DATEADD('cteDocProperties.MEASUREMENTINWORD', #2nd parameter, EXPIRYDATE)
-- WHEN P05.CODE = 'MMC' THEN DATEADD(YEAR, 4, EXPIRYDATE)
-- WHEN P05.CODE = 'LD00' THEN DATEADD(YEAR, 3, EXPIRYDATE)
ELSE EXPIRYDATE
END
FROM PW001P05 P05
INNER JOIN cteDP
ON cteDP.CODE = P05.CODE
WHERE P05.PIN = 1
AND P05.CODE IN ('ADC','MMC')
You can directly JOIN to the table expression:
UPDATE P05
SET
P05.EXPIRYDATE = CASE
WHEN P05.CODE = 'ADC' THEN
CASE WHEN MEASUREMENTINWORD = 'SECOND'
THEN DATEADD(SECOND, MEASSUREMENT, EXPIRYDATE)
CASE WHEN MEASUREMENTINWORD = 'DAY'
THEN DATEADD(DAY, MEASSUREMENT, EXPIRYDATE)
CASE WHEN MEASUREMENTINWORD = 'MONTH'
THEN DATEADD(MONTH, MEASSUREMENT, EXPIRYDATE)
CASE WHEN MEASUREMENTINWORD = 'YEAR'
THEN DATEADD(YEAR, MEASSUREMENT, EXPIRYDATE)
ELSE EXPIRYDATE
END
FROM PW001P05 P05
INNER JOIN
(
SELECT
CODE
, MEASSUREMENT
, DEFEXPIRYDATEDAYS
, CASE
WHEN MEASSUREMENT = 0 THEN 'DAY'
WHEN MEASSUREMENT = 1 THEN 'WEEK'
WHEN MEASSUREMENT = 2 THEN 'MONTH'
WHEN MEASSUREMENT = 3 THEN 'YEARS'
ELSE 'SECOND'
END AS MEASUREMENTINWORD
FROM PW001C06 C06 WHERE CODE IN ('ADC','MMC')
) cteDP
ON cteDP.CODE = P05.CODE
WHERE P05.PIN = 1
AND P05.CODE IN ('ADC','MMC')
I've a set of 6 Viewsthat i use to gather the data i need for an excel report.
View 1
SELECT *, MONTH(DataEmissione) AS Mese_Emissione, YEAR(DataEmissione) AS Anno_Emissione, CASE WHEN StatoEmissione = 'R' THEN - ImpVetComp WHEN StatoBiglietto = 'VR' THEN ImpVetComp * 0 WHEN StatoEmissione = 'NC' THEN ImpVetComp * 0 WHEN StatoEmissione = 'AN' THEN ImpVetComp * 0 ELSE ImpVetComp END AS Fare, DATENAME(month, DataEmissione) AS Issue_Month_Name, DATEPART(quarter, DataEmissione) AS Quarter, YEAR(DATEADD(month, - 3, DataEmissione)) AS AnnoFiscale, CASE WHEN MONTH(DataEmissione) < 4 THEN MONTH(DataEmissione) + 9 ELSE MONTH(DataEmissione) - 3 END AS Fiscal_Month, REPLACE(REPLACE(Destinazione, ' ', ''), '-', '') AS Destinazione_Normalizzata
FROM Gabbiano.dbo.PRAB0000
WHERE (DataEmissione >= DATEADD(year, - 3, GETDATE()))
UNION ALL
SELECT *, MONTH(DataEmissione) AS Mese_Emissione, YEAR(DataEmissione) AS Anno_Emissione, CASE WHEN StatoEmissione = 'R' THEN - ImpVetComp WHEN StatoBiglietto = 'VR' THEN ImpVetComp * 0 WHEN StatoEmissione = 'NC' THEN ImpVetComp * 0 WHEN StatoEmissione = 'AN' THEN ImpVetComp * 0 ELSE ImpVetComp END AS Fare, DATENAME(month, DataEmissione) AS Issue_Month_Name, DATEPART(quarter, DataEmissione) AS Quarter, YEAR(DATEADD(month, - 3, DataEmissione)) AS AnnoFiscale, CASE WHEN MONTH(DataEmissione) < 4 THEN MONTH(DataEmissione) + 9 ELSE MONTH(DataEmissione) - 3 END AS Fiscal_Month, REPLACE(REPLACE(Destinazione, ' ', ''), '-', '') AS Destinazione_Normalizzata
FROM Istantravel.dbo.PRAB0000
WHERE (DataEmissione >= DATEADD(year, - 3, GETDATE()))
UNION ALL
SELECT *, MONTH(DataEmissione) AS Mese_Emissione, YEAR(DataEmissione) AS Anno_Emissione, CASE WHEN StatoEmissione = 'R' THEN - ImpVetComp WHEN StatoBiglietto = 'VR' THEN ImpVetComp * 0 WHEN StatoEmissione = 'NC' THEN ImpVetComp * 0 WHEN StatoEmissione = 'AN' THEN ImpVetComp * 0 ELSE ImpVetComp END AS Fare, DATENAME(month, DataEmissione) AS Issue_Month_Name, DATEPART(quarter, DataEmissione) AS Quarter, YEAR(DATEADD(month, - 3, DataEmissione)) AS AnnoFiscale, CASE WHEN MONTH(DataEmissione) < 4 THEN MONTH(DataEmissione) + 9 ELSE MONTH(DataEmissione) - 3 END AS Fiscal_Month, REPLACE(REPLACE(Destinazione, ' ', ''), '-', '') AS Destinazione_Normalizzata
FROM Globmaster.dbo.PRAB0000
WHERE (DataEmissione >= DATEADD(year, - 3, GETDATE()))
Views 2
SELECT biglietti.Agenzia, biglietti.CodiceCompagnia, biglietti.StatoBiglietto, biglietti.StatoEmissione, biglietti.CodicePersonale, biglietti.TipoTratta, biglietti.ClasseVettore, biglietti.MastroCliente, biglietti.SottocCliente,
biglietti.DataEmissione, biglietti.DataVariazione, biglietti.DataPartenza, biglietti.ImponibileVettore, biglietti.TasseAerop, biglietti.ImpCorrispettivi, biglietti.CorrispettiviAdd, biglietti.ScontoCommissioni,
biglietti.NumeroPax, biglietti.Destinazione, biglietti.ImpVetComp, biglietti.ImportoComm, biglietti.ImportoCommPass, biglietti.NumeroBiglietto, biglietti.NomePasseggero, biglietti.TasseYQ, biglietti.TasseYR,
biglietti.CodicePnr, biglietti.Mese_Emissione, biglietti.Anno_Emissione, biglietti.Fare, biglietti.Nome_Mese_Emissione, biglietti.Quarter, biglietti.Prop, biglietti.AnnoFiscale,
compagnie.Descrizione AS NomeCompagnia, compagnie.Categoria, compagnie.Alleanza,
CASE WHEN biglietti.Destinazione_Normalizzata = 'VOID' THEN 'VOID' WHEN biglietti.Destinazione_Normalizzata = 'CNJ' THEN 'CAMBI' WHEN biglietti.Destinazione_Normalizzata = 'conj' THEN 'CONGIUNZIONI'
WHEN datalength(biglietti.Destinazione_Normalizzata)
= 0 THEN 'NON INDICATA' WHEN biglietti.Destinazione_Normalizzata LIKE 'OR.ISSUE%' THEN 'RIEMISSIONI' ELSE SUBSTRING(biglietti.Destinazione_Normalizzata, 1, 3) END AS Partenza,
CASE WHEN biglietti.Destinazione_Normalizzata = 'VOID' THEN 'VOID' WHEN biglietti.Destinazione_Normalizzata = 'CNJ' THEN 'CAMBI' WHEN biglietti.Destinazione_Normalizzata = 'conj' THEN 'CONGIUNZIONI'
WHEN datalength(biglietti.Destinazione_Normalizzata)
= 0 THEN 'NON INDICATA' WHEN biglietti.Destinazione_Normalizzata LIKE 'OR.ISSUE%' THEN 'RIEMISSIONI' ELSE SUBSTRING(biglietti.Destinazione_Normalizzata, LEN(biglietti.Destinazione_Normalizzata)
/ 2 + 1 - 3, 3) END AS Arrivo,
CASE WHEN biglietti.Destinazione_Normalizzata = 'VOID' THEN 'VOID' WHEN biglietti.Destinazione_Normalizzata = 'CNJ' THEN 'CAMBI' WHEN biglietti.Destinazione_Normalizzata = 'conj' THEN 'CONGIUNZIONI'
WHEN datalength(biglietti.Destinazione_Normalizzata)
= 0 THEN 'NON INDICATA' WHEN biglietti.Destinazione_Normalizzata LIKE 'OR.ISSUE%' THEN 'RIEMISSIONI' ELSE SUBSTRING(biglietti.Destinazione_Normalizzata, LEN(biglietti.Destinazione_Normalizzata)
/ 2 + 1, 3) END AS DestinazioneFinale,
CASE WHEN biglietti.Destinazione_Normalizzata = 'VOID' THEN 'VOID' WHEN biglietti.Destinazione_Normalizzata = 'CNJ' THEN 'CAMBI' WHEN biglietti.Destinazione_Normalizzata = 'conj' THEN 'CONGIUNZIONI'
WHEN datalength(biglietti.Destinazione_Normalizzata)
= 0 THEN 'NON INDICATA' WHEN biglietti.Destinazione_Normalizzata LIKE 'OR.ISSUE%' THEN 'RIEMISSIONI' ELSE RIGHT(biglietti.Destinazione_Normalizzata, 3) END AS Ritorno, biglietti.Mese_Fiscale,
biglietti.Destinazione_Normalizzata, biglietti.CodiceBiglietto
FROM dbo.Biglietti AS biglietti INNER JOIN
dbo.Compagnie AS compagnie ON biglietti.CodiceCompagnia = compagnie.Codice
WHERE (biglietti.DataEmissione >= DATEADD(year, - 3, GETDATE()))
And the other Views are similar.
Each is needed to performs calculations columns added with each Views.
However i reckon it's a bit inefficient.
How you would solve this?
I currently have this over long query that I'm trying to optimize. I looked at combining subquery's but I fail to get a count for every group in one query while having my constraints.
Is it possible to slim down the following query?
SELECT
val_year,
spg,
SUM(val_q1) val_q1,
SUM(val_q2) val_q2,
SUM(val_q3) val_q3,
SUM(val_q4) val_q4,
SUM(val_q1) + SUM(val_q2) + SUM(val_q3) + SUM(val_q4) val_total
FROM (
SELECT
val_year,
spg,
count_prods val_q1,
0 val_q2,
0 val_q3,
0 val_q4
FROM table_report
WHERE val_quarter = 'Q1'
UNION ALL
SELECT
val_year,
spg,
0 val_q1,
count_prods val_q2,
0 val_q3,
0 val_q4
FROM table_report
WHERE val_quarter = 'Q2'
UNION ALL
SELECT
val_year,
spg,
0 val_q1,
0 val_q2,
count_prods val_q3,
0 val_q4
FROM table_report
WHERE val_quarter = 'Q3'
UNION ALL
SELECT
val_year,
spg,
0 val_q1,
0 val_q2,
0 val_q3,
count_prods val_q4
FROM table_report
WHERE val_quarter = 'Q4'
)
table_report looks like this:
table_report:
val_year VARCHAR2(4)
spg VARCHAR2(256)
val_quarter VARCHAR2(2)
count_prods NUMBER(10)
You can replace the UNIONs with a single query using CASEs:
SELECT
val_year,
spg,
CASE WHEN val_quarter = 'Q1' THEN count_prods ELSE 0 END val_q1,
CASE WHEN val_quarter = 'Q2' THEN count_prods ELSE 0 END val_q2,
CASE WHEN val_quarter = 'Q3' THEN count_prods ELSE 0 END val_q3,
CASE WHEN val_quarter = 'Q4' THEN count_prods ELSE 0 END val_q4
FROM table_report
It looks like a PIVOT query, so you could do all in one
SELECT
val_year, spg,
coalesce(q1_total, 0) as q1,
coalesce(q2_total, 0) as q2,
coalesce(q3_total, 0) as q3,
coalesce(q4_total, 0) as q4,
coalesce(q1_total, 0) + coalesce(q2_total, 0) + coalesce(q3_total, 0) + coalesce(q4_total, 0) as total
FROM
(SELECT val_Year, spg, count_prod, val_quarter FROM test)
PIVOT (SUM(count_prod) as total
FOR (val_Quarter) IN ('Q1' AS q1, 'Q2' AS q2, 'Q3' AS q3, 'Q4' as q4))s ;
see SqlFiddle
As per my understanding, you can directly use group by query to get the sum count of each quarter -
like
select sum(count_prods) mysum, val_quarter as quarter group by quarter;
I'm not sure exactly what result you are trying to obtain, but perhaps you can try an approach like this:
SELECT
val_year,
spg,
val_q1,
val_q2,
val_q3,
val_q4,
val_q1 + val_q2 + val_q3 + val_q4 as val_total
FROM (
SELECT
val_year,
spg,
sum(case when val_quarter = 'Q1' then count_prods else 0 end) as val_q1,
sum(case when val_quarter = 'Q2' then count_prods else 0 end) as val_q2,
sum(case when val_quarter = 'Q3' then count_prods else 0 end) as val_q3,
sum(case when val_quarter = 'Q4' then count_prods else 0 end) as val_q4
FROM table_report
GROUP BY val_year, spg
)
Here it is in a no-data fiddle : http://sqlfiddle.com/#!4/9bf262/20
I am trying to find the best way to produce the following table in a report (rdlc) -
This is showing the teacher name on the far left column (pulled from a teachers table), then bookings from Monday to Friday (pulled from a bookingdays table). In the rows, an AM booking (BookingDuration: 0) will be in the top row, a PM booking (BookingDuration: 1) will be in the bottom row and finally a Full Day (BookingDuration: 2) will be in the top row but in upper case (dealt with in the RDLC).
The problem I am having is producing a table from SQL that is speedy and tidy. I believe I need to produce a PIVOT table, however I have tried many different methods.
There is also hourly bookings to consider (BookingDuration: 3). Each booking day has a start and end time. so if the start time is < 12:00 and the end time is > 12:00, full day. If start and end time < 12:00 then AM and finally if the end time and start time > 12:00, Full day. There may also be 2x AM hourly bookings (09:00- 10:00 and 10:30 - 11:30) but never more than 2. The same applies to PM. When this happens, the first AM will be in the top box and the second AM will be in the bottom box. I have been assured that 2x AM and 1x PM booking will never occur, so I have added validation in the create booking form to prevent this.
There is a row for every teacher. When a teacher has no bookings, the teacher will still be shown on the report but the values will be empty.
My table (BookingDays) is as follows -
ID - INT(PK)
BookingID - INT
BookingDate - date
DayText - VARCHAR(50)
StartTIme - decimal
EndTime - decimal
TeacherID int
BookingDuration - int
NoOfHours - decimal
TotalChargeAmount - decimal
TotalPayAmount - decimal
Status - int
There is also a teachers table containing Full name etc. This is joined to the BookingDays table by the TeacherID column.
Does anyone have any idea how I can go about doing this?
This is what I have tried so far (showing only Friday due to text limit). The problem with this one is I can't get the AM/PM hourly's showing if there is 2 -
with CTE_D as
(
SELECT DATEADD(ww, DATEDIFF(ww,0,CONVERT(date, #WeekStart, 103)), 0) as BookingDate
union all
select DATEADD(day, 1, BookingDate)
from CTE_D where BookingDate < DATEADD(ww, DATEDIFF(ww,0,CONVERT(date, #WeekStart, 103)), 6)
)
SELECT
t.Firstname,
t.Surname, t.Telephone, t.Mobile, t.Band, tb.Band, t.DefaultChargeRateDaily as 'TeacherDefaultChargeRateDaily', t.DefaultPayRateDaily as 'TeacherDefaultPayRateDaily', t.DefaultChargeRateAM, t.DefaultChargeRatePM, t.DefaultChargeRateDaily, t.DefaultPayRateAM, t.DefaultPayRatePM, t.DefaultPayRateDaily,
--------------------------------------------------------------------------------------------------------------------------
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 0 and s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayAM",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 0 and s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayAMTotalChargeAmount",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 0 and s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayAMTotalPayAmount",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 0 and s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayAMBandBookedAt",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 1 and s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayPM",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 1 and s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayPMTotalChargeAmount",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 1 and s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayPMTotalPayAmount",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 1 and s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayPMBandBookedAt",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 2 and s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayDaily",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 2 and s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayDailyTotalChargeAmount",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 2 and s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayDailyTotalPayAmount",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 2 and s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayDailyBandBookedAt",
COUNT(DISTINCT CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.ID END) as "FridayHourlyAMCount",
MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.ID ELSE NULL END) "FridayHourlyAM",
MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayHourlyAMSchoolName",
MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN REPLACE(REPLACE(LTRIM(RIGHT(CONVERT(VARCHAR, bd.StartTime, 100), 7)) + '-' + LTRIM(RIGHT(CONVERT(VARCHAR(20), bd.EndTime, 100), 7)), 'AM',''), 'PM', '') ELSE NULL END) "FridayHourlyAMTimes",
MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayHourlyAMTotalChargeAmount",
MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayHourlyAMTotalPayAmount",
MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayHourlyAMBandBookedAt",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.ID ELSE NULL END) "FridayHourlyAM2",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayHourlyAM2SchoolName",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN REPLACE(REPLACE(LTRIM(RIGHT(CONVERT(VARCHAR, bd.StartTime, 100), 7)) + '-' + LTRIM(RIGHT(CONVERT(VARCHAR(20), bd.EndTime, 100), 7)), 'AM',''), 'PM', '') ELSE NULL END) "FridayHourlyAM2Times",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayHourlyAM2TotalChargeAmount",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayHourlyAM2TotalPayAmount",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayHourlyAM2BandBookedAt",
COUNT(DISTINCT CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 and s.PrimarySchool = 1 THEN bd.ID END) as "FridayHourlyPMCount",
MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.ID ELSE NULL END) "FridayHourlyPM",
MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayHourlyPMSchoolName",
MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN REPLACE(REPLACE(LTRIM(RIGHT(CONVERT(VARCHAR, bd.StartTime, 100), 7)) + '-' + LTRIM(RIGHT(CONVERT(VARCHAR(20), bd.EndTime, 100), 7)), 'AM',''), 'PM', '') ELSE NULL END) "FridayHourlyPMTimes",
MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayHourlyPMTotalChargeAmount",
MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayHourlyPMTotalPayAmount",
MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayHourlyPMBandBookedAt",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.ID ELSE NULL END) "FridayHourlyPM2",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayHourlyPM2SchoolName",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN REPLACE(REPLACE(LTRIM(RIGHT(CONVERT(VARCHAR, bd.StartTime, 100), 7)) + '-' + LTRIM(RIGHT(CONVERT(VARCHAR(20), bd.EndTime, 100), 7)), 'AM',''), 'PM', '') ELSE NULL END) "FridayHourlyPM2Times",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayHourlyPM2TotalChargeAmount",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayHourlyPM2TotalPayAmount",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayHourlyPM2BandBookedAt",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and bd.NoOfHours >= 5.5 and s.PrimarySchool = 1 AND CONVERT(time(0), bd.EndTime) > CONVERT(time(0), '12:00:00') THEN bd.ID ELSE NULL END) "FridayHourlyFullDay",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and bd.NoOfHours >= 5.5 and s.PrimarySchool = 1 AND CONVERT(time(0), bd.EndTime) > CONVERT(time(0), '12:00:00') THEN s.SchoolName ELSE NULL END) "FridayHourlyFullDaySchoolName",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and bd.NoOfHours >= 5.5 and s.PrimarySchool = 1 AND CONVERT(time(0), bd.EndTime) > CONVERT(time(0), '12:00:00') THEN REPLACE(REPLACE(LTRIM(RIGHT(CONVERT(VARCHAR, bd.StartTime, 100), 7)) + '-' + LTRIM(RIGHT(CONVERT(VARCHAR(20), bd.EndTime, 100), 7)), 'AM',''), 'PM', '') ELSE NULL END) "FridayHourlyFullDayTimes",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and bd.NoOfHours >= 5.5 and s.PrimarySchool = 1 AND CONVERT(time(0), bd.EndTime) > CONVERT(time(0), '12:00:00') THEN bd.TotalChargeAmount ELSE NULL END) "FridayHourlyFullDayTotalChargeAmount",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and bd.NoOfHours >= 5.5 and s.PrimarySchool = 1 AND CONVERT(time(0), bd.EndTime) > CONVERT(time(0), '12:00:00') THEN bd.TotalPayAmount ELSE NULL END) "FridayHourlyFullDayTotalPayAmount",
MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and bd.NoOfHours >= 5.5 and s.PrimarySchool = 1 AND CONVERT(time(0), bd.EndTime) > CONVERT(time(0), '12:00:00') THEN bd.BandBookedAt ELSE NULL END) "FridayFullDayBandBookedAt",
COUNT(CASE WHEN s.PrimarySchool = 1 THEN bd.ID ELSE NULL END) "BookingCount",
--------------------------------------------------------------------------------------------------------------------------
t.Notes, t.DefaultChargeRateDaily, t.DefaultPayRateDaily
FROM Teachers t
cross join CTE_D d
inner join TeacherBands tb
on t.Band = tb.ID
left join BookingDays bd
on t.ID = bd.TeacherID and
bd.BookingDate = d.BookingDate and bd.BookingType = 0
left join BookingDurations bds
on bd.BookingDuration = bds.ID
left join BookingTypes bt
on bd.BookingType = bt.ID
left join Bookings b
on bd.BookingID = b.ID
left join Schools s
on b.School = s.ID and s.PrimarySchool = 1
WHERE Active = 0 and (bd.Status = 0 or bd.Status IS NULL) and (t.Status != 2) and t.PrimarySchool = 1
GROUP BY Firstname, Surname, t.Telephone, t.Mobile, t.Notes, t.Band, tb.Band, t.DefaultPayRateDaily, t.DefaultChargeRateDaily, t.DefaultChargeRateAM, t.DefaultChargeRatePM, t.DefaultChargeRateDaily, t.DefaultPayRateAM, t.DefaultPayRatePM, t.DefaultPayRateDaily
ORDER BY Surname, Firstname ASC
Hope this makes sense. I have tried to adapt a pivot table I have used previously, however to no avail. I can't seem to get the school name displayed when it outputs to the final columns.
WITH Bookings AS
( SELECT TeacherID,
[WeekDay] = DATENAME(WEEKDAY, BookingDate),
[0], [1], [2], [3],
[Status] = CASE
WHEN ([0] > 0 AND [1] > 0) THEN 'XXX'
WHEN [2] > 0 THEN 'XXX'
WHEN [0] > 0 THEN 'PM'
WHEN [1] > 0 THEN 'AM'
WHEN [3] > 0 AND StartTime <= CONVERT(TIME, '12:00:00') AND EndTime >= CONVERT(TIME, '12:00:00') THEN 'XXX'
WHEN [3] > 0 AND EndTime <= CONVERT(TIME, '12:00:00') THEN 'PM'
WHEN [3] > 0 AND StartTime >= CONVERT(TIME, '12:00:00') THEN 'AM'
END
FROM ( SELECT TeacherID,
BookingDate,
BookingDuration,
StartTime = CASE WHEN BookingDuration = 3 THEN CAST(MIN(StartTime) OVER(PARTITION BY TeacherID, BookingDate, BookingDuration) AS TIME) ELSE NULL END,
EndTime = CASE WHEN BookingDuration = 3 THEN CAST(MAX(EndTime) OVER(PARTITION BY TeacherID, BookingDate, BookingDuration) AS TIME) ELSE NULL END,
[x] = 1
FROM BookingDays bd
WHERE (Status = 0 OR Status IS NULL)
) BookingDays
PIVOT
( SUM(x)
FOR BookingDuration IN ([0], [1], [2], [3])
) pvt
WHERE BookingDate >= DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 0) AND BookingDate <= DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 6)
), PivotedBookings AS
( SELECT *
FROM Bookings
PIVOT
( MAX([Status])
FOR [WeekDay] IN ([MondayAM1], [MondayAM2], [MondayPM1], [MondayPM2], [MondayFullDay])
) pvt
)
SELECT ID,Firstname,Surname,Band,'£' + CONVERT(varchar(50),DefaultChargeRateDaily) + '/' + '£' + CONVERT(varchar(50), DefaultPayRateDaily) as 'BandRates',Telephone,Mobile,Teacher,TeacherAssistant,KeyStage,MAX(MondayAM1) MondayAM1,MAX(MondayAM2) MondayAM2,MAX(MondayPM1) MondayPM1,MAX(MondayPM2) MondayPM2,MAX(MondayFullDay) MondayFullDay, Notes
FROM (
SELECT t.ID,
t.Firstname,
t.Surname,
tb.Band,
t.DefaultChargeRateDaily,
t.DefaultPayRateDaily,
t.Telephone,
t.Mobile,
t.Teacher,
t.TeacherAssistant,
CASE WHEN t.Nursery > 0 THEN 'NUR' WHEN t.Reception > 0 THEN 'REC' WHEN t.Year1 > 0 THEN 'Y1' WHEN t.Year2 > 0 THEN 'Y2' WHEN t.Year3 > 0 THEN 'Y3' WHEN t.Year4 > 0 THEN 'Y4' WHEN t.Year5 > 0 THEN 'Y5' WHEN t.Year6 > 0 THEN 'Y6' WHEN t.Year7 > 0 THEN 'Y7' WHEN t.Year8 > 0 THEN 'Y8' WHEN t.Year9 > 0 THEN 'Y9' WHEN t.Year10 > 0 THEN 'Y10' WHEN t.Year11 > 0 THEN 'Y11' WHEN t.ALevel > 0 THEN 'ALevel' END + ' - ' + CASE WHEN t.ALevel > 0 THEN 'ALevel' WHEN t.Year11 > 0 THEN 'Y11' WHEN t.Year10 > 0 THEN 'Y10' WHEN t.Year9 > 0 THEN 'Y9' WHEN t.Year8 > 0 THEN 'Y8' WHEN t.Year7 > 0 THEN 'Y7' WHEN t.Year6 > 0 THEN 'Y6' WHEN t.Year5 > 0 THEN 'Y5' WHEN t.Year4 > 0 THEN 'Y4' WHEN t.Year3 > 0 THEN 'Y3' WHEN t.Year2 > 0 THEN 'Y2' WHEN t.Year1 > 0 THEN 'Y1' WHEN t.Reception > 0 THEN 'REC' WHEN t.Nursery > 0 THEN 'NUR' ELSE '' END as 'KeyStage',
MondayAM1 = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 0) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX' ELSE COALESCE(pb.MondayAM1, '') END,
MondayAM2 = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 0) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX' ELSE COALESCE(pb.MondayAM2, '') END,
MondayPM1 = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 0) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX' ELSE COALESCE(pb.MondayPM1, '') END,
MondayPM2 = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 0) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX' ELSE COALESCE(pb.MondayPM2, '') END,
MondayFullDay = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 0) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX' ELSE COALESCE(pb.MondayFullDay, '') END,
Notes
FROM Teachers t
LEFT JOIN PivotedBookings pb
ON pb.TeacherID = t.ID
LEFT JOIN TeacherBands tb
ON tb.ID = t.Band
LEFT JOIN AvailabilityNotes an
ON t.ID = an.TeacherID
WHERE t.Active = 0 and (t.Status = 1 or t.Status = 0) and t.PrimarySchool = 1
) T1
GROUP BY ID,Firstname,Surname,Telephone,Mobile,Teacher,TeacherAssistant,KeyStage,Notes,DefaultChargeRateDaily,DefaultPayRateDaily,Band
ORDER BY Surname,Firstname asc
I am trying to show oracle data in following format (need to produce in form of report- may be crystal report);
Table has following fields:
I am able to produce following results so far:
Query:
SELECT to_date('01/01/1999','dd/mm/yyyy') LOGDATE,
to_date('01/01/1999','dd/mm/yyyy') MONTH ,
''EMPCODE,
to_number('0') PROJECTCODE ,
''ACTIVITY,
to_number('0')TOTALTIME,
''ENTEREDBY,
''PROJECTDESC,
''SUB_SUBACT_SHORT_DESC,
B.*
FROM
(
select '1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31'
from dual
UNION ALL
select to_char(TO_DATE(:MONTH,'DD/MM/YYYY'),'DY'),
to_char(TO_DATE(:MONTH ,'DD/MM/YYYY') + 1,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+2,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+3,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+4,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+5,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+6,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+7,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+8,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+9,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+10,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+11,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+12,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+13,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+14,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+15,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+16,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+17,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+18,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+19,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+20,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+21,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+22,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+23,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+24,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+25,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+26,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+27,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+28,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+29,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+30,'DY')
from dual
) B
UNION ALL
SELECT LOGDATE,TRUNC(LOGDATE,'MM') as period,
EMPCODE,
PROJECTCODE,ACTIVITYCODE || '.' || SUBACTIVITY_CODE || '.' || SUB_SUBACTIVITY_CODE AS ACTIVITY,
TOTALTIME ,ENTEREDBY,
PROJECTDESC,
SUB_SUBACT_SHORT_DESC,
CASE WHEN to_char(logdate,'dd') = '01' THEN to_char(TOTALTIME) END d1,
CASE WHEN to_char(logdate,'dd') = '02' THEN to_char(TOTALTIME) END d2,
CASE WHEN to_char(logdate,'dd') = '03' THEN to_char(TOTALTIME) END d3,
CASE WHEN to_char(logdate,'dd') = '04' THEN TO_CHAR(TOTALTIME) END d4,
CASE WHEN to_char(logdate,'dd') = '05' THEN TO_CHAR(TOTALTIME) END d5,
CASE WHEN to_char(logdate,'dd') = '06' THEN TO_CHAR(TOTALTIME) END d6,
CASE WHEN to_char(logdate,'dd') = '07' THEN TO_CHAR(TOTALTIME) END d7,
CASE WHEN to_char(logdate,'dd') = '08' THEN TO_CHAR(TOTALTIME) END d8,
CASE WHEN to_char(logdate,'dd') = '09' THEN TO_CHAR(TOTALTIME) END d9,
CASE WHEN to_char(logdate,'dd') = '10' THEN TO_CHAR(TOTALTIME) END d10,
CASE WHEN to_char(logdate,'dd') = '11' THEN TO_CHAR(TOTALTIME) END d11,
CASE WHEN to_char(logdate,'dd') = '12' THEN TO_CHAR(TOTALTIME) END d12,
CASE WHEN to_char(logdate,'dd') = '13' THEN TO_CHAR(TOTALTIME) END d13,
CASE WHEN to_char(logdate,'dd') = '14' THEN TO_CHAR(TOTALTIME) END d14,
CASE WHEN to_char(logdate,'dd') = '15' THEN TO_CHAR(TOTALTIME) END d15,
CASE WHEN to_char(logdate,'dd') = '16' THEN TO_CHAR(TOTALTIME) END d16,
CASE WHEN to_char(logdate,'dd') = '17' THEN TO_CHAR(TOTALTIME) END d17,
CASE WHEN to_char(logdate,'dd') = '18' THEN TO_CHAR(TOTALTIME) END d18,
CASE WHEN to_char(logdate,'dd') = '19' THEN TO_CHAR(TOTALTIME) END d19,
CASE WHEN to_char(logdate,'dd') = '20' THEN TO_CHAR(TOTALTIME) END d20,
CASE WHEN to_char(logdate,'dd') = '21' THEN TO_CHAR(TOTALTIME) END d21,
CASE WHEN to_char(logdate,'dd') = '22' THEN TO_CHAR(TOTALTIME) END d22,
CASE WHEN to_char(logdate,'dd') = '23' THEN TO_CHAR(TOTALTIME) END d23,
CASE WHEN to_char(logdate,'dd') = '24' THEN TO_CHAR(TOTALTIME) END d24,
CASE WHEN to_char(logdate,'dd') = '25' THEN TO_CHAR(TOTALTIME) END d25,
CASE WHEN to_char(logdate,'dd') = '26' THEN TO_CHAR(TOTALTIME) END d26,
CASE WHEN to_char(logdate,'dd') = '27' THEN TO_CHAR(TOTALTIME) END d27,
CASE WHEN to_char(logdate,'dd') = '28' THEN TO_CHAR(TOTALTIME) END d28,
CASE WHEN to_char(logdate,'dd') = '29' THEN TO_CHAR(TOTALTIME) END d29,
CASE WHEN to_char(logdate,'dd') = '30' THEN TO_CHAR(TOTALTIME) END d30,
CASE WHEN to_char(logdate,'dd') = '31' THEN TO_CHAR(TOTALTIME) END d31
FROM TIMESHEET
WHERE PROJECTCODE = 2274
AND LOGDATE >= TO_DATE('01/08/2011','dd/mm/yyyy')
AND LOGDATE <= TO_DATE('31/08/2011','dd/mm/yyyy')
AND TOTALTIME>0
>Is this possible to group the results by Activity?
So 'pojectx' will be activity 1.11.1
Something like this should do the trick. Aside from Oracle's wonky date functions, it's pretty standard SQL that should work on most modern SQL implementation (so long as they support subqueries as virtual tables in the from clause):
select p.name as project_name ,
a.name as activity_name ,
t.*
from ( select extract(year,t.year) as year ,
extract(month,t.month) as month ,
t.project as project_id ,
t.activity as activity_id ,
sum( case extract(day,t.month) when 1 then t.time end ) as time_01 , -- summarize time by each day of the month
sum( case extract(day,t.month) when 2 then t.time end ) as time_02 , -- days with no data will have NULL
sum( case extract(day,t.month) when 3 then t.time end ) as time_03 , -- |
... -- |
sum( case extract(day,t.month) when 28 then t.time end ) as time_28 , -- |
sum( case extract(day,t.month) when 29 then t.time end ) as time_29 , -- |
sum( case extract(day,t.month) when 30 then t.time end ) as time_30 , -- |
sum( case extract(day,t.month) when 31 then t.time end ) as time_31 , -- >
sum( t.activity ) as total_activity ,
sum( t.time ) as total_time ,
from some_table t
where extract(year,t.month) = 2012
group by extract(year , t.month ) ,
extract(month , t.month ) ,
t.project ,
t.activity
) summary
join project p on p.project_id = summary.project_id
join activity a on a.activity_id = summary.activity_id
order by t.year ,
t.month ,
p.name ,
a.name
This covers all years, all months, all projects and all activities: add the appropriate conditions in the where clause of the virtual table to filter the source data.
After doing some research and with the some guidence from old posts on this link i have got the results i was looking for.
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:124812348063
QUERY:
select
'DATE'ACTIVITY,
'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20',
'21','22','23','24','25','26','27','28','29','30','31' ,
0 AS TOTAL
from dual
UNION ALL
select
'DAYS'ACTIVITY,
to_char(TO_DATE(:MONTH,'DD/MM/YYYY'),'DY'),
to_char(TO_DATE(:MONTH ,'DD/MM/YYYY') + 1,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+2,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+3,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+4,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+5,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+6,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+7,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+8,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+9,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+10,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+11,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+12,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+13,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+14,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+15,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+16,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+17,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+18,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+19,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+20,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+21,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+22,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+23,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+24,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+25,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+26,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+27,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+28,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+29,'DY'),
to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+30,'DY'),
0 TOTAL
FROM DUAL
UNION ALL
SELECT ACTIVITY,
MAX(DECODE (DT, '01', HRS, NULL)) c1,
MAX(DECODE (DT, '02', HRS, NULL)) c2,
MAX(DECODE (DT, '03', HRS, NULL)) c3,
MAX(DECODE (DT, '04', HRS, NULL)) c4,
MAX(DECODE (DT, '05', HRS, NULL)) c5,
MAX(DECODE (DT, '06', HRS, NULL)) c6,
MAX(DECODE (DT, '07', HRS, NULL)) c7,
MAX(DECODE (DT, '08', HRS, NULL)) c8,
MAX(DECODE (DT, '09', HRS, NULL)) c9,
MAX(DECODE (DT, '10', HRS, NULL)) c10,
MAX(DECODE (DT, '11', HRS, NULL)) c11,
MAX(DECODE (DT, '12', HRS, NULL)) c12,
MAX(DECODE (DT, '13', HRS, NULL)) c13,
MAX(DECODE (DT, '14', HRS, NULL)) c14,
MAX(DECODE (DT, '15', HRS, NULL)) c15,
MAX(DECODE (DT, '16', HRS, NULL)) c16,
MAX(DECODE (DT, '17', HRS, NULL)) c17,
MAX(DECODE (DT, '18', HRS, NULL)) c18,
MAX(DECODE (DT, '19', HRS, NULL)) c19,
MAX(DECODE (DT, '20', HRS, NULL)) c20,
MAX(DECODE (DT, '21', HRS, NULL)) c21,
MAX(DECODE (DT, '22', HRS, NULL)) c22,
MAX(DECODE (DT, '23', HRS, NULL)) c23,
MAX(DECODE (DT, '24', HRS, NULL)) c24,
MAX(DECODE (DT, '25', HRS, NULL)) c25,
MAX(DECODE (DT, '26', HRS, NULL)) c26,
MAX(DECODE (DT, '27', HRS, NULL)) c27,
MAX(DECODE (DT, '28', HRS, NULL)) c28,
MAX(DECODE (DT, '29', HRS, NULL)) c29,
MAX(DECODE (DT, '30', HRS, NULL)) c30,
MAX(DECODE (DT, '31', HRS, NULL)) c31,
nvl(sum(hrs),0) total
FROM
(
SELECT * FROM
(
SELECT
to_char(LOGDATE,'dd') as DT,TO_CHAR(TOTALTIME) AS HRS,
ACTIVITYCODE || '.' || SUBACTIVITY_CODE || '.' || SUB_SUBACTIVITY_CODE AS ACTIVITY
FROM TIMSHEET
WHERE PROJECTCODE = 2274
AND LOGDATE >= TO_DATE('01/08/2011','dd/mm/yyyy')
AND LOGDATE <= TO_DATE('31/08/2011','dd/mm/yyyy')
and totaltime>0
)
GROUP BY ACTIVITY , DT,HRS
)
GROUP BY ACTIVITY
RESULTS: