Writing JOIN Commands on Teradata - inner-join

q) Use COUNT and DISTINCT to determine how many distinct skus there are
in the skuinfo, skstinfo, and trnsact tables. Which skus are common to all tables, or unique to specific tables?
A) I was trying to find the solution to the above q in TERADATA. The first part was simple i was able to run three commands and got the distinct skus (stock keeping unit).
Now to find the common sku in all three table i am running the command but having errors :
SELECT COUNT(DISTINCT a.sku),COUNT(DISTINCT b.sku),COUNT(DISTINCT c.sku)
FROM skuinfo a INNER JOIN skstinfo b INNER JOIN trnsact c
ON a.sku=b.sku AND b.sku=c.sku;// Why is there error if i use **Where** in Place of ON?
**Error Occurred . . .**
[com.teradata.commons.datatools.sqlparsers.common.ParseException: Encountered ";" at line 3, column 31. Was expecting one of: "and" ... "at" ... "cross" ... "day" ... "full" ... "hour" ... "inner" ... "join" ... "left" ... "minute" ... "month" ... "on" ... "or" ... "right" ... "second" ... "timezone_hour" ... "timezone_minute" ... "year" ... "||" ... "(" ... "**" ... "+" ... "-" ... "*" ... "/" ... "mod" ... "." ... "[" ... ]
Please let me know why is my syntax not working, though most forums say the query is right. Thanks

Related

Table name in SQL Query matches with calcite Class name

I am trying to parse the SQL Statements with Calcite which is select * from Prutech.Member by using SQLParser.processQuery(sqlstatement) method. Calcite has a class name "Member" in the schema interface which is https://calcite.apache.org/javadocAggregate/org/apache/calcite/schema/Member.html
How to escape the Calcite class names from the given table names or column names.
Due to this I am getting the following exception.
org.apache.calcite.sql.parser.impl.ParseException: Encountered ". Member" at line 1, column 711.
Was expecting one of:
<EOF>
"AS" ...
"EXCEPT" ...
"EXTEND" ...
"FETCH" ...
"FOR" ...
"GROUP" ...
"HAVING" ...
"INTERSECT" ...
"LIMIT" ...
"MATCH_RECOGNIZE" ...
"OFFSET" ...
"ORDER" ...
"MINUS" ...
"TABLESAMPLE" ...
"UNION" ...
"WHERE" ...
"WINDOW" ...
"(" ...
<BRACKET_QUOTED_IDENTIFIER> ...
<QUOTED_IDENTIFIER> ...
<BACK_QUOTED_IDENTIFIER> ...
<IDENTIFIER> ...
<UNICODE_QUOTED_IDENTIFIER> ...
"/*+" ...
"NATURAL" ...
"JOIN" ...
"INNER" ...
"LEFT" ...
"RIGHT" ...
"FULL" ...
"CROSS" ...
"," ...
"OUTER" ...
"." <IDENTIFIER> ...
"." <QUOTED_IDENTIFIER> ...
"." <BACK_QUOTED_IDENTIFIER> ...
"." <BRACKET_QUOTED_IDENTIFIER> ...
"." <UNICODE_QUOTED_IDENTIFIER> ...
"." "*" ...

How to extract and combine specific data tables in hive database. Found some errors

SELECT
ps.hotel_id,
ps.name,
ps.country,
ps.city,
ps.chain_name,
ps.account_manager,
ps.adf_2_0,
t.booker_cc1,
sum(kpi.as_booked_roomnights) as ABRN,
sum(kpi.booked_roomnights) as BRN,
sum(kpi.cancelled_roomnights) as CRN,
sum(kpi.booked_price_euro) as booked_rev,
sum(kpi.stayed_roomnights) as SRN,
sum(kpi.stayed_price_euro) as stayed_rev,
sum(kpi.as_booked_commission_euro) as abc,
FROM reporting.kpi_booked_cancelled_stayed kpi
JOIN reporting.property_splits ps ON kpi.hotel_id=ps.hotel_id
JOIN fpa.transactions_detail t ON ps.hotel_id = t.hotel_id
where
to_date(kpi.yyyy_mm_dd) BETWEEN '2020-01-01' AND '2020-01-05'
AND ps.region ='APAC'
AND ps.is_global_chain ='1'
AND ps.is_open_bookable ='1'
GROUP BY 1,2,3,4,5,6,7
Hi, I wanted to extract some tables from hive and combine them together. However I got some error. Here is what I got:
***error while compiling statement: FAILED: ParseException line 17:0 cannot recognize input near 'FROM' 'reporting' '.' in selection target
Extra comma here:
sum(kpi.as_booked_commission_euro) as abc, --remove comma

Convert Statement to Crystal Reports SQL Expression

I have a SQL command that works great in SQL Server. Here's the query:
SELECT TOP 1000
(
SELECT COUNT(LINENUM)
FROM OEORDD D1
WHERE D1.ORDUNIQ = OEORDD.ORDUNIQ
)
- (SELECT COUNT(LINENUM)
FROM OEORDD D1
WHERE D1.ORDUNIQ = OEORDD.ORDUNIQ
AND D1.LINENUM > OEORDD.LINENUM)
FROM OEORDD
ORDER BY ORDUNIQ, LINENUM
The query looks at the total lines on an order, then looks at the current "LINENUM" field. With the value of the LINENUM field, it looks to see how many lines have a greater LINENUM value on the order and subtracts it from the number of lines on an order to get the correct Line number.
When I try to add it as a SQL expression in version 14.0.2.364 as follows:
(
(
SELECT COUNT("OEORDD"."LINENUM")
FROM "OEORDD" "D1"
WHERE "D1"."ORDUNIQ" = "OEORDD"."ORDUNIQ"
)
- (SELECT COUNT("OEORDD"."LINENUM")
FROM "OEORDD" "D1"
WHERE "D1"."ORDUNIQ" = "OEORDD"."ORDUNIQ"
AND "D1"."LINENUM" > "OEORDD"."LINENUM"
)
)
I get the error "Column 'SAMDB.dbo.OEORDD.ORDUNIQ' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If I try to add GROUP BY "OEORDD"."ORDUNIQ" at the end, I get "Incorrect syntax near the keyword 'GROUP'. I've tried adding "FROM OEORDD" at the end of query and it errors out on the word "FROM". I have the correct tables linked in the Database Expert.
EDIT --------------
I was able to get the first query working by getting rid of the alias, it's as follows:
(
SELECT COUNT(LINENUM)
FROM OEORDD
WHERE OEORDH.ORDUNIQ=OEORDD.ORDUNIQ)
)
However, I believe I need to use the alias in the second query to compare line numbers. I'm still stuck on that one.

sql server 2012 : how to optimize this like % query

This query takes too much time, so I try to optimize it. Do you have any idea or suggestion ?
I tried with fulltext on a procedure and a while loop ... it gets worst ( dbo.url has more than 100 000 lines ; dbo.url where status = 'tocheck' only 1000)
select tocheck.*
from dbo.url tocheck inner join dbo.url done
on tocheck.id != done.id
and tocheck.url like done.url+'%'
and done.status in ('tocheck','todo','done')
where tocheck.status = 'tocheck'
Edit :
I call a webservice multiple times with different urls :
urls look like http://ws.com/query?p1=a&p2=b (url1).
If I already called url http://ws.com/query?p1=a (url2), i don't want to call url1 cause :
url1 like url2+'%'
Thanks for your help.
Edit2 :
I add a column suburl that contains 'query?p1=a' for each url and modify the query :
select tocheck.*
from dbo.url tocheck inner join dbo.url done
on tocheck.id != done.id
and tocheck.suburl = done.suburl --NEW
and tocheck.url like done.url+'%'
and done.status in ('tocheck','todo','done')
where tocheck.status = 'tocheck'
More than 10 times shorter ... Phew !!
I think because of joining the table to itself through ids not equal there is much overhead as this is a cartesian product only excluding self joins for same id.
I suggest trying with a subquery. Then the outer query returns only 1000 (as you mentioned) tochecks whereas the subquery additionally excludes urls starting with the same characters:
select
tocheck.*
from
dbo.url tocheck
where
tocheck.status = 'tocheck'
and
tocheck.id not in (
select
done.id
from
dbo.url done
where
tocheck.url like done.url+'%'
and
done.status in ('tocheck','todo','done')
)

Double-JOIN in web2py. How to apply this SQL in web2py?

In my image-labeling app I want to display a list of labels assigned to an image along with lable-type and user information, who assigned this lable.
The main table is imageLabel. The tables auth_user and labelType contain additional information to the label.
Could you help me converting this SQL-statement to web2py:
SELECT labelType.name, imageLabel.labelValue, auth_user.first_name, auth_user.last_name from imageLabel
LEFT JOIN labelType
ON imageLabel.labelId = labelType.id
LEFT JOIN auth_user
ON imageLabel.userId = auth_user.id
WHERE imageLabel.imageId = 581
ORDER BY labelType.name
The data model is like this:
db.define_table('labelType',
Field('name','string'),
)
db.define_table('imageLabel',
Field('imageId','string'),
Field('labelId','integer'),
Field('userId','integer'),
Field('labelValue','text'),
)
db.define_table('image',
Field('imageId','string')
)
# + built-in auth tables
My attempt was:
labels = db((db.imageLabel.id == db.labelType.id) ).select(
db.imageLabel.labelValue, db.labelType.name, db.auth_user.first_name, db.auth_user.last_name, db.imageLabel.labelTimeStamp,
left=db.auth_user.on(db.imageLabel.userId==db.auth_user.id)
)
Which abviously doesn't work as there is no WHERE imageLabel.imageId = 581 part in the code. And I can't figure out how to use WHERE along with 2 "JOINS" in web2py :-(
Thank you very much in advance for any help!
EDIT: SOLUTION After reading answer from Anthony:
labels = db(
db.imageLabel.imageId == img.id
).select(
db.imageLabel.labelValue,
db.labelType.name,
db.auth_user.first_name,
db.auth_user.last_name,
db.imageLabel.labelTimeStamp,
left=[
db.labelType.on(db.imageLabel.labelId == db.labelType.id),
db.auth_user.on(db.imageLabel.userId==db.auth_user.id)
],
orderby=~db.labelType.name
)
In general, a select looks like db(query).select(...). The query part of that represents the WHERE clauses. If you have multiple WHERE clauses, you just use the & operator:
db((condition 1) & (condition 2) & ...)
As for left joins, the left argument of the .select() method can be a list if you need to specify multiple left joins:
left=[db.auth_user.on(db.imageLabel.userId==db.auth_user.id),
db.labelType.on(db.imageLabel.labelId==db.labelType.id)]
However, it's not clear that you really want left joins here -- you might just want inner joins (which can be specified using the join argument to the .select() method, or more simply as conditions in the query):
db((db.imageLabel.labelId == db.labelType.id) & # joins imageLabel and labelType
(db.imageLabel.userId == db.auth_user.id) & # joins imageLabel and auth_user
(db.imageLabel.imageId == 581))\
.select(db.imageLabel.labelValue, db.labelType.name, db.auth_user.first_name,
db.auth_user.last_name, db.imageLabel.labelTimeStamp)
Also, you should probably specify the three "Id" fields as reference type fields:
db.define_table('imageLabel',
Field('imageId', 'reference image'),
Field('labelId', 'reference labelType'),
Field('userId', 'reference auth_user'),
Field('labelValue', 'text'),
)
Finally, why do you need an imageId string field? The db.image table will already have an auto-incrementing integer "id" field to serve as a primary key for the table and uniquely identify each image.

Resources