Incorrect Syntax multi part identifier - sql-server-2008-r2

Currently I'm getting an incorrect syntax but can't locate its origins. Any thoughts on what i'm missing. Multi-part identifier pvxme.mt couldn't be found.
SELECT
PVXME.MT
PVXME.MT_VERSION
PVXME.START_DATE_LOCAL
PVXMEDS.CREATION_DATE_LOCAL
PVXMIHS.USER_NAME
PVXMEDE.PAT_ID
PVXMEDE.STRING_VALUE
PVXMEDE.NUM_VALUE AS MEDE_NUM_VALUE
RVXMIIF2.II
RVXMIIF2.NUM_VALUE AS MIIF_NUM_VALUE;
FROM
PVXMEDE
RIGHT OUTER JOIN
PVXMEDG ON PVXMEDG.ME = PVXMEDE.ME;
RIGHT OUTER JOIN
PVXMEDG ON PVXMEDG.SEQUENCE = PVXMEDE.SEQUENCE
AND PVXMEDG.SOURCE_TP = PVXMEDE.SOURCE_TP
AND PVXMEDG.SOURCE_ID = PVXMEDE.SOURCE_ID
AND PVXMEDG.SOURCE_VERSION = PVXMEDE.SOURCE_VERSION;
RIGHT OUTER JOIN
PVXMEDS ON PVXMEDS.ME = PVXMEDG.ME
AND PVXMEDS.SEQUENCE=PVXMEDG.SEQUENCE;
RIGHT OUTER JOIN
PVXME ON PVXME.ME = PVXMEDS.ME AND PVXME.MT=;
RIGHT OUTER JOIN
PVXMI ON PVXMI.MI = PVXME.MI;
LEFT OUTER JOIN
PVXMIHS ON PVXMIHS.MI = PVXME.MI;
LEFT OUTER JOIN
PVXMIID ON PVXME.ME = PVXMIID2.ME;
LEFT OUTER JOIN
RVXMIII ON PVXMIID2.MI = RVXMIIF2.MI
AND PVXMIID2.ID = RVXMIIF2.ID
AND PVXMIID2.ID_SEQUENCE = RVXMIIF2.ID_SEQUENCE
AND PVXMIID2.ME = RVXMIIF2.ME;
WHERE
(PVXMIHS.USER_NAME <> 'sipat'
AND PVXMIHS.WHAT = 'MethodPrepare'
AND PVXME.CX_STRING_4 = '20190117-7h40m'
AND PVXME.MT LIKE 'MK-0431%Tab CA%'
AND PVXMEDE.PAT_ID NOT LIKE '%Spectrum')
AND (RVXMIIF2.II = 'LeverageLimit'OR RVXMIIF2.II = 'XresidualLimit')

Try this:
do put a colon after each column in the SELECT list of columns
do NOT put a semicolon at the end of the list of columns and of each JOIN condition
do complete the RIGHT OUTER JOIN that references PVXME.MT = .......
Code:
SELECT
PVXME.MT,
PVXME.MT_VERSION,
PVXME.START_DATE_LOCAL,
PVXMEDS.CREATION_DATE_LOCAL,
PVXMIHS.USER_NAME,
PVXMEDE.PAT_ID,
PVXMEDE.STRING_VALUE,
PVXMEDE.NUM_VALUE AS MEDE_NUM_VALUE,
RVXMIIF2.II,
RVXMIIF2.NUM_VALUE AS MIIF_NUM_VALUE
FROM
PVXMEDE
RIGHT OUTER JOIN
PVXMEDG P1 ON P1.ME = PVXMEDE.ME -- use table alias P1 here
RIGHT OUTER JOIN
PVXMEDG P2 ON P2.SEQUENCE = PVXMEDE.SEQUENCE -- use table alias P2 here
AND P2.SOURCE_TP = PVXMEDE.SOURCE_TP
AND P2.SOURCE_ID = PVXMEDE.SOURCE_ID
AND P2.SOURCE_VERSION = PVXMEDE.SOURCE_VERSION
RIGHT OUTER JOIN
PVXMEDS ON PVXMEDS.ME = P2.ME
AND PVXMEDS.SEQUENCE = P2.SEQUENCE
RIGHT OUTER JOIN
PVXME ON PVXME.ME = PVXMEDS.ME AND PVXME.MT = ?????? -- ** COMPLETE THIS! **
RIGHT OUTER JOIN
PVXMI ON PVXMI.MI = PVXME.MI
LEFT OUTER JOIN
PVXMIHS ON PVXMIHS.MI = PVXME.MI
LEFT OUTER JOIN
PVXMIID ON PVXME.ME = PVXMIID2.ME
LEFT OUTER JOIN
RVXMIII ON PVXMIID2.MI = RVXMIIF2.MI
AND PVXMIID2.ID = RVXMIIF2.ID
AND PVXMIID2.ID_SEQUENCE = RVXMIIF2.ID_SEQUENCE
AND PVXMIID2.ME = RVXMIIF2.ME
WHERE
(PVXMIHS.USER_NAME <> 'sipat'
AND PVXMIHS.WHAT = 'MethodPrepare'
AND PVXME.CX_STRING_4 = '20190117-7h40m'
AND PVXME.MT LIKE 'MK-0431%Tab CA%'
AND PVXMEDE.PAT_ID NOT LIKE '%Spectrum')
AND (RVXMIIF2.II = 'LeverageLimit'OR RVXMIIF2.II = 'XresidualLimit')

Related

Postgres Error: missing FROM-clause entry for table

I have a query and am using left joins. I have the left join clause as follows:
left outer join ( select pup.brokerage_code, pcz.zip, count (pup.aggregate_id) as VerifiedAgentCount
from partner_user_profiles pup
join partner_user_roles pure on pure.user_profile_id = pup.id
join profile_coverage_zips pcz on pcz.profile_id = pup.id
where lower(pure.role) = 'agent'
and pup.verification_status like 'Verified%'
group by pup.brokerage_code, pcz.zip) vac on vac.brokerage_code = b.brokerage_code and pcz.zip = bcz.zip
However I am getting the error message saying that I am missing the FROM entry clause for "pcz" however I aliased the table in the join clause so I am not sure what is wrong.
You have defined the table alias pcz within the sub-select however the alias no longer exists when the outside the sub-select. At the point you have used it the appropriate alias is the one for the entire sub-select, in this case vac. So: vac.zip = = bcz.zip
left outer join ( select pup.brokerage_code, pcz.zip, count (pup.aggregate_id) as VerifiedAgentCount
from partner_user_profiles pup
join partner_user_roles pure on pure.user_profile_id = pup.id
join profile_coverage_zips pcz on pcz.profile_id = pup.id
where lower(pure.role) = 'agent'
and pup.verification_status like 'Verified%'
group by pup.brokerage_code, pcz.zip
) vac on vac.brokerage_code = b.brokerage_code
and vac.zip = bcz.zip

Syntax error raw sql on rails when query inner results

how do you query raw SQL on rails?
So I have this raw sql that I need to run on rails but giving me a syntax error. I also escape the extra parenthesis but still got a syntax error near the first inner join.
here's my code:
Spree::CorporateAccount.joins(" (((((
( inner join spree_memberships on spree_corporate_accounts.id = spree_memberships.corporate_account_id)
inner join spree_users on spree_memberships.user_id = spree_users.id)
left join spree_variant_price_sets on spree_corporate_accounts.variant_price_set_id = spree_variant_price_sets.id)
left join spree_addresses on spree_corporate_accounts.bill_address_id = spree_addresses.id)
left join spree_states on spree_addresses.state_id = spree_states.id)
left join spree_countries on spree_addresses.country_id = spree_countries.id)
left join spree_partner_accounts on spree_corporate_accounts.id = spree_partner_accounts.partnerable_id
").where(" spree_memberships.deleted_at IS null
AND (spree_partner_accounts.partnerable_type = 'Spree::CorporateAccount' OR spree_partner_accounts.partnerable_type IS NULL)
AND admin = true
")
but on sql this is perfectly fine.
SELECT
spree_corporate_accounts.id,
spree_corporate_accounts.company_name,
spree_memberships.ADMIN,
spree_users.email,
spree_users.doctor AS name,
spree_partner_accounts.account_key,
CASE spree_corporate_accounts.billing_type when 1 THEN 'Postbill' WHEN 2 THEN 'Creditcard' ELSE 'Individual'END,
spree_variant_price_sets.name AS priceset,
spree_addresses.address1,
spree_addresses.address2,
spree_addresses.city,
spree_addresses.zipcode,
spree_states.name AS state,
spree_countries.name AS country,
spree_addresses.phone,
spree_users.created_at
from (((((( spree_corporate_accounts inner join spree_memberships on spree_corporate_accounts.id = spree_memberships.corporate_account_id)
inner join spree_users on spree_memberships.user_id = spree_users.id)
left join spree_variant_price_sets on spree_corporate_accounts.variant_price_set_id = spree_variant_price_sets.id)
left join spree_addresses on spree_corporate_accounts.bill_address_id = spree_addresses.id)
left join spree_states on spree_addresses.state_id = spree_states.id)
left join spree_countries on spree_addresses.country_id = spree_countries.id)
left join spree_partner_accounts on spree_corporate_accounts.id = spree_partner_accounts.partnerable_id
where spree_memberships.deleted_at IS null
and (spree_partner_accounts.partnerable_type = 'Spree::CorporateAccount' OR spree_partner_accounts.partnerable_type IS NULL )
AND admin = true
if I do this. It will yield a different result. so what im thinking is the parenthesis evaluate the result first and then go to the next.
Spree::CorporateAccount.joins("inner join spree_memberships on spree_corporate_accounts.id = spree_memberships.corporate_account_id")
.joins("inner join spree_users on spree_memberships.user_id = spree_users.id")
.joins("left join spree_variant_price_sets on spree_corporate_accounts.variant_price_set_id = spree_variant_price_sets.id")
.joins("left join spree_addresses on spree_corporate_accounts.bill_address_id = spree_addresses.id")
.joins("left join spree_states on spree_addresses.state_id = spree_states.id")
.joins("left join spree_countries on spree_addresses.country_id = spree_countries.id")
.joins("left join spree_partner_accounts on spree_corporate_accounts.id = spree_partner_accounts.partnerable_id ")
.where("spree_memberships.deleted_at IS null
AND spree_partner_accounts.partnerable_type = 'Spree::CorporateAccount' OR spree_partner_accounts.partnerable_type IS NULL
AND admin = true
")

Can I JOIN table on the basis of the case statement in PostgreSQL

Can I JOIN the table on the basis of the case statement in PostgreSQL. I have written the one SQL in stored procedure into that I'm passing the one flag on that basis I want to jon the table. Please see the case statement,
JOIN lease_intervals li_active
ON ( li_active.cid = l.cid AND l.id = li_active.lease_id AND
l.active_lease_interval_id = li_active.id )
LEFT JOIN applications a
ON ( a.cid = li.cid AND li.lease_id = a.lease_id AND
a.lease_interval_id = li.id )
**CASE
WHEN pIsFromUI = TRUE
THEN JOIN property_integration_databases pid ON ( pid.cid = l.cid AND pid.property_id == l.property_id )
JOIN integration_databases id ON ( id.id = pid.integration_database_id AND id.cid = pid.cid )
ELSE
1
END**
Please let me know is there any alternate solution for above.
join
lease_intervals li_active on
li_active.cid = l.cid and l.id = li_active.lease_id and
l.active_lease_interval_id = li_active.id
left join
applications a on
a.cid = li.cid and li.lease_id = a.lease_id and
a.lease_interval_id = li.id
left join
property_integration_databases pid on
pid.cid = l.cid and pid.property_id = l.property_id and pisfromui
left join
integration_databases id on
id.id = pid.integration_database_id and id.cid = pid.cid and pisfromui

Cannot get view to work on SQL Server

I am trying to create a view that includes columns froms several tables.
This is what it looks like:
And this is my query:
SELECT
Billing.WebPortalBilling.WebPortalBillingId,
Billing.WebPortalBilling.CorporationId,
Billing.WebPortalBilling.TokenId,
Billing.WebPortalBilling.GatewaySupportFee,
Billing.WebPortalBilling.GatewayPerTransactionFee,
Billing.WebPortalBilling.PortalPerCustomerFee,
Billing.WebPortalBilling.PortalSupportFee,
Customer.Account.AccountNumber,
Billing.WebPortalBilling.IsActive,
Customer.Customer.Name,
Customer.Customer.TaxCode,
Company.CorporationStructure.Branch
FROM
Company.CorporationStructure
RIGHT OUTER JOIN
Customer.Account ON Company.CorporationStructure.CorporationStructureId = Customer.Account.CorporationStructureId
RIGHT OUTER JOIN
Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch
RIGHT OUTER JOIN
Billing.WebPortalBilling ON Customer.Account.CorporationId = Billing.WebPortalBilling.CorporationId
WHERE
(Billing.WebPortalBilling.IsActive = 1)
It's only returning 1 record, which is not correct. I'm trying to tie the Customer's name back to the WebPortalBilling table along with the account number and branth in the other two tables.
I'm new to sql, so be kind.
Thanks!
As commented the where is killing the outer
Try
SELECT
Billing.WebPortalBilling.WebPortalBillingId,
Billing.WebPortalBilling.CorporationId,
Billing.WebPortalBilling.TokenId,
Billing.WebPortalBilling.GatewaySupportFee,
Billing.WebPortalBilling.GatewayPerTransactionFee,
Billing.WebPortalBilling.PortalPerCustomerFee,
Billing.WebPortalBilling.PortalSupportFee,
Customer.Account.AccountNumber,
Billing.WebPortalBilling.IsActive,
Customer.Customer.Name,
Customer.Customer.TaxCode,
Company.CorporationStructure.Branch
FROM
Company.CorporationStructure
RIGHT OUTER JOIN
Customer.Account ON Company.CorporationStructure.CorporationStructureId = Customer.Account.CorporationStructureId
RIGHT OUTER JOIN
Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch
RIGHT OUTER JOIN Billing.WebPortalBilling
ON Customer.Account.CorporationId = Billing.WebPortalBilling.CorporationId
AND Billing.WebPortalBilling.IsActive = 1
Try this, I think left joins are clearer.
SELECT
B.WebPortalBillingId,
B.CorporationId,
B.TokenId,
B.GatewaySupportFee,
B.GatewayPerTransactionFee,
B.PortalPerCustomerFee,
B.PortalSupportFee,
C.AccountNumber,
B.IsActive,
C.Name,
C.TaxCode,
CS.Branch
FROM Customer.Customer C
LEFT JOIN Company.CorporationStructure CS ON CS.Branch = C.Branch
LEFT JOIN Customer.Account A ON CS.CorporationStructureId = A.CorporationStructureId
LEFT JOIN Billing.WebPortalBilling B ON A.CorporationId = B.CorporationId
WHERE B.IsActive = 1

Returning distinct columns from left outer join in db2

SELECT
nzy.NZPYYD
,nzy.NZZSYG
,nzy.NZJRYG
,acn.ANITCD
FROM
ACNTRA acn
LEFT OUTER JOIN NZYTFL nzy
ON (
nzy.NZCNO1 = acn.ANCNO1
AND nzy.NZCNO2 = acn.ANCNO2
AND nzy.NZCNO3 = acn.ANCNO3
AND nzy.NZCNO4 = acn.ANCNO4
AND nzy.NZCNO5 = acn.ANCNO5
AND nzy.NZSLKI = acn.ANSLKI
AND nzy.NZDLTM = ''
)
WHERE
acn.ANDLTM = ''
AND acn.ANTKCD = '1029'
AND nzy.NZTXKB = 1
The problem here is it gives 2 rows result.I want to get one unique row from the result of left outer join .Any help?
If both rows are identical, try
SELECT DISTINCT
nzy.NZPYYD
,nzy.NZZSYG
,nzy.NZJRYG
,acn.ANITCD
If not, you can try to SUM(), CONCAT(), MAX() or whatever the column with different values.
Difficult to be more precise without a sample output.