Solved

sql syntax

Posted on 2014-10-09
4
144 Views
Last Modified: 2014-10-24
I have below sql script. It works fine but I need to make it like summary in ID.
Attached excel has my original result from below query, and also the new result I would like to see.
Overall, I want to have a list of ID into a row. so I can create the report the way I want.
See any experts can help me. Thanks

SELECT     ID, Status, SectorVP, SectorVPApproval, SectorVPComments, DateSubmission, CURRENT_TIMESTAMP AS SystemCurrentTimeStamp, DATEDIFF(hh, DateSubmission,
                      CURRENT_TIMESTAMP) AS Hours_Difference
FROM         TravelApprovalForms
WHERE     (Status = 'Pending') AND (SectorVPApproval = 'Pending') AND (DateSubmission >= '09/01/2014') AND (DATEDIFF(hh, DateSubmission, CURRENT_TIMESTAMP) >= 24)
ORDER BY ID DESC

Original Result set:							
ID	STATUS	SECTORVP	SECTORVPAPPROVAL	SECTORVPCOMMENTS	DATESUBMISSION	SYSTEMCURRENTTIMESTAMP	HOURS_DIFFERENCE
548	Pending	TESTER	Pending		8/10/2014 9:34	9/10/2014 17:16	32
546	Pending	TESTER	Pending		7/10/2014 15:55	9/10/2014 17:16	50
545	Pending	TESTER	Pending	xxx	7/10/2014 15:54	9/10/2014 17:16	50
534	Pending	TESTER1	Pending	testing	7/10/2014 11:52	9/10/2014 17:16	54
580	Pending	TESTER1	Pending	testing	7/10/2014 11:52	9/10/2014 17:16	54
							
							
New Result set							
							
TESTER	548,546	
TESTER1	534, 580
					

Open in new window

0
Comment
Question by:ITsolutionWizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40372164
Seems you are seeking a comma separated list of IDs per SECTORVP, but why is 545 missing in the result? commas.png
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40372335
This result
| SECTORVP |       COLUMN_1 |
|----------|----------------|
|   TESTER |  545, 546, 548 |
|  TESTER1 |       534, 580 |

Open in new window


Produced by this query:
SELECT
      TAF.SECTORVP
    , MAX(CA.IDS)
FROM TravelApprovalForms AS TAF
    CROSS APPLY (
        SELECT
          STUFF((
                SELECT
                      ', ' + CAST(TAF2.ID AS varchar(20))
                FROM TravelApprovalForms AS TAF2
                WHERE TAF.SECTORVP = TAF2.SECTORVP
                ORDER BY TAF2.ID
                FOR XML PATH ('')
                )
             , 1, 1, '')
         ) AS CA (IDS)
GROUP BY
      SECTORVP
;

Open in new window

0
 
LVL 1

Author Comment

by:ITsolutionWizard
ID: 40381104
Great. thank but I try to add below
 where VPname <> null and totalListItem <> null

and the full script is below.

are not working :-(


SELECT ltrim(TAF.SECTORVP) as VPname, MAX(CA.IDS) as TotalListItem FROM TravelApprovalForms  

AS TAF
    CROSS APPLY (
        SELECT
          STUFF((
                SELECT
                      ', ' + CAST(TAF2.ID AS varchar(20))
                FROM TravelApprovalForms AS TAF2
                WHERE TAF.SECTORVP = TAF2.SECTORVP
                          ORDER BY TAF2.ID
                FOR XML PATH ('')
                )
             , 1, 1, '')

         ) AS CA (IDS)  
GROUP BY
      where VPname <> null and totalListItem <> null

      ltrim(SECTORVP)
        
        
;
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40381149
NULL cannot equal anything and it cannot be unequal to anything

when filtering for NULLs you MUST use
IS NULL
IS NOT NULL


additionally the WHERE clause MUST happen before the GROUP BY clause


WHERE VPname IS NOT NULL and totalListItem IS NOT NULL
GROUP BY
      ltrim(SECTORVP)
       
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question