can't join 2 queries in coldfusion - join

I've got a simple question for the coldfusion programmers, i wonder what i've done wrong in this code:
<cfquery name="get_account_plan" datasource="#DSN2#">
SELECT
A.*,
C.*
FROM
ACCOUNT_PLAN A,
#dsn_alias#.COMPANY C
WHERE
A.ACCOUNT_CODE= '#attributes.str_account_code#'
OR A.ACCOUNT_CODE= '#attributes.str_account_code#.%'
AND C.COMPANY_ID = A.ACCOUNT_ID
</cfquery>
why i get the outputs of this query different? for
<cfoutput>#get_account_plan.company_id#</cfoutput>
i get the value of 1 and for
<cfoutput>#get_account_plan.ACCOUNT_ID#</cfoutput>
I get the value of 419?

Check you order of precedence in the where clause. Use parentheses to make the "or" and "and" definition more clear.

You need to specific a type of join, that is a lot more strict.
Either inner join, outer join, left outer join, right outer join.
That's why your data returned is messed up.

Try this:
<cfquery name="get_account_plan" datasource="#DSN2#">
SELECT
A.*,
C.*
FROM
ACCOUNT_PLAN A INNER JOIN #dsn_alias#.COMPANY C ON C.COMPANY_ID = A.ACCOUNT_ID
WHERE
A.ACCOUNT_CODE= <cfqueryparam cfsqltype="cf_sql_varchar" value="#attributes.str_account_code#" />
OR A.ACCOUNT_CODE LIKE '#attributes.str_account_code#.%' <!--- Note LIKE. BTW, you need to sanatise this input! --->
</cfquery>

Related

Imformix sql 'outer' with and without parenthesis meaning

Having difficulty finding docs/ information on the '(' token and lexeme. Trying to convert to mysql, but the question is informix specific.
I want to know the syntactical difference, if any, for from table0 outer(table1, outer table2)
And from table0 outer table1, outer table2
According to the documentation parentheses are required when the WHERE clause includes a relationship between the subordinate tables in the OUTER clause, i.e. table1 and table2 in your example.
There is further information in the Informix-Extension Outer Joins section of the Guide to SQL: Syntax and in the section Joins that combine outer joins of the Guide to SQL: Tutorial.

Why does Hive warn that this subquery would cause a Cartesian product?

According to Hive's documentation it supports NOT IN subqueries in a WHERE clause, provided that the subquery is an uncorrelated subquery (does not reference columns from the main query).
However, when I attempt to run the trivial query below, I get an error FAILED: SemanticException Cartesian products are disabled for safety reasons.
-- sample data
CREATE TEMPORARY TABLE foods (name STRING);
CREATE TEMPORARY TABLE vegetables (name STRING);
INSERT INTO foods VALUES ('steak'), ('eggs'), ('celery'), ('onion'), ('carrot');
INSERT INTO vegetables VALUES ('celery'), ('onion'), ('carrot');
-- the problematic query
SELECT *
FROM foods
WHERE foods.name NOT IN (SELECT vegetables.name FROM vegetables)
Note that if I use an IN clause instead of a NOT IN clause, it actually works fine, which is perplexing because the query evaluation structure should be the same in either case.
Is there a workaround for this, or another way to filter values from a query based on their presence in another table?
This is Hive 2.3.4 btw, running on an Amazon EMR cluster.
Not sure why you would get that error. One work around is to use not exists.
SELECT f.*
FROM foods f
WHERE NOT EXISTS (SELECT 1
FROM vegetables v
WHERE v.name = f.name)
or a left join
SELECT f.*
FROM foods f
LEFT JOIN vegetables v ON v.name = f.name
WHERE v.name is NULL
You got cartesian join because this is what Hive does in this case. vegetables table is very small (just one row) and it is being broadcasted to perform the cross (most probably map-join, check the plan) join. Hive does cross (map) join first and then applies filter. Explicit left join syntax with filter as #VamsiPrabhala said will force to perform left join, but in this case it works the same, because the table is very small and CROSS JOIN does not multiply rows.
Execute EXPLAIN on your query and you will see what is exactly happening.

INNER JOIN on a table witch do not have any data

I'm trying to use the INNER JOIN functionality in my phpMyAdmin.
My query looks like this:
SELECT * FROM ___Bookings INNER JOIN ___Origins ON ___Bookings.BOO_Origin=___Origins.ORI_Id WHERE BOO_Id=1.
The problem is at this step nothing is populated into the ___Origins table. So my query returns 0 row.
How to change my query to return a row even if I do not have the joined table populated ?
Also what's the difference between JOIN and INNER JOIN ?
Thanks so much.
Basically, you want to join the tables based on the data in the __BOOKINGS table. That's a job for LEFT JOIN, not INNER JOIN (which is the same as JOIN).
Refer here for more information on SQL Joins: http://www.sql-join.com/sql-join-types/
There isn't a difference between Join and Inner Join (you can search for it also if this isn't enough).
For the other part - how can it return a row when there isn't one?
If you need a response from PHP you can set something like
if ($query->rowCount() > 0) {
echo $records;
}
else {echo "N";}
And in you other code just state if the response is "N" (or something else) do something or not do anything.

Using distinct in a join

I'm still a novice at SQL and I need to run a report which JOINs 3 tables. The third table has duplicates of fields I need. So I tried to join with a distinct option but hat didn't work. Can anyone suggest the right code I could use?
My Code looks like this:
SELECT
C.CUSTOMER_CODE
, MS.SALESMAN_NAME
, SUM(C.REVENUE_AMT)
FROM C_REVENUE_ANALYSIS C
JOIN M_CUSTOMER MC ON C.CUSTOMER_CODE = MC.CUSTOMER_CODE
/* This following JOIN is the issue. */
JOIN M_SALESMAN MS ON MC.SALESMAN_CODE = (SELECT SALESMAN_CODE FROM M_SALESMAN WHERE COMP_CODE = '00')
WHERE REVENUE_DATE >= :from_date
AND REVENUE_DATE <= :to_date
GROUP BY C.CUSTOMER_CODE, MS.SALESMAN_NAME
I also tried a different variation to get a DISTINCT.
/* I also tried this variation to get a distinct */
JOIN M_SALESMAN MS ON MC.SALESMAN_CODE =
(SELECT distinct(SALESMAN_CODE) FROM M_SALESMAN)
Please can anyone help? I would truly appreciate it.
Thanks in advance.
select distinct
c.customer_code,
ms.salesman_code,
SUM(c.revenue_amt)
FROM
c_revenue c,
m_customer mc,
m_salesman ms
where
c.customer_code = mc.customer_code
AND mc.salesman_code = ms.salesman_code
AND ms.comp_code = '00'
AND Revenue_Date BETWEEN (from_date AND to_date)
group by
c.customer_code, ms.salesman_name
The above will return you any distinct combination of Customer Code, Salesman Code and SUM of Revenue Amount where the c.CustomerCode matches an mc.customer_code AND that same mc record matches an ms.salesman_code AND that ms record has a comp_code of '00' AND the Revenue_Date is between the from and to variables. Then, the whole result will be grouped by customer code and salesman name; the only thing that will cause duplicates to appear is if the SUM(revenue) is somehow different.
To explain, if you're just doing a straight JOIN, you don't need the JOIN keywords. I find it tends to convolute things; you only need them if you're doing an "odd" join, like an LEFT/RIGHT join. I don't know your data model so the above MIGHT still return duplicates but, if so, let me know.

JPQL join two entities with no direct relations

I have an issue: When I am trying to join two tables which do not have a foreign key or a direct entity relation through my java code within themselves. I am using the below JPQL query: -
SELECT p FROM P p, OM orgm WHERE p.o.id = orgm.o.id and p.u.id = orgm.u.id and orgm.ma = true and p.u.id = ? AND p.o.id IN (:oId);
But this turns to a MySQL query which has a "cross join" which obviously is expensive.
What I need is to make sure that a similar query gives me an inner join MySQL query between the two tables.
I am trying to make usage of the "WITH" clause but seems that it doesn't work with inner join.
Please revert what can be done in this scenario.
Thanks in advance.

Resources