SQL Server select within select invalid column name - sql-server-2008-r2

I am using SQL Server. I have the following query:
select
convert(varchar(10), MAX(closedate), 101)
from
(select PSer.Signin_Date as closedate
from PSer
where ID = '12')
Note that the content within my from is more complicated than the simplified version I have.
I get a message saying
Invalid column name closedate

Make sure you're giving your subquery an alias.
from ( select PSer.Signin_Date as closedate from PSer where ID = '12') AS SOMENAME

use this:
select convert(varchar(10),MAX(t1.closedate),101)
from ( select PSer.Signin_Date as closedate from PSer where ID = '12') as t1
enjoy.

Related

Beginners Alias

SELECT *
FROM car_t
JOIN ( SELECT driver_id, gender, first_name,last_name
FROM driver_t
WHERE gender = 'Male');
I get an error for not using Alias. Can someone help me with that ? Thank you!
I need to do a join statement with a subquery.
It is a common requirement in SQL that every derived table must have an alias (although some databases are lax about it).
Your subquery produces a derived table, that you hence need to alias:
SELECT *
FROM car_t c
JOIN (
SELECT driver_id, gender, first_name,last_name FROM driver_t WHERE gender = 'Male'
) d ON ???
--^ table alias
Notes:
your query is clearly missing a JOIN condition to relate the two tables; it should appear after the ON keyword, that I added to the query
the subquery is actually not necessary here; you could just join the table, then select the needed columns
SELECT
c.*,
d.driver_id,
d.gender,
d.first_name,
d.last_name
FROM car_t c
JOIN driver_t d ON ???
WHERE d.gender = 'Male'
Well the error messages refers to your "derived table" (alias "subquery") which requires a name
SELECT *
FROM car_t
JOIN (
SELECT driver_id, gender, first_name,last_name
FROM driver_t
WHERE gender = 'Male'
) as x --<< the alias
But that will just lead you to the next error because you have no join condition.
So you need something like:
SELECT *
FROM car_t
JOIN (
SELECT driver_id, gender, first_name,last_name
FROM driver_t
WHERE gender = 'Male'
) as x on x.??? = car_t.???
You need to replace the ??? with the columns that link the two tables together.
But you don't really need the derived table anyway. You can simplify this to
SELECT
FROM car_t
JOIN driver_t as x on x.??? = car_t.???
WHERE x.gender = 'Male';

Return row(s) when no values are found in the query

I have a table MyTable that is populated by BizTalk from two outside agencies (AGC1 and AGC2) every day. I have recently run into a problem where AGC2 has been sending a blank file, so no records get updated.
I need to run a query like below, that would return
AGC1 01/28/2016
AGC2 NULL
But since I have no records for AGC2 I only get
AGC1 01/28/2016
How can I get AGC2 NULL as in the first resultset?
SELECT Agency, MAX(CONVERT(nvarchar(30), DATEENTERED, 101)), Case when MAX(CONVERT(nvarchar(30), DATEENTERED, 101)) = CONVERT(nvarchar(30), GETDATE(), 101) THEN 'True' ELSE 'False' end
FROM DBO.MYTABLE
GROUP BY AGENCY
Have another table containing:
NameTable
Name
-----
AGC1
AGC2
Then use this query:
Select n.Name, max(m.dateentered)
from MyTable m right join NameTable n
on m.Agency = n.Name
group by n.Name
A RIGHT JOIN with a table containing all agencies should do the trick. This version uses a derived table with VALUES:
SELECT
ag.Agency,
MAX(CONVERT(nvarchar(30), mt.DATEENTERED, 101)),
Case when MAX(CONVERT(nvarchar(30), mt.DATEENTERED, 101)) = CONVERT(nvarchar(30), GETDATE(), 101) THEN 'True' ELSE 'False' end
FROM
DBO.MYTABLE AS mt
RIGHT JOIN ( VALUES('AGC1'),('AGC2') ) AS ag(Agency) ON
ag.Agency=mt.Agency
GROUP BY
ag.AGENCY

T-SQL how to count the number of duplicate rows then print the outcome?

I have a table ProductNumberDuplicates_backups, which has two columns named ProductID and ProductNumber. There are some duplicate ProductNumbers. How can I count the distinct number of products, then print out the outcome like "() products was backup." ? Because this is inside a stored procedure, I have to use a variable #numrecord as the distinct number of rows. I put my codes like this:
set #numrecord= select distinct ProductNumber
from ProductNumberDuplicates_backups where COUNT(*) > 1
group by ProductID
having Count(ProductNumber)>1
Print cast(#numrecord as varchar)+' product(s) were backed up.'
obviously the error was after the = sign as the select can not follow it. I've search for similar cases but they are just select statements. Please help. Many thanks!
Try
select #numrecord= count(distinct ProductNumber)
from ProductNumberDuplicates_backups
Print cast(#numrecord as varchar)+' product(s) were backed up.'
begin tran
create table ProductNumberDuplicates_backups (
ProductNumber int
)
insert ProductNumberDuplicates_backups(ProductNumber)
select 1
union all
select 2
union all
select 1
union all
select 3
union all
select 2
select * from ProductNumberDuplicates_backups
declare #numRecord int
select #numRecord = count(ProductNumber) from
(select ProductNumber, ROW_NUMBER()
over (partition by ProductNumber order by ProductNumber) RowNumber
from ProductNumberDuplicates_backups) p
where p.RowNumber > 1
print cast(#numRecord as varchar) + ' product(s) were backed up.'
rollback

IS IN (Select statement, 'value', 'value')

I am trying to run the following SQL
DELETE FROM T_ATH_POSHLD WHERE T_ATH_POSHLD.A_INSID IN (SELECT T_ATH_POSHLD.A_INSID FROM T_ATH_POSHLD LEFT JOIN T_ATH_INS ON T_ATH_POSHLD.A_INSID = T_ATH_INS.A_INSID WHERE T_ATH_INS.A_INSCLSCDE1 = 'CASH' AND T_ATH_POSHLD.A_INSID NOT IN (SELECT A_INSID FROM T_ATH_CCY) AND A_ACCID IN (SELECT A_ACCID FROM T_ATH_EXTACC, '1212OEIC', '5667033ZS'))
and in particular, am trying to check whether an ACCID is in a set of values, some coming from a table and two hardcoded. How would I achieve this?
IN (SELECT A_ACCID FROM T_ATH_EXTACC, '1212OEIC', '5667033ZS')
Doesn't work, I get an 'Incorrect Syntax error'.
Thanks
You need to use UNION to add the 2 hardcoded values to the resultset that you are passing to the in clause.
IN (SELECT A_ACCID FROM T_ATH_EXTACC UNION ALL SELECT '1212OEIC' UNION ALL SELECT '5667033ZS')
IN (SELECT '1212OEIC', '5667033ZS', A_ACCID FROM T_ATH_EXTACC )

Having "___ in (select distinct ___ from #temp)" in Case statement

I'm trying to achieve this
select
case
when Org_CD = '1111' or Org_CD in (select distinct New_Org_CD from #temp) then 'International'
end as 'Organisation',
count(*)
from #AnotherTempTable
group by
case
when Org_CD = '1111' or Org_CD in (select distinct New_Org_CD from #temp) then 'International'
end
I received this error:
Column '#AnotherTempTable.Org_Cd' is
invalid in the select list because it
is not contained in either an
aggregate function or the GROUP BY
clause.
Is it because I cannot use "in" keyword inside the case statements? If yes, any known workarounds would be more than helpful!
I'd try this...
select
Org_CD, count(*)
from
#AnotherTempTable A
JOIN
(select distinct New_Org_CD from #temp UNION SELECT '1111') T ON A.Org_CD = T.New_Org_CD
group by
Org_CD
You can't have an inline IN like this (CASE + aggregate)
If this is not OK, please give sample data and output
I solved it with a variation of gbn's solution using 'Union'. Thanks all.