My Pivot
SELECT *
FROM
(
SELECT projectallocation.proAllocationID AS Sno,
temp.intro_name AS IntroTop,
projectallocation.introtoplevelEmpid AS TopEmp,
(companystructure.csshortname) AS Level,
introducermaster.intro_name AS IntroLow,
projectallocation.introlevelEmpid AS EmpID
FROM projectallocation
INNER JOIN dbo.IntroducerMaster ON dbo.introducermaster.empid = projectallocation.introLevelEmpid
INNER JOIN introducermaster AS temp ON temp.empiD = projectallocation.introtopLevelEmpid
INNER JOIN companyStructure ON companyStructure.HLevel = projectallocation.introleveID
WHERE projectallocation.projectID = 1
AND projectallocation.introleveID = 4
GROUP BY IntroducerMaster.Intro_Name,
temp.intro_name,
companyStructure.CSShortName,
projectallocation.proAllocationID,
projectallocation.introlevelEmpid,
projectallocation.introtoplevelEmpid,
projectallocation.introtoplevelid
) b PIVOT(MAX(introlow) FOR level IN(ch,
ed,
dir,
gm,
agm,
bdm,
smm)) PVT;
Output
sno Introtop topempid empid ch ed dir gm agm bdm smm
---------------------------------------------------------------------
1 Jhon.A emp01 emp05 null null null null null ajju.R null
output has 49 rows i include only one......
I want
sno Introtop topempid ch ed dir gm agm bdm smm empid
--------------------------------------------------------------------------
1 Jhon.A emp01 null null null null null ajju.R null emp005
the empid should be in last please help me!!!!
The Solution is Simple, change the SELECT * And replace the * with the Column Names in any order you wish.
Like this
SELECT
sno,
Introtop,
topempid,
ch,
ed,
dir,
gm,
agm,
bdm,
smm,
empid
FROM
Related
I have the following static pivot that works:
SELECT *
FROM
(
SELECT
d.CODE,
a.ANAPARA - Isnull(b.ANAPARA,0) AS ANAPARA,
d.NAME_,
c.DUEDATE,
a.TAKSIT - Isnull(b.TAKSIT,0) AS TAKSIT,
e.CURCODE
FROM
(SELECT
PARENTREF,
SUM(TOTAL) AS ANAPARA,
SUM(INTTOTAL) AS FAIZ,
SUM(BSMVTOTAL) AS BSMV,
SUM(KKDFTOTAL) AS KKDF,
SUM(TOTAL+INTTOTAL+BSMVTOTAL+KKDFTOTAL) AS TAKSIT
FROM LG_011_BNCREPAYTR
WHERE TRANSTYPE = 0
GROUP BY PARENTREF) a
LEFT OUTER JOIN (SELECT
PARENTREF,
SUM(TOTAL) AS ANAPARA,
SUM(INTTOTAL) AS FAIZ,
SUM(BSMVTOTAL) AS BSMV,
SUM(KKDFTOTAL) AS KKDF,
SUM(TOTAL+INTTOTAL+BSMVTOTAL+KKDFTOTAL) AS TAKSIT
FROM LG_011_BNCREPAYTR
WHERE TRANSTYPE = 1
GROUP BY PARENTREF) b
ON a.PARENTREF = b.PARENTREF
INNER JOIN (SELECT
PARENTREF,
CREDITREF,
DUEDATE,
OPRDATE
FROM LG_011_BNCREPAYTR) c
ON a.PARENTREF=c.PARENTREF
INNER JOIN LG_011_BNCREDITCARD d
ON c.CREDITREF=d.LOGICALREF
INNER JOIN L_CURRENCYLIST e
ON d.TRCURR=e.CURTYPE OR (d.TRCURR=0 AND e.CURTYPE=160)
WHERE e.FIRMNR=11
) x
PIVOT
(
SUM(TAKSIT)
FOR CURCODE IN ([USD],[EUR],[YEN])
) xx
It creates the following table:
CODE ANAPARA NAME_ DUEDATE EUR USD YEN
001 103.35 CAR LOAN 2015-01-01 NULL 150.01 NULL
002 106.89 CONSUMER LOAN 2015-03-10 190.35 NULL NULL
003 110.44 CAR LOAN 2015-04-20 NULL NULL 200.51
004 103.22 CAR LOAN 2015-05-04 150.02 NULL NULL
However, I would like to convert it into a dynamic pivot. Hence, I would like to change the code so that it gets the distinct currency codes from e.CURCODE and produces the same pivot table.
THANKS A LOT.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Below is the mysql select statement followed by the explain output.
I have tried adding indexes where I thought was appropriate but with no extra speed.
There are 2 rows in the 'explain' output that stand out (id 7 and 8 where the rows are over 35,000).
I think the sql needs to be restructured but I don't know how to change it. Any help would be appreciated.
EXPLAIN SELECT wo.JobStatusID
,wo.JobNo
,ql.RackCode
,ql.Description
,c.CustName
,s.ShipDate
,s.Qty
,pl.PrepLocation
,wl.WeldLocation
,line.WeldLine
,pm.Initials
,pqty.ShipTotal
,pqty.POTotal
,po.POCount
,s2.Notes
,bm.UnitQty
,labour.AssemblyLabour
,labour.WeldLabour
,labour.TotalGUTLabour + labour.TotalGUTSetUp / pqty.POTotal AS GUTLabour
,labour.TotalPrepLabour + labour.TotalPrepSetUp / pqty.POTotal AS PrepLabour
,pqty.POTotal
FROM WorkOrders wo
JOIN (
SELECT JobNo
,MasterJobNo AS RelevantJobNo
FROM WorkOrders
WHERE MasterJobNo != 0
UNION
SELECT JobNo
,JobNo AS RelevantJobNo
FROM WorkOrders
WHERE MasterJobNo = 0
) AS r
ON r.JobNo = wo.JobNo
LEFT JOIN QuoteLog ql
ON ql.QuoteID = wo.QuoteID
AND wo.FOCPlayArea = 0
LEFT JOIN Customer c
ON c.CustID = ql.CustID
LEFT JOIN WOSchedule s
ON s.JobNo = wo.JobNo
AND s.ObsoleteInd = 0
LEFT JOIN (
SELECT JobNo
,Notes
FROM WOSchedule
ORDER BY ShipDate DESC
) AS s2
ON s2.JobNo = wo.JobNo
LEFT JOIN PrepLocation pl
ON pl.PrepLocationID = wo.PrepLocationID
LEFT JOIN WeldLocation wl
ON wl.WeldLocationID = wo.WeldLocationID
LEFT JOIN ProgramManager pm
ON pm.ProgramManagerID = wo.ProgramManagerID
LEFT JOIN (
SELECT JobNo
,SUM(POQty) AS POTotal
,SUM(ShipQty) AS ShipTotal
FROM WOOrderDetails wod
JOIN WODetailType wodt
ON wodt.DetailTypeID = wod.DetailTypeID
AND wodt.ReleaseModeID = 1
AND wodt.Inactive = 0
WHERE wod.ObsoleteInd = 0
GROUP BY wod.JobNo
) AS pqty
ON pqty.JobNo = wo.JobNo
LEFT JOIN (
SELECT Jobno
,COUNT(*) AS POCount
FROM WOPOs
GROUP BY JobNo
) AS po
ON po.JobNo = wo.JobNo
LEFT JOIN BOMMaterialList bm
ON bm.JobNo = r.RelevantJobNo
LEFT JOIN WeldLine line
ON wo.WeldLineID = line.WeldLineID
LEFT JOIN (
SELECT x.JobNo
,x.BOMDetailID
,SUM(x.TotalPrepLabour) AS TotalPrepLabour
,SUM(x.TotalPrepSetUp) AS TotalPrepSetUp
,SUM(x.TotalGUTLabour) AS TotalGUTLabour
,SUM(x.TotalGUTSetUp) AS TotalGUTSetUp
,SUM(x.WeldLabour * x.WeldEfficiency) AS WeldLabour
,SUM(AssemblyLabour * AssemblyEfficiency * UnitQty) AS AssemblyLabour
FROM (
SELECT bm.JobNo
,bm.BOMDetailID
,bm.UnitQty
,SUM(bm.UnitQty * pl.HitTime * pl.NoHits * pl.NoMen) AS TotalPrepLabour
,SUM(pl.SetUp) AS TotalPrepSetUp
,SUM(bm.UnitQty * gl.HitTime * gl.NoHits * gl.NoMen) AS TotalGUTLabour
,SUM(gl.SetUp) AS TotalGUTSetUp
,bm.WeldLabour
,bm.WeldEfficiency
,bm.AssemblyLabour
,bm.AssemblyEfficiency
FROM BOMMaterialList bm
LEFT JOIN BOMPrepLabour pl
ON pl.BOMDetailID = bm.BOMDetailID
LEFT JOIN BOMGUTLabour gl
ON gl.BOMDetailID = bm.BOMDetailID
GROUP BY BOMDetailID
) AS x
GROUP BY x.JobNo
) AS labour
ON bm.BOMDetailID = labour.BOMDetailID
WHERE ( wo.JobStatusID = 1
OR wo.JobStatusID = 2
OR wo.JobStatusID = 3
)
GROUP BY wo.JobNo
ORDER BY wo.JobStatusID
,wl.WeldLocation
,line.WeldLine
,wo.JobNo;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 1418 "Using temporary; Using filesort"
1 PRIMARY wo eq_ref PRIMARY,IDX_1 PRIMARY 4 r.JobNo 1 "Using where"
1 PRIMARY ql eq_ref PRIMARY PRIMARY 4 sbpdf3_custom014.wo.QuoteID 1
1 PRIMARY c eq_ref PRIMARY PRIMARY 2 sbpdf3_custom014.ql.CustID 1
1 PRIMARY s ref PRIMARY,IDX_2 PRIMARY 4 r.JobNo 28
1 PRIMARY <derived4> ALL NULL NULL NULL NULL 2884
1 PRIMARY pl eq_ref PRIMARY PRIMARY 4 sbpdf3_custom014.wo.PrepLocationID 1
1 PRIMARY wl eq_ref PRIMARY PRIMARY 4 sbpdf3_custom014.wo.WeldLocationID 1
1 PRIMARY pm eq_ref PRIMARY PRIMARY 4 sbpdf3_custom014.wo.ProgramManagerID 1
1 PRIMARY <derived5> ALL NULL NULL NULL NULL 1244
1 PRIMARY <derived6> ALL NULL NULL NULL NULL 1310
1 PRIMARY bm ref IDX1 IDX1 4 r.RelevantJobNo 19
1 PRIMARY line ref PRIMARY PRIMARY 4 sbpdf3_custom014.wo.WeldLineID 1
1 PRIMARY <derived7> ALL NULL NULL NULL NULL 1102
7 DERIVED <derived8> ALL NULL NULL NULL NULL 35736 "Using temporary; Using filesort"
8 DERIVED bm index NULL PRIMARY 4 NULL 35268
8 DERIVED pl ref PRIMARY PRIMARY 4 sbpdf3_custom014.bm.BOMDetailID 1
8 DERIVED gl ref PRIMARY PRIMARY 4 sbpdf3_custom014.bm.BOMDetailID 1
6 DERIVED WOPOs index NULL IDX_1 4 NULL 1804 "Using index"
5 DERIVED wod ref IDX_1 IDX_1 1 1660 "Using where; Using temporary; Using filesort"
5 DERIVED wodt eq_ref PRIMARY PRIMARY 2 sbpdf3_custom014.wod.DetailTypeID 1 "Using where"
4 DERIVED WOSchedule ALL NULL NULL NULL NULL 2884 "Using filesort"
2 DERIVED WorkOrders range IDX_2 IDX_2 4 NULL 15 "Using where; Using index"
3 UNION WorkOrders ref IDX_2 IDX_2 4 689 "Using index"
NULL "UNION RESULT" <union2,3> ALL NULL NULL NULL NULL NULL
There's a lot to look through here, but I did spot this subquery:
SELECT JobNo
,MasterJobNo AS RelevantJobNo
FROM WorkOrders
WHERE MasterJobNo != 0
UNION
SELECT JobNo
,JobNo AS RelevantJobNo
FROM WorkOrders
WHERE MasterJobNo = 0
which I believe could be re-written like so:
SELECT JobNo
,CASE WHEN MasterJobNo != 0
THEN MasterJobNo
ELSE JobNo END AS RelevantJobNo
FROM WorkOrders
And once we re-write it this way, we wonder why it needs to be a subquery, rather than simply joining to the table. And then I look again and see that we've already used this table in the query, and so we can eliminate that join entirely, and just include the CASE expression in the appropriate places. Moreover, this expression is only used in one place that I could spot (the join to BOMMaterialList).
I also noticed this subquery:
SELECT JobNo
,Notes
FROM WOSchedule
ORDER BY ShipDate DESC
I think we can eliminate this, as well. ORDER BY without a LIMIT has no meaning here when used as a derived table, and again: this table is already included in the query in another place. The only difference between the data provided this s2 subquery and the prior s table is the check on the ObsoleteInd field, and the only place data from s2 is used elsewhere is the Notes field in the select clause. The s table is only used in select list of the ShipDate and Qty items.
So I can get us down to this, which should show some improvement:
SELECT wo.JobStatusID
,wo.JobNo
,ql.RackCode
,ql.Description
,c.CustName
,case when s.ObsoleteInd = 0 then s.ShipDate else null end as ShipDate
,case when s.ObsoleteInd = 0 then s.Qty else null end as Qty
,pl.PrepLocation
,wl.WeldLocation
,line.WeldLine
,pm.Initials
,pqty.ShipTotal
,pqty.POTotal
,po.POCount
,s.Notes
,bm.UnitQty
,labour.AssemblyLabour
,labour.WeldLabour
,labour.TotalGUTLabour + labour.TotalGUTSetUp / pqty.POTotal AS GUTLabour
,labour.TotalPrepLabour + labour.TotalPrepSetUp / pqty.POTotal AS PrepLabour
,pqty.POTotal
FROM WorkOrders wo
LEFT JOIN QuoteLog ql
ON ql.QuoteID = wo.QuoteID
AND wo.FOCPlayArea = 0
LEFT JOIN Customer c
ON c.CustID = ql.CustID
LEFT JOIN WOSchedule s
ON s.JobNo = wo.JobNo
LEFT JOIN PrepLocation pl
ON pl.PrepLocationID = wo.PrepLocationID
LEFT JOIN WeldLocation wl
ON wl.WeldLocationID = wo.WeldLocationID
LEFT JOIN ProgramManager pm
ON pm.ProgramManagerID = wo.ProgramManagerID
LEFT JOIN (
SELECT JobNo
,SUM(POQty) AS POTotal
,SUM(ShipQty) AS ShipTotal
FROM WOOrderDetails wod
JOIN WODetailType wodt
ON wodt.DetailTypeID = wod.DetailTypeID
AND wodt.ReleaseModeID = 1
AND wodt.Inactive = 0
WHERE wod.ObsoleteInd = 0
GROUP BY wod.JobNo
) AS pqty
ON pqty.JobNo = wo.JobNo
LEFT JOIN (
SELECT Jobno
,COUNT(*) AS POCount
FROM WOPOs
GROUP BY JobNo
) AS po
ON po.JobNo = wo.JobNo
LEFT JOIN BOMMaterialList bm
ON bm.JobNo = CASE WHEN wo.MasterJobNo != 0 THEN wo.MasterJobNo ELSE wo.JobNo END
LEFT JOIN WeldLine line
ON wo.WeldLineID = line.WeldLineID
LEFT JOIN (
SELECT x.JobNo
,x.BOMDetailID
,SUM(x.TotalPrepLabour) AS TotalPrepLabour
,SUM(x.TotalPrepSetUp) AS TotalPrepSetUp
,SUM(x.TotalGUTLabour) AS TotalGUTLabour
,SUM(x.TotalGUTSetUp) AS TotalGUTSetUp
,SUM(x.WeldLabour * x.WeldEfficiency) AS WeldLabour
,SUM(AssemblyLabour * AssemblyEfficiency * UnitQty) AS AssemblyLabour
FROM (
SELECT bm.JobNo
,bm.BOMDetailID
,bm.UnitQty
,SUM(bm.UnitQty * pl.HitTime * pl.NoHits * pl.NoMen) AS TotalPrepLabour
,SUM(pl.SetUp) AS TotalPrepSetUp
,SUM(bm.UnitQty * gl.HitTime * gl.NoHits * gl.NoMen) AS TotalGUTLabour
,SUM(gl.SetUp) AS TotalGUTSetUp
,bm.WeldLabour
,bm.WeldEfficiency
,bm.AssemblyLabour
,bm.AssemblyEfficiency
FROM BOMMaterialList bm
LEFT JOIN BOMPrepLabour pl
ON pl.BOMDetailID = bm.BOMDetailID
LEFT JOIN BOMGUTLabour gl
ON gl.BOMDetailID = bm.BOMDetailID
GROUP BY BOMDetailID
) AS x
GROUP BY x.JobNo
) AS labour
ON bm.BOMDetailID = labour.BOMDetailID
WHERE ( wo.JobStatusID = 1
OR wo.JobStatusID = 2
OR wo.JobStatusID = 3
)
GROUP BY wo.JobNo
ORDER BY wo.JobStatusID
,wl.WeldLocation
,line.WeldLine
,wo.JobNo;
There are likely a few other improvements like this possible, but I suspect most improvement will come from examining your index use.
I have query that is similar to the following and wish to remove the repeating rows in the second/third column...need examples where I can use the following (top 1, max, min, etc...) or other best method without using derived query to remove the duplicate values. Thanks JK
WITH cteEMPAssistants (executive_personnel_number, assistant_personnel_number, assistant_type, SecType) AS
(SELECT DISTINCT CASE
WHEN ISNUMERIC(KA.KIT_asgn_emplid) = 1 THEN CAST(CAST(KA.KIT_asgn_emplid AS INT) AS VARCHAR(11))
ELSE ''
END AS executive_personnel_number ,
CAST(CAST(KAP.emplid AS INT) AS VARCHAR(11)) AS assistant_personnel_number ,
ISNULL(LATT1.xlatlongname, '') AS assistant_type ,
CAST(LATT1.fieldvalue AS VARCHAR(4)) AS SecType ,
ISNULL(LATT3.xlatshortname, '') AS assign_role
FROM dbo.KIT_ASGN_PRNT AS KAP
LEFT OUTER JOIN dbo.KIT_ASSIGNMENTS AS KA ON KA.emplid = KAP.emplid
AND KA.effdt = KAP.effdt
LEFT OUTER JOIN dbo.KIT_EMPLOYEES AS EXECT ON EXECT.EMPLID = KA.KIT_ASGN_EMPLID
LEFT OUTER JOIN dbo.KIT_EMPLOYEES AS ASST ON ASST.EMPLID = KAP.EMPLID
LEFT OUTER JOIN dbo.XLATITEM AS LATT1 ON LATT1.fieldname = 'KIT_ASGN_TYPE'
AND LATT1.fieldvalue = KAP.KIT_asgn_type
LEFT OUTER JOIN dbo.XLATITEM AS LATT3 ON LATT3.fieldname = 'KIT_ASGN_ROLE'
AND LATT3.fieldvalue = KA.KIT_asgn_role
AND LATT3.xlatshortname = 'Primary'
WHERE KAP.effdt =
(SELECT MAX(effdt)
FROM dbo.KIT_ASGN_PRNT
WHERE emplid = KAP.emplid
AND effdt <= GETDATE() ) --Return data only when employee and assistant active; null is for temps
AND (EXECT.EMP_STATUS = 'A'
OR EXECT.EMP_STATUS IS NULL )
AND ASST.EMP_STATUS = 'A' --Return all floating secretaries
AND (KAP.KIT_asgn_type = 'F' --Return all assigned secretaries, who are assigned to a person and not a task
OR (KAP.KIT_asgn_type IN ('A',
'AF')
AND KA.KIT_asgn_person = 'Y' ) ) )
SELECT CAST(EMP.KIT_EMPLID AS VARCHAR(15)) AS employeeNumber ,
KAP.emplid AS Assistant ,
SECRES.SecType AS SecType
FROM dbo.KIT_EMPLOYEES AS EMP
LEFT OUTER JOIN cteEMPAssistants AS SECRES ON EMP.KIT_EMPLID = SECRES.assistant_personnel_number
WHERE (EMP.EMP_STATUS = 'A')
AND (EMP.PER_ORG IN ('EMP',
'CWR'))
AND (ISNULL(EMP.KIT_NETWORK_ID, '') <> '')
Here is sample output:
employeeNumber Assistant SecType
--------------- --------- -----------------
1234 1112 A
1234 1112 A
1234 1112 A
1234 1112 A
4567 1113 NULL
1278 1114 NULL
1365 1115 NULL
1298 1116 A
1476 1117 A
1191 1118 NULL
You should be able to append this to the end of your query:
GROUP BY EMP.KIT_EMPLID, KAP.emplid, SECRES.SecType
I get the following result:
**SID** **KL** **ABT** **KLH** **ABTH**
013239 PKB4 GT NULL NULL
013239 TM4A KD NULL NULL
013243 KSB4 GT NULL NULL
013243 TM4A KD NULL NULL
with this query:
SELECT DISTINCT
SID, KL, ABT, KLH, ABTH
FROM
[SGB]
WHERE
SID IN (SELECT SID FROM [SGB] GROUP BY SID HAVING COUNT(*) > 1)
I'd like to update KLH and ABTH with the content of KL and ABT from the first duplicate.
should look like this:
**SID** **KL** **ABT** **KLH** **ABTH**
013239 PKB4 GT PKB4 GT
013239 TM4A KD PKB4 GT
013243 KSB4 GT KSB4 GT
013243 TM4A KD KSB4 GT
Many thanks!
Here is query which update all duplicated SID with first value:
;WITH tempSGB AS (
SELECT SID
, KL
, ABT
, KLH
, ABTH
--Next column set numbers in order for rows
, ROW_NUMBER() OVER (PARTITION BY SID ORDER BY KL) AS ROWNUM
FROM SGB
WHERE SID IN (SELECT SID FROM SGB GROUP BY SID HAVING COUNT(*) > 1)
)
UPDATE s SET
s.KLH = tmp.KL
, s.ABTH = tmp.ABT
FROM tempSGB tmp
INNER JOIN SGB s ON s.SID = tmp.SID
WHERE ROWNUM = 1; --here we choose only first row from duplicated
About ROW_NUMBER()
But you did not mention how query can decide which row is first from all duplicated(in the example i set in order by KL)
Here SQL Fiddle is a sample query with data for testing
UPDATE B
SET KLH=A.KL,
ABTH=A.ABT
FROM [SGB] A JOIN [SGB] B ON A.SID=B.SID
select * into #temptable from [SGB]
update #temptable t
set **KLH** =**KL**
set **ABTH**=**ABT**
from [SGB] s
inner join s.**SID**=t.**SID**
I'm not an expert in T-SQL so here I'm trying to find the % compliance for flu vaccine ,TB test and resiprator test by supervisor for medical staffs. Each employee has a supervisor name linked to their employee info. The below code works fine and it's giving me the % for the above tests. The problem is that I want to get the ID, Name and Department by Supervisor and the % compliance.
The expected output is like this:
Supervisor ID NAME Dept %Flu %TB %FIT
Elaine Jong 98% 100% 52%
001 MARY SURGERY
002 SUSAN SURGERY
James Ande 100% 98% 78%
267 JIM INPATIENT
789 SAM INPATIENT
Current OUTPUT
%Flu %TB %FIT
Elaine Jong 98% 100% 52%
James Ande 100% 98% 78%
And the Query:
SELECT E.FLDSUPRNAME AS Supervisor,
1.0*SUM(
CASE WHEN I.FLDDATE IS NULL
THEN 0 ELSE 1
END)/SUM(1) AS Percent_Flu_Compliant,
1.0*SUM(
CASE WHEN F.FLDDATE IS NULL OR (F.FLDDATE+365) < GETDATE()
THEN 0 ELSE 1
END) / SUM(1)
AS Percent_Fit_Compliant,
1.0*SUM(
CASE WHEN PPDx.FLDDATEDUE IS NULL
AND TBSSx.FLDDATEDUE IS NULL
AND CDUEx.FLDDATEDUE IS NULL
THEN 1 ELSE 0
END) /SUM(1) AS Percent_TB_Compliant
FROM EMPLOYEE E
LEFT OUTER JOIN DEPT D
ON D.FLDCODE= E.FLDDEPT
LEFT OUTER JOIN IMMUNE I ON I.FLDEMPLOYEE = E.FLDREC_NUM AND I.FLDTYPE IN ('109', '111')
AND I.FLDDATE = ( SELECT MAX(FLDDATE) FROM IMMUNE I2 WHERE E.FLDREC_NUM = I2.FLDEMPLOYEE
AND I2.FLDTYPE IN ('109','111') ) AND I.FLDDATE >= #Flu_Date AND I.FLDDATE <= GETDATE()
LEFT OUTER JOIN FITTEST F ON E.FLDREC_NUM = F.FLDEMPLOYEE
AND F.FLDDATE = (SELECT MAX(FLDDATE) FROM FITTEST F2 WHERE E.FLDREC_NUM = F2.FLDEMPLOYEE)
LEFT OUTER JOIN REQEXAM PPDx
ON PPDx.FLDEMPLOYEE = E.FLDREC_NUM
AND PPDx.FLDPHYSICAL = '110' AND
PPDx.FLDDATEDUE <= getdate()
LEFT OUTER JOIN REQEXAM PPDL
ON PPDL.FLDEMPLOYEE = E.FLDREC_NUM
AND PPDL.FLDPHYSICAL = '110'
LEFT OUTER JOIN REQEXAM TBSSx
ON TBSSx.FLDEMPLOYEE = E.FLDREC_NUM
AND TBSSx.FLDPHYSICAL = 'TBSS' AND
TBSSx.FLDDATEDUE <= getdate()
LEFT OUTER JOIN REQEXAM TBSSL
ON TBSSL.FLDEMPLOYEE = E.FLDREC_NUM
AND TBSSL.FLDPHYSICAL = 'TBSS'
LEFT OUTER JOIN REQEXAM CDUEx
ON CDUEx.FLDEMPLOYEE = E.FLDREC_NUM
AND CDUEx.FLDPHYSICAL = '109' AND
CDUEx.FLDDATEDUE <= getdate()
LEFT OUTER JOIN EMP S
ON S.FLDREC_NUM = E.FLDREC_NUM
WHERE E.FLDCOMP = #company
AND E.FLDSTATUS = 'A'
AND E.FLDSUPRNAME <> ' '
AND E.FLDID <> ' '
GROUP BY E.FLDSUPRNAME
ORDER BY E.FLDSUPRNAME
If I add ID,NAME and Dept on select and group by , SUM(1) will turn to 1 or 0, so I'm getting either 100% or 0% for all supervisors.
Any help on this is really appreciated.
thanks for your time.
USE an UNION, add blank columns to your first query and remove the order by:
SELECT (CASE WHEN ID IS NULL THEN Supervisor ELSE '' END) ,ID, name,dept,Percent_Flu_Compliant,Percent_TB_Compliant,Percent_Fit_Compliant FROM
(
SELECT E.FLDSUPRNAME AS Supervisor, NULL as ID, NULL as name, NULL as dept
(...)
GROUP BY hiddensupervisor, Supervisor, ID, name, dept
UNION ALL
SELECT E.FLDSUPRNAME Supervisor, E.id, E.name, E.dept, NULL as Percent_Flu_Compliant, NULL as Percent_TB_Compliant, NULL asPercent_Fit_Compliant
FROM Employee
) as q
ORDER BY supervisor, (CASE WHEN ID IS NULL THEN 1 ELSE 0 END),ID
we add the hidden supervisor column to be able to fit employees under their supervisor but leave that field blank there (we also could not add it and use case in the outer query, dunno which one would be faster). Apparently we have to try with case