I have two tables like this:
I want to select In and Out values from log details where id=1 in log table.
I tried this for In value:
SELECT Log.Time, LogDetail.Value
FROM Log where ID=1
INNER JOIN LogDetail
ON Log.ID=LogDetail.ID where Name="In";
Is this a true query?
If you will try to run this query - you will see it can't be executed. The reason is two where statements it contains.
Query can contain only one where statement in most DBMS (while this statement can contain multiple conditions joined together by logical operators).
Also it is better to use table aliases - this makes query more readable and prevents possible ambiguities.
So your query should look like:
select L.Time, LD.Value
from Log as L
inner join LogDetail as LD on L.ID=LD.ID
where L.ID=1 and LD.Name="In"
Related
For readability, I have defined "org_location_ext" clause in the query as follows.
This "org_location_ext" is first used to join with the main fact-table "LOCATION_SALES".
It is used in other JOIN conditions as well.
According to the BigQuery documentation : https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#with_clause
The WITH clause contains one or more named subqueries which execute
every time a subsequent SELECT statement references them
I want to know the behavior for this case.
Does this query executes the "org_location_ext" WITH clause multiple times ?
Or when the SELECT query gets executed, a temporary table is created for "org_location_ext" and use this temporary table for all the JOINs.
Basically, after the first JOIN with the fact-table , later joins use that "filtered" result for their joins , or do they rerun the WITH clause ?
WITH org_location_ext AS (
SELECT *
FROM ORG_LOC_MASTER AS loc_master
JOIN LOC_REGN1 as regn1 ON loc_master.id = regn1.id
JOIN ...
JOIN ...
)
SELECT
..
org_location_ext.store_class,
org_location_ext.country,
org_location_ext.
..
..
FROM LOCATION_SALES AS sales
JOIN org_location_ext ON org_location_ext.area_id = sales.area_id AND org_location_ext.date = sales.date
JOIN ....
JOIN ....
JOIN COUNTRY_VAT AS vat ON vat.key1 =TBL_Y.key1 AND vat.country_code = org_location_ext.country_code
It depends on the query plan. Consider checking a query plan. You'll see how many times any specific table is accessed.
I have a left join command that also checks for matching letters and two other combinctions in the data between the joined tables. The DISTINCT ON is to ensure the location_plus has no duplication as results from the table on the right. To test my results I added an AND to check for a particular account numbers results. This AND component seems to be completely ignored, returning almost all of the records instead of the ~10 for that a/c number. Why is this?
SELECT
DISTINCT ON (location_plus)
a.*,b.*
FROM schema.source_table b
LEFT JOIN schema.other_reference a
ON a.loc_id = (substring(b.loc_id,3))::integer
WHERE left(b.loc_id,1)=left(a.table_name,1)
OR ((left(b.loc_id,1)='B' AND left(a.table_name,1)='A') OR (left(b.loc_id,1)='B' AND left(a.table_name,1)='B'))
and b.account_number='12345678'
AND has precendence over OR. So
WHERE left(b.loc_id,1)=left(a.table_name,1)
OR (...)
and b.account_number='12345678'
is
WHERE left(b.loc_id,1)=left(a.table_name,1)
OR ((...) and b.account_number='12345678')
where you probably want it to be
WHERE (left(b.loc_id,1)=left(a.table_name,1) OR (...))
and b.account_number='12345678'
Use parentheses as shown to fix this.
I am trying to write a query that should perform join (or where clause) using the following criteria:
Subquery of left table should contain results of subquery from the right table (results also should be grouped).
sql script creating tables with data
Exact criteria is the following:
Select clients who:
For each Question.Id in [ClientSegment]'s questionIds
Should be at least one (equivalent on Any in LINQ) ClientAnswers.AnswerId from those in [ClientSegmentAnswers]
How I can achieve that without using for loops (cursors) in a single query?
UPD
Added script for creating tables and data
Expected result
Query that selects Bob as only matching consumers for ClientSegment with id = 1;
SQL JOINS will help for achieve this in single query,
E.g :
select
c.name,
c.id,
...
from Question as que
left join ClientSegment as cs on que.id=cs.id
left join ClientSegmentAnswers as csans on csans.ClientSegmentID=cs.id
left join Answer as ans on ans.id=csans.answerid
left join ClientAnswer as ca on ca.answerid=ans.answerid
left join Client as c on c.id=ca.ClientID
where ca.ClientID is not null
This query will return only clientname who are answering atleast one question also you can modify the query for your need.
I need to redo sql statement in legacy Foxpro application and don't understand whether it is meaningful at all. Syntax is a bit specific - it extracts data from temporary table into the same temporary table ( overwriting) with some joins.
SELECT aa.*,b.spa_date FROM (ALIAS()) aa INNER JOIN jobs ON aa.seq=jobs.seq ;
LEFT JOIN job2 ON jobs.job_no=job2.rucjob;
left join jobs b on b.job_no=job2.job_no;
WHERE jobs.qty1<>0 INTO CURSOR (ALIAS())
Since only one field is added from joined tables ( spa_date ) is there any point in 2 left joins or I am missing something. Isn't it equivalent to
SELECT aa.*,jobs.spa_date FROM (ALIAS()) aa INNER JOIN jobs ON aa.seq=jobs.seq ;
WHERE jobs.qty1<>0 INTO CURSOR (ALIAS())
They are different because b.spa_date come from the second left join. You may be missing filtered rows without both left joins.
You would need to know the intent of the original query and perhaps rewrite it to make more sense but I'd say the two queries are different.
I have a query which works, goes like this:
Select
count(InsuranceOrderLine.AntallPotensiale) as potensiale,
COUNT(InsuranceOrderLine.AntallSolgt) as Solgt,
InsuranceProduct.Name,
InsuranceProductCategory.Name as Kategori
From
InsuranceOrderLine, InsuranceProduct, InsuranceProductCategory
where
InsuranceOrderLine.FKInsuranceProductId = InsuranceProduct.InsuranceProductID
and InsuranceProduct.FKInsuranceProductCategory = InsuranceProductCategory.InsuranceProductCategoryID
Group by
InsuranceProduct.name, InsuranceProductCategory.Name
This query over returns what I need, but when I try to add more table (InsuranceOrder) to be able to get the regardingUser column, then all the count values are way high.
Select
count(InsuranceOrderLine.AntallPotensiale) as Potensiale,
COUNT(InsuranceOrderLine.AntallSolgt) as Solgt,
InsuranceProduct.Name,
InsuranceProductCategory.Name as Kategori,
RegardingUser
From
InsuranceOrderLine, InsuranceProduct, InsuranceProductCategory, InsuranceSalesLead
where
InsuranceOrderLine.FKInsuranceProductId = InsuranceProduct.InsuranceProductID
and InsuranceProduct.FKInsuranceProductCategory = InsuranceProductCategory.InsuranceProductCategoryID
Group by
InsuranceProduct.name, InsuranceProductCategory.Name,RegardingUser
Thanks in advance
You're adding one more table to your FROM statement, but you don't specify any JOIN condition for that table - so your previous result set will do a FULL OUTER JOIN (cartesian product) with your new table! Of course you'll get duplication of data....
That's one of the reasons that I'm recommending never to use that old, legacy style JOIN - do not simply list a comma-separated bunch of tables in your FROM statement.
Always use the new ANSI standard JOIN syntax with INNER JOIN, LEFT OUTER JOIN and so on:
SELECT
count(iol.AntallPotensiale) as Potensiale,
COUNT(iol.AntallSolgt) as Solgt,
ip.Name,
ipc.Name as Kategori,
isl.RegardingUser
FROM
dbo.InsuranceOrderLine iol
INNER JOIN
dbo.InsuranceProduct ip ON iol.FKInsuranceProductId = ip.InsuranceProductID
INNER JOIN
dbo.InsuranceProductCategory ipc ON ip.FKInsuranceProductCategory = ipc.InsuranceProductCategoryID
INNER JOIN
dbo.InsuranceSalesLead isl ON ???????? -- JOIN condition missing here !!
When you do this, you first of all see right away that you're missing a JOIN condition here - how is this new table InsuranceSalesLead linked to any of the other tables already used in this SQL statement??
And secondly, your intent is much clearer, since the JOIN conditions linking the tables are where they belong - right with the JOIN - and don't clutter up your WHERE clauses ...
It looks like you added the table join which slightly multiplies count of rows - make sure, that you properly joining the table. And be careful with aggregate functions over several joined tables - joins very often lead to duplicates