Seems I cannot select the temporary table's column by the column name it inherits from the views I'm selecting from. I get the error: "Msg 207, Level 16, State 1, Procedure GetConsolidatedData, Line 44 [Batch Start Line 0]
Invalid column name 'comp_id'."
select *
into #tmp0
from Competitor_View
where [Competitor Name] like #competitor_name;
select *
into #tmp1
from #tmp0
where [Competitor Part Number] like #competitor_part;
select *
into #CustomerResult
from #tmp1
where [Koyo Part Number] like #koyo_part;
--------------
select *
into #tmp3
from Customer_View
where [Customer Name] like #customer_name;
select *
into #tmp4
from #tmp3
where [Customer Part Number] like #customer_part;
select *
into #CompetitorResult
from #tmp4
where [Koyo Part Number] like #koyo_part;
--everything after this fails
select
#CompetitorResult.comp_id,
#CompetitorResult.[Competitor Part Number],
#CompetitorResult.[Competitor Comment],
#CompetitorResult.[Competitor Name],
#CompetitorResult.[Koyo Part Number] as [Associated Koyo Part Number],
#CustomerResult.cust_id,
#CustomerResult.[Customer Part Number],
#CustomerResult.[Customer Comment],
#CustomerResult.[Customer Name]
from #CompetitorResult
join #CustomerResult
on #CompetitorResult.[Koyo Part Number] = #CustomerResult.[Koyo Part Number];
I know I've got the right name for the column because running the following confirms that:
SELECT *
FROM tempdb.sys.columns
WHERE object_id = OBJECT_ID('tempdb..#CompetitorResult')
Your Query should look something like this... (without any temp tables)
select cp.comp_id
,cp.[Competitor Part Number]
,cp.[Competitor Comment]
,cp.[Competitor Name]
,cp.[Koyo Part Number] as [Associated Koyo Part Number]
,cu.cust_id
,cu.[Customer Part Number]
,cu.[Customer Comment]
,cu.[Customer Name]
from Competitor_View cp
INNER JOIN Customer_View cu ON cp.[Koyo Part Number] = cu.[Koyo Part Number]
where cp.[Koyo Part Number] like #koyo_part
AND cp.[Competitor Part Number] like #competitor_part
AND cp.[Competitor Name] like #competitor_name
AND cu.[Customer Name] like #customer_name
AND cu.[Customer Part Number] like #customer_part
AND cu.[Koyo Part Number] like #koyo_part
Related
This is my code for a query to produce a specific report. This works, however when I added the last select statement
(Select Name
from RPT_CUSTOM_LIST_VALUES
where CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_10) AS [Application]
The RI1.Cust_10 column holds multiple values delimited by commas. How can I get it so that the look up table pulls each value and provides the correct name for that value? I cannot create or modify the tables within this database.
select
RI1.incident_id as [Project Incident #],
(Select Name from RPT_CUSTOM_LIST_VALUES
where CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_02) as [Business Name],
RI1.NAME as [Project Name],
RI1.INCIDENT_STATUS_NAME as [Phase],
(Select Name from RPT_CUSTOM_LIST_VALUES
where CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_09) as [Key Milestone Name],
convert(nvarchar(10), RI1.CUST_26,103) as [Key Milestone Date], -- leave as date
convert(nvarchar(10), RI1.CUST_29,103) as [Target Completion Date], -- leave as date
RI1.SEVERITY_NAME as [Status Color],
RI1.CUST_01 as [Status Summary],
RI1.OWNER_NAME as [IT Owner],
(Select Name from RPT_CUSTOM_LIST_VALUES
where CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_10) AS [Application]
from
RPT_INCIDENTS RI1
where
RI1.PROJECT_ID = 445
and RI1.IS_DELETED = 0
and (RI1.INCIDENT_STATUS_NAME <> '5.1-Cancelled' and RI1.INCIDENT_STATUS_NAME <> '5.2-Completed')
My output should be, however, the last column should have names not values. The values are from the Lookup table and I need a way to pull that data so that the values are now names.
Report Output
I can't test without your data, but hopefully it will work for you:
select RI1.incident_id as [Project Incident #]
, [Business Name] = s1.Name
,RI1.NAME as [Project Name]
,RI1.INCIDENT_STATUS_NAME as [Phase]
, [Key Milestone Name] = s2.Name
,convert(nvarchar(10), RI1.CUST_26,103) as [Key Milestone Date] -- leave as date
,convert(nvarchar(10), RI1.CUST_29,103) as [Target Completion Date] -- leave as date
,RI1.SEVERITY_NAME as [Status Color]
,RI1.CUST_01 as [Status Summary]
,RI1.OWNER_NAME as [IT Owner]
, [Application] = LEFT(s3.App,LEN(APP) - SIGN(LEN(s3.APP)))
From RPT_INCIDENTS AS RI1
OUTER APPLY (Select TOP 1 i.Name from RPT_CUSTOM_LIST_VALUES as i where i.CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_02) as s1
OUTER APPLY (Select TOP 1 i.Name from RPT_CUSTOM_LIST_VALUES as i where i.CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_09) as s2
OUTER APPLY (SELECT App = (
Select i.Name + ','
from RPT_CUSTOM_LIST_VALUES as i
where i.CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_10
FOR XML PATH(''))
) as s3
where RI1.PROJECT_ID = 445
and RI1.IS_DELETED = 0
and (RI1.INCIDENT_STATUS_NAME <> '5.1-Cancelled' and RI1.INCIDENT_STATUS_NAME <> '5.2-Completed')
Want to tune the performance by reducing the number of SELECT on OPENQUERY by doing JOIN operation.
Here is how my query look like.
Query:
;WITH CTE AS
(
SELECT
[EmployeeFirstName], [EmployeeMiddleName],
[EmployeeLastName], [EmployeeAddress]
FROM
Employee
WHERE
[EmployeeSalary] >= 200000
)
SELECT
[First Name], [Fathers Husband Name], [Last Name], [Department Name]
FROM
OPENQUERY ([Depart], 'SELECT [First Name], [Fathers Name], [Last Name],[Department Name]
FROM Department
INNER JOIN CTE C1 ON PATINDEX(''[First Name]'',''C1.[EmployeeFirstName]'') > 0');
Error:
Invalid object name 'CTE'
Try the following
;WITH CTE AS
(
SELECT
[EmployeeFirstName], [EmployeeMiddleName],
[EmployeeLastName], [EmployeeAddress]
FROM
LocalServer.Database.Schema.Employee
WHERE
[EmployeeSalary] >= 200000
)
SELECT
[First Name], [Fathers Husband Name], [Last Name], [Department Name]
--It's better to specify the table eg: C1.ColumnName
FROM
(
SELECT [First Name], [Fathers Name], [Last Name],[Department Name]
FROM LinkedServer.Database.Schema.Department
INNER JOIN CTE C1
ON PATINDEX([First Name],C1.[EmployeeFirstName]) > 0
) TBL;
i have used below sub-query for returning maximum data of each (Document Number) from my query but it returns just one record while i have many records.
SELECT tblTransmittals.[Owner Document Number]
, tblTransmittals.[Int-Tr- NO]
, tbltransmittalNo.[Internal-Tr-Date]
FROM tblTransmittals
INNER JOIN tbltransmittalNo
ON tblTransmittals.[Int-Tr- NO] = tbltransmittalNo.[Int-Tr-NO]
WHERE (((tbltransmittalNo.[Internal-Tr-Date])=
(SELECT Max(A.[Internal-Tr- Date])
FROM (SELECT tblTransmittals.[Owner Document Number]
, tbltransmittalNo. [Internal-Tr-Date]
FROM tblTransmittals
INNER JOIN tbltransmittalNo
ON tblTransmittals.[Int-Tr-NO] = tbltransmittalNo.[Int-Tr-NO]
) AS A
WHERE A.[owner Document Number]=[tblTransmittals].[Owner Document Number])));
i mean if data of query is like below :
Owner Document Number Int-Tr-NO Internal-Tr-Date
doc-0001 tt-0002 10-oct-2016
doc-0001 tt-0003 12-0ct-2017
doc-0005 tt-0100 18-sep-2015
i would like after using subquery it returns:
Owner Document Number Int-Tr-NO Internal-Tr-Date
doc-0001 tt-0003 12-0ct-2017
doc-0005 tt-0100 18-sep-2015
i tried and checked everything but i do not know why does not work?
Not totally clear what you want here and I am assuming this is sql server. Think you want something like this.
select *
from
(
SELECT t.[Owner Document Number]
, t.[Int-Tr- NO]
, tn.[Internal-Tr-Date]
, RowNum = ROW_NUMBER() over (partition by t.[Owner Document Number] order by tbltransmittalNo.[Internal-Tr-Date] desc)
FROM tblTransmittals t
INNER JOIN tbltransmittalNo tn ON t.[Int-Tr- NO] = tn.[Int-Tr-NO]
) x
where x.RowNum = 1
SELECT tblTransmittals.[Owner Document Number], tblTransmittals.[Int-Tr-NO], tbltransmittalNo.[Internal-Tr-Date]
FROM tblTransmittals INNER JOIN tbltransmittalNo ON tblTransmittals.[Int-Tr-NO] = tbltransmittalNo.[Int-Tr-NO]
WHERE (((tbltransmittalNo.[Internal-Tr-Date])=
(SELECT Max([Internal-Tr-Date])
FROM (SELECT T.[Owner Document Number], T.[Int-Tr-NO], TN.[Internal-Tr-Date]
FROM tblTransmittals AS T LEFT JOIN tbltransmittalNo AS TN ON T.[Int-Tr-NO] = TN.[Int-Tr-NO]) AS A
WHERE A.[owner Document Number]=[tblTransmittals].[Owner Document Number])));
Your query is totally incorrect as it only returns one row due to the max() on your sub-query:
(SELECT Max(A.[Internal-Tr- Date])
FROM (SELECT tblTransmittals.[Owner Document Number], tbltransmittalNo. [Internal-Tr-Date]
FROM tblTransmittals INNER JOIN tbltransmittalNo
ON tblTransmittals.[Int-Tr-NO] = tbltransmittalNo.[Int-Tr-NO]) AS A
WHERE A.[owner Document Number]=[tblTransmittals].[Owner Document Number]);
##this query only returns 1 value
In your scenario above, the max() query will return "12-0ct-2017" and your final query will look like this:
SELECT tblTransmittals.[Owner Document Number], tblTransmittals.[Int-Tr- NO], tbltransmittalNo.[Internal-Tr-Date]
FROM tblTransmittals INNER JOIN tbltransmittalNo
ON tblTransmittals.[Int-Tr- NO] = tbltransmittalNo.[Int-Tr-NO]
WHERE (((tbltransmittalNo.[Internal-Tr-Date])= '20171012'
I'm guessing that you want to return max(date) on each of the [Owner Document Number] and so I would do this:
SELECT A.[Owner Document Number], A.[Int-Tr- NO], B.[Internal-Tr-Date]
FROM tblTransmittals as A INNER JOIN tbltransmittalNo as B
ON A.[Int-Tr- NO] = B.[Int-Tr-NO]
JOIN
(SELECT C.[Owner Document Number], max(D.[Internal-Tr-Date])
FROM tblTransmittals as C INNER JOIN tbltransmittalNo as D
ON C.[Int-Tr-NO] = D.[Int-Tr-NO] GROUP BY C.[Owner Document Number]) AS E
ON A.[Owner Document Number] = E.[Owner Document Number]
and B.[Internal-Tr-Date] = E.[Internal-Tr-Date]
Good luck.
I am using Microsoft SQL Server 2012 and I have this query (below). It returns everything correctly but I cannot figure out how to get only the 4th row to return.
I know how to do it with a simple query but with a query this complex I don't know how to only return the 4th row. Since I am using MS SQL Server, I cannot use Limit/Offset. How and where should I place code to only print the 4th row? I'm stumped. Thanks.
SELECT T.[First Name],
T.[Last Name],
T.[Middle Name],
T.[Country Name],
T.[Designation Name],
Salary.Emp_Salary AS 'Current Salary'
FROM
(
SELECT
Emp_First_Name AS "First Name",
Emp_Last_Name AS "Last Name",
Emp_Middle_Name AS "Middle Name",
Country_Name AS "Country Name",
Desig_Name as "Designation Name",
MAX(Emp_Salary_Change_Year) AS "Time"
FROM Employee_Details
INNER JOIN Country ON Employee_Details.Emp_Country_Id = Country.Country_Id
INNER JOIN State ON Employee_Details.Emp_State_Id = State.State_Id
INNER JOIN Designation ON Employee_Details.Desig_Id = Designation.Desig_Id
INNER JOIN Salary ON Employee_Details.Emp_Id = Salary.Emp_Id
GROUP BY Emp_First_Name, Emp_Last_Name,
Emp_Middle_Name, Country_Name, Desig_Name) AS T
INNER JOIN Salary ON T.Time = Salary.Emp_Salary_Change_Year
ORDER BY [Current Salary];
Use a CTE with ROW_NUMBER()
;WITH Cte AS
(
SELECT Col1, Col2, ...,
ROW_NUMBER() OVER (ORDER BY ...) AS RowNum
FROM Table
)
SELECT *
FROM Cte
WHERE RowNum = 4
You can also, use the OFFSET/FETCH keyword.
SELECT *
FROM yourTable
ORDER by yourSortColumn
OFFSET 3 ROWS -- Skip Three rows.
FETCH FIRST 1 ROW ONLY;
You can use TOP this way (first you select only 4 rown and the ordering desc select 1 row)
select TOP 1 from (
SELECT TOP 4 T.[First Name],
T.[Last Name],
T.[Middle Name],
T.[Country Name],
T.[Designation Name],
Salary.Emp_Salary AS 'Current Salary'
FROM
(
SELECT
........
) AS T
INNER JOIN Salary ON T.Time = Salary.Emp_Salary_Change_Year
ORDER BY [Current Salary])
ORDER BY [Current Salary] DESC
How to rewrite the below code as working sql. I need to retrieve 'item' from another table based on my new 'newcol'
The code is.
select
s.code,
o.[work name],
case when o.[work name] is null
then s.code
else o.[work name]
end newcol,
item=(select item from table1 where table1.code=newcol)
from
shoplog s
left outer join odc o on o.barcode=s.code
You can use sub-query to achieve what you need.
select a.code, a.[work name], a.newcol, b.code as item
from
(select
s.code,
o.[work name],
case when o.[work name] is null
then s.code
else o.[work name]
end newcol
from
#shoplog s
left outer join #odc o on o.barcode=s.code
)a
inner join
#table1 b
on (a.newcol = b.code)