I have two tables
Threads:
i_id thread_note seq_id
1 ABC 2
2 CDE 2
3 FGH 1
4 IJK 2
Notes:
i_id note_text entered_date
1 stack 09/08/2017
5 queue 07/07/2014
3 push 09/07/1996
I want the output as
i_id thread_note seq_id note_text entered_date
1 ABC 2 stack 09/08/2017
2 CDE 2 null null
3 FGH 1 push 09/07/1996
4 IJK 2 null null
5 null null queue 07/07/2014
How do I achieve this? The tables are not related to each other.
Note: This is different from most of the questions similar to this asked because there are some "i_id" values which are present in threads table but not in notes table and there are some "i_id" values present in notes table but not in threads table
Use a full outer join:
SELECT
COALESCE(t.i_id, n.i_id) AS i_id,
t.thread_note,
t.seq_id,
n.note_text,
n.entered_date
FROM Threads t
FULL OUTER JOIN Notes n
ON n.i_id = t.i_id
ORDER BY
i_id;
Note that having the need to do a full outer join often can indicate a problem with your relational model, because it means you don't know the key relationships between your tables.
Demo
Edit:
If you are using a database such as MySQL which does not support a full outer join, we can still simulate one:
SELECT *
FROM Threads t
LEFT JOIN Notes n
ON n.i_id = t.i_id
UNION ALL
SELECT *
FROM Threads t
RIGHT JOIN Notes n
ON n.i_id = t.i_id
WHERE t.i_id IS NULL;
First, you need to get all i_id from all tables in a subquery. Once you have the rows, join this to the two tables to get the columns you need,
SELECT a.i_id,
b.thread_note,
b.seq_id,
c.Note_text,
c.entered_date
FROM
(
SELECT i_id FROM Threads UNION
SELECT i_id FROM Notes
) a
LEFT JOIN Threads b
ON a.i_id = b.i_id
LEFT JOIN Notes c
ON a.i_id = c.i_id
ORDER BY a.i_id
Here's a Demo.
You could just use a FULL OUTER JOIN here. If I make some test data:
DECLARE #threads TABLE (i_id INT, thread_note NVARCHAR(3), seq_id INT);
INSERT INTO #threads SELECT 1, 'ABC', 2;
INSERT INTO #threads SELECT 2, 'CDE', 2;
INSERT INTO #threads SELECT 3, 'FGH', 1;
INSERT INTO #threads SELECT 4, 'IJK', 2;
DECLARE #notes TABLE (i_id INT, note_text NVARCHAR(10), entered_date DATE);
INSERT INTO #notes SELECT 1, 'stack', '20170809';
INSERT INTO #notes SELECT 5, 'queue', '20140707';
INSERT INTO #notes SELECT 3, 'push', '19960709';
Then my query is simply:
SELECT
ISNULL(t.i_id, n.i_id) AS i_id,
t.thread_note,
t.seq_id,
n.note_text,
n.entered_date
FROM
#threads t
FULL OUTER JOIN #notes n ON n.i_id = t.i_id
ORDER BY
ISNULL(t.i_id, n.i_id);
No need to make a list of unique I_ids.
Use the below query
select Isnull(n.i_id,t.i_id), [thread_note],seq_id,Notetest,Enddate
from [dbo].[note] n FULL OUTER JOIN [dbo].[thread] t on n.[i_id]=t.[i_id]
order by Isnull(n.i_id,t.i_id)
Related
I am wondering if there is a better way to write this query. It achieves the target result but my colleague would prefer it be written without the subselects into temp tables t1-t3. The main "challenge" here is transposing the data from dbo.ReviewsData into a single row along with the rest of the data joined from dbo.Prodcucts and dbo.Reviews.
CREATE TABLE dbo.Products (
idProduct int identity,
product_title varchar(100)
PRIMARY KEY (idProduct)
);
INSERT INTO dbo.Products VALUES
(1001, 'poptart'),
(1002, 'coat hanger'),
(1003, 'sunglasses');
CREATE TABLE dbo.Reviews (
Rev_IDReview int identity,
Rev_IDProduct int
PRIMARY KEY (Rev_IDReview)
FOREIGN KEY (Rev_IDProduct) REFERENCES dbo.Products(idProduct)
);
INSERT INTO dbo.Reviews VALUES
(456, 1001),
(457, 1002),
(458, 1003);
CREATE TABLE dbo.ReviewFields (
RF_IDField int identity,
RF_FieldName varchar(32),
PRIMARY KEY (RF_IDField)
);
INSERT INTO dbo.ReviewFields VALUES
(1, 'Customer Name'),
(2, 'Review Title'),
(3, 'Review Message');
CREATE TABLE dbo.ReviewData (
RD_idData int identity,
RD_IDReview int,
RD_IDField int,
RD_FieldContent varchar(100)
PRIMARY KEY (RD_idData)
FOREIGN KEY (RD_IDReview) REFERENCES dbo.Reviews(Rev_IDReview)
);
INSERT INTO dbo.ReviewData VALUES
(79, 456, 1, 'Daniel'),
(80, 456, 2, 'Love this item!'),
(81, 456, 3, 'Works well...blah blah'),
(82, 457, 1, 'Joe!'),
(84, 457, 2, 'Pure Trash'),
(85, 457, 3, 'It was literally a used banana peel'),
(86, 458, 1, 'Karen'),
(87, 458, 2, 'Could be better'),
(88, 458, 3, 'I can always find something wrong');
SELECT P.product_title as "item", t1.ReviewedBy, t2.ReviewTitle, t3.ReviewContent
FROM dbo.Reviews R
INNER JOIN dbo.Products P
ON P.idProduct = R.Rev_IDProduct
INNER JOIN (
SELECT D.RD_FieldContent AS "ReviewedBy", D.RD_IDReview
FROM dbo.ReviewsData D
WHERE D.RD_IDField = 1
) t1
ON t1.RD_IDReview = R.Rev_IDReview
INNER JOIN (
SELECT D.RD_FieldContent AS "ReviewTitle", D.RD_IDReview
FROM dbo.ReviewsData D
WHERE D.RD_IDField = 2
) t2
ON t2.RD_IDReview = R.Rev_IDReview
INNER JOIN (
SELECT D.RD_FieldContent AS "ReviewContent", D.RD_IDReview
FROM dbo.ReviewsData D
WHERE D.RD_IDField = 3
) t3
ON t3.RD_IDReview = R.Rev_IDReview
EDIT: I have updated this post with the create statements for the tables as opposed to an image of the data (shame on me) and a more specific description of what exactly needed to be improved. Thanks to all for the comments and patience.
As others have said in comments, there is nothing objectively wrong with the query. However, you could argue that it's verbose and hard to read.
One way to shorten it is to replace INNER JOIN with CROSS APPLY:
INNER JOIN (
SELECT D.RD_FieldContent AS 'ReviewedBy', D.RD_IDReview
FROM dbo.ReviewsData D
WHERE D.RD_IDField = 1
) t1
ON t1.RD_IDReview = R.Rev_IDReview
APPLY lets you refer to values from the outer query, like in a subquery:
CROSS APPLY (
SELECT D.RD_FieldContent AS 'ReviewedBy'
FROM dbo.ReviewsData D
WHERE D.RD_IDField = 1 AND D.RD_IDReview = R.Rev_IDReview
) t1
I think of APPLY like a subquery that brings in new columns. It's like a cross between a subquery and a join. Benefits:
The query can be shorter, because you don't have to repeat the ID column twice.
You don't have to expose columns that you don't need.
Disadvantages:
If the query in the APPLY references outer values, then you can't extract it and run it all by itself without modifications.
APPLY is specific to Sql Server and it's not that widely-used.
Another thing to consider is using subqueries instead of joins for values that you only need in one place. Benefits:
The queries can be made shorter, because you don't have to repeat the ID column twice, and you don't have to give the output columns unique aliases.
You only have to look in one place to see the whole subquery.
Subqueries can only return 1 row, so you can't accidentally create extra rows, if only 1 row is desired.
SELECT
P.product_title as 'item',
(SELECT D.RD_FieldContent
FROM dbo.ReviewsData D
WHERE D.RD_IDField = 1 AND
D.RD_IDReview = R.Rev_IDReview) as ReviewedBy,
(SELECT D.RD_FieldContent
FROM dbo.ReviewsData D
WHERE D.RD_IDField = 2 AND
D.RD_IDReview = R.Rev_IDReview) as ReviewTitle,
(SELECT D.RD_FieldContent
FROM dbo.ReviewsData D
WHERE D.RD_IDField = 3 AND
D.RD_IDReview = R.Rev_IDReview) as ReviewContent
FROM dbo.Reviews R
INNER JOIN dbo.Products P ON P.idProduct = R.Rev_IDProduct
Edit:
It just occurred to me that you have made the joins themselves unnecessarily verbose (#Dale K actually already pointed this out in the comments):
INNER JOIN (
SELECT D.RD_FieldContent AS 'ReviewedBy', D.RD_IDReview
FROM dbo.ReviewsData D
WHERE D.RD_IDField = 1
) t1
ON t1.RD_IDReview = R.Rev_IDReview
Shorter:
SELECT RevBy.RD_FieldContent AS 'ReviewedBy'
...
INNER JOIN dbo.ReviewsData RevBy
ON RevBy.RD_IDReview = R.Rev_IDReview AND
RevBy.RD_IDField = 1
The originally submitted query is undoubtedly and unnecessarily verbose. Having digested various feedback from the community it has been revised to the following, working splendidly. In retrospect I feel very silly for having done this with subselects originally. I am clearly intermediate at best when it comes to SQL - I had not realized an "AND" clause could be included in the "ON" clause in a "JOIN" statement. Not sure why I would have made such a poor assumption.
SELECT
P.product_title as 'item',
D1.RD_FieldContent as 'ReviewedBy',
D2.RD_FieldContent as 'ReviewTitle',
D3.RD_FieldContent as 'ReviewContent'
FROM dbo.Reviews R
INNER JOIN dbo.Products P
ON P.idProduct = R.Rev_IDProduct
INNER JOIN dbo.ReviewsData D1
ON D1.RD_IDReview = R.Rev_IDReview AND D1.RD_IDField = 1
INNER JOIN dbo.ReviewsData D2
ON D2.RD_IDReview = R.Rev_IDReview AND D2.RD_IDField = 2
INNER JOIN dbo.ReviewsData D3
ON D3.RD_IDReview = R.Rev_IDReview AND D3.RD_IDField = 3
I have a table of locations each of which can have a parent location
LocationId | ParentLocationId
-----------------------------
1 null
2 1
3 2
4 2
I managed to create a recursive CTE which gives me parent location id (plus the original location id) for any given location id
WITH GetLocationParents AS
(
select [LocationId], [ParentLocationId] from Locations
where LocationId = 3
UNION ALL
select i.[LocationId], i.[ParentLocationId]
from Locations i
join GetLocationParents cte on cte.ParentLocationId = i.LocationId
)
SELECT [ParentLocationId] FROM GetLocationParents
WHERE [ParentLocationId] is not NULL;
e.g. where LocationId = 3 would return:
ParentLocationId
----------------
3
2
1
In another table I have a query which will return LocationId as one of the fields:
select exi.PersonId, exi.LocationId from Persons e
left join PersonHasLocations exi on e.PersonId = exi.PersonId
left join Locations i on exi.LocationId = i.LocationId
Which with a where clause would return something like:
PersonId | LocationId
---------------------
100 3
I'm trying to combine these queries to get the result:
PersonId | LocationId
---------------------
100 3
100 2
100 1
I'm trying the following but it's still only returning the first row:
WITH
GetLocationParents AS
(select [LocationId], [ParentLocationId] from Locations
--where LocationId = 3
UNION ALL
select i.[LocationId], i.[ParentLocationId]
from Locations i inner join GetLocationParents cte
on cte.ParentLocationId = i.LocationId),
GetPersons AS
(select exi.PersonId, exi.LocationID from Persons e
left join PersonHasLocations exi on e.PersonID = exi.PersonId
left join Locations i on exi.LocationId = i.LocationID)
SELECT * FROM GetLocationParents gip
INNER JOIN GetPersons ge on ge.LocationId = gip.LocationID
WHERE ge.PersonId = 100
Is it possible to merge a recursive query with a normal query like this?
I guess you have a small bug in your cte. I would suggest to change the query as follows:
DECLARE #t TABLE (
LocationId int,
ParentLocationId int
)
INSERT INTO #t VALUES
(1, NULL)
,(2, 1)
,(3, 2)
,(4, 2)
;WITH GetLocationParents AS
(
select [LocationId] AS k, [LocationId], [ParentLocationId] from #t
UNION ALL
select k, i.[LocationId], i.[ParentLocationId]
from GetLocationParents cte
join #t i on cte.ParentLocationId = i.LocationId
)
SELECT *
FROM GetLocationParents
WHERE k = 3
With this you receive a list with the value you filter on in the first column and all depending "levels" above this in the second column. This can then be used in order to join to your second table.
Keep in mind that - depending on your number of levels - you will have to take care of MAX RECUSRSION.
I have two different databases, the same column.I want to copy from the old database to the new database by comparing the student ID numbers between the tables and having the same number.
For example:
Database name 1: StudentInformation, table name: Student
StudentID Image
---------------------
123 1.png
142 2.png
175 3.png
475 4.png
Database name 2: StudentInformation2, table name: NewStudent
StudentID Image
--------------------------
145 a14.png
196 7.png
175 Null
875 9.png
475 Null
The two tables have common IDs (ID=175 and ID=475)
I want to get this table as a result:
New StudentInformation3, table name: NewSameStudents
StudentID Image
----------------------
175 3.png
475 4.png
You should try using the Merge Statement of SQL Server
Merge Statement Here
Here is one approach:
SELECT s1.StudentID, s1.Image
FROM StudentInformation.[schema].student s1
WHERE EXISTS (SELECT 1
FROM StudentInformation2.[schema].NewStudent s2
WHERE s1.StudentID = s2.StudentID
);
Demo
If you want to populate a new table using the results from the above select, then you may use the INSERT INTO ... SELECT construct:
INSERT INTO StudentInformation3.[schema].NewSameStudents (StudentID, Image)
SELECT s1.StudentID, s1.Image
FROM StudentInformation.[schema].student s1
WHERE EXISTS (SELECT 1
FROM StudentInformation2.[schema].NewStudent s2
WHERE s1.StudentID = s2.StudentID
);
But, I might recommend against this, since you can just create a (non materialized) view using the first query. Also, NewSameStudents is a derived table, and therefore might have to updated frequently, which could be a hassle.
Use inner join and isert into select statement for inserting in new table
insert into NewSameStudents(studentid,image)
select a.studentid,a.image from Student a inner join newstudent n
on a.studentid=n.studentid
where n.image is null
Below is the fully qualified SQL Query where it allows to select data between databases and even between different database servers.
SELECT * FROM [SERVER].[DATABASE].[SCHEMA].[TABLE]
SELECT
a.StudentID,
b.Image
FROM
StudentInformation.Student a
JOIN
StudentInformation2.Student b
ON a.StudentID = b.StudentID
Your 3rd table make no sense if you mean
New StudentInformation3, table name: NewSameStudents
StudentID Image
----------------------
175 3.png
475 4.png
You can use inner join
SELECT StudentID,Image
FROM StudentInformation1
INNER JOIN StudentInformation2
ON StudentInformation1.StudentID = StudentInformation1.StudentID;
try this:
WITH StudentInformation AS (
SELECT 123 AS StudentID, '1.png' AS Image UNION ALL
SELECT 142, '2.png' UNION ALL
SELECT 175, '3.png' UNION ALL
SELECT 475, '4.png'
),
StudentInformation2 AS (
SELECT 145 AS StudentID, 'a14.png' AS Image UNION ALL
SELECT 196, '7.png' UNION ALL
SELECT 175, NULL UNION ALL
SELECT 875, '9.png' UNION ALL
SELECT 475, NULL
)
SELECT s1.StudentID, s1.Image
FROM StudentInformation2 s2 left join
StudentInformation s1 ON s1.StudentID = S2.StudentID
Use Merge From SQL:
Merge into [TABLE_TARGET] as u
USING (Select * from TABLE) as c
on
u.FIELD1 = c.FIELD2 ---JOIN
--------------------
WHEN MATCHED THEN
-------------------
Update SET
FIELD_FROM_TARGET = c.FIELD_FROM_TABLE
----------------------
WHEN NOT MATCHED THEN
----------------------
Insert (
FIELD_FROM_TARGET
) VALUES
(
FIELD_FROM_TABLE
);
Hello I have following SQL query
SELECT K.name AS Name, K.surname AS Surname, U1.akce AS Event,
U2.[text] AS [Scheme], U1.[text] AS [Registered under>],
( U1.x - (
SELECT Count(K.ubytov)
FROM klient
WHERE ubytov = U2.[text]) ) AS [Free space]
FROM klient K
INNER JOIN ubytov U1 ON U1.[text] = K.ubytov
LEFT OUTER JOIN ubytov U2 ON U1.z = U2.id WHERE U1.akce = '140012-02'
ORDER BY U1.[text]
I'm trying to achieve that in column Free space would be (value from ubytov.x that matches U1.z = U2.id) - (total number of rows from table klient that has the same value in U1.[text]=K.ubytov)
In table klient column ubytov I have values that matches ubytov.text and in ubytov.z I have value that matches ubytov.x in different row.
Would somebody help me solve this out please?
Thank you for your time.
An example:
Table klient
ID_K ubytov
1 RoomOwner
2 RoomOwner
table ubytov
id text x z
1 roomType1 2 NULL
2 RoomOwner NULL 1
Desired Output:
Name Surname Event Scheme Registered under: Free space
Nam1 Surname1 Even1 Scheme1 RoomOwnerName 0 // (because 2 counts from klient) - (roomType1 x)
Although, it wasn't much clear because of missing columns.
I tried to build the required query using a CTE expression.
Here is the sqlfiddle code.
Let me know, if this is what you are looking for.
create table klient
(
id_k int,
ubytov varchar(25)
)
go
create table ubytov
(
id int,
text varchar(25),
x int null,
z int null
)
go
insert into klient(id_k, ubytov)
select 1, 'RoomOwner'
union select 2, 'RoomOwner'
go
insert into ubytov(id, text, x, z)
select 1, 'roomType1', 2, null
union select 2, 'RoomOwner', null, 1
go
;WITH cte_klint_counts_by_ubytov
AS
(
SELECT
ubytov,
Count(ubytov) AS ubytovCount
FROM klient
GROUP BY ubytov
)
SELECT
U2.[text] AS [Scheme],
U1.[text] AS [Registered under>],
(isnull(U1.x, 0) - isnull(c.ubytovCount, 0)) AS [Free space]
FROM
klient K
INNER JOIN ubytov U1 ON U1.[text] = K.ubytov
LEFT OUTER JOIN ubytov U2 ON U1.z = U2.id
LEFT OUTER JOIN cte_klint_counts_by_ubytov c ON c.ubytov = U2.[text]
ORDER BY u1.[text]
OK - I have looked and looked and found a lot of examples but nothing quite meeting my need. Maybe I used the wrong words to search with, but I could use your help. I will provide as much detail as I can.
I need to produce a report that merges fields from two tables, or rather a view and a table, into one table for a report. Here is the statement I am using:
SELECT A.ConfInt, A.Conference,
NULL as Ordered,
NULL as Approved,
NULL as PickedUp,
SUM(dbo.Case_Visit_Payments.Qty) AS Qty
FROM dbo.Conferences as A INNER JOIN
dbo.Case_Table ON A.ConfInt = dbo.Case_Table.Conference_ID INNER JOIN
dbo.Case_Visit_Payments ON dbo.Case_Table.Case_ID = dbo.Case_Visit_Payments.Case_ID
WHERE (dbo.Case_Visit_Payments.Item_ID = 15 AND A.ProjectCool = 1)
GROUP BY A.Conference, A.ConfInt
UNION
SELECT B.ConfInt,
B.Conference,
SUM(dbo.Cool_Fan_Order.NumberOfFansRequested) AS Ordered,
SUM(dbo.Cool_Fan_Order.Qty_Fans_Approved) AS Approved,
SUM(dbo.Cool_Fan_Order.Qty_Fans_PickedUp) AS PickedUp,
NULL AS Qty
FROM dbo.Conferences as B LEFT OUTER JOIN
dbo.Cool_Fan_Order ON B.ConfInt = dbo.Cool_Fan_Order.Conference_ID
where B.ProjectCool = 1
GROUP BY B.Conference, B.ConfInt
And here are the results:
4 Our Lady NULL NULL NULL 11
4 Our Lady 40 40 40 NULL
7 Holy Rosary 20 20 20 NULL
11 Little Flower NULL NULL NULL 21
11 Little Flower 5 5 20 NULL
19 Perpetual Help NULL NULL NULL 2
19 Perpetual Help 20 20 20 NULL
What I would strongly prefer is to not have the duplicating rows, such as:
4 Our Lady 40 40 40 11
7 Holy Rosary 20 20 20 NULL
11 Little Flower 5 5 20 21
19 Perpetual Help 20 20 20 2
I hope this question was clear enough. Any Suggestions would be greatly appreciated. And I do mark as answered. :)
Gregory
you could use your actual query as a subQuery, use an aggregate function (MAX OR SUM) on your non-duplicated values and group by the non aggregated columns
SELECT ConfInt, Conference, MAX(Ordered), MAX(Approved), MAX(PickedUp), MAX(Qty)
FROM (<your actualQuery>)
GROUP BY ConfInt, Conference.
The quick answer is to wrap your query inside another one,
SELECT ConfInt
, Conference
, SUM(Ordered) AS Ordered
, SUM(Approved) As Approved
, SUM(PickedUp) AS PickedUp
, SUM(Qty) AS Qty
FROM (
<your UNION query here>
)
GROUP BY ConfInt, Conference
This is not the only way to achieve the result set, but its the quickest fix to meet the specified requirements.
As an alternative, I believe these queries will return equivalent results:
We could use a correlated subquery in the SELECT list to get Qty:
;WITH q AS
( SELECT B.ConfInt
, B.Conference
, SUM(o.NumberOfFansRequested) AS Ordered
, SUM(o.Qty_Fans_Approved) AS Approved
, SUM(o.Qty_Fans_PickedUp) AS PickedUp
FROM dbo.Conferences as B
LEFT
JOIN dbo.Cool_Fan_Order o ON o.Conference_ID = B.ConfInt
WHERE B.ProjectCool = 1
GROUP BY B.ConfInt, B.Conference
)
SELECT q.ConfInt
, q.Conference
, q.Ordered
, q.Approved
, q.PickedUp
, ( SELECT SUM(v.Qty)
FROM dbo.Case_Table t
JOIN dbo.Case_Visit_Payments v ON v.Case_ID = t.Case_ID
WHERE t.Conference_ID = q.ConfInt
AND v.Item_ID = 15
) AS Qty
FROM q
ORDER BY q.ConfInt, q.Conference
Or, we could use LEFT JOIN operation on the two queries, rather than UNION. (We know that the query referencing Cool_Fan_Order can be the LEFT side of the outer join, because we know that it returns at least as many rows as the other query. (Basically, we know that the other query can't return values of ConfInt and Conference that aren't in the Cool_Fan_Order query.)
;WITH p AS
( SELECT A.ConfInt
, A.Conference
, SUM(v.Qty) AS Qty
FROM dbo.Conferences as A
JOIN dbo.Case_Table t ON t.Conference_ID = A.ConfInt
JOIN dbo.Case_Visit_Payments v ON v.Case_ID = t.Case_ID
WHERE A.ProjectCool = 1
AND v.Item_ID = 15
GROUP BY A.ConfInt, A.Conference
)
, q AS
( SELECT B.ConfInt
, B.Conference
, SUM(o.NumberOfFansRequested) AS Ordered
, SUM(o.Qty_Fans_Approved) AS Approved
, SUM(o.Qty_Fans_PickedUp) AS PickedUp
FROM dbo.Conferences as B
LEFT
JOIN dbo.Cool_Fan_Order o ON B.ConfInt = o.Conference_ID
WHERE B.ProjectCool = 1
GROUP BY B.ConfInt, B.Conference
)
SELECT q.ConfInt
, q.Conference
, q.Ordered
, q.Approved
, q.PickedUp
, p.Qty
FROM q
LEFT
JOIN p ON p.ConfInt = q.ConfInt AND p.Conference = q.Conference
ORDER BY q.ConfInt, q.Conference
The choice between those three (they all return an equivalent resultset under all conditons), boils down to readability and maintainability, and performance. On large enough rowsets, there may be some observable performance differences between the three statements.
I'd just put the join to Cool_Fan_Order in the first select and remove the union.
Does this return the same results?
select
A.ConfInt,
A.Conference,
sum(dbo.Cool_Fan_Order.NumberOfFansRequested) as Ordered,
sum(dbo.Cool_Fan_Order.Qty_Fans_Approved) as Approved,
sum(dbo.Cool_Fan_Order.Qty_Fans_PickedUp) as PickedUp,
sum(sub.Qty) as Qty
from
dbo.Conferences as A
left outer join
(
select
c.ConfInt,
cvp.Qty
from dbo.Conferences c
inner join dbo.Case_Table ct
on a.confInt=ct.Conference_ID
inner join dbo.Case_Visit_Payments cvp
on ct.Case_ID=cvp.Case_ID
where cvp.Item_ID=15
) sub
on a.ConfInt=sub.ConfInt
left outer join dbo.Cool_Fan_Order
on A.ConfInt = dbo.Cool_Fan_Order.Conference_ID
where
(
A.ProjectCool = 1
)
group by
A.Conference,
A.ConfInt