Related
I am trying to join these two tables, or use a sub-query to achieve the results I need.
As of now I have the first query pulling hourly totals with a overall total between MIN(time) and MAX(time). I need to join the second query to get the total scans from MIN(time) and MAX(time)
Any ideas of where I could be going wrong?
select
UPPER(t.operator) as Operator,
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 6 THEN 1 ELSE 0 END)) ELSE NULL END AS '6AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 7 THEN 1 ELSE 0 END)) ELSE NULL END AS '7AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 8 THEN 1 ELSE 0 END)) ELSE NULL END AS '8AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 9 THEN 1 ELSE 0 END)) ELSE NULL END AS '9AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 10 THEN 1 ELSE 0 END)) ELSE NULL END AS '10AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 11 THEN 1 ELSE 0 END)) ELSE NULL END AS '11AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 12 THEN 1 ELSE 0 END)) ELSE NULL END AS '12PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 13 THEN 1 ELSE 0 END)) ELSE NULL END AS '1PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 14 THEN 1 ELSE 0 END)) ELSE NULL END AS '2PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 15 THEN 1 ELSE 0 END)) ELSE NULL END AS '3PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 16 THEN 1 ELSE 0 END)) ELSE NULL END AS '4PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 17 THEN 1 ELSE 0 END)) ELSE NULL END AS '5PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (COUNT(CASE WHEN DATEPART(HOUR,t.time) > 17 THEN t.eventtype ELSE NULL END)) ELSE NULL END AS '6+PM',
SUM(CASE WHEN t.eventtype = 'Replenishment Complete' THEN 1 ELSE 0 END) as TotalCanisters
from mck_hvs.replevent t
where
CAST(t.time as DATE) = CAST(GETDATE() as DATE)
and t.stationtype IN ('T', 'S')
and t.eventtype = 'Replenishment Complete'
group by
t.operator,
t.eventtype
Second query:
select
UPPER(o.operator) as Operator,
SUM(CASE WHEN o.eventtype = 'GoodScan' THEN 1 ELSE 0 END) as TotalScans,
convert( varchar(19), MIN(o.time), 8) as LogonTime,
DATEDIFF(MINUTE, MIN(o.time), MAX(o.time)) as TotalMinLogon
from mck_hvs.replevent o
where
CAST(o.time as DATE) = CAST(GETDATE() as DATE)
and o.eventtype IN ( 'Replenishment Complete', 'GoodScan' )
group by
o.operator
order by o.operator
Something like the below should get you headed to a solution. I removed the group on t.eventtype and removed from the case statements because the where clause already limited the results for you.
select
op.Operator,
op.TotalScans,
op.LogonTime,
op.TotalMinLogon,
b.[6AM],
b.[7AM],
b.[8AM],
b.[9AM],
b.[10AM],
b.[11AM],
b.[12PM],
b.[1PM],
b.[2PM],
b.[3PM],
b.[4PM],
b.[5PM],
b.[6+PM]
from
(
select
UPPER(o.operator) as Operator,
SUM(CASE WHEN o.eventtype = 'GoodScan' THEN 1 ELSE 0 END) as TotalScans,
convert( varchar(19), MIN(o.time), 8) as LogonTime,
DATEDIFF(MINUTE, MIN(o.time), MAX(o.time)) as TotalMinLogon
from mck_hvs.replevent o
where
--will use o.time index now:
o.time >= CAST(GETDATE() as DATE)
and o.time < CAST(dateadd(Day,1,GETDATE()) as DATE)
and o.eventtype IN ( 'Replenishment Complete', 'GoodScan' )
group by
o.operator
) as op
left join
(
select
UPPER(t.operator) as Operator,
SUM(CASE WHEN DATEPART(HOUR,t.time) = 6 THEN 1 ELSE 0 END) AS '6AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 7 THEN 1 ELSE 0 END) AS '7AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 8 THEN 1 ELSE 0 END) AS '8AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 9 THEN 1 ELSE 0 END) AS '9AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 10 THEN 1 ELSE 0 END) AS '10AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 11 THEN 1 ELSE 0 END) AS '11AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 12 THEN 1 ELSE 0 END) AS '12PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 13 THEN 1 ELSE 0 END) AS '1PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 14 THEN 1 ELSE 0 END) AS '2PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 15 THEN 1 ELSE 0 END) AS '3PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 16 THEN 1 ELSE 0 END) AS '4PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 17 THEN 1 ELSE 0 END) AS '5PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) > 17 THEN 1 ELSE 0 END) AS '6+PM',
SUM(CASE WHEN t.eventtype = 'Replenishment Complete' THEN 1 ELSE 0 END) as TotalCanisters
from mck_hvs.replevent t
where
--will use o.time index now:
o.time >= CAST(GETDATE() as DATE)
and o.time < CAST(dateadd(Day,1,GETDATE()) as DATE)
and t.stationtype IN ('T', 'S')
and t.eventtype = 'Replenishment Complete'
group by
t.operator
) as b
on b.Operator = op.Operator
SELECT l.* ,
r.TotalScans ,
r.LogonTime ,
r.TotalMinLogon
FROM ( SELECT UPPER(t.operator) AS Operator ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 6
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '6AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 7
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '7AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 8
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '8AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 9
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '9AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 10
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '10AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 11
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '11AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 12
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '12PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 13
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '1PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 14
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '2PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 15
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '3PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 16
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '4PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 17
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '5PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( COUNT(CASE WHEN DATEPART(HOUR, t.time) > 17
THEN t.eventtype
ELSE NULL
END) )
ELSE NULL
END AS '6+PM' ,
SUM(CASE WHEN t.eventtype = 'Replenishment Complete'
THEN 1
ELSE 0
END) AS TotalCanisters
FROM #replevent t
WHERE CAST(t.time AS DATE) = CAST(GETDATE() AS DATE)
AND t.stationtype IN ( 'T', 'S' )
AND t.eventtype = 'Replenishment Complete'
GROUP BY t.operator ,
t.eventtype
) AS l
INNER JOIN ( SELECT UPPER(o.operator) AS Operator_r ,
SUM(CASE WHEN o.eventtype = 'GoodScan' THEN 1
ELSE 0
END) AS TotalScans ,
CONVERT(VARCHAR(19), MIN(o.time), 8) AS LogonTime ,
DATEDIFF(SECOND, MIN(o.time), MAX(o.time)) AS TotalMinLogon
FROM #replevent o
WHERE CAST(o.time AS DATE) = CAST(GETDATE() AS DATE)
AND o.eventtype IN ( 'Replenishment Complete',
'GoodScan' )
GROUP BY o.operator
) AS r ON l.Operator = r.Operator_r
ORDER BY l.Operator;
I am trying to get a count of activities by month and group them by a department name. The report is based on a date parameter that I input to the query "#rptDate". What I have below works, but I'm guessing there is a better way of doing the same thing and hoping someone can shed some light.
Select Count(*) As Month1Count, 0 As Month2Count, 0 As Month3Count,
0 As Month4Count, 0 as Month5Count, 0 as Month6Count, 0 as Month7Count,
0 as Month8Count, 0 as Month9COunt, 0 as Month10Count, 0 as Month11Count,
0 as Month12Count, DepartmentName,ActivityDescription
from reports.WorkOrders
WHERE Month(BeginDate) = Month(#rptDate) and Year(BeginDate) = Year(#rptDate)
Group By DepartmentName, ActivityDescription
UNION
Select 0 As Month1Count, COUNT(*) As Month2Count, 0 as Month3Count,
0 as Month4Count, 0 as Month5Count, 0 as Month6Count, 0 as Month7Count,
0 as Month8Count, 0 as Month9COunt, 0 as Month10Count, 0 as Month11Count,
0 as Month12Count, DepartmentName,ActivityDescription
from reports.WorkOrders
WHERE Month(BeginDate) = Month(#rptDate) + 1 and Year(BeginDate) = Year(DateAdd(month,1,#rptDate))
Group By DepartmentName, ActivityDescription
UNION
Select 0 As Month1Count, 0 As Month2Count, Count(*) as Month3Count,
0 as Month4Count, 0 as Month5Count, 0 as Month6Count, 0 as Month7Count,
0 as Month8Count, 0 as Month9COunt, 0 as Month10Count, 0 as Month11Count,
0 as Month12Count, DepartmentName, ActivityDescription
from reports.WorkOrders
WHERE Month(BeginDate) = Month(#rptDate) + 2 and Year(BeginDate) = Year(DateAdd(month,2,#rptDate))
Group By DepartmentName, ActivityDescription
I haven't tested the following, so it's possible there are some typos or minor errors, but I think the following is what you want:
SELECT
sum(case when Month(BeginDate) = Month(dateadd(month, 0, #rptDate)) then 1 else 0 end) as Month1Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 1, #rptDate)) then 1 else 0 end) as Month2Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 2, #rptDate)) then 1 else 0 end) as Month3Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 3, #rptDate)) then 1 else 0 end) as Month4Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 4, #rptDate)) then 1 else 0 end) as Month5Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 5, #rptDate)) then 1 else 0 end) as Month6Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 6, #rptDate)) then 1 else 0 end) as Month7Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 7, #rptDate)) then 1 else 0 end) as Month8Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 8, #rptDate)) then 1 else 0 end) as Month9Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 9, #rptDate)) then 1 else 0 end) as Month10Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 10, #rptDate)) then 1 else 0 end) as Month11Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 11, #rptDate)) then 1 else 0 end) as Month12Count
DepartmentName,
ActivityDescription
FROM
reports.WorkOrders
WHERE
Year(BeginDate) + right('0'+Month(BeginDate),2)
between Year(#rptDate) + right('0'+Month(#rptDate),2)
and Year(dateadd(month,11,#rptDate)) + right('0'+Month(dateadd(month,11,#rptDate)),2)
GROUP BY
DepartmentName,
ActivityDescription
Do let me know how you get on with this. If I've made typos and you can't correct for them, do report the error message and/or any problems with the results.
Is there another way I can sum up counts with less code?
I'm using a view (my only option) to try to find out if a customer spent money during any two of the last 5 calendar years
Table name:
V_PERSON
V_REVENUE
Columns:
V_PERSON.ID
V.REVENUE.PersonID
V.REVENUE.Year1 revenue for the year (currently 2016)
V.REVENUE.Year2 revenue for the year (currently, 2015)
V.REVENUE.Year3
V.REVENUE.Year4
V.REVENUE.Year5
Here's what I've tried:
SELECT V_PERSON.ID
FROM V_PERSON
WHERE
(
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year1 > 0
)
+
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year2 > 0
)
+
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year3 > 0
)
+
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year4 > 0
)
+
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year5 > 0
)
) >= 2
Here's one option using exists with multiple case statements:
select id
from person p
where exists (
select 1
from revenue r
where p.id = r.personid
and case when r.year1 > 0 then 1 else 0 end +
case when r.year2 > 0 then 1 else 0 end +
case when r.year3 > 0 then 1 else 0 end +
case when r.year4 > 0 then 1 else 0 end +
case when r.year5 > 0 then 1 else 0 end >= 2
)
How about a CASE statement:
SELECT V_PERSON.ID
FROM V_PERSON
WHERE
(
SELECT (case when V_REVENUE.Year1 > 0 then 1 else 0 end) +
(case when V_REVENUE.Year2 > 0 then 1 else 0 end) +
(case when V_REVENUE.Year3 > 0 then 1 else 0 end) +
(case when V_REVENUE.Year4 > 0 then 1 else 0 end) +
(case when V_REVENUE.Year5 > 0 then 1 else 0 end)
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID) >= 2
We have a complex sql query with five subquerys and the group by clause. If I restrict the database server cache to 16m (with the option "-ch 16m") we get randomized different results from the same sql query (these issue is reproducible on different machines). Our customer have the same issue on his system without reducing the cache with the -ch option and with 8g of free memory.
What is a possible explanation for such a behavior?
We think that it would be better to get a warning message or a database server crash than inconsistent results.
DB-Version: 11.0.1.2744
DB-Size: ~1g
We tested it also with the newest 11.0.1.XXXX EBF release.
Here the sql:
SELECT (select year(beginnt) * 100 + month ( beginnt ) FROM geschaeftsjahr WHERE beginnt <= '2012-06-30' and
endet >= '2012-06-30' and
mandant_nr = i1_mandant_nr) geschaeftsjahresbeginn_vorjahr_yyyymm,
(select beginnt from geschaeftsjahr where beginnt <= '2012-06-30' and
endet >= '2012-06-30' and
mandant_nr = i1_mandant_nr) geschaeftsjahresbeginn_vorjahr_datum,
(select year(beginnt) * 100 + month ( beginnt ) from geschaeftsjahr where beginnt <= '2013-06-30' and
endet >= '2013-06-30' and
mandant_nr = i1_mandant_nr) geschaeftsjahresbeginn_yyyymm,
(select beginnt from geschaeftsjahr where beginnt <= '2013-06-30' and
endet >= '2013-06-30' and
mandant_nr = i1_mandant_nr) geschaeftsjahresbeginn_datum,
"v_kostenstellenplan"."ks1_kstnummer",
"v_kostenstellenplan"."ks1_bezeichnung",
isnull(sum ( if ksb1_jahr*100+ksb1_monat >= year('2013-06-01')*100+month('2013-06-01') then if not isnull("bereich",'') = 'C' then "ksb1_betrag" else 0 endif else 0 endif * get_vorzeichen_kore(ksb1_mandant_nr,
ksb1_kontonummer)) ,0) "betrag_kosten",
isnull(sum ( if ksb1_jahr*100+ksb1_monat >= year('2013-06-01')*100+month('2013-06-01') then if isnull("bereich",'') = 'C' then "ksb1_betrag" else 0 endif else 0 endif * get_vorzeichen_kore(ksb1_mandant_nr,
ksb1_kontonummer)) ,0) "betrag_ertrag",
betrag_kosten + betrag_ertrag "betrag",
isnull(sum ( if ksb1_jahr*100+ksb1_monat >= year('2013-06-01')*100+01 then if not isnull("bereich",'') = 'C' then "ksb1_betrag" else 0 endif else 0 endif * get_vorzeichen_kore(ksb1_mandant_nr,
ksb1_kontonummer)) ,0) "betrag_kosten_jahresbeginn",
isnull(sum ( if ksb1_jahr*100+ksb1_monat >= year('2013-06-01')*100+01 then if isnull("bereich",'') = 'C' then "ksb1_betrag" else 0 endif else 0 endif * get_vorzeichen_kore(ksb1_mandant_nr,
ksb1_kontonummer)) ,0) "betrag_ertrag_jahresbeginn",
betrag_kosten_jahresbeginn + betrag_ertrag_jahresbeginn "betrag_jahresbeginn",
isnull(sum ( if not isnull("bereich",'') = 'C' then if isnull("ks1_kumulation",'g') = 'a' then /*seit auftragsbeginn*/ "ksb1_betrag" else /*seit Jahresbeginn*/ if ksb1_jahr*100+ksb1_monat >= year('2013-06-01')*100+01 then "ksb1_betrag" else 0 endif endif else 0 endif * get_vorzeichen_kore(ksb1_mandant_nr,
ksb1_kontonummer)) ,0) "betrag_kosten_baubeginn",
isnull(sum ( if isnull("bereich",'') = 'C' then if isnull("ks1_kumulation",'g') = 'a' then /*seit auftragsbeginn*/ "ksb1_betrag" else /*nur Vorjahreszahlen*/ if ksb1_jahr*100+ksb1_monat >= year('2013-06-01')*100+01 then "ksb1_betrag" else 0 endif endif else 0 endif * get_vorzeichen_kore(ksb1_mandant_nr,
ksb1_kontonummer)) ,0) "betrag_ertrag_baubeginn",
betrag_kosten_baubeginn + betrag_ertrag_baubeginn "betrag_baubeginn",
isnull(sum ( "ksb1_betrag" ),0) "betrag_vorjahr",
count() count,
"i_daten"."i1_name",
"i_daten"."i1_mandant_nr",
"i_daten"."i1f1_zahlenformat_auswertung",
isnull(v_kostenstellenplan.ks1_inaktiv,
'n' ) kst_inaktiv,
"v_kostenstellenplan"."kl_nr1",
"v_kostenstellenplan"."kl_bez1",
"v_kostenstellenplan"."kl_bez_ergebnis1",
"v_kostenstellenplan"."kl_nr2",
"v_kostenstellenplan"."kl_bez2",
"v_kostenstellenplan"."kl_bez_ergebnis2",
"v_kostenstellenplan"."kl_nr3",
"v_kostenstellenplan"."kl_bez3",
"v_kostenstellenplan"."kl_bez_ergebnis3",
"v_kostenstellenplan"."kl_nr4",
"v_kostenstellenplan"."kl_bez4",
"v_kostenstellenplan"."kl_bez_ergebnis4",
"v_kostenstellenplan"."kl_nrn",
"v_kostenstellenplan"."ks1_typ",
"v_kostenstellenplan"."ks1_kumulation",
isnull((select k.ks1_verantwortlicher from kostenstelle k where k.ks1_kstnummer = "v_kostenstellenplan"."ks1_kstnummer" and
k.ks1_mandant_nr = "v_kostenstellenplan"."ks1_mandant_nr"),'') kst_verantwortlicher FROM "v_kostenstellenplan" join "konto_kst_statistik_budget" on "v_kostenstellenplan"."ks1_mandant_nr" = "konto_kst_statistik_budget"."ksb1_mandant_nr" and
"v_kostenstellenplan"."ks1_kstnummer" = "konto_kst_statistik_budget"."ksb1_kostenstelle" and
"v_kostenstellenplan"."kl1_typ" = 'ks2' join "konto" on "konto"."kontonummer" = "konto_kst_statistik_budget"."ksb1_kontonummer" and
"konto"."mandant_nr" = "konto_kst_statistik_budget"."ksb1_mandant_nr" join "i_daten" on "konto"."mandant_nr" = "i_daten"."i1_mandant_nr" where not ksb1_quelle = string(char(70),
char(51)) and
not "left"(ksb1_quelle,1) = CHAR(66) and
string('D1;D2;D3;F1;F2;F4;F5;F50;F51;F52;K1;K2;L1;N1;N2;N3;N4;N5;O1;O2;P1;',
'F20;F21;F23;') like string(char(37),ksb1_quelle,
char(59) ,
char(37)) and
/*Seit Baubeginn oder Geschäftsjahresbeginn*/ ksb1_jahr*100+ksb1_monat >= if isnull("v_kostenstellenplan"."ks1_kumulation",'g') = 'a' then 0 else year('2012-06-01')*100+month('2012-06-01') end if and
ksb1_jahr*100+ksb1_monat <= year('2013-06-30')*100+month('2013-06-30') and
"kontonummer" not in (907010,997010,907011,997011 ) and
ks1_kstnummer in (select kk.kk1_kostenstelle from kostenstelle_Klassierung kk join klassierung_typ on kk1_typ = kt1_typ join klassierung on kl1_id = kk1_klassierung_id where kk1_mandant_nr = isnull(i1_kostenstellen_vererben_von_mandant_nr,
i1_mandant_nr) and
kk1_typ = 'kst' and
kl1_nummer >= '7150' and
kl1_nummer <= '7150' ) and
i1_mandant_nr in (13) GROUP BY "v_kostenstellenplan"."ks1_kstnummer",
"v_kostenstellenplan"."ks1_bezeichnung",
"i_daten"."i1_name",
"i_daten"."i1_mandant_nr",
"i_daten"."i1f1_zahlenformat_auswertung",
"v_kostenstellenplan"."ks1_inaktiv",
"v_kostenstellenplan"."kl_nr1",
"v_kostenstellenplan"."kl_bez1",
"v_kostenstellenplan"."kl_bez_ergebnis1",
"v_kostenstellenplan"."kl_nr2",
"v_kostenstellenplan"."kl_bez2",
"v_kostenstellenplan"."kl_bez_ergebnis2",
"v_kostenstellenplan"."kl_nr3",
"v_kostenstellenplan"."kl_bez3",
"v_kostenstellenplan"."kl_bez_ergebnis3",
"v_kostenstellenplan"."kl_nr4",
"v_kostenstellenplan"."kl_bez4",
"v_kostenstellenplan"."kl_bez_ergebnis4",
"v_kostenstellenplan"."kl_nrn",
"v_kostenstellenplan"."ks1_typ",
"v_kostenstellenplan"."ks1_kumulation",
ks1_mandant_nr HAVING ( "betrag_kosten" <> 0 OR "betrag_ertrag" <> 0 OR "betrag" <> 0 OR "betrag_kosten_jahresbeginn" <> 0 OR "betrag_ertrag_jahresbeginn" <> 0 OR "betrag_jahresbeginn" <> 0 OR "betrag_kosten_baubeginn" <> 0 OR "betrag_ertrag_baubeginn" <> 0 ) ORDER BY i_daten.i1_mandant_nr,
"v_kostenstellenplan"."kl_nr1",
"v_kostenstellenplan"."kl_nr2",
"v_kostenstellenplan"."kl_nr3",
"v_kostenstellenplan"."kl_nr4",
"ks1_kstnummer"
The column "betrag_kosten" for example delivers different results (see the printscreen).
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