SELECT user_info.s_name, user_info.name, user_info.f_name, user_info.usr_id, user_info.img_path, Village_master.v_nm
FROM Village_master
INNER JOIN User_reg_master ON Village_master.v_id = User_reg_master.v_id
INNER JOIN user_info ON User_reg_master.usr_id = user_info.usr_id
WHERE user_info.usr_id NOT LIKE #u_id
AND user_info.usr_id NOT LIKE (
SELECT pers_dict_master.pers_dict_ids
FROM pers_dict_ids
WHERE pers_dict_master.usr_id=#u_id
)
usr_id| pers_dict_usr_id
1 | 13
1 | 6
The problem you are facing is this ,
user_info.usr_id NOT LIKE
(SELECT pers_dict_master.pers_dict_ids
FROM pers_dict_ids
WHERE pers_dict_master.usr_id=#u_id)
This is your sub query
(SELECT pers_dict_master.pers_dict_ids
FROM pers_dict_ids
WHERE pers_dict_master.usr_id=#u_id)
It is fetching two columns.But In the LIKE command you are using a one Column.
So make it work USE NOT INCOMMAND
SELECT user_info.s_name,
user_info.name,
user_info.f_name,
user_info.usr_id,
user_info.img_path,
Village_master.v_nm
FROM Village_master
INNER JOIN User_reg_master ON Village_master.v_id = User_reg_master.v_id
INNER JOIN user_info ON User_reg_master.usr_id = user_info.usr_id
WHERE user_info.usr_id NOT LIKE #u_id
AND user_info.usr_id NOT IN
( SELECT pers_dict_master.pers_dict_ids
FROM pers_dict_ids
WHERE pers_dict_master.usr_id=#u_id )
You should probably include some more detail about your issue. Is the sub query expected to return more than one result? If so then you could simply use TOP(ie. select top 1 ...) to get just a single result and add an ORDER BY to the sub query if you want to get a certain sorted top value from that result set.
If the issue is that the sub query returns more than 1 result when it shouldn't, then your problem lies deeper. It looks like you may have meant to use IN for the subquery and for a different comparison, but it's difficult to tell. Perhaps you meant this?:
user_info.usr_id NOT IN
(SELECT pers_dict_master.pers_dict_ids
FROM pers_dict_ids WHERE pers_dict_master.usr_id=#u_id)
Related
I'm trying to write a nested TOP + DISTINCT query in Teradata SQL. My query looks like this:
SELECT TOP 5
*
FROM
(SELECT DISTINCT k_name.KUNDE_NAME1
FROM DB_DWH_MART_AKM_PLT.VW_F_EVENT f_ev
INNER JOIN DBX_DWH_SBX_AKM_PRD.TB_KUNDE_EKP_NAME_AKTUELL k_name ON f_ev.AUFTRAGGEBER_EKP = k_name.EKP
WHERE f_ev.PROCESS_NO = 1075)
I get an error:
Expected something like a name or a Unicode delimited identifier... between ) and ;".
I don't know what I did wrong.
The DISTINCT query would execute correctly on its own.
Why not use a group by?
SELECT TOP 5 k_name.KUNDE_NAME1
FROM DB_DWH_MART_AKM_PLT.VW_F_EVENT f_ev
INNER JOIN DBX_DWH_SBX_AKM_PRD.TB_KUNDE_EKP_NAME_AKTUELL k_name ON f_ev.AUFTRAGGEBER_EKP = k_name.EKP
WHERE f_ev.PROCESS_NO = 1075
GROUP BY k_name.KUNDE_NAME1 --instead of using distinct
ORDER BY k_name.KUNDE_NAME1; --remove or modify as needed
This question already has answers here:
SQL join: selecting the last records in a one-to-many relationship
(13 answers)
Closed 6 years ago.
I wrote a query to compare 2 columns in different tables (TRELAY VS TUSERDEF8). The query works great, except that it retrieves the top record in the TUSERDEF8 table which has a many to one relationship to the TRELAY table.
The tables are linked by TRELAY.ID = TUSERDEF8.N01. I would like to retrieve the latest record from TUSERDEF8 and compare that record with the TRELAY record. I plan to use the max value of the index column (TUSERDEF8.ID) to determine the latest record.
I am using SQL Server.
My code is below, but I'm not sure how to change the query to retrieve the last TUSERDEF8 record. Any help is appreciated.
SELECT
TRELAY.ID, TRELAY.S15,
TUSERDEF8.S04, TUSERDEF8.N01, TUSERDEF8.S06
FROM
TRELAY
INNER JOIN
TUSERDEF8 ON TRELAY.ID = TUSERDEF8.N01
WHERE
LEFT(TRELAY.S15, 1) <> LEFT(TUSERDEF8.S04, 1)
AND NOT (TRELAY.S15 LIKE '%MEDIUM%' AND
TUSERDEF8.S04 LIKE '%N/A%' AND
TUSERDEF8.S06 LIKE '%EACMS%')
Making the assumption that your IDs are int(s) then the below might work?
SELECT TOP 1 TRELAY.ID, TRELAY.S15, TUSERDEF8.S04, TUSERDEF8.N01, TUSERDEF8.S06
FROM TRELAY INNER JOIN TUSERDEF8
ON TRELAY.ID = TUSERDEF8.N01
WHERE LEFT(TRELAY.S15, 1) <> LEFT(TUSERDEF8.S04, 1)
AND NOT (
TRELAY.S15 LIKE '%MEDIUM%'
AND TUSERDEF8.S04 LIKE '%N/A%'
AND TUSERDEF8.S06 LIKE '%EACMS%'
)
ORDER BY TUSERDEF8.ID DESC
HTH
Dave
You could do this:
With cteLastRecord As
(
Select S04, N01, S06,
Row_Number() Over (Partition By N01, Order By ID Desc) SortOrder
From TUSERDEF8
)
SELECT
TRELAY.ID, TRELAY.S15,
TUSERDEF8.S04, TUSERDEF8.N01, TUSERDEF8.S06
FROM
TRELAY
INNER JOIN
(Select S04, N01, S06 From cteLastRecord Where SortOrder = 1) TUSERDEF8 ON TRELAY.ID = TUSERDEF8.N01
WHERE
LEFT(TRELAY.S15, 1) <> LEFT(TUSERDEF8.S04, 1)
AND NOT (TRELAY.S15 LIKE '%MEDIUM%' AND
TUSERDEF8.S04 LIKE '%N/A%' AND
TUSERDEF8.S06 LIKE '%EACMS%')
I believe that your expected output is still a little ambiguous.
It sounds to me like you want only the record from the output where TUSERDEF8.ID is at its max. If that's correct, then try this:
SELECT TRELAY.ID, TRELAY.S15, TUSERDEF8.S04, TUSERDEF8.N01, TUSERDEF8.S06
FROM TRELAY
INNER JOIN TUSERDEF8 ON TRELAY.ID = TUSERDEF8.N01
WHERE LEFT(TRELAY.S15, 1) <> LEFT(TUSERDEF8.S04, 1)
AND NOT (TRELAY.S15 LIKE '%MEDIUM%' AND
TUSERDEF8.S04 LIKE '%N/A%' AND
TUSERDEF8.S06 LIKE '%EACMS%')
AND TUSERDEF8.ID IN (SELECT MAX(TUSERDEF8.ID) FROM TUSERDEF8)
EDIT: After reviewing your recent comments, it would seem something like this would be more suitable:
SELECT
, C.ID
, C.S15,
, D.S04
, D.N01
, D.S06
FROM (
SELECT A.ID, A.S15, MAX(B.ID) AS MaxID
FROM TRELAY AS A
INNER JOIN TUSERDEF8 AS B ON A.ID = B.N01
WHERE
LEFT(A.S15, 1) <> LEFT(B.S04, 1)
AND NOT (A.S15 LIKE '%MEDIUM%' AND
B.S04 LIKE '%N/A%' AND
B.S06 LIKE '%EACMS%')
GROUP BY A.ID, A.S15
) AS C
INNER JOIN TUSERDEF8 AS D ON C.ID = D.N01 AND C.MaxID = D.ID
Using an ID column to determine which row is "last" is a bad idea
Using cryptic table names like "TUSERDEF8" (how is it different from TUSERDEF7) is a very bad idea, along with completely cryptic column names like "S04".
Using prefixes like "T" for table is a bad idea - it should already be clear that it's a table.
Now that all of that is out of the way:
SELECT
R.ID,
R.S15,
U.S04,
U.N01,
U.S06
FROM
TRELAY R
INNER JOIN TUSERDEF8 U ON U.N01 = R.ID
LEFT OUTER JOIN TUSERDEF8 U2 ON
U2.N01 = R.ID AND
U2.ID > U.ID
WHERE
U2.ID IS NULL AND -- This will only happen if the LEFT OUTER JOIN above found no match, meaning that the row in U has the highest ID value of all matches
LEFT(R.S15, 1) <> LEFT(U.S04, 1) AND
NOT (
R.S15 LIKE '%MEDIUM%' AND
U.S04 LIKE '%N/A%' AND
U.S06 LIKE '%EACMS%'
)
I have two Tables as below..
tbPatientEncounter
tbVoucher
when i execute select query as below
Select EncounterIDP,EncounterNumber from tbPatientEncounter
it returens me 180 rows. and
Select VoucherIDP,EncounterIDF from tbVoucher
above query returns me 165 rows.
but i want to execute select query that returns me data like EncounterIDP not in tbVoucher, for that i have tried below Select query...
Select * from tbPatientEncounter pe
where pe.EncounterIDP not in
(Select v.EncounterIDF from tbVoucher v )
it doesn't returns any row. in first image it shows EncounterIDP 9 in tbPatientEncounter, but it not inserted in tbVoucher for that i have tried select Query like
Select * from tbVoucher where EncounterIDF = 9
it returns me 0 rows.
My question is what is wrong with my above Not In Query.?
In all likelihood, the problem is NULL values in tbVoucher. Try this:
Select *
from tbPatientEncounter pe
where pe.EncounterIDP not in (Select v.EncounterIDF
from tbVoucher v
where v.EncounterIDF is not NULL
)
Are you comparing the correct fields in tbVoucher?
Try using a left join
Select EncounterIDP,EncounterNumber from tbPatientEncounter
left join tbVoucher on EncounterIDP = EncounterIDF
where EncounterIDF is null
Call me a skeptic because I don't see anything wrong with your query. Is this really all in the query or did you simplify it for us?
Select * from tbPatientEncounter pe
where pe.EncounterIDP not in
(Select v.EncounterIDF from tbVoucher v )
This is probably a stupid question to most of you but I was wondering whether you can rename a column using the 'AS' keyword and a select statement?
Here is my SQL:
Select Main.EmpId
, Associate_List.costCenter, Assignments.Area
, Main.Assignments_1 AS (
Select Assignment_Name
from Assignments
where Assignment_Number = 1
and Assignments.Area = '#Someparemeter'
)
from associate_list
, main
, APU_CC
, Assignments
where Main.Empid = Associate_List.Empid
and substring(Associate_List.CostCenter,1,4) = APU_CC.CostCentre
The only part of SQL I'm wondering about is:
Main.Assignments_1 AS (
Select Assignment_Name
from Assignments
where Assignment_Number = 1
and Assignments.Area = '#Someparemeter'
)
Is this possible or am I talking jibberish or is this just a stupid thing to do?
Many Thanks
The part after as is not a value but a variable name; the SQL database will use it to reference the value of the result set so you can compare/sort/filter them. Therefore this is not possible.
If you must do this, you must read the documentation of your database how to build dynamic queries. But I suggest against it because it will cause strange errors that will be very hard to debug.
I'm not quite sure what you are driving at.
If there is a column in the Main table called Assignments_1, you can rename it in your query (to give a different header at the top of the output or for some other reason) like this...
SELECT MAIN.ASSIGNMENT_1 AS MY_NEW_NAME
FROM etc.
If you want a derived table in your query you name it like this...
SELECT MAIN.ASSIGNMENT_1,
SELECT *
FROM (SELECT THIS, THAT, THE_OTHER
FROM SOME_TABLE) AS DERIVED_TABLE
FROM etc.
If you didn't want either of those things, please clarify and we'll try to help.
In SQL Server, you can assign an alias to a column with AS like so:
...
ColumnName AS ColumnAlias,
...
And you can do it for a "sub-select" like you have in your example. (I wouldn't write the query quite like that--I'd run the subquery first and then plop the result into the second query like so:
DECLARE #Assignment_Name varca(100) -- or however long
SELECT #Assignment_Name = Assignment_Name
from Assignments
where Assignment_Number = 1
and Assignments.Area = #Someparemeter
SELECT
...
#Assignment_Name = Assignment_Name,
...
But you can do this:
Select m.EmpId, l.costCenter,
(Select Area From Assignments a
Where Assignment_Number = 1
And Area = '#Someparemeter') As Area,
(Select Assignment_Name From Assignments a
Where Assignment_Number = 1
And Area = '#Someparemeter') As Assignments_1
From associate_list l
Join main m On m.Empid = l.Empid
Join APU_CC c On c.CostCentre = substring(l.CostCenter,1,4)
or this:
Select m.EmpId, l.costCenter, Asgn.Area,
Asgn.Assignment_Name as Assignments_1
From associate_list l
Join main m On m.Empid = l.Empid
Join APU_CC c On c.CostCentre = substring(l.CostCenter,1,4)
Cross Join (Select Assignment_Name From Assignments a
Where Assignment_Number = 1
And Area = '#Someparemeter') as Asgn
I'm trying to get a stored procedure to work using the following syntax:
select count(sl.Item_Number)
as NumOccurrences
from spv3SalesDocument as sd
left outer join spv3saleslineitem as sl on sd.Sales_Doc_Type = sl.Sales_Doc_Type and
sd.Sales_Doc_Num = sl.Sales_Doc_Num
where
sd.Sales_Doc_Type='ORDER' and
sd.Sales_Doc_Num='OREQP0000170' and
sl.Item_Number = 'MCN-USF'
group by
sl.Item_Number
having count (distinct sl.Item_Number) = 0
In this particular case when the criteria is not met the query returns no records and the 'count' is just blank. I need a 0 returned so that I can apply a condition instead of just nothing.
I'm guessing it is a fairly simple fix but beyond my simple brain capacity.
Any help is greatly appreciated.
Wally
First, having a specific where clause on sl defeats the purpose of the left outer join -- it bascially turns it into an inner join.
It sounds like you are trying to return 0 if there are no matches. I'm a T-SQL programmer, so I don't know if this will be meaningful in other flavors... and I don't know enough about the context for this query, but it sounds like you are trying to use this query for branching in an IF statement... perhaps this will help you on your way, even if it is not quite what you're looking for...
IF NOT EXISTS (SELECT 1 FROM spv3SalesDocument as sd
INNER JOINs pv3saleslineitem as sl on sd.Sales_Doc_Type = sl.Sales_Doc_Type
and sd.Sales_Doc_Num = sl.Sales_Doc_Num
WHERE sd.Sales_Doc_Type='ORDER'
and sd.Sales_Doc_Num='OREQP0000170'
and sl.Item_Number = 'MCN-USF')
BEGIN
-- Do something...
END
I didn't test these but off the top of my head give them a try:
select ISNULL(count(sl.Item_Number), 0) as NumOccurrences
If that one doesn't work, try this one:
select
CASE count(sl.Item_Number)
WHEN NULL THEN 0
WHEN '' THEN 0
ELSE count(sl.Item_Number)
END as NumOccurrences
This combination of group by and having looks pretty suspicious:
group by sl.Item_Number
having count (distinct sl.Item_Number) = 0
I'd expect this having condition to approve only groups were Item_Number is null.
To always return a row, use a union. For example:
select name, count(*) as CustomerCount
from customers
group by
name
having count(*) > 1
union all
select 'No one found!', 0
where not exists
(
select *
from customers
group by
name
having count(*) > 1
)