Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting the Error "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS." with this query. I understand it has something to do with the INNER JOIN, but I do not know how to fix it.

SQL
SELECT (Grp.name) AS Wayside, (Grp.state) AS ST,
( select 'G' 
			+ SUBSTRING(CONVERT(VARCHAR(100), CAST(wea AS DECIMAL (38, 2))), 3, 3) + '/' 
			+ SUBSTRING(CONVERT(VARCHAR(100), CAST(WEA AS DECIMAL (38,2))),6,3)
			, Base1 AS 'Primary', AVG(ssi1) OVER (PARTITION BY wea,Base1, CONVERT(DATE,Date_time)) as Primary_SSI
			FROM RT_Group_Status
) AS 'Group_ID', 
(SELECT [Name] + ',' + [State]
 FROM [nms_cfg4].[dbo].[Base_Equipment]
 WHERE Base_Equip_Address = (SELECT TOP 1
                             Base_Equip_Address
                             FROM [nms_cfg4].[dbo].[be_xref_oa]
                             WHERE x_pbase = master.dbo.ufnStringToPbase([base1])
                             )
 ) + '(' + [base1] + ')' AS 'Primary',
( SELECT    [Name] + ',' + [State]
FROM [nms_cfg4].[dbo].[Base_Equipment]
WHERE  Base_Equip_Address = (SELECT TOP 1
                             Base_Equip_Address
                             FROM     [nms_cfg4].[dbo].[be_xref_oa]
                             WHERE    x_pbase = master.dbo.ufnStringToPbase([base2])
                             )
) + '(' + [base2] + ')' AS 'Secondary'
FROM [nms_rt].[dbo].[RT_Group_Status] AS Cov
INNER JOIN [nms_cfg4].[dbo].[ATCS_Group] As Grp ON Grp.Group_Address = Cov.[WEA]
ORDER BY Wayside


What I have tried:

This is my original query which works; I am trying to add an averaging of one column to this query.

SQL
SELECT  ( Grp.name ) AS Wayside, ( Grp.state ) AS ST,
        ( SELECT    'G'
                    + SUBSTRING(CONVERT(VARCHAR(100), CAST(WEA AS DECIMAL(38, 2))), 3, 3) + '/'
                    + SUBSTRING(CONVERT(VARCHAR(100), CAST(WEA AS DECIMAL(38, 2))), 6, 3)
        ) AS 'Group_ID',
        ( SELECT    [Name] + ',' + [State]
          FROM      [nms_cfg4].[dbo].[Base_Equipment]
          WHERE     Base_Equip_Address = ( SELECT TOP 1
                                                    Base_Equip_Address
                                           FROM     [nms_cfg4].[dbo].[be_xref_oa]
                                           WHERE    x_pbase = master.dbo.ufnStringToPbase([base1])
                                         )
        ) + '  ( ' + [base1] + ')' AS 'Primary',
        CAST(Cov.Average_SSI1 as VARCHAR(3)) as 'Primary_SSI',
        ( SELECT    [Name] + ',' + [State]
          FROM      [nms_cfg4].[dbo].[Base_Equipment]
          WHERE     Base_Equip_Address = ( SELECT TOP 1
                                                    Base_Equip_Address
                                           FROM     [nms_cfg4].[dbo].[be_xref_oa]
                                           WHERE    x_pbase = master.dbo.ufnStringToPbase([base2])
                                         )
        ) + '  ( ' + [base2] + ')' AS 'Secondary',
        CAST(Cov.Average_SSI2 as VARCHAR(3)) as 'Secondary_SSI',
        CAST([Date_Time] AS DATE) AS Date
FROM [NMS_RT].[dbo].[RT_Group_Average] AS Cov
INNER JOIN [nms_cfg4].[dbo].[ATCS_Group] AS Grp
          ON Grp.Group_Address = Cov.[WEA]
WHERE Date_Time >= DATEADD(DAY, -1, GETDATE())
ORDER BY Date, Wayside


Thanks,
Posted
Updated 16-Nov-18 3:56am

With a little more work, I was able to get this portion to work as I wanted it to;

SQL
SELECT 'G' 
		+ SUBSTRING(CONVERT(VARCHAR(100), CAST(WEA AS DECIMAL (38, 2))), 3, 3) + '/' 
		+ SUBSTRING(CONVERT(VARCHAR(100), CAST(WEA AS DECIMAL (38,2))),6,3) AS 'Line/Group'
		, (SELECT [Name] + ',' + [State] + '   '
 FROM [nms_cfg4].[dbo].[Base_Equipment]
 WHERE Base_Equip_Address = (SELECT TOP 1
                             Base_Equip_Address
                             FROM [nms_cfg4].[dbo].[be_xref_oa]
                             WHERE x_pbase = master.dbo.ufnStringToPbase([base1])
                             )
 ) + '('+[base1]+')' AS 'Primary Base', AVG(ssi1) OVER (PARTITION BY wea,Base1, CONVERT(DATE,Date_time)) as Primary_SSI
			FROM RT_Group_Status


I moved this portion up in to the subquery;

SQL
FROM [nms_cfg4].[dbo].[Base_Equipment]
WHERE Base_Equip_Address = (SELECT TOP 1
                            Base_Equip_Address
                            FROM [nms_cfg4].[dbo].[be_xref_oa]
                            WHERE x_pbase = master.dbo.ufnStringToPbase([base1])
                            )
) + '('+[base1]+')' AS 'Primary Base',


However that does not help for the overall.

Thanks,
 
Share this answer
 
SQL
( select 'G' 
    + SUBSTRING(CONVERT(VARCHAR(100), CAST(wea AS DECIMAL (38, 2))), 3, 3) + '/' 
    + SUBSTRING(CONVERT(VARCHAR(100), CAST(WEA AS DECIMAL (38,2))),6,3), 
    Base1 AS 'Primary', 
    AVG(ssi1) OVER (PARTITION BY wea,Base1, CONVERT(DATE,Date_time)) as Primary_SSI
    FROM RT_Group_Status
) AS 'Group_ID', 

You're trying to return three separate values in a single column. That's not going to work.

You need to separate the columns; and you don't need the sub-queries:
SQL
'G' 
    + SUBSTRING(CONVERT(VARCHAR(100), CAST(wea AS DECIMAL (38, 2))), 3, 3) + '/' 
    + SUBSTRING(CONVERT(VARCHAR(100), CAST(WEA AS DECIMAL (38,2))),6,3), 
As 'Group_ID', 
Base1 As 'Primary',
AVG(ssi1) OVER (PARTITION BY wea, Base1, CONVERT(date, Date_time)) As Primary_SSI
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900