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
")
Related
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')
how change this query from postgresql into oracle?
SELECT tbmm.tbmm_id,
tbmm.tbmm_email AS seller_mail,
'FB' AS seller_type,
concat(tbmm.tbmm_firstname, COALESCE(tbmm.tbmm_middlename),
COALESCE(tbmm.tbmm_lastname)) AS seller_owner_name,
tbmss.tbmss_name AS seller_store_name,
tbmss.tbmss_description AS seller_description,
tbmss.tbmss_phone AS seller_phone,
btrim(concat(COALESCE(tbmma.tbmma_addr1), COALESCE(tbmma.tbmma_addr2,
tbmma.tbmma_addr1), COALESCE(tbmma.tbmma_addr3, tbmma.tbmma_addr1),
COALESCE(tbmkc.tbmkct_nama), COALESCE(tbmkl.tbmkl_nama),
COALESCE(tbmma.tbmma_postcode, '0') AS seller_store_address,
tbmb.tbmb_name AS bank_name,
concat(tbmb.tbmb_name, COALESCE(tbmsd.tbmsd_bank_account_branch) AS
bank_desc,
tbmsd.tbmsd_bank_account_no AS bank_account_no,
tbmsd.tbmsd_bank_account_name AS bank_account_name,
tbmsd.tbmsd_no_id AS seller_id_card,
'F' AS flag_alfamind,
0 AS commision,
tbmm.tbmm_mage_entity_id,
tbmm.tbmm_update_date AS member_update_date,
tbmss.tbmss_update_date AS store_update_date,
tbmsd.tbmsd_update_date AS document_update_date,
tbmma.tbmma_update_date AS address_update_date
FROM coreapp.tb_master_member tbmm
JOIN coreapp.tb_master_seller_store tbmss ON tbmm.tbmm_id =
tbmss.tbmm_id
JOIN coreapp.tb_master_seller_document tbmsd ON tbmsd.tbmm_id =
tbmm.tbmm_id
JOIN coreapp.tb_master_bank tbmb ON tbmb.tbmb_id = tbmsd.tbmb_id
JOIN coreapp.tb_master_member_group tbmg ON tbmg.tbmm_id = tbmm.tbmm_id
LEFT JOIN coreapp.tb_master_member_address tbmma ON tbmma.tbmm_id =
tbmm.tbmm_id AND tbmma.tbmma_address_type = 140 AND tbmma.tbmma_status = 23
LEFT JOIN coreapp.tb_master_kecamatan tbmkc ON
tbmkc.tbmkct_kecamatan_id = tbmma.tbmkct_kecamatan_id
LEFT JOIN coreapp.tb_master_kelurahan tbmkl ON tbmkl.tbmkl_kelurahan_id
= tbmma.tbmkl_kelurahan_id
WHERE tbmg.tbmg_id = 4;
i have try to query on my sql developer but something error with query
now I'm just try until one day and still can't run
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
I'm getting error while executing a sql query in Alpha Anywhere Query (AlphaADO) section.
The code i'm executing is attached.
SELECT * FROM
(SELECT
tbl_wo_kitting.wo_kitting_id,
tbl_wo_kitting.level_no,
tbl_wo_kitting.line_no_parent,
tbl_wo_kitting.line_no AS kitting_line_no,
tbl_wo_kitting.production_type,
tbl_wo_kitting.quantity AS kitting_quantity,
tbl_wo_kitting.release_to_wr,
tbl_wo_kitting.mpn,
m_product.m_product_id,
m_product.value,
m_product.name,
m_product.storelocator,
tbl_wo_project.wo_project_id,
tbl_wo_project.line_no AS project_line_no,
tbl_wo_project.quantity AS project_quantity,
m_transaction_moveqty_v.qtyonhand AS qtyonhand,
tbl_work_order.work_order_id,
tbl_work_order.reference_key,
case
when tbl_wo_kitting.level_no, = 'P+'
then '0'
else tbl_wo_kitting.level_no,
end as linenumber,
case
when tbl_wo_kitting.line_no_parent = 'PT+'
then '0'
else tbl_wo_kitting.line_no_parent
end as linenoparent
FROM (tbl_wo_kitting tbl_wo_kitting
INNER JOIN (m_product m_product
INNER JOIN m_transaction_moveqty_v m_transaction_moveqty_v
ON m_product.m_product_id = m_transaction_moveqty_v.m_product_id )
ON tbl_wo_kitting.m_product_id = m_product.m_product_id
INNER JOIN (tbl_wo_project tbl_wo_project
INNER JOIN tbl_work_order tbl_work_order
ON tbl_wo_project.work_order_id = tbl_work_order.work_order_id )
ON tbl_wo_kitting.wo_project_id = tbl_wo_project.wo_project_id )) AS TABLE1
ORDER BY tbl_wo_kitting.level_no, (string_to_array(linenoparent, '.'))::int[],(string_to_array(linenumber, '.'))::int[]
Any idea? This is selecting from PostgreSQL.
Thank you very much in advance.
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