SQL Server View get count from related table where column = "True" - sql

I have two related tables in SQL Server in a 1 to many relationship (Applicant, Reference).
I want a view that will retrieve all the data from the Applicant table and also add another column to the view that tells me how many related Reference rows there are where the "Complete" column is True.

Something like this using a subquery:
select applicantfield1, applicantfield2,
(select count(*) from
reference where reference.applicantkey = applicant.applicantkey
and reference.complete = 1) AS referencecount
from applicant
Unless the complete field is in the applicant table (not the reference table). If so, it would be more like this:
select applicantfield1, applicantfield2,
(select count(*) from
reference where
reference.applicantkey = applicant.applicantkey) AS referencecount
from applicant
where applicant.complete = 1

Something like the below. You need to join on a table. This will handle where the applicant also doesn't have a True reference.
SELECT A.*, isnull(r.comptotal,0) as CompleteTotal
FROM Applicant as a Left Join
(SELECT ApplicantId, Count(Complete) as comptotal
FROM Reference Where Complete=1 Group by ApplicantID) as r
on a.ApplicantId = r.applicantId

Related

Selecting Matching values from three different table and combining them in one table in Oracle

I have Four tables as follows
Review (REV_ID pk , REV_NAME)
Meeting (MEETING_ID pk,MEETING_NAME,REV_ID fk to Review)
Task (TASK_ID pk,TASK_NAME,REV_ID fk to Review)
Answer (ANS_ID pk,ANS_NAME,REV_ID fk to Review)
Now I want to select a particular Review and want to create a table with
Linked meetings
Linked answers
Linked tasks
How shall I proceed with it?
I tried writing join query but I was only able to get data if Rev_ID is present in all tables?
select * from
(SELECT *
FROM meeting
WHERE EXISTS (SELECT *
FROM review WHERE meeting.rev_id
=review.rev_id)
and meeting.rev_id=142),
(SELECT *
FROM answer
WHERE EXISTS (SELECT *
FROM review WHERE answer.rev_id
=rev.rev_id)
and answer.ans_rev_id=142),
(SELECT *
FROM task
WHERE EXISTS (SELECT *
FROM review WHERE task.rev_id
=review.rev_id)
and task.rev_id=142) r;
Note : Here I tried static Rev_ID =142 to check data.
From above query i am getting output only if data exist in all four table, But if Data does not exist in any table, It does not return remaining value.
I want at-least names of all table's in final output.
Try the following, let us know if this meets your requirements.
SELECT rv.rev_id,
rv.rev_name,
mt.meeting_name,
tk.task_name,
ans.ans_name
FROM review rv
LEFT OUTER JOIN meeting mt ON (rv.rev_id = mt.rev_id)
LEFT OUTER JOIN task tk ON (rv.rev_id = tk.rev_id)
LEFT OUTER JOIN answer ans ON (rv.rev_id = ans.rev_id)
WHERE rv.rev_id = 142
SQL Fiddle Demo
If the above SQL is fine, prefix it with create table or view syntax to combine them into one.

How I use where clause with computed column to get related records only

I read this FAQ http://www.firebirdfaq.org/faq289/ and I want to use select to count detail record from current record so I tried this
((
select count(*) from detail_table where detail_table.id = id
))
But I did not get correct values I got numbers like 19 where in fact it is the total number of records in detail_table! How can I get only the count of records related to current master record in master table?
The problem is that id refers to the id column of detail_table and not of the master table. So it is the equivalent of:
select count(*) from detail_table where detail_table.id = detail_table.id
Which means you are simply counting all rows. Instead - assuming the master table is master_table - you should use:
select count(*) from detail_table where detail_table.id = master_table.id
Note that, as also mentioned in the FAQ you link to, you should really consider using a view instead of a computed column when referencing other tables as it is not very good for performance.
The view equivalent would be something like
CREATE OR ALTER VIEW master_with_detail_count
AS
SELECT master_table.id, coalesce(c.detail_count, 0) as detail_count
FROM master_table
LEFT JOIN (SELECT id, count(*) as detail_count FROM detail GROUP BY id) c
ON c.id = master.id

SQL query multiple tables in database

I need to query 3 tables from a database.
The table Client includes client id no. and client name.
The table Property includes property id no. and property name amongst other things.
The table clientsInterestedInProperties includes client id no. property id no. and date of visit to property.
I want to list the name of clients who have an interest in a specific property (with the name, not id. no) and the date they visited the property.
For example say the property is called Barker Hall, who is interested in it and when did they visit?
Can anyone help?
First, use main filtering condition in your from table. So you must start with
select ... from Property ... ,
Then connect visitations to the property. It will generate different row for each visit, with
LEFT JOIN to get visitation to the property
Then we need to get readably client names, not IDs, so we do
LEFT JOIN to get client names
And some additional filtering/conditions can be added with
WHERE clauses ( or INNER JOIN ) , like visiting period.
Example SQL for your case ( i don't know your table/column names )
select PropertyName, ClientsInProp_VisitDate, ClientName
from Property
left join ClientsInProp on ClientsInProp_Propertyid = Property_PropertyID
left join Client on Client_Clientid = ClientsInProp_ClientId
where Proprty_Name = 'House 1' and ClientsInProp_VisitDate > '01.10.2013'
Cheers !
What you want is an IN clause:
SELECT * FROM Client
WHERE ClientId IN
(SELECT ClientID
FROM clientsInterestedInProperties cip
INNER JOIN Property p
ON cip.PropertyID = p.PropertyID
WHERE p.ProperyName = #propertyName)

Retrieve different row from same table

i hava a set of following tables
customer(cus_id,cus_name);
jointAccount(cus_id,acc_number,relationship);
account(acc_number,cus_id)
now i want to create a select statement to list all the jointAccounts,
it should included the both customer name, and relationship.
I have no idea how to retrieve both different user name, is that possible to do this?
Generally speaking, yes. I'm assuming you mean you want to get customer info for both sides of the joint account per your jointAccount table. Not sure what database you're using so this answer is assuming MySQL.
You can join on the same table twice in a single SQL query. I'm assuming you have not yet created your tables, as you have cus_id listed twice in the jointAccount table. Typically these would be something like cus_id1 and cus_id2, which I've used in my sample query below.
Example:
SELECT c1.cus_id AS cust1_id, c1.cus_name AS cust1_name
, c2.cus_id AS cust2_id, c2.cus_name AS cust2_name, j.relationship
FROM customer c1
INNER JOIN jointAccount j
ON c1.cus_id = j.cus_id1
, customer c2
INNER JOIN jointAccount j
ON c2.cus_id = j.cus_id2
I haven't tested this but that's the general idea.
try this query:
SELECT * FROM jointAccount a LEFT JOIN customer c ON a.cus_id = c.cus_id;
just replace the * with the name of the columns you need.

SQL Update one table comparing info from two tables

I have the following problem:
Let's suppose I defined TWO tables
USERS
ID (int. key)
NAME (String)
SALARY (currency)
USERSADD
ID (int. key)
TYPE (String)
The 2nd table stores additional information for USERS. Obviously the real tables are more complicated but this is the idea. (Don't ask me why another table is created instead of adding fields to the first table, this is my boss's idea).
Now I am trying to UPDATE the first table if a condition from second table is satisfied.
Something like this:
UPDATE USERS U, USERSADD A
SET U.SALARY = 1000
WHERE U.ID = A.ID
AND A.TYPE = 'Manager'
In Netbeans Derby I have an error: ", found in column X", and it refers to the comma between the two tables (UPDATE USERS U, USERSADD A). I hope I was clear enough...
Would somebody be kind enough to provide me with a solution? Thanks in advance.
UPDATE USERS
SET SALARY = 1000
WHERE ID IN (
SELECT ID FROM USERSADD
WHERE TYPE = 'Manager')
UPDATE USERS
SET USERS.SALARY = 1000
FROM USERS JOIN USERSADD ON USERS.ID = USERSADD.ID
WHERE USERSADD.TYPE ='MANAGER'
The syntax you are using uses an implicit INNER JOIN. It would be better for you to use an explicit join. Try something like this:
UPDATE Users
SET Salary = 1000
FROM Users u
INNER JOIN Usersadd a on u.id=a.id
AND a.Type = 'Manager
UPDATE USERSU
SET SALARY = 1000
WHERE exist IN (
SELECT ID
FROM USERSADD A
WHERE TYPE = 'Manager'
AND U.id = A.id
)