I have a query like:
SELECT data FROM MSG_1 WHERE created>'2014-02-24' and data like '%2012177%'
here rather than hard coding dataId value '2012177', i need to get this value from query like:
SELECT distinct dataId FROM Item where Src='MKT' and ValueDt>'2014-02-01'.
How can i do that?
you could probably try :
SELECT data FROM MSG_1 WHERE created>'2014-02-24' and data in
(SELECT distinct dataId FROM Item where Src='MKT' and ValueDt>'2014-02-01')
SELECT m.data
FROM MSG_1 m
JOIN (SELECT distinct dataId as d
FROM Item
where Src='MKT' and ValueDt>'2014-02-01'
) t
ON m.data like '%'+t.d+'%'
WHERE m.created>'2014-02-24'
edited to suit sql server.
Hope this helps:
SELECT
data
FROM
MSG_1 M1
JOIN (SELECT distinct dataId FROM Item where Src='MKT' and ValueDt>'2014-02-01') M2
ON M1.Data = concat('%', M2.dataId ,'%')
WHERE
M1.created>'2014-02-24'
Related
could you please help me on the below query?
Using the query
SELECT LineGernal.Id, LineGernal.Description, LineGernal.SSMS, BaseAddOns.Id
from LineGernal Inner Join
BaseAddOns
on LineGernal.Id=BaseAddOns.ParentLineGernalID
Output-
Result Needed-
Thanks
Rajendra
You can use group by and min as follows:
SELECT LineGernal.Id, LineGernal.Description, LineGernal.SSMS, min(BaseAddOns.Id) as id
from LineGernal Inner Join
BaseAddOns
on LineGernal.Id=BaseAddOns.ParentLineGernalID
GROUO BY LineGernal.Id, LineGernal.Description, LineGernal.SSMS
I would recommend pre-aggregation in a subquery:
select li.id as lineGeneralId, lg.description, lg.ssms, bao.id as BaseAddOnsId
from LineGernal lg
inner join (
select ParentLineGernalID, min(id) as id
from BaseAddOns
group by ParentLineGernalID,
) bao on lg.id = bao.ParentLineGernalID
I am trying to create an update query that will update my table values based on pallet number match.
SELECT [pallet] [quantity]
FROM dba.Inventory
This will returns 2 columns, one with pallet and the other with count.
I need to put this in a update statement that will match each pallet between here and table TABLE1 and update the counts in TABLE1
Use Common Table Expression.
Syntax goes like
with CTE_Values()
AS
( --- Your Statement---)
Update T
Set Col = C.col
From Table T Join CTE_Values C
On .....
I think that one easy solution (without evaluating the SELECT itself that looks quite convoluted) is to put the whole SELECT into a CTE and use it as source for your update. Something like this:
;WITH SrcCte AS (
SELECT pallet, SUM(total) as quantity
FROM (
SELECT r1.pallet, total
FROM
(SELECT plet_nbr, COUNT(serl_nbr) as total FROM Inventory group by plet_nbr )c1
right join
(select pallet from dbo.RFS where status NOT LIKE 'Not Available')r1
on r1.pallet = c1.plet_nbr
UNION all
Select r2.pallet, sum(iloc_qty) as total
FROM
(SELECT plet_nbr, iloc_qty FROM Inventory WHERE([matl_nbr] Like '#%')) c2
right join
(select pallet from dbo.RFS where status NOT LIKE 'Not Available') r2
on r2.pallet = c2.plet_nbr
where iloc_qty is not null
GROUP BY r2.pallet
)
AS final
GROUP BY pallet
)
UPDATE Dest
SET Dest.Cnt = Src.Quantity
FROM Table1 AS Dest
JOIN SrcCte AS Src ON Src.pallet = Dest.pallet
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)
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 )
I have some records as in the image. I want to write a sql query to fetch the latest unique records so that I only have two records on the basis of UserAlertTicketID and latest Created Date. In this case the output would be like:
"CB23C56F-B067-415E-AD62-59DF4DA4F26D" "2" "2011-03-04 09:49:59.440" "9EDB3DBC-4685-414D-A48B-04CA8285A2D1"
"9FA4D72B-8BB3-4CE9-BCA2-C334AF47EB30" "3" "2011-03-04 09:05:46.817" "94C67A9C-3818-4AB5-A6F6-CD7BD69FAEC7"
Kindly Help!!!
Thanks
SELECT TOP 2 * FROM table GROUP BY UserAlertTicketID ORDER BY CreatedDate DESC
EDIT: this may not do what you want, however try this:
SELECT TOP 2 T1.* FROM Table T1 LEFT JOIN Table T2
ON (T1.AlertTicketEventID = T2.AlertTicketEventID AND T1.CreatedDate < T2.CreatedDate)
WHERE T2.AlertTicketEventID IS NULL ORDER BY T1.CreatedDate DESC
This method will avoid using aggregates and should be much faster.
Something like this?
SELECT
a.AlertTicketEventId,
a.AlertTicketStatusID,
a.CreatedDate,
a.UserAlertTicketID
FROM <Table> a
INNER JOIN (
SELECT
AlertTicketStatusID
,MAX(CreatedDate) AS LastDate
FROM <Table>
GROUP BY AlertTicketStatusID
) b ON a.AlertTicketStatusID = b.AlertTicketStatusID AND a.CreatedDate = b.CreatedDate