Can't get the SUM function to work with my query - sql

I'm trying to SUM some warehouse stock values, grouped by zones, but can't get it to work.
The following query returns all materials on stock. Same material is placed on more locations, in more zones. So material AAA can be located on two locations in zone 111, and three locations in zone 222
SELECT Zones.Label,
Materials.Material,
Shelves.Label as ShelfLabel,
Items.StockQty
FROM Zones INNER JOIN
(dbo.LangTexts(null) AS Texts INNER JOIN (WarehouseStatus INNER JOIN
(Shelves INNER JOIN (Positions INNER JOIN
(Materials INNER JOIN Items
ON Materials.Material = Items.Material)
ON Positions.ID = Items.Owner)
ON Shelves.ID = Positions.Owner)
ON WarehouseStatus.Status = Items.Status)
ON Texts.TextID = WarehouseStatus.Text)
ON Zones.ID = Shelves.Owner
WHERE Zones.Label IS NOT NULL
ORDER BY Materials.Material, Zones.Label
Output:
output screenshot
As I have tried to markup in the screenshot, I need the total StockQty grouped by zone (label column)
I'm trying to achieve that by modifying the query like this.
SELECT Zones.Label,
Materials.Material,
Shelves.Label as ShelfLabel,
SUM(Items.StockQty)
FROM Zones INNER JOIN
(dbo.LangTexts(null) AS Texts INNER JOIN (WarehouseStatus INNER JOIN
(Shelves INNER JOIN (Positions INNER JOIN
(Materials INNER JOIN Items
ON Materials.Material = Items.Material)
ON Positions.ID = Items.Owner)
ON Shelves.ID = Positions.Owner)
ON WarehouseStatus.Status = Items.Status)
ON Texts.TextID = WarehouseStatus.Text)
ON Zones.ID = Shelves.Owner
WHERE Zones.Label IS NOT NULL
GROUP BY Zones.Label
ORDER BY Materials.Material, Zones.Label
But it keeps returning the following error.
Msg 8120, Level 16, State 1, Line 6
Column 'Materials.Material' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I have been dealing with such error before, but don't understand exactly what I'm doing wrong. Sometimes I succeed by changing the query back and forth, but this time I really stuck.
Hope that someone is possible to guide me.
Thank's in advance.
As the column Shelves.Label actually not bring any value in this matter, I ended up with the following query based on #Md.SumanKabir's answer.
SELECT Zones.Label,
Materials.Material,
SUM(Items.StockQty)
FROM Zones INNER JOIN
(dbo.LangTexts(null) AS Texts INNER JOIN (WarehouseStatus INNER JOIN
(Shelves INNER JOIN (Positions INNER JOIN
(Materials INNER JOIN Items
ON Materials.Material = Items.Material)
ON Positions.ID = Items.Owner)
ON Shelves.ID = Positions.Owner)
ON WarehouseStatus.Status = Items.Status)
ON Texts.TextID = WarehouseStatus.Text)
ON Zones.ID = Shelves.Owner
WHERE Zones.Label IS NOT NULL
GROUP BY Zones.Label, Materials.Material
ORDER BY Materials.Material, Zones.Label

The correct query would be :
SELECT Zones.Label,
Materials.Material,
Shelves.Label as ShelfLabel,
SUM(Items.StockQty)
FROM Zones INNER JOIN
(dbo.LangTexts(null) AS Texts INNER JOIN (WarehouseStatus INNER JOIN
(Shelves INNER JOIN (Positions INNER JOIN
(Materials INNER JOIN Items
ON Materials.Material = Items.Material)
ON Positions.ID = Items.Owner)
ON Shelves.ID = Positions.Owner)
ON WarehouseStatus.Status = Items.Status)
ON Texts.TextID = WarehouseStatus.Text)
ON Zones.ID = Shelves.Owner
WHERE Zones.Label IS NOT NULL
GROUP BY Zones.Label, Materials.Material, Shelves.Label
ORDER BY Materials.Material, Zones.Label
You need to use the columns in GROUP BY which are not used in any aggregate functions like SUM
You can get more information on how to use GROUP BY Here

The problem here is that your grouping does not match your select. You select 3 columns Zones.Label, Materials.Material and Shelves.Label, but you group only by Zones.Label. Therefore, it does not know what to do with the 2 other columns.
Either you have to aggregate them like you did with the sum or you have to add them to the grouping.

Related

Access Subquery On mulitple conditions

This SQL query needs to be done in ACCESS.
I am trying to do a subquery on the total sales, but I want to link the sale to the province AND to product. The below query will work with one or the other: (po.product_name = allp.all_products) AND (p.province = allp.all_province); -- but it will no take both.
I will be including every month into this query, once I can figure out the subquery on with two criteria.
Select
p.province as [Province],
po.product_name as [Product],
all_price
FROM
(purchase_order po
INNER JOIN person p
on p.person_id = po.person_id)
left join
(
select
po1.product_name AS [all_products],
sum(pp1.price) AS [all_price],
p1.province AS [all_province]
from (purchase_order po1
INNER JOIN product pp1
on po1.product_name = pp1.product_name)
INNER JOIN person p1
on po1.person_id = p1.person_id
group by po1.product_name, pp1.price, p1.province
)
as allp
on (po.product_name = allp.all_products) AND (p.province = allp.all_province);
Make the first select sql into a table by giving it an alias and join table 1 to table 2. I don't have your table structure or data to test it but I think this will lead you down the right path:
select table1.*, table2.*
from
(Select
p.province as [Province],
po.product_name as [Product]
--removed this ,all_price
FROM
(purchase_order po
INNER JOIN person p
on p.person_id = po.person_id) table1
left join
(
select
po1.product_name AS [all_products],
sum(pp1.price) AS [all_price],
p1.province AS [all_province]
from (purchase_order po1
INNER JOIN product pp1
on po1.product_name = pp1.product_name)
INNER JOIN person p1
on po1.person_id = p1.person_id
group by po1.product_name, pp1.price, p1.province --check your group by, I dont think you want pp1.price here if you want to aggregate
) as table2 --changed from allp
on (table1.product = table2.all_products) AND (table1.province = table2.all_province);

How do I get the max record from a inner joined field, SQL?

I need to pull the MAX seendate from a subquery it looks like and I can do it for a specific Item but as soon as I take away the WHERE clause for a specific Item, it does not display anything see below:
SELECT Item.Barcode,
Hetype.Description,
StockTakeDetails.SeenDate,
Users.UserName,
defcon.Defcon_text,
JOBS.Job_Ref,
JOBS."Due Back"
FROM Item
INNER JOIN Hetype ON Item.Type = Hetype.Type
INNER JOIN StockTakeDetails ON Item.Itemref = StockTakeDetails.xItemID
INNER JOIN Users ON Users.UID = StockTakeDetails.xUserSeen
INNER JOIN EQLISTS ON Item.CurrentJob = EQLISTS.Eql_no
INNER JOIN JOBS ON EQLISTS.Job_no = JOBS.JobNo
INNER JOIN defcon ON JOBS.Status = defcon.Defcon_idx
WHERE JOBS."Due Back" >= TIMESTAMP '2019-01-01 00:00:00' AND
JOBS.Status <> 5 AND
Item.Barcode = '038223' AND
StockTakeDetails.IDX = (SELECT Max(StockTakeDetails.IDX)
FROM StockTakeDetails INNER JOIN Item ON StockTakeDetails.xItemID =
Item.Itemref WHERE Item.Barcode = '038223')
This works fine, how ever if I take out the Barcode clause it does not show any data:
SELECT Item.Barcode,
Hetype.Description,
StockTakeDetails.SeenDate,
Users.UserName,
defcon.Defcon_text,
JOBS.Job_Ref,
JOBS."Due Back"
FROM Item
INNER JOIN Hetype ON Item.Type = Hetype.Type
INNER JOIN StockTakeDetails ON Item.Itemref = StockTakeDetails.xItemID
INNER JOIN Users ON Users.UID = StockTakeDetails.xUserSeen
INNER JOIN EQLISTS ON Item.CurrentJob = EQLISTS.Eql_no
INNER JOIN JOBS ON EQLISTS.Job_no = JOBS.JobNo
INNER JOIN defcon ON JOBS.Status = defcon.Defcon_idx
WHERE JOBS."Due Back" >= TIMESTAMP '2019-01-01 00:00:00' AND
JOBS.Status <> 5 AND
StockTakeDetails.IDX = (SELECT Max(StockTakeDetails.IDX)
FROM StockTakeDetails INNER JOIN Item ON StockTakeDetails.xItemID =
Item.Itemref)
please let me know how else I can do this.
Thank you
This ended up working for what I needed Thank you all, all answers helped me get here.
SELECT i1.Barcode,
Max(StockTakeDetails.SeenDate),
Hetype.Description
FROM Item i1
INNER JOIN Hetype ON i1.Type = Hetype.Type
LEFT JOIN StockTakeDetails ON i1.Itemref = StockTakeDetails.xItemID
INNER JOIN Users ON Users.UID = StockTakeDetails.xUserSeen
INNER JOIN EQLISTS ON i1.CurrentJob = EQLISTS.Eql_no
INNER JOIN JOBS ON EQLISTS.Job_no = JOBS.JobNo
INNER JOIN defcon ON JOBS.Status = defcon.Defcon_idx
WHERE JOBS."Due Back" >= TIMESTAMP '2019-01-01 00:00:00' AND
JOBS.Status <> 5
GROUP BY i1.Barcode,
JOBS."Due Back",
JOBS.Status,
Hetype.Description
Do you just need and max StockDetails seendate?
SELECT
Item.Barcode
,max(StockTakeDetails.IDX) as max_idx
FROM StockTakeDetails
INNER JOIN Item
ON StockTakeDetails.xItemID = Item.Itemref
GROUP BY Item.Barcode
Instead of using this query as a single-value filter in your WHERE, you could join this subquery in on both Barcode and max_idx.
Alternatively, you could modify you're major query to have it group by whatever the relevant unique fields are, and take the max(StockTakeDetails.SeenDate)
Think you need GROUP BY Item.Barcode at the end of the subquery, where you replaced the specific item code. When you took it out, you're passing a single value back for the entire table, instead of one max detail per barcode.
Most probably you need to constrain Barcode in the subquery. Note aliases, always use table aliases to avoid confusion
SELECT i1.Barcode,
Hetype.Description,
StockTakeDetails.SeenDate,
Users.UserName,
defcon.Defcon_text,
JOBS.Job_Ref,
JOBS."Due Back"
FROM Item i1
INNER JOIN Hetype ON i1.Type = Hetype.Type
INNER JOIN StockTakeDetails ON i1.Itemref = StockTakeDetails.xItemID
INNER JOIN Users ON Users.UID = StockTakeDetails.xUserSeen
INNER JOIN EQLISTS ON i1.CurrentJob = EQLISTS.Eql_no
INNER JOIN JOBS ON EQLISTS.Job_no = JOBS.JobNo
INNER JOIN defcon ON JOBS.Status = defcon.Defcon_idx
WHERE JOBS."Due Back" >= TIMESTAMP '2019-01-01 00:00:00' AND
JOBS.Status <> 5 AND
StockTakeDetails.IDX = (SELECT Max(s.IDX)
FROM StockTakeDetails s
INNER JOIN Item i2 ON s.xItemID = i2.Itemref
WHERE i2.Barcode = i1.Barcode)

Possible to write this query without aggregate function?

SELECT l.LocID, COUNT(ulr.UserID)
FROM Locations l
LEFT OUTER JOIN UserLocationRights ulr ON l.LocID = ulr.LocID
LEFT OUTER JOIN Devices d ON l.LocID = d.LocID
LEFT OUTER JOIN UserModelRights umr ON d.ModelName = umr.ModelName
AND ulr.UserID = umr.UserID
GROUP BY l.LocID, ulr.UserID, d.ModelName
ORDER BY l.LocID, ulr.UserID
I want the results to be the LocID of all of the entries in Locations and the second column to be the number of Users who have rights to this location that also have the rights to at least one of the Devices in the Location which is determined by the entries in UserModelRights.
I can only figure out how to get what I want like this:
SELECT l.LocID, IsNull(UserHasModelRightInLoc.UserCount, 0) UserCount
FROM Locations l
LEFT OUTER JOIN (
SELECT ulr.LocID, COUNT(UserModelRightsPerLocation.UserID) UserCount
FROM UserLocRights ulr
INNER JOIN (
SELECT l.LocID, umr.UserID
FROM UserModelRights umr
INNER JOIN Devices d ON umr.ModelName = d.ModelName
INNER JOIN Locations l ON d.LocID = l.LocID
GROUP BY umr.ModelName, umr.UserID, l.LocID
) UserModelRightsPerLocation ON ulr.LocID = UserModelRightsPerLocation.LocID
AND ulr.UserID = UserModelRightsPerLocation.UserID
GROUP BY ulr.LocID
) UserHasModelRightInLoc ON l.LocID = UserHasModelRightInLoc.LocID
ORDER BY l.LocID
I don't know if this is possible, but I assume that there could be a way to use GROUP BY to get what I want using my first, much smaller query. The issue is that I think I need to do multiple GROUP BY in a specific order and I don't know if that's possible or makes sense in SQL.
Is there a way to get the result I want without using an aggregate function? If not, maybe narrow it down to a single one?
I find this a bit hard to follow without sample data. But based on your description, this might do what you want:
SELECT l.LocID, COUNT(DISTINCT ulr.UserID)
FROM Locations l LEFT JOIN
UserLocationRights ulr
ON l.LocID = ulr.LocID LEFT JOIN
Devices d
ON l.LocID = d.LocID LEFT JOIN
UserModelRights umr
ON d.ModelName = umr.ModelName AND
ulr.UserID = umr.UserID
GROUP BY l.LocID
ORDER BY l.LocID;

Left Join not working

I am a SQL Server beginner and so I have been using MS Access to create queries and then have been amending them for SQL Server.
I have created a query which includes a left join, which works perfectly in Access but when I copy the SQL code across to SQL Server it does not show the null values and I cannot work out why.
I want to show all of the attribute values whether they include a value or not. Can anyone help? The code is as follows:
SELECT DISTINCT
tbLease.LeaseTitle
, tbAttributeValue.AttributeTemplateDefinitionLinkID
, tbAttributeValue.Value
FROM
((((tbBusinessUnit
LEFT JOIN
tbAttributeValue ON tbBusinessUnit.BusinessUnitID = tbAttributeValue.ParentID)
INNER JOIN
tbBuildingLinkBusinessUnit ON tbBusinessUnit.BusinessUnitID = tbBuildingLinkBusinessUnit.BusinessUnitID)
INNER JOIN
tbBuilding ON tbBuildingLinkBusinessUnit.BuildingID = tbBuilding.BuildingID)
INNER JOIN
(tbUnitLocation
INNER JOIN
tbUnit ON tbUnitLocation.UnitID = tbUnit.UnitID)
ON tbBuilding.BuildingID = tbUnitLocation.LocationID)
INNER JOIN
tbLease ON tbUnit.UnitID = tbLease.UnitID
WHERE
(((tbAttributeValue.AttributeTemplateDefinitionLinkID) = 30
Or (tbAttributeValue.AttributeTemplateDefinitionLinkID) = 31
Or (tbAttributeValue.AttributeTemplateDefinitionLinkID) = 32));
Hard to say what you want without some examples but you might want this:
WHERE coalesce(tbAttributeValue.AttributeTemplateDefinitionLinkID,30) in (30,31,32);
This will give you items where AttributeTemplateDefinitionLinkID is null
(that is it did not join on the left join) OR is one of those 3 values.
Right now if you don't join with the left join it will not display that row because of the where condition, so your left join is the same as an inner join.
The NULL values you would normally see with a left join are getting filtered out by your WHERE clause.
Any time you add filters to a WHERE clause that apply to an outer joined table, it will effectively make it the same as an inner join unless you include NULL values as an option in your where clause as well.
SELECT DISTINCT
tbLease.LeaseTitle,
tbAttributeValue.AttributeTemplateDefinitionLinkID,
tbAttributeValue.Value
FROM
tbBusinessUnit
LEFT JOIN tbAttributeValue ON tbBusinessUnit.BusinessUnitID = tbAttributeValue.ParentID
INNER JOIN tbBuildingLinkBusinessUnit ON tbBusinessUnit.BusinessUnitID = tbBuildingLinkBusinessUnit.BusinessUnitID
INNER JOIN tbBuilding ON tbBuildingLinkBusinessUnit.BuildingID = tbBuilding.BuildingID
INNER JOIN tbUnitLocation ON tbBuilding.BuildingID = tbUnitLocation.LocationID
INNER JOIN tbUnit ON tbUnitLocation.UnitID = tbUnit.UnitID
INNER JOIN tbLease ON tbUnit.UnitID = tbLease.UnitID
WHERE
tbAttributeValue.AttributeTemplateDefinitionLinkID in (30, 31, 32)
or tbAttributeValue.AttributeTemplateDefinitionLinkID is null
Move the right table filters from where clause to ON condition when you are using Left Outer Join else Left join will be implicitly converted to INNER JOIN. Try this
SELECT DISTINCT tblease.leasetitle,
tbattributevalue.attributetemplatedefinitionlinkid,
tbattributevalue.value
FROM tbbusinessunit
LEFT JOIN tbattributevalue
ON tbbusinessunit.businessunitid = tbattributevalue.parentid
AND ( tbattributevalue.attributetemplatedefinitionlinkid IN
( 30, 31, 32 )
OR tbattributevalue.attributetemplatedefinitionlinkid IS
NULL )
INNER JOIN tbbuildinglinkbusinessunit
ON tbbusinessunit.businessunitid =
tbbuildinglinkbusinessunit.businessunitid
INNER JOIN tbbuilding
ON tbbuildinglinkbusinessunit.buildingid = tbbuilding.buildingid
INNER JOIN tbunitlocation
ON tbbuilding.buildingid = tbunitlocation.locationid
INNER JOIN tbunit
ON tbunitlocation.unitid = tbunit.unitid
INNER JOIN tblease
ON tbunit.unitid = tblease.unitid

SQL Error: ORA-01489: result of string concatenation is too long

I am working on the query below:
select ip.intake_id,
ip.estimated_years,
ip.gender_code,
LISTAGG(ip.race_code, ',') WITHIN GROUP (ORDER BY ip.race_code) as race_code,
eth.ethnicity_code,
i.living_arrangements,
p.dep_actv_military_flag,
LISTAGG(ale.allegation_super_type_code) WITHIN GROUP (ORDER BY ale.allegation_super_type_code) as maltreatment_type_code,
LISTAGG(ale.initial_report_disp_code) WITHIN GROUP (ORDER BY ale.initial_report_disp_code) as maltreatment_dispo_lvl,
ip.deceased_flag,
LISTAGG(ch.characteristic_code, ',') WITHIN GROUP (ORDER BY ch.characteristic_code) as chara_codes,
LISTAGG(ich.intake_characteristic_code, ',') WITHIN GROUP (ORDER BY ich.intake_characteristic_code) as intake_chara_codes,
pe.removed_date,cm.petition_submitted_flag,cm.created_date,atr.person_id
from intake i inner join intake_participant ip on i.intake_id = ip.intake_id
left outer join reporter r ON i.intake_id=r.intake_id
left outer join ethnicity eth on eth.person_id = ip.person_id
left outer join person p on p.person_id = ip.person_id
left outer join allegation ale on ale.intake_id = i.intake_id
left outer join characteristic ch on ch.person_id = ip.person_id
left outer join intake_characteristic ich on ich.intake_id = i.intake_id
left outer join placement_episode pe on pe.child_id = ip.person_id
left outer join complaint cm on cm.petitioner_id = ip.person_id
left outer join attorney atr on atr.person_id = ip.person_id
left outer join intake_participant_role apr on apr.intake_participant_id = ip.intake_participant_id
group by ip.intake_id,ip.estimated_years,ip.gender_code,eth.ethnicity_code,i.living_arrangements,p.dep_actv_military_flag,
ip.deceased_flag,pe.removed_date,cm.petition_submitted_flag,cm.created_date,atr.person_id
when I am running this query I am getting following error message:
Error report:
SQL Error: ORA-01489: result of string concatenation is too long
01489. 00000 - "result of string concatenation is too long"
*Cause: String concatenation result is more than the maximum size.
*Action: Make sure that the result is less than the maximum size.
But when I remove the line :
left outer join reporter r ON i.intake_id=r.intake_id
from my query then it executes without any error message. The working query is given below:
select ip.intake_id,
ip.estimated_years,
ip.gender_code,
LISTAGG(ip.race_code, ',') WITHIN GROUP (ORDER BY ip.race_code) as race_code,
eth.ethnicity_code,
i.living_arrangements,
p.dep_actv_military_flag,
LISTAGG(ale.allegation_super_type_code) WITHIN GROUP (ORDER BY ale.allegation_super_type_code) as maltreatment_type_code,
LISTAGG(ale.initial_report_disp_code) WITHIN GROUP (ORDER BY ale.initial_report_disp_code) as maltreatment_dispo_lvl,
ip.deceased_flag,
LISTAGG(ch.characteristic_code, ',') WITHIN GROUP (ORDER BY ch.characteristic_code) as chara_codes,
LISTAGG(ich.intake_characteristic_code, ',') WITHIN GROUP (ORDER BY ich.intake_characteristic_code) as intake_chara_codes,
pe.removed_date,cm.petition_submitted_flag,cm.created_date,atr.person_id
from intake i inner join intake_participant ip on i.intake_id = ip.intake_id
left outer join ethnicity eth on eth.person_id = ip.person_id
left outer join person p on p.person_id = ip.person_id
left outer join allegation ale on ale.intake_id = i.intake_id
left outer join characteristic ch on ch.person_id = ip.person_id
left outer join intake_characteristic ich on ich.intake_id = i.intake_id
left outer join placement_episode pe on pe.child_id = ip.person_id
left outer join complaint cm on cm.petitioner_id = ip.person_id
left outer join attorney atr on atr.person_id = ip.person_id
left outer join intake_participant_role apr on ipr.intake_participant_id = ip.intake_participant_id
group by ip.intake_id,ip.estimated_years,ip.gender_code,eth.ethnicity_code,i.living_arrangements,p.dep_actv_military_flag,
ip.deceased_flag,pe.removed_date,cm.petition_submitted_flag,cm.created_date,atr.person_id
I am not sure why this error occured. Can someone help me to figure out the problem? I got same questions from these links link1 and link2, but I didn't get the solution to my question from these links.
The join to table "reporter" is probably increasing the record count (this could only be the case if column "intake_id" is not a unique key of "reporter"). By increasing the record count, you are generating more strings that LISTAGG must concatenate together within each group. If the total length of concatenated strings exceeds 4000 bytes, LISTAGG will fail with the error you see.
Let's take a different approach: just like when you're aggregating sums and joining to other tables, sometimes you have to materialize the results before you additional one-to-many join so your aggregation isn't artificially inflated due to multiple records in joining tables. This approach is assuming you don't need the repetition that is occurring due to the additional table joins however.
I think by creating the inline view the results of the race_Code will fit within size limits. This could be done using a CTE as well. Simply put, provided you don't need the record duplication, you may need to materialize the listAgg results individually first then join them back in. If you have other problems with the other listAggs, you may want to create multiple CTE's and then join them all back together in the end. This approach simply uses a inline view.
select B.intake_id,
B.estimated_years,
B.gender_code,
B.race_code,
eth.ethnicity_code,
B.living_arrangements,
p.dep_actv_military_flag,
LISTAGG(ale.allegation_super_type_code) WITHIN GROUP (ORDER BY ale.allegation_super_type_code) as maltreatment_type_code,
LISTAGG(ale.initial_report_disp_code) WITHIN GROUP (ORDER BY ale.initial_report_disp_code) as maltreatment_dispo_lvl,
B.deceased_flag,
LISTAGG(ch.characteristic_code, ',') WITHIN GROUP (ORDER BY ch.characteristic_code) as chara_codes,
LISTAGG(ich.intake_characteristic_code, ',') WITHIN GROUP (ORDER BY ich.intake_characteristic_code) as intake_chara_codes,
pe.removed_date,cm.petition_submitted_flag,cm.created_date,atr.person_id
from (SELECT ip.intake_id,
ip.estimated_years,
ip.gender_code,
ip.deceased_flag,
ip.person_id,
ip.intake_participant_id,
LISTAGG(ip.race_code, ',') WITHIN GROUP (ORDER BY ip.race_code) as race_code, i.living_arrangements
FROM intake i
INNER JOIN intake_participant ip on i.intake_id = ip.intake_id
GROUP BY ip.intake_id, ip.estimated_years, ip.gender_code,
i.living_arrangements, ip.deceased_flag, ip.person_id,
ip.intake_participant_id) B
left outer join ethnicity eth on eth.person_id = B.person_id
left outer join person p on p.person_id = B.person_id
left outer join allegation ale on ale.intake_id = B.intake_id
left outer join characteristic ch on ch.person_id = B.person_id
left outer join intake_characteristic ich on ich.intake_id = i.intake_id
left outer join placement_episode pe on pe.child_id = B.person_id
left outer join complaint cm on cm.petitioner_id = B.person_id
left outer join attorney atr on atr.person_id = B.person_id
left outer join intake_participant_role apr on ipr.intake_participant_id = B.intake_participant_id
GROUP BY ip.intake_id,ip.estimated_years,ip.gender_code, eth.ethnicity_code,i.living_arrangements,p.dep_actv_military_flag, ip.deceased_flag,pe.removed_date,cm.petition_submitted_flag, cm.created_date,atr.person_id