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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Horizontally Displaying Fields from 4 Views

Author  Topic 

westcote
Starting Member

1 Post

Posted - 2014-04-01 : 11:18:53
I would like for each of the queries to have all of the selected fields shown horizontally in one table.

For example,
Commercial Created | Commercial Closed | Commercial UserId | Residential Created | Residential Closed | Residential UserId | Other Created | Other Closed | Other UserId.


Here is what I have now and it is displaying the fields as I would like them to. In the code below, each Views is acting as an individual table and then joined together to make another table when the query is executed.

WITH t1 AS (
SELECT vSalesReportProcessDetail.[RequestId]
,vSalesReportProcessDetail.Process
,vSalesReportProcessDetail.Entered
,vSalesReportProcessDetail.Closed
/*ROW_NUMBER() over (ORDER BY vSalesReportProcessDetail.[RequestId]) rn*/
FROM[Sales].[dbo].[vSalesReportProcessDetail]
WHERE Process = 'Commercial' ),


t2 AS (
SELECT
vSalesReportProcessDetail.[RequestId]
,vSalesReportProcessDetail.Process
,vSalesReportProcessDetail.Entered
,vSalesReportProcessDetail.Closed
/*ROW_NUMBER() over (ORDER BY vSalesReportProcessDetail.[RequestId]) rn*/
FROM [Sales].[dbo].[vSalesReportProcessDetail]
WHERE Process = 'Residential'),



SELECT t1.[RequestId]
,t1.Process
,t1.Entered AS 'Commercial Entered'
,t1.Closed AS 'Commercial Closed'
,t2.Process
,t2.Entered AS 'Residential Entered'
,t2.Closed AS 'Residential Closed'




However, I need to add 3 more Views. When I try to add one more additional View from another table to this query, I get an error. I need to include the UserId which is in the SalesReportProcessUser table.



WITH t1 AS (
SELECT vSalesReportProcessDetail.[RequestId]
,vSalesReportProcessDetail.Process
,vSalesReportProcessDetail.Entered
,vSalesReportProcessDetail.Closed
/*ROW_NUMBER() over (ORDER BY vSalesReportProcessDetail.[RequestId]) rn*/
FROM[Sales].[dbo].[vSalesReportProcessDetail]
WHERE Process = 'FrontDoor' ),


t2 AS (
SELECT
vSalesReportProcessDetail.[RequestId]
,vSalesReportProcessDetail.Process
,vSalesReportProcessDetail.Entered
,vSalesReportProcessDetail.Closed
/*ROW_NUMBER() over (ORDER BY vSalesReportProcessDetail.[RequestId]) rn*/
FROM [Sales].[dbo].[vSalesReportProcessDetail]
WHERE Process = 'Residential'),


t3 AS (
SELECT

vSalesReportProcessUser.[RequestId]
,vSalesReportProcessUser.UserID
/*ROW_NUMBER() over (ORDER BY vSalesReportProcessUser.[RequestId]) rn*/
FROM

[Sales].[dbo].[vSalesReportProcessUser])


SELECT t1.[RequestId]
,t1.Process
,t1.Entered AS 'Commercial Entered'
,t1.Closed AS 'Commercial Closed'
,t2.Process
,t2.Entered AS 'Residential Entered'
,t2.Closed AS 'Residential Closed'
,t3.UserId




Thank you for your help in advance.

Best regards

WestCote

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-04-13 : 14:59:08
As I understand you dont need all these views. Yo ca simply this as below

SELECT t.*,
t1.UserId
FROM
(
SELECT [RequestId]
,MAX(CASE WHEN Process = 'Commercial' THEN Process END)AS 'CommercialProcess'
,MAX(CASE WHEN Process = 'Commercial' THEN Entered END) AS 'Commercial Entered'
,MAX(CASE WHEN Process = 'Commercial' THEN Closed END) AS 'Commercial Closed'
,MAX(CASE WHEN Process = 'Residential' THEN Process END) AS 'Residential Process'
,MAX(CASE WHEN Process = 'Residential' THEN Entered END) AS 'Residential Entered'
,MAX(CASE WHEN Process = 'Residential' THEN Closed END) AS 'Residential Closed'
FROM [Sales].[dbo].[vSalesReportProcessDetail]
GROUP BY RequestId
)t
INNER JOIN [Sales].[dbo].[vSalesReportProcessUser] t1
ON t1.RequestId = t.RequestId


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -