Error Trying to do Left Outer Join BigQuery - sql

I'm trying to do a relatively simple outer join but quite new to Big Query and I'm getting the following Error: internal error: missing closing bracket at: 2.3 - 2.39
SELECT
([130493328.ga_sessions_20170312].date),
([130493328.ga_sessions_20170312].total.visits),
([130493328.social].engagedUsers)
FROM ([130493328.ga_sessions_20170312]),
LEFT OUTER JOIN
[130493328.social]
ON
([130493328.ga_sessions_20170312].date) = ([130493328.social].date);
Could someone let me know where I'm going wrong?
Thanks

Try writing this with table aliases:
SELECT ga.date, ga.total.visits, s.engagedUsers
FROM [130493328.ga_sessions_20170312] ga LEFT OUTER JOIN
[130493328.social] s
ON ga.date = s.date;
You should also check if you are using Legacy SQL or Standard SQL. The square braces would not be appropriate in Standard SQL.

Related

Convert Legacy SQL Outer JOIN *=, =* to ANSI

I need to convert a legacy SQL outer Join to ANSI.
The reason for that being, we're upgrading from a legacy DB instance (2000/5 ?) to SQL 2016.
Legacy SQL query :-
SELECT
--My Data to Select--
FROM counterparty_alias ca1,
counterparty_alias ca2,
counterparty cp,
party p
WHERE cp.code *= ca1.counterparty_code AND
ca1.alias = 'Party1' AND
cp.code *= ca2.counterparty_code AND
ca2.alias = 'Party2' AND
cp.code *= p.child_code AND
cp.category in ('CAT1','CAT2')
Here, Party1 and Party2 Are the party type codes and CAT1 and CAT2 are the category codes. They're just data; I have abstracted it, because the values don't really matter.
Now, when I try to replace the *= with a LEFT OUTER JOIN, I get a huge mismatch on the Data, both in terms of the number of rows, as well as the Data itself.
The query I'm using is this :
What am I doing wrong ?
SELECT
--My Data to Select--
FROM
counterparty cp
LEFT OUTER JOIN counterparty_alias ca1 ON cp.code = ca1.counterparty_code
LEFT OUTER JOIN counterparty_alias ca2 ON cp.code = ca2.counterparty_code
LEFT OUTER JOIN party p ON cp.code = p.child_code
WHERE
ca1.alias = 'Party1' AND
ca2.alias = 'Party2' AND
cp.category in ('CAT1','CAT2')
Clearly , in all the three legacy joins , the cp (counterparty) table is on the Left hand Side of the *=. So that should translate to a LEFT OUTER JOIN WITH all the three tables. However, my solution doesn't seem to to be working
How can I fix this ? What am I doing wrong here ?
Any help would be much appreciated. Thanks in advance :)
EDIT
I also have another query like this :
SELECT
--My Data to Select--
FROM dbo.deal d,
dbo.deal_ccy_option dvco,
dbo.deal_valuation dv,
dbo.strike_modifier sm
WHERE d.deal_id = dvco.deal_id
AND d.deal_id = dv.deal_id
AND dvco.base + dvco.quoted *= sm.ccy_pair
AND d.maturity_date *= sm.expiry_date
In this case, both the dvco and d tables seem to be doing a LEFT OUTER JOIN on the same table sm. How do I proceed about this ?
Maybe join in on the same table and use an alias sm1 and sm2 ?
Or should I use sm as the central table and change the join to RIGHT OUTER JOIN on dvco and d tables ?
I think the problem with your translation is that you are using conditions on the right tables in the where clause instead of in the on clause.
When I tried to translate it, this is the translation I've got:
FROM counterparty cp
LEFT JOIN counterparty_alias ca1 ON cp.code = ca1.counterparty_code
AND ca1.alias = 'Party1'
LEFT JOIN counterparty_alias ca2 ON cp.code *= ca2.counterparty_code
AND ca2.alias = 'Party2'
LEFT JOIN party p ON cp.code = p.child_code
WHERE cp.category in ('CAT1','CAT2')
However, it's hard to know if I'm correct since you didn't provide sample data, desired results, or even a complete query.
If you're doing a conversion, it has been my experience that *= is a RIGHT OUTER JOIN and =* is a LEFT OUTER JOIN in terms of a straight conversion.
I am converting hundreds of stored procs and views now and through testing this is what matches. I run the query as the original first, then make the changes and re-run it with the ANSI compliant code.
The data returned needs to be the same for consistency in our application.
So for your second query I think it would look something like this:
FROM dbo.deal d
INNER JOIN dbo.deal_ccy_option dvco ON d.deal_id = dvco.deal_id
INNER JOIN dbo.deal_valuation dv ON d.deal_id = dv.deal_id
RIGHT OUTER JOIN dbo.strike_modifier sm ON d.maturity_date = sm.expiry_date
AND (dvco.base + dvco.quoted) = sm.ccy_pair
Thanks for the help and sorry for the late post, but I got it to work with a quick hack, using the Query Designer Tool inbuilt in SSMS. It simply refactored all my queries and put in the correct Join, Either Left or Right , and the Where condition as an AND condition on the Join itself, so I was getting the correct data result set for both pre and post, only sometimes the data sorting/ordering was a little off.
I got lost with deadlines and couldnt update with the solution earlier. Thanks again for the help. Hope this helps someone else too !!
Still a little bit unsure though why the ordering/sorting was a little off if the Join condition was the same and the filters as well, because data was a 100 % match.
To get the query Designer to Work , just select your legacy SQL, and
open the Query Designer by pressing Ctrl + Shift + Q or Goto Main Menu
ToolBar => Query => Design Query in Editor.
Thats it. This will refactor your legacy code to new ANSI standards. You wll get the converted query with the new Joins that you can copy and test. Worked 100% of the time for me, except in some cases where the sorting was not matching, which you can check by adding a simple order by clause to both pre and post to compare the data.
For reference, I cross checked with this post :
http://sqlblog.com/blogs/john_paul_cook/archive/2013/03/02/using-the-query-designer-to-convert-non-ansi-joins-to-ansi.aspx

"Error: Field '[REDACTED].field_id' not found on either side of the JOIN", Google BigQuery

As a beginner to Google's BigQuery platform, I have found it almost similar to MySql regarding its syntax. However, I am receiving an issue with my query where it is not finding a column on either side of the Inner Join I am performing.
A sample query below:
SELECT
base_account.random_table_name_transaction.context_id,
base_account.random_table_name_transaction.transaction_id,
base_account.random_table_name_transaction.meta_recordDate,
base_account.random_table_name_transaction.transaction_total,
base_account.random_table_name_transaction.view_id,
base_account.random_table_name_view.user_id,
base_account.random_table_name_view.view_id,
base_account.random_table_name_view.new_vs_returning,
base_account.random_table_name_experience.view_id,
base_account.random_table_name_experience.experienceId,
base_account.random_table_name_experience.experienceName,
base_account.random_table_name_experience.variationName,
base_account.random_table_name_experience.iterationId,
base_account.random_table_name_experience.isControl
FROM
[base_account.random_table_name_transaction] transactiontable
INNER JOIN
base_account.random_table_name_view viewtable
ON
transactiontable.view_id=viewtable.view_id
INNER JOIN
[base_account.random_table_name_experience] experiencetable
ON
viewtable.view_id=experiencetable.view_id
WHERE experiencetable.experienceId = 96659 or experiencetable.experienceId = 96660
In this case, when I run it within the BigQuery platform, after a few seconds of the query running I am returned an error:
"Error: Field 'base_account.random_table_name_experience.experienceId' not found on either side of the JOIN".
However, when I run the same query however I perform a SELECT * query, it does execute properly and returns the data I expect.
Is there something missing with my syntax as to why it is failing? I can confirm that each column I am trying to return does exist in each respected table.
Make sure to use standard SQL for your query to avoid some of the surprising aliasing rules with legacy SQL and to get more informative error messages. Your query would be:
#standardSQL
SELECT
base_account.random_table_name_transaction.context_id,
base_account.random_table_name_transaction.transaction_id,
base_account.random_table_name_transaction.meta_recordDate,
base_account.random_table_name_transaction.transaction_total,
base_account.random_table_name_transaction.view_id,
base_account.random_table_name_view.user_id,
base_account.random_table_name_view.view_id,
base_account.random_table_name_view.new_vs_returning,
base_account.random_table_name_experience.view_id,
base_account.random_table_name_experience.experienceId,
base_account.random_table_name_experience.experienceName,
base_account.random_table_name_experience.variationName,
base_account.random_table_name_experience.iterationId,
base_account.random_table_name_experience.isControl
FROM
`base_account.random_table_name_transaction` transactiontable
INNER JOIN
base_account.random_table_name_view viewtable
ON
transactiontable.view_id=viewtable.view_id
INNER JOIN
`base_account.random_table_name_experience` experiencetable
ON
viewtable.view_id=experiencetable.view_id
WHERE experiencetable.experienceId = 96659 OR experiencetable.experienceId = 96660;
Note that the only changes I made were to put the #standardSQL at the start (to enable standard SQL) and to escape the table names with backticks rather than brackets.

SQL syntax error using joins

Hoping I can get some help with my SQL syntax. I haven't been able to fix the problem on my own. I used a syntax checker which it says my code is good but I'm getting an error. Any help is greatly appreciated!
SELECT DATALIVE.CO_ALLOCATION_TAIL.PO_KEY,
DATALIVE.CO_ALLOCATION_TAIL.SO_KEY,
DATALIVE.CO_PICK_LOTS_DETAIL.SO_KEY,
Sum(DATALIVE.CO_ALLOCATION_TAIL.QTY_ALLOC) AS SumOfQTY_ALLOC,
Sum(DATALIVE.CO_ALLOCATION_TAIL.PO_ALLOC_QTY) AS SumOfPO_ALLOC_QTY,
Sum(DATALIVE.CO_PICK_LOTS_DETAIL.QTY) AS Picked_Qty,
Min(DATALIVE.CO_ALLOCATION_TAIL.ALLOC_DATE) AS MinOfALLOC_DATE,
Max(DATALIVE.CO_ALLOCATION_TAIL.ALLOC_DATE) AS MaxOfALLOC_DATE,
DATALIVE.CO_SORDER.STATUS
FROM (DATALIVE.CO_ALLOCATION_TAIL
INNER JOIN DATALIVE.CO_SORDER.SO_KEY
ON DATALIVE.CO_ALLOCATION_TAIL.SO_KEY = DATALIVE.CO_SORDER.SO_KEY)
INNER JOIN DATALIVE.CO_PICK_LOTS_DETAIL
ON DATALIVE.CO_ALLOCATION_TAIL.SO_KEY = DATALIVE.CO_PICK_LOTS_DETAIL.SO_KEY
GROUP BY DATALIVE.CO_ALLOCATION_TAIL.PO_KEY,
DATALIVE.CO_ALLOCATION_TAIL.SO_KEY,
DATALIVE.CO_SORDER.STATUS,
DATALIVE.CO_PICK_LOTS_DETAIL.SO_KEY
HAVING (((DATALIVE.CO_SORDER.STATUS) = 'O'))
INNER JOIN DATALIVE.CO_SORDER.SO_KEY
That's a column, not a table. Needs a table, like DATALIVE.CO_SORDER.

Query is too complex error in ms access?

I tried to bind 3 queries into single query, using this code but when i click "Datasheet View" it showing error "QUERY IS TOO COMPLEX".
This is my code
SELECT
RPT_Invoice_Less.InvoiceNumber,
RPT_Invoice_Less.Terms,
RPT_Invoice_Less.Invoicedate,
RPT_Invoice_Less.OurQuote,
RPT_Invoice_Less.SalesPerson,
RPT_Customer.CustomerName,
RPT_Customer.CustomerId,
RPT_Customer.ContactPerson,
RPT_Customer.BillingAddress,
RPT_Customer.DeliveryAddress,
RPT_Invoice_Less.OrderNumber,
RPT_Invoice_Less.ShippingBy,
RPT_Invoice_Less.ShipReferenceNo,
RPT_Invoice_Less.Notes,
RPT_Invoice_Less.Price,
RPT_Invoice_Less.Discount,
RPT_Invoice_Less.Shipping,
RPT_Invoice_Less.Tax,
RPT_Invoice_Less.GrandTotal,
RPT_Company.CompanyName,
RPT_Company.CompanyId,
RPT_Company.RegistrationNumber,
RPT_Company.Address,
RPT_Company.MobileNumber,
RPT_Company.FaxNumber,
RPT_Company.CompanyEmail,
RPT_Company.CompanyWebsite,
RPT_Company.VatTinNumber
FROM
(RPT_Invoice_Less
INNER JOIN RPT_Customer
ON RPT_Invoice_Less.CustomerId=RPT_Customer.CustomerId)
INNER JOIN
RPT_Company
ON RPT_Invoice_Less.CompanyId=RPT_Company.CompanyId;
Try to use the built in designer to reproduce as close as you can, if not replicate the query, I get the impression looking at that there maybe an issue around the FROM part of that query
Thank you guys finally I solved with your ideas and my current code i pasted below
SELECT RPT_Invoice_Less.InvoiceNumber, RPT_Invoice_Less.Terms, RPT_Invoice_Less.Invoicedate, RPT_Invoice_Less.OurQuote, RPT_Invoice_Less.SalesPerson,
RPT_Customer.CustomerName, RPT_Customer.CustomerId, RPT_Customer.ContactPerson, RPT_Customer_Address.BillingAddress, RPT_Customer_Address.DeliveryAddress, RPT_Invoice_Less.OrderNumber, RPT_Invoice_Less.ShippingBy, RPT_Invoice_Less.ShipReferenceNo, RPT_Invoice_Less.Notes, RPT_Invoice_Less.Price, RPT_Invoice_Less.Discount, RPT_Invoice_Less.Shipping, RPT_Invoice_Less.Tax, RPT_Invoice_Less.GrandTotal,
RPT_Company.CompanyName, RPT_Company.CompanyId, RPT_Company.RegistrationNumber, RPT_Company_Address.Address, RPT_Company.MobileNumber, RPT_Company.FaxNumber, RPT_Company.CompanyEmail, RPT_Company.CompanyWebsite, RPT_Company.VatTinNumber
FROM (((RPT_Invoice_Less INNER JOIN RPT_Customer ON RPT_Invoice_Less.CustomerId = RPT_Customer.CustomerId) INNER JOIN RPT_Company ON RPT_Invoice_Less.CompanyId = RPT_Company.CompanyId) INNER JOIN RPT_Company_Address ON RPT_Invoice_Less.CompanyId = RPT_Company_Address.AddressId) INNER JOIN RPT_Customer_Address ON RPT_Invoice_Less.CustomerId = RPT_Customer_Address.CustomerId;
This code working successfull.

Too many parallel nested transactions

Location: lckmgr.cpp:385
Expression: 0
SPID: 94
Process ID: 3752
Description: Too many parallel nested transactions
my script has many table to left out join, and there is one table left out join itself, which tabel has 2,100,000 records.When I run the script in the sql server 2005, it will return this error message:Too many parallel nested transactions.
How to deal with? Please give me some help. Thank you very much.
LEFT OUTER JOIN
DW.HouseholdDetail H
ON H.HouseholdCompositionID = T.HouseholdCompositionID
LEFT OUTER JOIN
T_IC_CODE TRole
ON H.TenancyRoleCode = TRole.Code_ID
LEFT OUTER JOIN
DW.HouseholdDetail RH
ON H.RelatedToClientCoreID = RH.ClientCoreID
AND RH.HouseholdCompositionID = H.HouseholdCompositionID
LEFT OUTER JOIN
T_IC_CODE RTRole
ON RH.TenancyRoleCode = RTRole.Code_ID
LEFT OUTER JOIN
#TenantErrM AS TE
ON T.TenancyAccountID = TE.TenancyAccountID
It appears that this is a known issue in SQL Server. Please see the related Microsoft KB article 940935, which recommends:
The fix for this issue was first released in Cumulative Update 3.
Maybe you need install SQL2005 SP4. See KB article 939537 for details on updating.