Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= - mysqli

I keep getting this error message and I tried a few thing that I found but nothing has worked. here is my subquery;
icd9_code =(select icd9_code from paragon_rpt.dbo.TSM910_ICD9_REF
where icd9_int_id = (select Top 2 (icd9_int_id) from paragon_rpt.dbo.TPM317_VISIT_PROCEDURE
where vst_int_id =a.vst_int_id
and
icd9_int_id in (Select icd9_int_id from SGMH_Reports.dbo.NHSN_ICD10_2)
)),

changing the = to in worked after I added a ) after a.vst_in_id.
icd9_code =(select icd9_code from paragon_rpt.dbo.TSM910_ICD9_REF
where icd9_int_id in (select Top 2 (icd9_int_id) from paragon_rpt.dbo.TPM317_VISIT_PROCEDURE
where vst_int_id =a.vst_int_id)
and
icd9_int_id in (Select icd9_int_id from SGMH_Reports.dbo.NHSN_ICD10_2)),
Thanks so much for your help!

Related

Exclude Duplicate or Exisisting Values in PostgreSQL

I have report like this:
I tried to remove the duplicate values in documentno as paymentno column using :
DELETE FROM c_payment pa USING (
SELECT MIN(ctid) as ctid, pa.documentno
FROM c_payment pa
GROUP BY pa.documentno
HAVING COUNT(*) > 1
) b
WHERE pa.documentno = b.documentno
AND pa.ctid <> b.ctid) documentno
but it's still not working, any other advice on what method should I use?
You should remove the last closing brace together with the word documentno.
By using sequence number logic will work for my case
ROW_NUMBER() OVER( PARTITION BY pym.c_payment_id ORDER BY pym.c_bpartner_id)
as seq_no, coalesce (ci.documentno, '-') as inv_no ,
instead of DELETE with or without subquery
DELETE FROM c_payment pa USING (
SELECT MIN(ctid) as ctid, pa.documentno
FROM c_payment pa
GROUP BY pa.documentno
HAVING COUNT(*) > 1
) b
WHERE pa.documentno = b.documentno
AND pa.ctid <> b.ctid) documentno

Correlated subquery in order by clause in DB2

I had a query similar to the following and was wondering that DB2 complained about the correlation use in the ORDER BY clause. It errored with something like
[42703][-206] "A.ID" is not valid in the context where it is used..
SQLCODE=-206, SQLSTATE=42703
I was able to rewrite the query to avoid the correlation usage but I couldn't find a reference in the documenation about this. Is this a bug or am I just not able to find details on the expected behavior?
SELECT a.id
FROM A a
ORDER BY (
SELECT COUNT(*)
FROM B b
WHERE b.id = a.id
)
You can't use correlated query in order by clause. However there is many ways to get same result, for example
select count(*) as count_num ,a.ID
from
a join b on a.ID=b.ID
GROUP BY a.ID
order by 1 DESC
solution 1:
SELECT a.id, (select count(*) from B where B.id=a.id) nbOFB
FROM A
order by 2
solution 2:
select * from (
SELECT a.id, (select count(*) from B where B.id=a.id) nbOFB
FROM A
) tmp
order by nbOFB
Solution 3:
SELECT a.id, c.nb
FROM A
inner join lateral
(
select count(*) nb from B where B.id=a.id
) c on 1=1
order by c.nb
Solution 4 :
SELECT a.id, ifnull(c.nb, 0) nb
FROM A
left outer join
(
select b.id, count(*) nb from B group by b.id
) c on a.id=c.id
order by ifnull(c.nb, 0)
Solution 5:
with c as (
select b.id, count(*) nb from B group by b.id
)
SELECT a.id, ifnull(c.nb, 0) nb
FROM A left outer join c on a.id=c.id
order by ifnull(c.nb, 0)

Using EXISTS as a column in TSQL

Is it possible to use the value of EXISTS as part of a query?
(Please note: unfortunately due to client constraints, I need SQLServer 2005 compatible answers!)
So when returning a set of results, one of the columns is a boolean value which states whether the subquery would return any rows.
For example, I want to return a list of usernames and whether a different table contains any rows for each user. The following is not syntactically correct, but hopefully gives you an idea of what I mean...
SELECT T1.[UserName],
(EXISTS (SELECT *
FROM [AnotherTable] T2
WHERE T1.[UserName] = T2.[UserName])
) AS [RowsExist]
FROM [UserTable] T1
Where the resultant set contains a column called [UserName] and boolean column called [RowsExist].
The obvious solution is to use a CASE, such as below, but I wondered if there was a better way of doing it...
SELECT T1.[UserName],
(CASE (SELECT COUNT(*)
FROM [AnotherTable] T2
WHERE T1.[UserName] = T2.[UserName]
)
WHEN 0 THEN CAST(0 AS BIT)
ELSE CAST(1 AS BIT) END
) AS [RowsExist]
FROM [UserTable] T1
Your second query isn't valid syntax.
SELECT T1.[UserName],
CASE
WHEN EXISTS (SELECT *
FROM [AnotherTable] T2
WHERE T1.[UserName] = T2.[UserName]) THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT)
END AS [RowsExist]
FROM [UserTable] T1
Is generally fine and will be implemented as a semi join.
The article Subqueries in CASE Expressions discusses this further.
In some cases a COUNT query can actually perform better though as discussed here
I like the other guys sql better but i just wrote this:
with bla as (
select t2.username, isPresent=CAST(1 AS BIT)
from t2
group by t2.username
)
select t1.*, isPresent = isnull(bla.isPresent, CAST(0 AS BIT))
from t1
left join blah on t1.username=blah.username
From what you wrote here I would alter your first query into something like this
SELECT
T1.[UserName], ISNULL(
(
SELECT
TOP 1 1
FROM [AnotherTable]
WHERE EXISTS
(
SELECT
1
FROM [AnotherTable] AS T2
WHERE T1.[UserName] = T2.[UserName]
)
), 0)
FROM [UserTable] T1
But actually if you use TOP 1 1 you would not need EXISTS, you could also write
SELECT
T1.[UserName], ISNULL(
(
SELECT
TOP 1 1
FROM [AnotherTable] AS T2
WHERE T1.[UserName] = T2.[UserName]
), 0)
FROM [UserTable] T1

How to write a T-SQL query to select top 1 records for each client?

I have a simple script that I am trying to get the most recent records per client on. How do I do this in TSQL? This is my code currently, however, this is only selecting one record total. This one record displays most recent record for ALL clients and not EACH client! How can I reformulate this please?
SELECT TOP 1
C.ClientID, actual_date
From ClientRecords C
WHERE (#ClientID is NULL or C.Client_ID = #ClientID)
Group by C.ClientID, actual_date
ORDER BY C.actual_date
Aggregate the column by using MAX() function on actual_date
SELECT C.ClientID, MAX(actual_date) max_DATE
From ClientRecords C
WHERE (#ClientID is NULL or C.Client_ID = #ClientID)
Group by C.ClientID
ORDER BY C.actual_date
This has not been tested, but it should look something like:
select
c.clientId, max(actual_date) as Actual_date
from clientrecords C
group by c.clientID
order by c.clientID
That will give you the highest actual date for each client, ordered by the clientId.
Thanks guys, but I found a little more satisfactory solutoin to this:
WITH rs AS
(
SELECT
C.ClientID, actual_date,ROW_NUMBER() OVER(ORDER BY C.ClientID, actual_date)rid
From ClientRecords C
)
SELECT * FROM rs WHERE rid =1
You can use SUBQUERY for that purpose:
SELECT
C.ClientID ,
(SELECT MAX(C1.actual_date) FROM ClientRecords C1 WHERE C1.Client_ID = C.Client_ID) AS MaxDate
FROM ClientRecords C
WHERE (#ClientID is NULL or C.Client_ID = #ClientID)
Group by C.ClientID, actual_date
ORDER BY C.actual_date

Error "Invalid column name" in CTE

I'm having an issue using a column alias for a join in a cte. Invalid column name on the line with RowNumber2 >= (t1.RowNumber - 20) Anyone have a suggestion? Thanks..
DECLARE #latestDate Date = dbo.LatestDateWithPricingVolCountOver4k()
;WITH AllSymbsAndDates AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY Symbol ORDER BY TradingDate) AS RowNumber,
Symbol, TradingDate
FROM tblSymbolsMain
CROSS JOIN tblTradingDays
WHERE TradingDate <= #latestDate
),
SymbsDatesGrouped AS
(
SELECT * FROM
(
SELECT
t1.Symbol, t1.TradingDate, t2.TradingDate AS TradingDate2, t1.RowNumber,
t2.RowNumber AS RowNumber2
FROM AllSymbsAndDates t1
JOIN AllSymbsAndDates t2 ON t1.Symbol = t2.Symbol
AND RowNumber2 >= (t1.RowNumber - 20)
) t
)
SELECT
Symbol, TradingDate, TradingDate2, RowNumber, RowNumber2
FROM
SymbsDatesGrouped
ORDER BY
Symbol, TradingDate, TradingDate2
You can't reference a column alias in the WHERE or JOIN clauses - actually the only clause where you can reference an alias from the SELECT list is either in the ORDER BY (or in an outer scope, e.g. selecting from a subquery or CTE).
In this case, the solution is pretty trivial. Why not just say:
AND t2.RowNumber >= (t1.RowNumber - 20)
?