Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
werseyjersey
Starting Member
47 Posts |
Posted - 2009-02-06 : 14:20:42
|
I have the following query which provides me with a set of data that works for the data I need to return for a report, see below: SELECT DISTINCT M_CostCenter_6_Curr.GLSegment AS GL_CC, M_CostCenter_6_Curr.OrganizationDescription, tSY_PERSON_CODES.PersonCode AS TerminationType, tPERSONS.LastName, tPERSONS.FirstName, tPERSONS.MiddleName, tSY_PERSON_CODES_1.PersonCode AS Gender, tETHNIC_CODES.EthnicCode, tREASON_CODES.ReasonCode, CONVERT(varchar(10), tEMPLOYMENT_STATUS.OriginalHireDate, 101) AS OrigHireDt, CONVERT(varchar(10), tEMPLOYMENT_STATUS.TerminationDate, 101) AS TermDate, DATEDIFF(y, tEMPLOYMENT_STATUS.OriginalHireDate, tEMPLOYMENT_STATUS.TerminationDate) / 365.25 AS YrsofSrvcOrgHireFROM tEMPLOYMENT_CODES INNER JOINtEMPLOYMENT_STATUS ON tEMPLOYMENT_CODES.EmploymentCodeIdNo = tEMPLOYMENT_STATUS.EmploymentStatus INNER JOINtSY_PERSON_CODES ON tEMPLOYMENT_STATUS.TerminationTypeIdNo = tSY_PERSON_CODES.PersonCodeIdNo INNER JOINtREASON_CODES ON tEMPLOYMENT_STATUS.EmploymentStatusReasonIdNo = tREASON_CODES.ReasonCodeIdNo INNER JOINtPERSONS ON tEMPLOYMENT_STATUS.PersonIdNo = tPERSONS.PersonIdNo INNER JOINtETHNIC_CODES ON tPERSONS.EthnicCodeIdNo = tETHNIC_CODES.EthnicCodeIdNo INNER JOINtSY_PERSON_CODES AS tSY_PERSON_CODES_1 ON tPERSONS.GenderIdNo = tSY_PERSON_CODES_1.PersonCodeIdNo INNER JOINtPERSON_POSITIONS ON tEMPLOYMENT_STATUS.PersonIdNo = tPERSON_POSITIONS.PersonIdNo INNER JOINtPOSITION_CODES ON tPERSON_POSITIONS.PositionIdNo = tPOSITION_CODES.PositionIdNo INNER JOINM_CostCenter_6_Curr ON tPOSITION_CODES.PositionIdNo = M_CostCenter_6_Curr.PositionIdNoWHERE (tEMPLOYMENT_STATUS.EmploymentStatusToEffectDate >= '3000-01-01 00:00:00.000') AND (tEMPLOYMENT_CODES.EmploymentCode = 'Terminated') AND (tEMPLOYMENT_STATUS.TerminationDate >= CONVERT(DATETIME, '2008-12-01 00:00:00', 102))In addition, I need to take results of the YrsofSrvcOrgHire column and divide it into categories that are as follows:<90 days anything that was 0.0 (90 days = .24640657)0.1 – 0.999999 days was <1 year1.0 – 2.999999 days was <3 year3.0 and greater days was >3Anyone have a suggestion of the best way to accomplish this. I need to have a column for each of the above categories so I can pull that into my report. Your help is greatly appreciated.werseyjersey |
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-06 : 14:30:39
|
Search for Crosstab queries in here. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-07 : 08:58:58
|
group by common fields and just use likeSUM(CASE WHEN YrsofSrvcOrgHire=0.0 THEN youraggfieldhere ELSE 0 END) AS [<90 days],SUM(CASE WHEN YrsofSrvcOrgHire BETWEEN 0.1 AND 0.9999 THEN youraggfieldhere ELSE 0 END) AS [<1 year],SUM(CASE WHEN YrsofSrvcOrgHire BETWEEN 1.0 AND 2.9999 THEN youraggfieldhere ELSE 0 END) AS [<3 year],SUM(CASE WHEN YrsofSrvcOrgHire>=3.0 THEN youraggfieldhere ELSE 0 END) AS [> 3],... |
|
|
werseyjersey
Starting Member
47 Posts |
Posted - 2009-02-07 : 18:47:22
|
Ok I am fairly new to SQL and not understanding where this should go within the query I have above. FYI the YrsofServcOrgHire is created from the following: (DATEDIFF(y, tEMPLOYMENT_STATUS.OriginalHireDate, tEMPLOYMENT_STATUS.TerminationDate) / 365.25) AS YrsofSrvcOrgHireI am also not understanding the "youraggfieldhere" is this my fields above? Or is this just a random name I give it sorry to ask so many questions just like to understand why I am doing something.werseyjersey |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-07 : 19:07:10
|
Like this:SELECT YOURREQUIRED COLUMNS.............,SUM(CASE WHEN YrsofSrvcOrgHire=0.0 THEN YrsofSrvcOrgHire ELSE 0 END) AS [<90 days],SUM(CASE WHEN YrsofSrvcOrgHire BETWEEN 0.1 AND 0.9999 THEN YrsofSrvcOrgHire ELSE 0 END) AS [<1 year],SUM(CASE WHEN YrsofSrvcOrgHire BETWEEN 1.0 AND 2.9999 THEN YrsofSrvcOrgHire ELSE 0 END) AS [<3 year],SUM(CASE WHEN YrsofSrvcOrgHire>=3.0 THEN YrsofSrvcOrgHire ELSE 0 END) AS [> 3]FROM(SELECT DISTINCT M_COSTCENTER_6_CURR.GLSEGMENT AS GL_CC, M_COSTCENTER_6_CURR.ORGANIZATIONDESCRIPTION, TSY_PERSON_CODES.PERSONCODE AS TERMINATIONTYPE, TPERSONS.LASTNAME, TPERSONS.FIRSTNAME, TPERSONS.MIDDLENAME, TSY_PERSON_CODES_1.PERSONCODE AS GENDER, TETHNIC_CODES.ETHNICCODE, TREASON_CODES.REASONCODE, CONVERT(VARCHAR(10),TEMPLOYMENT_STATUS.ORIGINALHIREDATE, 101) AS ORIGHIREDT, CONVERT(VARCHAR(10),TEMPLOYMENT_STATUS.TERMINATIONDATE, 101) AS TERMDATE, DATEDIFF(Y,TEMPLOYMENT_STATUS.ORIGINALHIREDATE,TEMPLOYMENT_STATUS.TERMINATIONDATE) / 365.25 AS YRSOFSRVCORGHIRE FROM TEMPLOYMENT_CODES INNER JOIN TEMPLOYMENT_STATUS ON TEMPLOYMENT_CODES.EMPLOYMENTCODEIDNO = TEMPLOYMENT_STATUS.EMPLOYMENTSTATUS INNER JOIN TSY_PERSON_CODES ON TEMPLOYMENT_STATUS.TERMINATIONTYPEIDNO = TSY_PERSON_CODES.PERSONCODEIDNO INNER JOIN TREASON_CODES ON TEMPLOYMENT_STATUS.EMPLOYMENTSTATUSREASONIDNO = TREASON_CODES.REASONCODEIDNO INNER JOIN TPERSONS ON TEMPLOYMENT_STATUS.PERSONIDNO = TPERSONS.PERSONIDNO INNER JOIN TETHNIC_CODES ON TPERSONS.ETHNICCODEIDNO = TETHNIC_CODES.ETHNICCODEIDNO INNER JOIN TSY_PERSON_CODES AS TSY_PERSON_CODES_1 ON TPERSONS.GENDERIDNO = TSY_PERSON_CODES_1.PERSONCODEIDNO INNER JOIN TPERSON_POSITIONS ON TEMPLOYMENT_STATUS.PERSONIDNO = TPERSON_POSITIONS.PERSONIDNO INNER JOIN TPOSITION_CODES ON TPERSON_POSITIONS.POSITIONIDNO = TPOSITION_CODES.POSITIONIDNO INNER JOIN M_COSTCENTER_6_CURR ON TPOSITION_CODES.POSITIONIDNO = M_COSTCENTER_6_CURR.POSITIONIDNO WHERE (TEMPLOYMENT_STATUS.EMPLOYMENTSTATUSTOEFFECTDATE >= '3000-01-01 00:00:00.000') AND (TEMPLOYMENT_CODES.EMPLOYMENTCODE = 'Terminated') AND (TEMPLOYMENT_STATUS.TERMINATIONDATE >= CONVERT(DATETIME,'2008-12-01 00:00:00',102)))ZGROUP BY YOURREQUIRED COLUMNS................ORDER BY YOURREQUIRED COLUMNS............ |
|
|
werseyjersey
Starting Member
47 Posts |
Posted - 2009-02-07 : 20:52:22
|
Thank you that was excellent! I got it to work and we are happy campers. Thanks again you guys are AWESOME!werseyjersey |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-07 : 20:55:42
|
Cool .You are most Welcome. |
|
|
|
|
|
|
|