SQL query symbol "*=" - join

What does the symbol "*=" stand for in a SELECT statement? How does this affect the performance? Can we replace it with JOINS?
Thanks
STR

In some database systems,
SELECT ... FROM a, b WHERE a.field1 *= b.field2
is an old syntax for a LEFT OUTER JOIN:
SELECT ... FROM a LEFT JOIN b ON a.field1 = b.field2
Microsoft SQL Server, for example, considers the *= syntax to be deprecated since version 2005.
So yes, you can replace *= with a JOIN. In fact, you should. (I doubt that it affects performance in any relevant way, but your queries might stop working in newer versions of your database engine.)

In some implementations of SQL, a SELECT statement that has a assignment operator(=) can be used to create the relationship between a column heading and the expression that defines the values for the column.
So an example might be:
SELECT name = 'renamed_column_name'
FROM users
More Info:
Unfortunately the = operator can mean both assign and equality.
For assignment:
DECLARE #Counter INT;
SET #Counter = 1;
For equality:
The = is a equality operator states that the left side must equal the right side.
The could mean a value must equal the result returned by a subquery, or a variable must equal a literal, no matter the case... a = b means that a and b have to have the same value.
SELECT * FROM users LEFT JOIN posts ON users.id = posts.user_id

Related

Using Left Outer Join in Redshift Update Query result in ERROR: Target table must be part of an equijoin predicate

I have this query and running it resulted in the error
SQL Error [XX000]: ERROR:
Target table must be part of an equijoin predicate
UPDATE sandbox.f_contribution
SET lpct = NVL(l.percentage, 0)
FROM sandbox.f_contribution AS f
LEFT OUTER JOIN sandbox.f_contribution_last AS l
ON f.year = l.year AND f.location = l.location AND f.category = l.category
WHERE f.year = 2020;
The docs say that LEFT OUTER JOIN is not supported and it suggest to "use a subquery that clearly separates the join conditions from the criteria that qualify rows for updates", so I tried modifying the query as such
but the same error persists:
UPDATE sandbox.f_contribution
SET lpct = NVL(c.percentage, 0)
FROM (
select l.percentage
from sandbox.f_contribution AS f
LEFT OUTER JOIN sandbox.f_contribution_last AS l
ON f.year = l.year AND f.location = l.location AND f.category = l.category
) c WHERE f_contribution.year = 2020;
How should I modify the same query to run it in Redshift?
You've read up on what was the issue in your first SQL. Your second is missing the necessary information to perform the UPDATE, specifically in the WHERE clause.
Your subquery produces a set of rows with one column called percent. Your target table has a column called lpct that get set to a value from the subquery (or zero if NULL), but which subquery value? How is the UPDATE suppose to apply these values to the target?
I suspect you need your WHERE clause to have some alignment test that clears this confusion up. The WHERE clause is basically how to join the data in the FROM clause with the data in the subquery.
Guessing at your intent is risky but you might be intending to do:
UPDATE sandbox.f_contribution
SET lpct = Nvl(c.percentage, 0)
FROM sandbox.f_contribution_last AS c
WHERE f_contribution.year = c.year
AND f_contribution.location = c.location
AND f_contribution.category = c.category
AND f_contribution.year = 2020;

proc sql inner join behavior and required select statements

I recently started using SAS, only receiving a basic training that didn't cover proc sql. I'd like to read up a bit more on SAS sql when I have the time.
For now, I found a solution to what I wanted to do, but I'm having difficulties understanding what is happening.
My issue started when I wanted to find out which subjects in my dataset have a certain value for all their records. I made use of my previously written snippet of code that I thought I understood. I just tried adding a couple more variables and group by statements:
data have;
input subject:$1. myvar:1. mycount:1.;
datalines;
a 1 1
a 0 2
a 0 3
b 1 1
b 0 2
b 1 3
c 1 1
c 1 2 /*This subject has myvar = 1 for all its observations*/
;
run;
*find subjects;
proc sql;
create table want as
/* select*/
/* distinct x.subject */
/* from */
(select distinct subject, count(myvar) as myvar_c
from have where myvar = 1 group by subject) x,
(select distinct subject, max(mycount) as max_c
from have group by subject) y
where x.subject = y.subject and x.myvar_c = y.max_c;
quit;
When removing the commented 'select distinct x.subject from' in the create table statement, the above code works as should.
However, I've previously also created another piece of code, to select all subjects in my dataset that have two types of records:
data have2;
input subject:$1. mytype:1.;
datalines;
a 1
a 0
a 0
b 1
b 0
b 1
c 1
c 1 /*This subject doesn't have two types of records in all its observations*/
;
run;
*Find subjects;
proc sql;
create table want2 as select
distinct x.subject from
have2 x,
(select distinct subject, count(distinct mytype) as mytype_c from have2 group by subject) y
where y.mytype_c = 2 and x.subject = y.subject;
quit;
Which is similar, but didn't require the additional select statement. The first code has 3 select statements, the second code only requires two select statements.
Can someone inform me why this is exactly required?
Or link me some good documentation that lists the specifications of these types of joins - can anyone also inform me of the specific name of this type of join where you only use a comma?
while I'm writing, also see that could've used my code I initially wrote to find subjects that have only 1 type of record and tweak it for my current issue >.< but still would like to know what is happening in the first example.
The SQL join construct
FROM ONE, TWO, THREE, …
is known as a CROSS JOIN and is a join without criteria. The comma (,) syntax is less prevalent today and the following construct is recommended
FROM ONE
CROSS JOIN TWO
CROSS JOIN THREE
The result set is a cartesian product and the number of rows is the product of the number of rows in the cross joined tables.
When the query has criteria (WHERE clause) the join is an INNER JOIN.
The SAS documentation for Proc SQL is a good starting point and includes examples.
joined-table Component
Joins a table with itself or with other tables or views.
…
Table of Contents
Syntax
Required Arguments
Optional Argument
Details
Types of Joins
Joining Tables
Table Limit
Specifying the Rows to Be Returned
Table Aliases
Joining a Table with Itself
Inner Joins
Outer Joins
Cross Joins
Union Joins
Natural Joins
Joining More Than Two Tables
Comparison of Joins and Subqueries
General tip:
If you want to fool around (fiddle) with SQL queries in a browser, try visiting
SQL Fiddle web site.

How can I do jointure in rails

I need to do a jointure between 2 tables to have all fields from Sleep and latlong from Poi.
How can I do ? with Join or with Includes
#sleeps = Sleep.where(track_id: track).includes(:poi)
#sleeps = Sleep.where(track_id: track).joins(:poi)
What's the difference beetwen the both ?
An .includes is left join whereas a .join is an inner join. Note: you may need to add a reference to effectively complete a left join as one query (internal optimizations often default to 2 queries for includes alone) e.g.:
Sleep.includes(:poi).references(:poi).where(...)

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.

union between requests with remplacement variables in sqlplus

I have 14 fields which are similar and I search the string 'A' on each of them. I would like after that order by "position" field
-- some set in order to remove a lot of useless text
def col='col01'
select '&col' "Fieldname",
&col "value",
position
from oneTable
where &col like '%A%'
/
-- then for the second field, I only have to type two lines
def col='col02'
/
...
def col='col14'
/
Write all the fields which contains 'A'. The problem is that those field are not ordered by position.
If I use UNION between table, I cannot take advantage of the substitution variables (&col), and I have to write a bash in unix in order to make the replacement back into ksh. The problem is of course that database code have to be hard-coded in this script (connection is not easy stuff).
If I use a REFCURSOR with OPEN, I cannot group the results sets together. I have only one request and cannot make an UNION of then. (print refcursor1 union refcursor2; print refcursor1+refcursor2 raise an exception, select * from refcursor1 union select * from refcursor2, does not work also).
How can concatenate results into one big "REFCURSOR"? Or use a union between two distinct run ('/') of my request, something like holding the request while typing new definition of variables?
Thank you for any advice.
Does this answer your question ?
CREATE TABLE #containingAValueTable
(
FieldName VARCHAR(10),
FieldValue VARCHAR(1000),
position int
)
def col='col01'
INSERT INTO #containingAValueTable
(
FieldName , FieldValue, position
)
SELECT '&col' "Fieldname",
&col "value",
position
FROM yourTable
WHERE &col LIKE '%A%'
/
-- then for the second field, I only have to type two lines
def col='col02'
INSERT INTO...
/
def col='col14'
/
select * from #containingAValueTable order by postion
DROP #containingAValueTable
But I'm not totally sure about your use of the 'substitution variable' called "col" (and i only have SQL Server to test my request so I used explicit field names)
edit : Sorry for the '#' charcater, we use it so often in SQL Server for temporaries, I didn't even know it was SQL Server specific (moreover I think it's mandatory in sql server for creating temporary table). Whatever, I'm happy I could be useful to you.

Resources