INNER JOIN WITH LIKE AND PREFIX - sql

ITEM1 in table A and B-ITEM1 in Table B, I want to join them so A-ITEM1 = B-ITEM with an inner join since they are the same thing only with different prefix. Any help would be appreciated.
/*My scripts*/
SELECT TOP 50
A.ITEMNMBR AS 'Item Number',
A.QTYONHND AS 'Quantity On Hand'
FROM [NSR].dbo.[IV00102] A
INNER JOIN [ART].dbo.[IV00101] B
ON A.ITEMNMBR = B.ITEMNMBR
ORDER BY A.ITEMNMBR
/The problem with that part is in NSR database the itemnumber is NSR-ITEM1 and for the ART database the item number = ART-ITEM1/
/I want the result to show... because is the same item/
ITEMNMBR QTYONHND
NSR-ITEM1 12
ART-ITEM1 12

You can use string functions in ON clauses.
Perhaps this one will help.
ON REPLACE(A.ITEMNBR, 'NSR-', 'ART-') = B.ITEMNBR
This will "repair" values like NSR-ITEM1 by turning them into ART-ITEM1. Then they can be tested for equality.
But, to be clear, this is not a great solution to your problem. It's brittle because you may have ITEMNBR values that don't follow the NSR-whatever pattern. Those will not match.

Related

how to do if query in postgres

I want to make a query in which if the query does not find records for the query not exist, then it looks to see if these codes are present in the array field. The join will go through the code c_oplmlp/c_serv
select distinct ocf.code_head_mo , cs.c_oplmp, r."name", ocf.code_state, rm2.name_short,
ocf.id_condition_mc
from puomp.op_registry r
join puomp.op_case_finish ocf on r.id_registry =ocf.id_registry
join puomp.op_patient pt on ocf.id_patient =pt.id_patient
join puomp.op_case sl on ocf.id_case_finish =sl.id_case_finish
join puomp.op_case_ext cs on sl.id_case =cs.id_case
left join nsi.ref_mo rm2 on r.id_head_mo =rm2.id_mo
where not exists(select * from lic_app_new lm
left join ksg_p kp
where
lm.fc_mo=ocf.code_head_mo
and (lm.c_serv =cs.c_oplmp) if( (lm.c_serv !=cs.c_oplmp) then
lm.c_serv=kp.smj_prof )
endif;
)
ksg_p table look like this
ksg_p
|---id
|---name
|---c_prof(same as c_serv)(main code)
|---smj_prof(related to c_prof)
The idea is if query does not find by main code(c_serv) it search by related codes. This is codes of illneses. Smj_prof contains related codes of illness code. For example ucler has a code of 3( witch is code of gastroenterology) and it has related codes of 9(therapy) and 10(pediatrics). It means that pacient can be treated in gastroenterology, therapy and pediatrics
Help pls i dont even had an idea of how to do this.
What if you need just add public. to SELECT options
example:
select distinct public.ocf.code_head_mo, public.cs.c_oplmp, ...etc...

Calculate number of distinct instances of value in column

long time lurker. I've searched and searched though none of the solutions work for me.
I'm working in a Sybase (ASE) db (most mssql/mysql transactional db solutions will work just fine)
In my example, I'm trying to calculate/count the number of times a specific 'party_id' is listed in a column. The problem I'm having is that it's only counting FOR each row- so of course the count is always going to be 1.
See output:
(I would like for party_id 130568 to show '2' in the refs column, 125555 to show '5', etc.)
output
Here is my query:
select
count(distinct p.party_id) as refs,
p.party_id,
sp_first_party(casenum),
c.casenum,
mld.mailing_list,
p.our_client
from cases c
inner join party p on c.casenum=p.case_id
inner join names n on n.names_id=p.party_id
inner join mailing_list_defined mld on n.names_id=mld.names_id
where
mld.mailing_list like 'Mattar Stars'
and mld.addr_type like 'Home'
and n.deceased='N'
and p.our_client='Y'
group by p.party_id, c.casenum, mld.mailing_list, p.our_client
order by sp_first_party(casenum) asc
Any tips would be greatly appreciated.
Thank you
Sounds like you need to be using an APPLY statement. Not sure if the join criteria on the APPLY statement is correct, but you should be able to extrapolate the logic. See if that will work with Sybase.
SELECT pic.PartyInstanceCount AS refs
,p.party_id
,sp_first_party(casenum)
,c.casenum
,mld.mailing_list
,p.our_client
FROM cases AS c
INNER JOIN party AS p ON c.casenum = p.case_id
INNER JOIN names AS n ON n.names_id = p.party_id
INNER JOIN mailing_list_defined AS mld ON n.names_id = mld.names_id
OUTER APPLY (
SELECT COUNT(1) AS PartyInstanceCount
FROM party p2
WHERE p2.case_id = c.casenum
) pic
WHERE mld.mailing_list LIKE 'Mattar Stars'
AND mld.addr_type LIKE 'Home'
AND n.deceased = 'N'
AND p.our_client = 'Y'
ORDER BY
sp_first_party(casenum) ASC

SQL report filtering, results stop after first item

and thanks in advance. I am a newbie, working on one of my first reports. I have orders, which have a terminal assigned them (a "DC"). The report is set up to return all open orders, the "DC", and a few other columns (driver #, city, etc). I made a drop down filter to use so I can look at one, several, or all of the DCs. My problem is, it stops looking after the first item that is checked in the drop down list. So if the first item in the list has 100 orders, but the rest of them have thousands more, it only shows me the 100 orders. Am I making any sense here? I am not sure what information from my report's setup would be pertinent here.
This is the query that the report is based on. Using SQL Report Builder.
SELECT
o.OrderTrackingID,
cm.accountno,
o.ClientRefNo,
o.DCoName,
o.DStreet,
o.DCity,
o.DState,
o.DZip,
o.DZone,
t.TerminalName as 'OrderDC',
e.LastName as 'DrvLast',
e.FirstName as 'DrvFirst',
e.DriverNo,
et.TerminalName as 'DriverDC'
FROM Orders o
FULL JOIN OrderDrivers od ON o.OrderTrackingID = od.OrderTrackingID
FULL JOIN Employees e ON od.DriverID = e.ID
FULL JOIN ClientMaster cm ON o.ClientID = cm.ClientID
FULL JOIN Terminals t ON o.TerminalID = t.TerminalID
FULL JOIN Terminals et ON e.TerminalID = et.TerminalID
WHERE o.Status = 'N'
Order By o.aTimeStamp ASC
(I am writing this as an answer even if it isn't an complete answer mostly because the comment field is kind of limited.)
In the SQL you posted the below stands out as wrong
FULL JOIN Terminals t ON o.TerminalID = t.TerminalID
FULL JOIN Terminals et ON e.TerminalID = et.TerminalID
You are joining the same table twice but the is nothing that separated the two joins and this is my guess why you are not getting any more orders in your report.
I don't now what the drop down list corresponds to but I assume it is some kind of identifier in the Terminals table.
From a pure SQL point of view I would expect something like this
FULL JOIN Terminals t ON o.TerminalID = t.TerminalID
WHERE t.someColumn IN (value1, value2)
where value1 and value2 comes from the drop down list.
I see in your select part that you include the same column from both of the Terminals JOIN you have and I expect those two columns to always have the same values. You should need that column only once in your select list.
Not a solution but maybe this can get you in the right direction.

left join brining back more records than it should

I'm trying to do a simple join and I'm not sure what is going on here. I have two tables: pend_bominv_det which has 305 rows and alldet which has around 12k
I'm trying to get all of the 305 records from pend_bominv_det and only those that match the claim number from alldet. Any ideas?
select distinct c_clm
,manager_name
,'exp_bom' as categ
,time_prd_nm
,report_month
,report_year
,report_end_date
from IN.pend_bominv_det a
left outer join IN.alldet
on a.pearl_clmno = c_clm
where a.pearl_clmno is not null
and time_prd_nm = 'WEEK 2'
Is there perhaps multiple instances of the same claim number in the pend_bominv_det table? I think you should be using a right join here.
Hope this helps

Sorting rows by count of a many-to-many associated record

I know there are a lot of other SO entries that seem like this one, but I haven't found one that actually answers my question so hopefully one of you can either answer it or point me to another SO question that is related.
Basically, I have the following query that returns Venues that have any CheckIns that contain the searched Keyword ("foobar" in this example).
SELECT DISTINCT v.*
FROM "venues" v
INNER JOIN "check_ins" c ON c."venue_id" = v."id"
INNER JOIN "keywordings" ks ON ks."check_in_id" = c."id"
INNER JOIN "keywords" k ON ks."keyword_id" = k."id"
WHERE (k."name" = 'foobar')
I want to SELECT and ORDER BY the count of the matched Keyword for each given Venue. E.g. if there have been 5 CheckIns that have been created, associated with that Keyword, then there should be a returned column (called something like keyword_count) with the value 5 which is sorted.
Ideally this should be done without any queries in the SELECT clause, or preferably none at all.
I've been struggling with this for a while and my mind is just going blank (perhaps it's been too long a day) so some help would be greatly appreciated here.
Thanks in advance!
Sounds like you need something like:
SELECT v.x, v.y, count(*) AS keyword_count
FROM "venues" v
INNER JOIN "check_ins" c ON c."venue_id" = v."id"
INNER JOIN "keywordings" ks ON ks."check_in_id" = c."id"
INNER JOIN "keywords" k ON ks."keyword_id" = k."id"
WHERE (k."name" = 'foobar')
GROUP BY v.x, v.y
ORDER BY 3