?
Solved

sql syntax

Posted on 2014-10-09
4
Medium Priority
?
145 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

762 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