Query:
SELECT t1.id,
t1.ads_city,
t1.ads_title,
t1.ads_description,
t1.ads_type,
t2.ads_activate,
t2.postads_id,
t2.ads_id
FROM table_1 t1
JOIN nextpostads t2 ON t1.id = t2.postads_id
WHERE MATCH(t1.ads_title,t1.ads_description) AGAINST ('LCD projector ,' IN BOOLEAN MODE)
AND t2.ads_activate='Yes'
AND t1.ads_type='offering'
I have 2 record first record title is
" LCD projector,plasma display,recording speaker products"
and second record title is
" Interactive products(projection screen,projectors,touch panel,network camera)"
But from the above query I am not related result what is problem with it ?
My guess is the t2.ads_activate value isn't "Yes" and/or the t1.ads_type value isn't "offering". Try:
SELECT t1.id,
t1.ads_city,
t1.ads_title,
t1.ads_description,
t1.ads_type,
t2.ads_activate,
t2.postads_id,
t2.ads_id
FROM table_1 t1
JOIN nextpostads t2 ON t1.id = t2.postads_id
WHERE MATCH(t1.ads_title,t1.ads_description) AGAINST ('LCD projector ,' IN BOOLEAN MODE)
If the records you expect are returned, add the missing WHERE clauses , testing them one at a time. Otherwise, there's something with your JOIN...
Related
I have a table that has over 30 columns, it's patient medical record data so it has many fields. I have another table that Verified doctor information.
Say table 1 has columns patientID, patientName, patientAddress, ...., PrescriberID, PrescriberName, PrescriberAddress, PrescriberCity, PrescriberState, PrescriberZip, PrescriberPhone, ... plus many others
Table 2 has PrescriberID, PrescriberName, PrescriberAddress, PrescriberCity, PrescriberState, PrescriberZip, PrescriberPhone
How do I query so that if PrescriberID from table 1 is in table 2, name/address/city/state/zip/phone come from table 2, but if not, leave the information that was there?
EDIT: I Tried a suggestion and it worked. Thank you.
SELECT t1.Id, t1.PBM_ID, t1.CLIENT_ID, t1.CLAIMNUMBER, t1.PATIENTLNAME, t1.PATIENTFNAME, t1.PATIENTGENDER, t1.PATIENTADDRESS1, t1.PATIENTADDRESS2, t1.PATIENTCITY, t1.PATIENTSTATE, t1.PATIENTZIP, t1.PATIENTDATEOFBIRTH, t1.PATIENTCELLPHONE, t1.DATEOFINJURY, t1.CLAIMORGIN, t1.DOS, t1.SUBCARRIER, t1.GROUPDESCRIPTION, t1.SUBGROUP, t1.POLICYNUMBER, t1.JURISDICTIONSTATE, t1.ADJUDICATIONDATE, t1.CLIENTBILLRECEIVEDDATE, t1.PRESCRIBERDEA, t1.PRESCRIBERNPI, COALESCE(t2.Name, t1.PRESCRIBERNAME) as PRESCRIBERNAME, COALESCE(t2.Address, t1.PRESCRIBERADDRESS) as PRESCRIBERADDRESS, COALESCE(t2.City, t1.PRESCRIBERCITY) as PRESCRIBERCITY, COALESCE(t2.State, t1.PRESCRIBERSTATE) as PRESCRIBERSTATE, COALESCE(t2.Zip, t1.PRESCRIBERZIP) as PRESCRIBERZIP, COALESCE(t2.Phone, t1.PRESCRIBERPHONE) as PRESCRIBERPHONE, t1.PHARMACYPAIDDATE, t1.PHARMACYNABP, t1.PHARMACYNPI, t1.PHARMACYTAXID, t1.PHARMACYNAME, t1.PHARMACYPHONE, t1.PHARMACYADDRESS, t1.PHARMACYCITY, t1.PHARMACYSTATE, t1.PHARMACYZIP, t1.THIRDPARTYREPRICERNABP, t1.THIRDPARTYNAME, t1.THIRDPARTYNETWORK, t1.THIRDPARTYCLAIMSOURCE, t1.RXNUMBER, t1.DRUGNDC, t1.DRUGGPI, t1.DRUGNAME, t1.DRUGSTRENGTH, t1.DRUGTYPE, t1.ISCOMPOUND, t1.INDIVIDUALMED, t1.CUMULATIVEMED, t1.LEGALCLASS, t1.DAW, t1.QUANTITY, t1.DAYSSUPPLY, t1.REFILLNUMBER, t1.INVOICENUMBER, t1.BILLEDDATE, t1.BILLEDAMOUNT, t1.AWP, t1.STATEFEE_UCPRICE, t1.SAVINGS, t1.EMPLOYERNAME, t1.PANUMBER, t1.PAAPPROVINGADJUSTER, t1.ADJUSTERNAME, t1.ADJUSTEREMAIL, t1.ADJUSTERPHONE, t1.BRANCHCODE, t1.BRANCHNAME, t1.LoadDate, t1.FileType, t1.AdjusterFname, t1.AdjusterLname, t1.Clean_NDC, t1.CASEMANAGERNAME, t1.CASEMANAGEREMAIL
FROM PBM_Common as t1
LEFT OUTER JOIN NPI_Records as t2
ON t1.PRESCRIBERNPI = t2.NPI_Number;
First start by writing a SQL statement that will give you the result set that contains your logic. Use a LEFT OUTER JOIN to get all records from table1 and only those records from table2 that match for each table1.prescriberid. Use COALESCE() to pick the table2 values, unless they are null, then use table1 values:
SELECT t1.patientID, t1.patientName, t1.patientAddress, ...., t1.PrescriberID, COALESCE(t2.PrescriberName, t1.PrescriberName) as PrescriberName, COALESCE(t2.PrescriberAddress, t1.PrecsriberAddress) as PrescriberAddress, ... plus many others
FROM table1 as t1
LEFT OUTER JOIN table2 as t2
ON t1.PrescriberID = t2.PrescriberID;
You could just stop there as you have the data you want. If you must override the values in your table1 table, then you'll need to develop this SELECT statement into an UPDATE statement. You can see some common ways to do this here. For this example I will use the CTE approach:
With correct_date AS
(
SELECT t1.patientID, t1.patientName, t1.patientAddress, ...., t1.PrescriberID, COALESCE(t2.PrescriberName, t1.PrescriberName) as PrescriberName, COALESCE(t2.PrescriberAddress, t1.PrecsriberAddress) as PrescriberAddress, ... plus many others
FROM table1 as t1
LEFT OUTER JOIN table2 as t2
ON t1.PrescriberID = t2.PrescriberID
)
UPDATE T2
SET PrescriberName = cd.PrescriberName,
PrescriberAddress = cd.PrescriberAddress
... plus many others
FROM table2 as T2
INNER JOIN correct_data cd
ON T2.PatientID = cd.PatiendID
Probably best to make a backup of table1 before running anything undo-able like an UPDATE statement.
Lastly, I think it's worth pointing out that this schema is terrible. Why store attributes of the prescriber in both tables where situations like the one you are trying to solve for can even exist. Just remove these columns from Table1 and leave only PrescriberID and then join in Table2 when you need those values.
SQL is not recognizing the variable I am calling in the main SELECT statement. The variable I am calling is the execs.block_order_quantity. I name this variable in the WITH statement at the beginning of the query. The code is below, and the error is attached as a picture. I have run the select statement without the WITH execs as( piece and it works just fine.
WITH execs as(SELECT th.exec_id, SUM(eha.tck_ord_qty) as "BLOCK_ORDER_QUANTITY"
FROM t1 eha
join t2 th
on eha.exec_id = th.exec_id
where th.trdng_desk_sname IN ('NAME')
and th.trd_dt between to_date('201840101', 'YYYYMMDD')
and to_date ('20140301', 'YYYYMMDD')
and exists
(SELECT 1
FROM t2t eth
WHERE eth.TRD_ID = eha.TRD_ID
AND eth.trd_stat_cd = 'EX')
group by th.exec_id)
SELECT DISTINCT
th.trd_dt as "TRADE_DATE",
eah.ord_cap_qty as "CAP_AMOUNT",
execs.block_order_quantity as "BLOCK_ORDER_QUANTITY",
eah.alloc_ovrrd_rsn_cd as "ALLOC_OVRRD_RSN_CD",
CASE --create allocation case id
WHEN(eh.manl_alloc_ind = 'Y'
OR NVL (eah.trdr_drct_qty, 0) > 0
OR NVL (eah.trdr_drct_wgt_ratio, 0) > 0)
THEN
'Y'
ELSE
'N'
END
AS "ALLOCATION_OVRRD_IND",
CASE
WHEN (eh.manl_alloc_ind = 'Y'
OR NVL (eah.trdr_drct_qty, 0) > 0
OR NVL (eah.trdr_drct_wgt_ratio, 0) > 0)
THEN
TH.EXEC_TMSTMP
ELSE
NULL
END
AS "ALLOCATION_OVRRD_TIMESTAMP",
eah.alloc_adj_assets_rt_curr_amt as "FUND_ADJ_NET_ASSETS",
eah.as_alloc_exec_hld_qty as "FUND_HOLDINGS_QUANTITY",
th.as_trd_iv_lname as "SECURITY_NAME",
th.as_trd_fmr_ticker_symb_cd as "TICKER",
CASE
WHEN NVL(th.limit_prc_amt, 0) > 0 THEN 'LIMIT' ELSE NULL END
AS "FUND_ORDER_TYPE"
from t1 eah
join t3 eh
on eah.exec_id = eah.exec_id
join t2 th
on th.trd_id = eah.trd_id
join t4 tk
on tk.tck_id = eah.tck_id
join t5 pm
on eah.pm_person_id_src = pm.person_id_src
where th.trdng_desk_sname IN('NAME')
and th.trd_dt between to_date('20140101', 'YYYYMMDD')
and to_date ('20140301', 'YYYYMMDD')
and rownum < 15
You need to join to the execs common table expression (CTE) name in your main query, e.g.:
...
from t1 eah
join t3 eh
on eah.exec_id = eah.exec_id
join t2 th
on th.trd_id = eah.trd_id
join execs
on execs.exec_id = eh.exec_id
join t4 tk
...
I'm not sure you actually want a CTE here, it looks like you could just do the aggregation in the main query as you're referencing the same tables; but there may be something I'm missing, like duplicates introduced by later joins.
Incidentally, the first on clause there looks wrong, as both sides refer to the same column in the same table; so it should probably be:
...
from t1 eah
join t3 eh
on eh.exec_id = eah.exec_id
join t2 th
Having distinct is sometimes a sign that there joins aren't right and there are duplicates you want to suppress which shouldn't really be there, so you may not need that once the join condition is fixed; and that might allow a simple aggregate to work too if it was giving the wrong result before. (Or there could stil be some other reason it's not appropriate.)
Also, the where rownum < 15 will give an indeterminate set of rows as you aren't ordering the result set before applying that filter.
I run this query given below, which shows error. I can not understand where is the problem.
SELECT T2.ENTRY_DATE , T1.OUTLET_NAME, T1.OUTLET_ADDRESS, T1.PROPRITOR_NAME, T1.MOBILE_NUMBER , T2.STATUS , T2.OUT_PRICE , ( SELECT ((T2.ITEM_CTN* T4.FACTOR)+ T2.ITEM_QTY )* T2.OUT_PRICE AS AMOUNT )
FROM T_ORDER_DETAIL T2, T_ITEM T4
WHERE T2.ITEM_ID=T4.ITEM_ID )
FROM T_OUTLET T1, T_ORDER_DETAIL T2
WHERE T2.OUTLET_ID=T1.OUTLET_ID AND T2.STATUS='Y' and T2.SR_ID=119222 and T2.ENTRY_DATE= TO_DATE('07/02/2017','dd/mm/yyyy');
The error is:
ORA-00923: FROM keyword not found where expected
Mam Ash, Check This.
Use JOINS.
SELECT T2.ENTRY_DATE, T1.OUTLET_NAME, T1.OUTLET_ADDRESS, T1.PROPRITOR_NAME, T1.MOBILE_NUMBER , T2.STATUS , T2.OUT_PRICE ,
((T2.ITEM_CTN* T4.FACTOR)+ T2.ITEM_QTY )* T2.OUT_PRICE AS AMOUNT
FROM T_OUTLET T1
inner join T_ORDER_DETAIL T2 on
T2.OUTLET_ID=T1.OUTLET_ID
AND T2.STATUS='Y'
and T2.SR_ID=119222
and T2.ENTRY_DATE= TO_DATE('07/02/2017','dd/mm/yyyy')
inner join T_ITEM T4 on
T2.ITEM_ID=T4.ITEM_ID
I am getting an error saying that I cannot use Top 10 with distinct I am wondering if there is any way I can get my query to work on that fashion. this is what the error is saying .
[Teradata Database] [6916] TOP N Syntax error: Top N option is not supported with DISTINCT option.
Query Below:
Thank you.
Select Distinct TOP 10 t1.Adjustment_ID, t1.OfficeNum, t1.InvoiceNum, t1.PatientNum,
t1.CurrentStatus, t1.AdjustmentTotal, t1.SubmittedOn, t1.UserSubmitted,
t1.Invoice_Type, t1.Pat_First_Name, t1.Pat_Last_Name,t2.Reason_Code FROM App_UnityAdj_AdjInfo_Tbl t1
Left Join RCM_WORK_PRD.App_UnityAdj_AdjRecord_Tbl t2
On t1.Adjustment_ID = t2.Adjustment_ID
Where t1.UserSubmitted = 'Name' AND (t1.CurrentStatus = 'Pending' OR t1.CurrentStatus = 'Deny')
What are you trying to do? You have columns in the SELECT that are not in the GROUP BY. You also have TOP without ORDER BY, which is suspicious.
One simple method is to move all the SELECT columns to the GROUP BY:
select TOP 10 t1.Adjustment_ID, t1.OfficeNum, t1.InvoiceNum, t1.PatientNum,
t1.CurrentStatus, t1.AdjustmentTotal, t1.SubmittedOn, t1.UserSubmitted,
t1.Invoice_Type, t1.Pat_First_Name, t1.Pat_Last_Name, t2.Reason_Code
from App_UnityAdj_AdjInfo_Tbl t1 Left Join
RCM_WORK_PRD.App_UnityAdj_AdjRecord_Tbl t2
On t1.Adjustment_ID = t2.Adjustment_ID
where t1.UserSubmitted = 'Name' AND
t1.CurrentStatus in ('Pending', 'Deny')
group by t1.Adjustment_ID, t1.OfficeNum, t1.InvoiceNum, t1.PatientNum,
t1.CurrentStatus, t1.AdjustmentTotal, t1.SubmittedOn, t1.UserSubmitted,
t1.Invoice_Type, t1.Pat_First_Name, t1.Pat_Last_Name,t2.Reason_Code;
I think I figured it out, in case anyone wants to know it is sample 10
Select Distinct t1.Adjustment_ID, t1.OfficeNum, t1.InvoiceNum, t1.PatientNum,
t1.CurrentStatus, t1.AdjustmentTotal, t1.SubmittedOn, t1.UserSubmitted,
t1.Invoice_Type, t1.Pat_First_Name, t1.Pat_Last_Name,t2.Reason_Code FROM App_UnityAdj_AdjInfo_Tbl t1
Left Join RCM_WORK_PRD.App_UnityAdj_AdjRecord_Tbl t2
On t1.Adjustment_ID = t2.Adjustment_ID
Where t1.AssignedTo IS null AND (t1.CurrentStatus = 'Pending')
sample 10
I have this sql:
Select
t1.field01,
t1.field02,
t2.name02,
t2.surname02,
from
lib01/FirstFile as t1,
lib02/SecondFile (lib02/ThirdFile) as t2
where
t1.field01 = t2.field02 (or t3.filed02)
I need to have the condition that if t1.field02 is equal to "AX" I have to read the second file (lib02/SecondFile), if t1.field02 is equal to "BX" I have to read by the third file (lib02/ThirdFile).
Select
CASE
WHEN ((Select t1.field02 from lib01/FirstFile as t1 ) = AX)
THEN Select t2.name02, t2.surname02
from lib02/SecondFile (lib02/ThirdFile) as t2
CASE
WHEN ((Select t1.field02 from lib01/FirstFile as t1 ) = BX)
THEN Select t3.name02, t3.surname02
from (lib02/ThirdFile) as t3
END
Change the code after each THEN clause if that retrieve is not what you want, but that is the logic.
And try to avoid use old syntax for join