SELECT Product.prodName, Runs.buildNumber, Runs.prodDate
FROM Product
INNER JOIN Runs on prodId where Runs.runId=118
The above is my query for the schema:
[Please refer comment.It doesn't let me post images here]
It gives me an error : An expression of non-boolean type specified in a context where a condition is expected
Small syntax error is there
Inner Join Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name where condition;
Update Your query as follows
SELECT Product.prodName, Runs.buildNumber, Runs.prodDate
FROM Product
INNER JOIN Runs on Product.columnname=Runs.columnname where Runs.runId=118
Just Go to this link there is a clean and neat example of joins
You query needs to be like this
SELECT Product.prodName, Runs.buildNumber, Runs.prodDate
FROM Product
INNER JOIN Runs r on p.prodId=r.prodId where Runs.runId=118
Your syntax is wrong:
The on condition must be a boolean condition.
SELECT Product.prodName, Runs.buildNumber, Runs.prodDate
FROM Product
INNER JOIN Runs on prodId=... where Runs.runId=118
So it must be <column from Product> = <column from Runs>
From Itzik Ben-Gan's book Microsoft SQL Server 2012 T-SQL Fundamentals
INNER JOINapplies a Cartesian product between the two input tables as in a cross join, and then it filters rows based on a predicated that you specify.
Based on above description, it must be predicate expression after ON, such as Product.prodId=Runs.prodId
Related
I am trying to select data from same table twice and it works but it is not allowing me to use the first table in the list in INNER JOIN, if I use the second table from the list in the join it works
So this is the query that works fine
SELECT
TOP 1
RIT_First.LastModified,
RIT_Last.LastModified
FROM
dbo.ItemTransaction RIT_First,
dbo.ItemTransaction RIT_Last
JOIN dbo.Item RI ON RIT_Last.ItemId = RI.Id -- Using the second table in the join
But when I do this
SELECT
TOP 1
RIT_First.LastModified,
RIT_Last.LastModified
FROM
dbo.ItemTransaction RIT_First,
dbo.ItemTransaction RIT_Last
JOIN dbo.Item RI ON RIT_First.ItemId = RI.Id -- Using the first table in the join
I start getting this error
The multi-part identifier "RIT_First.ItemId" could not be bound.
So is it like we can use only last table in the list of tables in the join or I am doing something wrong?
The problem here is your mix and matching of ANSI-89 JOINs and ANSI-92 JOINs. Just don't use ANSI-89 JOINs any more; they were superseded 30 years ago.
If you use ANSI-92 JOINs the whole way through, you don't have a problem:
SELECT TOP (1)
RIT_First.LastModified,
RIT_Last.LastModified
FROM dbo.ItemTransaction RIT_First
CROSS JOIN dbo.ItemTransaction RIT_Last
JOIN dbo.Item RI ON RIT_First.ItemId = RI.Id
ORDER BY {Expression(s)};
If, for some bizarre reason, you are compelled to use an ANSI-89 JOIN, then you would need to put the join criteria in the WHERE:
SELECT TOP (1)
RIT_First.LastModified,
RIT_Last.LastModified
FROM dbo.ItemTransaction RIT_First,
dbo.ItemTransaction RIT_Last,
dbo.Item RI
WHERE RIT_First.ItemId = RI.Id
ORDER BY {Expression(s)};
But, like I said, just don't do that. See this article by Aaron Bertrand for more information: Bad Habits to Kick : Using old-style JOINs
I'm new to SQL and need a push in the right direction.
I currently have a working SQL query accessing 3 tables in a database, and I need to add a JOIN using a 4th table, but the syntax escapes me. To simplify, what I have now is:
SELECT
t1_col1, t1_col2, t2_col1, t2_col2, t3_col1
FROM
table1, table2, table3
WHERE
{some conditions}
ORDER BY
t1_col1 ASC;
What I need to do is to add a LEFT OUTER JOIN selecting 2 columns from table4 and have ON t1_field1 = t4_field1, but whatever I try, I'm getting syntax errors all over the place. I don't seem to understand the correct syntax.
I tried
SELECT *
FROM table1
LEFT OUTER JOIN table2;
which has no errors, but as soon as I start SELECTing columns and adding conditions, I get stuck.
I would greatly appreciate any assistance with this.
You do not specify the join criteria. Those would be in your WHERE clause under "some conditions". So, I will make up so that I can show syntax. The syntax you show is often termed "old". It has been discouraged for 15 years or more in the SQL Server documentation. Microsoft consistently threatens to stop recognizing the syntax. But, apparently they have not followed through on that threat.
The syntax errors you are getting occur because you are mixing the old style joins (comma separated with WHERE clause) with the new style (LEFT OUTER JOIN) with ON clauses.
Your existing query should be changed to something like this. The tables are aliased because it makes it easier to read and is customary. I just made up the JOIN criteria.
SELECT t1_col1, t1_col2, t2_col1, t2_col2, t3_col1
FROM table1 t1
INNER JOIN table2 t2 ON t2.one_ID = t1.one_ID
INNER JOIN table3 t3 ON t3.two_ID = t2.two_ID
LEFT OUTER JOIN table4 t4 ON t4.three_ID = t3.three_ID
I hope that helps with "a push in the right direction."
You may also want to read this post that explains the different ways to join tables in a query. What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
Also for the record the "OLD STYLE" of joining tables (NOT RECOMMENDED) would look like this (but do NOT do this - it is a horrible way to write SQL). And it does not work for left outer joins. Get familiar with using the ...JOIN...ON... syntax:
SELECT t1_col1, t1_col2, t2_col1, t2_col2, t3_col1
FROM table1 t1, table2 t2, table3 t3
LEFT OUTER JOIN table4 t4 ON t4.three_ID = t3.three_ID
WHERE
t2.one_ID = t1.one_ID
AND t3.two_ID = t2.two_ID
I'm recently studying SQL Server. I read a book and it says that merge join requires at least one equal sign(=) on ON clause in SQL Server.
So, I tried this query below and I found that the error occurs.
SELECT *
FROM TABLE_1 AS T1
INNER MERGE JOIN TABLE_2 AS T2
ON T1.COL_1 > T2.COL_2
Error Message:
Msg 8622, Level 16, State 1, Line 6 Query processor could not produce
a query plan because of the hints defined in this query. Resubmit the
query without specifying any hints and without using SET FORCEPLAN.
And this book also says this can be done in case of Full Outer Join.
So I tried this query below and found it committed successfully with no errors.
SELECT *
FROM TABLE_1 AS T1
FULL OUTER MERGE JOIN TABLE_2 AS T2
ON T1.COL_1 > T2.COL_2
I tried to search for the reason but I couldn't find any explanation about this.
Can anyone tell me why SQL Server doesn't allow merge join without an equality operator unless it's full outer join?
Thank you for reading my question
You used Inner Merge Join. Merge clause is a join hint that tells sql engine to work more efficient.
Merge joins have the fastest algorithm since each row only needs to be read once from the source inputs. Also, optimizations occurring in other join operators can give those operators better performance under certain conditions.
if you must use '>' operator, use regular Inner Join, like this.
SELECT *
FROM TABLE_1 AS T1
INNER JOIN TABLE_2 AS T2
ON T1.COL_1 > T2.COL_2
Unable to write Join condition using ON clause.
This is Working:
SELECT STUDENT.STD_NAME,CLASS_SUBJECT.SUB_NAME
FROM STUDENT CROSS JOIN CLASS_SUBJECT
WHERE STUDENT.CLS_ID=CLASS_SUBJECT.CLS_ID
This is not working:
SELECT STUDENT.STD_NAME,CLASS_SUBJECT.SUB_NAME
FROM STUDENT CROSS JOIN CLASS_SUBJECT ON STUDENT.CLS_ID=CLASS_SUBJECT.CLS_ID
CROSS JOIN does not use an ON clause. It produces a cartesian product, matching all records from both tables. There are only very rare cases where you actually want this.
If you want conditional record matching between tables (for example where key values match), you should use INNER JOIN or one of the OUTER JOIN variants (LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN).
Combining CROSS JOIN with a WHERE clause for conditional record matching is possible, but using INNER JOIN is recommended in such cases.
I am newbie to Stack overflow and also SQL server reporting services. So please excuse me for the format of the question.
So here is the situation:
I am developing a SSRS report which needs to be grouped by an Count of Distinct product names as shown below.
I created a text box called ProdCount with an expression
COUNTDISTNCT(Fields!Product.value,"DataSet1")
which gives me the count 63 within the scope of DataSet1.
Now i need to group the data by taking product names where the above formula is >1 .
=IIF(ProdCount>1,Fields!Product.value,Nothing)
My Problem:
I tried to call the ProdCount from the calculated field since i
cant use the aggregate functions in Calculated Fields and use
the second expression by using
= ReportItems!ProdCount.value
which gives me an error FieldValue Denying ReportItems
I tried to combine the above two expressions by creating a calculated field by
IIF(CountDistinct(Fields!Product.Value,"DataSet1")>1,Fields!Product.Value,Nothing)
which gives me an error Calculated fields cannot have expressions
I tried to use Report Variables in the same way as above(1) which was not working either.
I also tried to use CROSS JOIN in the query
Select Count(Distinct(Product Name)
from Query1
Cross join
My Main Query which give me the data
which is taking more time to execute.
So Can anyone help me with solution where i can group the data by combining the above two expressions.
Please excuse me for the format. I was confused with framing question. I accept all your edits , so that i can learn in future.
Here is my code:
SELECT * FROM
--Query1 which counts the number of distinct products)
(SELECT DISTINCT COUNT(gproduct.ProductName) AS ProdCount
FROM Table1
LEFT JOIN Table4
ON Table1.column=Table1.column
LEFT JOIN Table2
ON Table3.Column = TTable1.Column
LEFT JOIN
(
SELECT Distinct Table6.Name AS ProductName,Table9.ColumnId
FROM Table6
INNER JOIN Table7
ON Table6.Column=Table7.Column
INNER JOIN Table8
ON Table7.Column=Table8.Column
INNER JOIN Table9
ON Table9.Column=Table8.Column
)gproduct
ON Table1.ColumnId=gproduct.ColumnId
GROUP BY gproduct.ColumnId,
)qProduct
CROSS JOIN
--My main Query which get data from different table including Product name
(SELECT
Upper(CASE WHEN (CASE WHEN Table4.Column =1 THEN 'Yes' ELSE 'NO' END)='YES'
THEN gab.ProductName
ELSE
Table2.productName
END) AS Product,
FROM Table1 AS ec
LEFT JOIN Table2 AS ep
ON --
LEFT JOIN Table3 AS ebrd
ON --
Left JOIN Table4 AS etpc
ON --
LEFT JOIN Table5 AS gst
ON --
LEFT JOIN
(
SELECT Distinct Table6.Name AS ProductName,Table9.ColumnId
FROM Table6
INNER JOIN Table7
ON Table6.Column=Table7.Column
INNER JOIN Table8
ON Table7.Column=Table8.Column
INNER JOIN Table9
ON Table9.Column=Table8.Column
) gab
ON Table1.ColumnId=gab.ColumnId
)QMain
Personally I would try to solve the problem in query itself instead of SSRS report. According the data you provided it would be something like:
SELECT
ProductName,
count(distinct Product)
from
YourTable
group by
ProductName
having count(distinct product) > 1
Later on creating SSRS report should be quite easy.