Multiple counts in a multi table query SQL - sql

I would like to count The different requests by survey Id's and grouping it by SubjectValue
I have done this on just the one table with a sub query, but I'm not too sure to do it with several. Could anyone help me out?
This is how the 3 tables are joined. The only values of note are
subjectValue - Table A
Request_Id - Table A
Survey_Id - Table C
SELECT TableA.SubjectValue
FROM TableB INNER JOIN
TableA ON TableB.ID = TableA.Request_ID INNER JOIN
Table C ON TableB.Details_ID = TableC.ID
May I also add that all counts should be returned in the same row.
there are 3 different survey Id's so the count will need a where clause on the survey_id.
Hope that makes sense.
Many thanks in advance.

You can use generic Cross-tab method
select
TableA.SubjectValue,
SUM(case when somecol='request1' then 1 else 0 end) as request1,
SUM(case when somecol='request2' then 1 else 0 end) as request2,
.
TableB INNER JOIN
TableA ON TableB.ID = TableA.Request_ID INNER JOIN
Table C ON TableB.Details_ID = TableC.ID
group by
TableA.SubjectValue

You probably dont need to join to table C (surveys) assuming you have a foreign key to it on table B (Requests).
try this.
SELECT TableA.SubjectValue, COUNT(TableB.SurveyID)
FROM TableB
INNER JOIN TableA ON TableB.ID = TableA.Request_ID
Group by TableA.SubjectValue
EDIT: To Include SurveyID use this..
SELECT TableC.SurveyID, TableA.SubjectValue, COUNT(TableB.RequestId)
FROM TableA
INNER JOIN TableB ON TableB.SurveyID = TableA.SurveyID
INNER JOIN TableC ON TableC.RequestID = TableB.RequestID
Group by TableA.SubjectValue, TableC.SurveyID
(hope i didnt get my A, B's and C's mixed up.)

Related

How to find differences between 2 tables with different key names

So say I have 2 tables. TableA has keys FirstName, LastName, Age, Location. TableB has keys 1FirstName, 1LastName, 1Age, 1Location. Table A is a working table, and TableB is a reference table. How could I go about finding what records exist in TableB that DO NOT exist in TableA
Using an outer join will join all rows of left table to either right table or NULL
Then including only NULL from right hand side will give you what you need.
TableB as left side
SELECT
*
FROM (
SELECT
b.*,
a.*
FROM TableB b
LEFT OUTER JOIN TableA a
ON
b.1FirstName = a.Firstname
AND
b.1LastName = a.Lastname
AND
b.1Age = a.Age
AND
b.1Location = a.Location
) q
WHERE q.FirstName IS NULL

SQL Multiple joins with OR condition

I have a following tables:
TableA
id
name
TableB
id
tableA_id
TableC
id
tableA_id
So tables B and C have tableA_id fk.
I need a query which will return all id-s from TableA that have reference in either TableB or TableC.
If I do standard join, it will return only values that have reference in both tables B and C.
I could do this using two queries, one joins B, other joins C and use UNION to merge results, but I dont want to write same query twice.
Is there a way to achieve this 'OR' JOIN??
Try with this
SELECT DISTINCT TABLEA.ID_A, TABLEA.NAME
FROM TABLEA,TABLEB,TABLEC
WHERE TABLEA.ID_A = TABLEB.ID_A AND TABLEA.ID_A = TABLEC.ID_A
Using DISTINCT TAG you eliminate duplicates.
Something like this?
SELECT
*
FROM
TableA,
TableB,
TableC
WHERE
TableA.id = TableB.tableA_id
OR TableA.id = TableC.tableA_id
You can use left outer joins and checks for the ids:
SELECT a.id FROM A a
LEFT JOIN B b ON a.id = b.tableA_id LEFT JOIN C c ON a.id = c.tableA_id
WHERE b.id IS NOT NULL OR c.id IS NOT NULL

My attempt at LEFT JOIN isn't producing desired result. Any ideas?

I am seriously suffering from brain dead as I have done this successfully several times in the past.
This time, it isn't working.
I have 2 tables, tableA and tableB
TableA has all the surmons records
TableB has some but not all surmons.
The common key between them is surmonId.
The requirement is to display the surmons from tableB where there is a match between tableA and tableB but at the same time, display ALL the surmons from tableA.
In other words, give me from tableB any records that exist and all the records on tableA.
The lef join query below is only giving me records that exist in tableB.
Select distinct l.surmons from tableB b left join tableA a on b.surmonId = a.surmonId.
There are only 10 surmons on tableB and that's all I am getting.
Where am I messing up?
Thanks a lot in advance
Either switch order of your tables:
SELECT DISTINCT a.surmons
FROM tableA a
LEFT JOIN tableB b
ON a.surmonId = b.surmonId
Or use my favorite, the RIGHT JOIN:
SELECT DISTINCT a.surmons
FROM tableB b
RIGHT JOIN tableA a
ON b.surmonId = a.surmonId
If you want everything from tableA, you need to make the left join from tableA to tableB.
Select distinct a.surmons from tableA a left join tableB b on a.surmonId = b.surmonId

SQL select statement ORDER BY relationship to a second table?

I have table A, which contains a row for an int representing the table B object they relate to.
Multiple As can reference the same B. B does not reference A
I want to return As ordered by a row in the B object they relate to.
Is there a way to do this in one SQL statement? Or even 2?
Thank you.
You can put anything in your SELECT list and ORDER BY any column you'd like as long as it's in tablea or tableb
SELECT a.ID
FROM tablea
INNER JOIN tableb ON tablea.ID = tableb.ID
ORDER BY tableb.ID
Have you tried using
Select (columns that you want to display)
from TableA INNER JOIN TableB
ON TableA.col = TableB.col
Order By TableB.ColumnName

Join and showing different columns from tables

I have a simple SQL question, I thought it would be quite straight forward but have got myself in a muddle. Any help would be appreciated
I have table A which contains a last updated
Table A has a one to many with Table B
Table B has a one to many with Table C
I want to show all rows of table C with the last updated time from table A. I have tried some joins but they dont seem to be quite working. Ideally I want somehting like
select a.lastUpdated c.* from TableA a, TableC c where
a.id in (select a_id from TableB where (select b_id from TableC where c_id = select
id from TableC where XXXX=YYYY))
so I can pass in an id for table C and then get one row returned with the last updated time present.
XXX=YYY would be my criteria for returning one row of table C.
Any help or pointers appreciated
Thanks
Something like
SELECT c.*
FROM TableA AS a
INNER JOIN TableB AS b
ON a.a_id = b.b_id
INNER JOIN TableC AS c
ON b.b_id = c.c_id
WHERE a.lastUpdated = c.lastUpdated;
Should work. This is a situation where a striaght INNER JOIN should suffice; unless of course I have missed something.
I hope this helps.
You should be able to do this by joining A and B together, aggregating the results at the c_id level, and then joining in C:
select tc.*, maxlastupdated
from tablec tc left outer join
(select tb.c_id, max(lastupdated) as maxlastupdated
from tablea ta join
tableb tb
on ta.b_id = tb.b_id
group by ta.id
) ta
on tc.c_id = ta.c_id
You need to drive your SQL query from Table C.
The query below displays the updated timestamp column from table A.
Since it is a one-to-many in the direction of tables A --> B --> C
You will inevitably end-up with a lot of rows in table C - all with the same timestamp.
SELECT c.*, a1.update_timestamp
FROM table_c c, table_b b, table_a a1
WHERE c.join_column = b.join_column
AND b.join_column = a1.join_column
AND a1.update_timestamp =
(SELECT max(a2.update_timestamp) FROM table_a a2
WHERE a2.<identifying columns> = a1.<identifying columns>
);