I am new to SQL.
I am currently trying to write a query where i would like to list down all the details in my tables. I am using joins to get the together and everything is working fine. Where i get stuck is when i try to use count with my other columns. The issue is that the count i am referring to is a text field and as per that table the same id appears multiple times and i want to get the count from my query. My query looks like this
select col1, col2, col3, count(col4)
from table1 c
left join table2 a on c.id = a.id
however this does not work. I would appreciate any leads.
Use and study on group by. Your code should be something like below.
select col1, col2, col3, count(col4)
from table1 c
left join table2 a on c.id = a.id
group by col1, col2, col3
You need to add group by clause
select col1, col2, col3, count(col4)
from table1 c
left join table2 a on c.id = a.id
group by col1, col2, col3
Related
I have 2 tables like this:
Table A:
guv, col1, col2
Table B:
guv, col3, col4, col5..
Now each A and B have one to many relationship, so when I run the following query:
select * from A,B where a.guv. = b.guv
It returns all the rows in B that match the join, how do I return only one row(based on some order in one of the columns) that matches?
I tried to do this using Top as read in some other answers, but its not supported by aws athena.
You may use ROW_NUMBER() function within the join query as the following:
SELECT guv, col1, col2, col3, col4, col5
FROM
(
SELECT A.guv, A.col1, A.col2, B.col3, B.col4, B.col5,
ROW_NUMBER() OVER (PARTITION BY A.guv ORDER BY B.col3) rn
FROM TableA A JOIN TableB B
ON A.guv=B.guv
) T
WHERE rn = 1
In ROW_NUMBER() OVER (PARTITION BY A.guv ORDER BY B.col3) you may change the order by B.col3 to any other column order.
Is it a good approach to Find Maximums from nested SQL query and then use that maximum value again in the same query.
For example:
Select Col1, Col2, Col3, Col2/Col3 AS Col4, CASE Alot of statements END Col5
from Table A inner join Table B on A.Id = B.Id
Inner Join Table C on B.Id = C.Id
Inner Join Table D on C.Id = D.Id
Now I need to find Maximum integer from Col5 and then later use that Max Value to divide Col4 value.
I also need to display col1 and col2 along with results.
Expected result:
Col1, Col2, (Col4/MaxValue of Col5)
Could anybody guide me what's the best solution in this case?
If those are the only results you need, then no subquery/CTE is needed:
Select Col1, Col2,
( (Col2 / Col3) /
max(case Alot of statements end) over ()
) as ratio
from Table A inner join
Table B
on A.Id = B.Id Inner Join
Table C
on B.Id = C.Id Inner Join
Table D
on C.Id = D.Id;
Tim's answer is definitely a good answer if you need col5 more than once in the final result set.
You can use a common table expression:
WITH cte AS (
SELECT Col1, Col2, Col3, Col2/Col3 AS Col4,
CASE Alot of statments END Col5
FROM TableA A
INNER JOIN TableB B
ON A.Id = B.Id
INNER JOIN TableC C
ON B.Id = C.Id
INNER JOIN TableD D
ON C.Id = D.Id
)
SELECT t.Col1, t.Col2,
t.Col4 / MAX(t.Col5) OVER ()
FROM cte t
I am trying to accomplish the following, and I am not sure if it is possible. I have a SELECT Statement that contains an inner SELECT for two of the table columns like so:
SELECT
col1,
col2,
(SELECT SUM(col1)
FROM table2)
AS FirstResultToAdd,
(SELECT SUM(col2)
FROM table3)
AS SecondResultToAdd,
FROM Table1
So my question is: Is it possible to perform a calculation, such as doing a SUM of "FirstResultToAdd" and "SecondResultToAdd, and returning that as a single column result on "Table1"? Also to keep in mind, I have excluded any joins of the tables to keep the example simple.
I believe you want to perform some logic on the result of Sub-query
To add the two sub-query result
SELECT col1,
col2,
(SELECT col1
FROM table2)
AS FirstResultToAdd,
(SELECT col2
FROM table3)
AS SecondResultToAdd,
(SELECT col1
FROM table2)
+
(SELECT col2
FROM table3)
AS total
FROM table1
To make the query more readable you can make the original query as Sub-Select and perform the logic in Outer query
just nest one more time...
select col1, col2, sum( FirstResultToAdd )
from (
SELECT
col1,
col2,
(SELECT col1
FROM table2)
AS FirstResultToAdd,
(SELECT col2
FROM table3)
AS SecondResultToAdd,
FROM Table1
)
Edit: Fixed Group By
Try this:
Select A.Col1,
A.Col2,
(B.Col3 + C.Col4)
From(
(Select Col1,
Col2
From [Table1]) A
Inner join (Select Sum(Col3) AS Col3
From [Table2]) B on 1 = 1
Inner join (Select Sum(Col4) AS Col4
From [Table3]) C on 1 = 1
)
Group By A.Col1,
A.Col2,
B.Col3,
C.Col4
I have a query with some subqueries inside and I want to add a sum query to sum them all.
How can I do that?
example:
Id,
(SELECT COUNT(*) FROM table1 LEFT JOIN table2 on ...) as col1,
(SELECT COUNT(*) FROM table3 LEFT JOIN table4 on ...) as col2,
** Sum of both col1 and col2 here **
Try this:
SELECT ID, col1, col2, [Total] = (col1 + col2)
FROM (
SELECT Id,
(SELECT COUNT(*) FROM table1 LEFT JOIN table2 on ...) as col1,
(SELECT COUNT(*) FROM table3 LEFT JOIN table4 on ...) as col2
FROM [TABLE]) T
Hope that helps.
the easiest way would be to treat all your query as a subquery
select Id, col1 + col2 as total
from
(<yourCode>) s
Because it's not possible to use alias in the same "level of query" in the select clause.
I have two tables which I am left joining together.
SELECT Col3, tab1.Col1, tab1.Col2 FROM
(SELECT Col1,Col2
FROM Table1
GROUP BY Col1,Col2) tab1
LEFT JOIN
(SELECT Col3, Col1, Col2
FROM Table2
GROUP BY Col3, Col1, Col2) tab2
ON tab2.Col2 = tab1.Col2 AND tab2.Col1 = tab1.Col1
At the moment for the rows in Table1 which do not exist in Table2 I return a row where Col3 is Null. As I am grouping data based on Col3, it would be good if I could somehow get the value of Col3 instead of Null.....
Is this possible??
So I am trying to return every possible combination of col1 and col2, per value of col3. The problem is when col3 does not contain a particular combination of col1,col2 I am getting nulls for col3...
Assuming Col3 is some kind of category, and is a primary key of a category table, you might do this:
select Category.Col3,
tab1.Col1,
tab1.Col2,
sum (tab2.YourAggregate) SumOfSomething
-- Take all categories
from Category
-- Cartesian product with Tabl1
cross join Table1 tab1
-- Find matching records in Table2, if they exist
left join Table2 tab2
on Category.Col3 = Tab2.Col3
and Tab1.Col1 = Tab2.Col1
and Tab1.Col2 = Tab2.Col2
group by Category.Col3,
tab1.Col1,
tab1.Col2
Cross join produces Cartesian product of tables involved, retrieving Col3 which might not be found in Table2.
A LEFT JOIN will produce nulls in the right table where no match could be found for Col1 and Col2 in the left table. This is the expected behavior.
If you need help writing a different query, you'll have to post your data structures and some sample data to play with.
Just switch your tables (or use right join):
SELECT tab2.Col3, tab2.Col1, tab2.Col2 FROM
(SELECT distinct Col3, Col1, Col2 FROM Table2) tab2
LEFT JOIN
(SELECT distinct Col1,Col2 FROM Table1) tab1
ON tab2.Col2 = tab1.Col2 AND tab2.Col1 = tab1.Col1