SQL joining tables and having where clause from the joined table - sql

Currently not working...
SELECT COUNT(*) FROM CLICKHISTORY
INNER JOIN EDSC ON CLICKHISTORY.SOURCEID = EDSC.SOURCEID
WHERE EDSC.DOMAIN='SCMDomain';
Currently this returns Count = 0, and in words I want to merge two tables on the sourceID's and then filter out all data so we are only left with EDSC.DOMAIN= 'SCMDomain.
EDSC and CLICKHISTORY are the two tables. Thanks

Here is an alternative way of writing it without the JOIN:
SELECT COUNT(*)
FROM CLICKHISTORY
WHERE CLICKHISTORY.SOURCEID IN (
SELECT EDSC.SOURCEID
FROM EDSC
WHERE EDSC.DOMAIN = 'SCMDomain')

Related

Get all columns from other tables with a distinct

I am doing a distinct to filter by 2 columns, but I need it to bring me all the columns of the query, in this case it only brings me "idMes" and "idAnio", I need it to show me the other columns as well.
How could I do it?
this is my sentence:
SELECT DISTINCT e.idMes, e.idAnio FROM expensas as e INNER JOIN anios as a on e.idAnio = a.idAnio INNER JOIN meses as m on e.idMes = m.idMes;
Select * gives you all columns

How join two query by removing inner query name in MS Access

I have two tables. One table has floor number(tb_FloorNumber.FloorNumber. records :For example 1 to 15) and another table which has Floor number and User_Id column(tb_Emp_Master.FloorNumber, tb_Emp_Master.User_Id). I want to bring all the records from tb_FloorNumber and only the records from tb_Emp_Master with the condition (User_Id = "fat35108").
I know I can do this with two queries like this :
Query 1:
SELECT DISTINCT tb_Emp_Master.FloorNumber
FROM tb_Emp_Master
WHERE (((tb_Emp_Master.User_Id)="fat35108"));
Query2:
SELECT DISTINCT tb_FloorNumber.FloorNumber, Query1.FloorNumber
FROM tb_FloorNumber LEFT JOIN Query1 ON tb_FloorNumber.FloorNumber = Query1.FloorNumber;
But I want to write this query with sing query instead of using Query1 inside the Query 2
I have tried like this:
SELECT DISTINCT tb_FloorNumber.FloorNumber, tb_Emp_Master.FloorNumber
FROM tb_FloorNumber LEFT JOIN tb_Emp_Master ON tb_FloorNumber.FloorNumber = tb_Emp_Master.FloorNumber
WHERE (((tb_Emp_Master.User_Id)="fat35108"));
But it brings only one record (For instance 8)
Please help me how to write this
If you set the condition:
tb_Emp_Master.User_Id = "fat35108"
in the WHERE clause, then you actually get an INNER JOIN instead of a LEFT JOIN because you filter only the matched rows from tb_Emp_Master.
Use tb_Emp_Master in the LEFT JOIN instead of Query1 and set the condition in the ON clause:
SELECT DISTINCT
tb_FloorNumber.FloorNumber,
tb_Emp_Master.FloorNumber
FROM tb_FloorNumber LEFT JOIN tb_Emp_Master
ON tb_FloorNumber.FloorNumber = tb_Emp_Master.FloorNumber AND tb_Emp_Master.User_Id = "fat35108";
I don't know why you need DISTINCT so I use it too.

Querying a SQL Query for Distinct Records

I have a SQL Query that joins 3 tables and pulls 3 total columns out of them. I was try to figure out how to Query that Query to get distinct records from only one of the columns. Here is what I have so far
Select Distinct Make.NAME
From
(
Select MakeModel.MAKE_ID, Make.NAME, Vehicle.MODEL_YR
From NagsInfo.dbo.Make
INNER JOIN NagsInfo.dbo.MakeModel
on Make.MAKE_ID = MakeModel.MAKE_ID
INNER JOIN NagsInfo.dbo.Vehicle
on MakeModel.MAKE_MODEL_ID = Vehicle.MAKE_MODEL_ID
Where Vehicle.MODEL_YR = #YEAR
)
I keep getting multiple different syntax errors, I believe the most recent one telling me that the parentheses were incorrect, but everywhere I looked they are required for Sub queries.
Why use a subquery at all? Why not just do:
SELECT DISTINCT M.NAME
FROM NagsInfo.dbo.Make M
JOIN NagsInfo.dbo.MakeModel MM ON M.MAKE_ID = MM.MAKE_ID
JOIN NagsInfo.dbo.Vehicle V ON MM.MAKE_MODEL_ID = V.MAKE_MODEL_ID
WHERE V.MODEL_YR = #YEAR

Update using Distinct SUM

I have found a few good resources that show I should be able to merge a select query with an update, but I just can't get my head around of the correct formatting.
I have a select statement that is getting info for me, and I want to pretty much use those results to Update an account table that matches the accountID in the select query.
Here is the select statement:
SELECT DISTINCT SUM(b.workers)*tt.mealTax as MealCost,b.townID,b.accountID
FROM buildings AS b
INNER JOIN town_tax AS tt ON tt.townID = b.townID
GROUP BY b.townID,b.accountID
So in short I want the above query to be merged with:
UPDATE accounts AS a
SET a.wealth = a.wealth - MealCost
Where MealCost is the result from the select query. I am sure there is a way to put this into one, I just haven't quite been able to connect the dots to get it to run consistently without separating into two queries.
First, you don't need the distinct when you have a group by.
Second, how do you intend to link the two results? The SELECT query is returning multiple rows per account (one for each town). Presumably, the accounts table has only one row. Let's say that you wanted the average MealCost for the update.
The select query to get this is:
SELECT accountID, avg(MealCost) as avg_Mealcost
FROM (SELECT SUM(b.workers)*tt.mealTax as MealCost, b.townID, b.accountID
FROM buildings AS b INNER JOIN
town_tax AS tt
ON tt.townID = b.townID
GROUP BY b.townID,b.accountID
) a
GROUP BY accountID
Now, to put this into an update, you can use syntax like the following:
UPDATE accounts
set accounts.wealth = accounts.wealth + asum.avg_mealcost
from (SELECT accountID, avg(MealCost) as avg_Mealcost
FROM (SELECT SUM(b.workers)*tt.mealTax as MealCost, b.townID, b.accountID
FROM buildings AS b INNER JOIN
town_tax AS tt
ON tt.townID = b.townID
GROUP BY b.townID,b.accountID
) a
GROUP BY accountID
) asum
where accounts.accountid = asum.accountid
This uses SQL Server syntax, which I believe is the same as for Oracle and most other databases. Mysql puts the "from" clause before the "set" and allows an alias on "update accounts".

SQL with 0 counts

I have the following sql query :
SELECT DATE(procedures.start) date, name, COUNT(procedure_types.id) count
FROM `procedure_types`
LEFT OUTER JOIN procedures on procedure_types.id = procedures.procedure_type_id
WHERE (DATE(procedures.start) = '2009-10-24')
GROUP BY DATE(procedures.start), procedure_types.id
ORDER BY DATE(procedures.start), procedure_types.id
There are two tables procedures & procedure_types.
Procedure has procedure_type_id which points to the procedure_types table.
The query is executing fine, but I want to list the procedure_types with count(0) even if they aren't being referenced by any procedures on that date.
I know it's something to do with the joins..can someone please help ?
Your WHERE clause is excluding procedure_types with no procedures. You need to move it to the JOIN:
SELECT DATE(procedures.start) date, name, COUNT(procedure_types.id) count
FROM `procedure_types`
LEFT OUTER JOIN procedures on procedure_types.id = procedures.procedure_type_id
AND DATE(procedures.start) = '2009-10-24'
GROUP BY DATE(procedures.start), procedure_types.id
ORDER BY DATE(procedures.start), procedure_types.id
Left Outer Join will select all records from Procedures table and only matching from Procedure_Types. You want to select all records from Procedure_Types even if no matching record in Procedures, so you need Right Outer Join. See this for more info on JOINS.