How to get distinct id in subquery from fact table - sql

Trying to return all the fields in my sub query minus the duplicated childid's in the fact table cor.score. I don't want the duplicate id's to inflate my count. Every id needs to be counted once.
select distinct cs.childid
from
(select s.sitename, c.primarylanguage, count(Primarylanguage) as 'Count'
from cor.scores cs
left join cor.sites s on s.id = cs.siteid
left join cor.children c on c.id = cs.childid
group by s.sitename, c.primarylanguage)
Error:
Msg 102, Level 15, State 1, Line 7 Incorrect syntax near ')'.
What's the best way to go about this?

select distinct cs_childid
from
(
select s.sitename,cs.childid as cs_childid, c.primarylanguage, count(Primarylanguage) as 'Count'
from cor.scores cs
left join cor.sites s on s.id = cs.siteid
left join cor.children c on c.id = cs.childid
group by s.sitename, c.primarylanguage,cs.childid
)as T

Do you want this:
1. can return any column of subquery
select s.childid,s.[Count]
FROM (
SELECT s.sitename, c.primarylanguage,cs.childid, count(Primarylanguage)OVER(PARTITION BY s.sitename) as 'Count'
FROM cor.scores cs
LEFT join cor.sites s on s.id = cs.siteid
) AS s LEFT join cor.children c on c.id = s.childid
Or this:
2. Only can return group column and aggregate columns
select s.childid,s.[Count]
FROM (
SELECT s.sitename,cs.childid, count(Primarylanguage)OVER(PARTITION BY s.sitename) as 'Count'
FROM cor.scores cs
LEFT join cor.sites s on s.id = cs.siteid
GROUP BY s.sitename,cs.childid
) AS s LEFT join cor.children c on c.id = s.childid

Related

How to create distinct count from queries with several tables

I am trying to create one single query that will give me a distinct count for both the ActivityID and the CommentID. My query in MS Access looks like this:
SELECT
tbl_Category.Category, Count(tbl_Activity.ActivityID) AS CountOfActivityID,
Count(tbl_Comments.CommentID) AS CountOfCommentID
FROM tbl_Category LEFT JOIN
(tbl_Activity LEFT JOIN tbl_Comments ON
tbl_Activity.ActivityID = tbl_Comments.ActivityID) ON
tbl_Category.CategoryID = tbl_Activity.CategoryID
WHERE
(((tbl_Activity.UnitID)=5) AND ((tbl_Comments.PeriodID)=1))
GROUP BY
tbl_Category.Category;
I know the answer must somehow include SELECT DISTINCT but am not able to get it to work. Do I need to create multiple subqueries?
This is really painful in MS Access. I think the following does what you want to do:
SELECT ac.Category, ac.num_activities, aco.num_comments
FROM (SELECT ca.category, COUNT(*) as num_activities
FROM (SELECT DISTINCT c.Category, a.ActivityID
FROM (tbl_Category as c INNER JOIN
tbl_Activity as a
ON c.CategoryID = a.CategoryID
) INNER JOIN
tbl_Comments as co
ON a.ActivityID = co.ActivityID
WHERE a.UnitID = 5 AND co.PeriodID = 1
) as caa
GROUP BY ca.category
) as ca LEFT JOIN
(SELECT c.Category, COUNT(*) as num_comments
FROM (SELECT DISTINCT c.Category, co.CommentId
FROM (tbl_Category as c INNER JOIN
tbl_Activity as a
ON c.CategoryID = a.CategoryID
) INNER JOIN
tbl_Comments as co
ON a.ActivityID = co.ActivityID
WHERE a.UnitID = 5 AND co.PeriodID = 1
) as aco
GROUP BY c.Category
) as aco
ON aco.CommentId = ac.CommentId
Note that your LEFT JOINs are superfluous because the WHERE clause turns them into INNER JOINs. This adjusts the logic for that purpose. The filtering is also very tricky, because it uses both tables, requiring that both subqueries have both JOINs.
You can use DISTINCT:
SELECT
tbl_Category.Category, Count(DISTINCT tbl_Activity.ActivityID) AS CountOfActivityID,
Count(DISTINCT tbl_Comments.CommentID) AS CountOfCommentID
FROM tbl_Category LEFT JOIN
(tbl_Activity LEFT JOIN tbl_Comments ON
tbl_Activity.ActivityID = tbl_Comments.ActivityID) ON
tbl_Category.CategoryID = tbl_Activity.CategoryID
WHERE
(((tbl_Activity.UnitID)=5) AND ((tbl_Comments.PeriodID)=1))
GROUP BY
tbl_Category.Category;

SQL Left Join Syntax Error

Can someone please help resolve an issue, I am left joining two CTE's?
Msg 102, Level 15, State 1, Line 19
Incorrect syntax near '('.
Msg 156, Level 15, State 1, Line 26
Incorrect syntax near the keyword 'left'.
Query:
with Cte_LatestPatInfo as
(select max(a.LogID) as LastPatRec, p.unitnum, p.PatNum, a.iplan from rrscsql2.arhighdollar.dbo.tblarinfoHistory a
inner join rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
on a.patID=p.patID and a.LogID=p.LogID
where a.Active = 1
and a.ReasonCode is not null
group by p.unitnum, p.PatNum, a.iplan)
select *,rc.Description from rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
inner join cte_LatestPatInfo li
on p.PatNum=li.PatNum and p.UnitNum=li.UnitNum and p.LogID = li.LastPatRec
inner join rrscsql2.arhighdollar.dbo.tblarinfoHistory ah
on ah.LogID=p.LogID and ah.iplan = li.iplan and p.PatID=ah.PatID
left join rrscsql2.ARHighDollar.dbo.tblReasonCodes rc
on ah.ReasonCode=rc.ReasonCode,
cte_EOMDenials as
(select d.* from rrscsql2.Denials.dbo.tblDenialMonthEnd d
Inner join rrscsql3.Facilities.dbo.vwFacilities f
on d.UnitNum = f.UnitNum and f.Owner like '%LifePoint%'
Inner join rrscsql2.Denials.dbo.tblDispositionDictionary t
on d.disposition=t.disposition and DispositionType like'O%'
Where datediff(mm, monthending, GETDATE()) = 1
and DATEDIFF(mm,DischDate,monthending)>2)
left join cte_EOMDenials d
on p.unitnum=d.unitnum and p.patnum = d.patnum and p.insplan=d.iplan
You have to introduce all of your CTEs first via a single WITH, and then you can use them in a final query:
with Cte_LatestPatInfo as
(select max(a.LogID) as LastPatRec, p.unitnum, p.PatNum, a.iplan from
rrscsql2.arhighdollar.dbo.tblarinfoHistory a
inner join rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
on a.patID=p.patID and a.LogID=p.LogID
where a.Active = 1
and a.ReasonCode is not null
group by p.unitnum, p.PatNum, a.iplan),
cte_EOMDenials as
(select d.* from rrscsql2.Denials.dbo.tblDenialMonthEnd d
Inner join rrscsql3.Facilities.dbo.vwFacilities f
on d.UnitNum = f.UnitNum and f.Owner like '%LifePoint%'
Inner join rrscsql2.Denials.dbo.tblDispositionDictionary t
on d.disposition=t.disposition and DispositionType like'O%'
Where datediff(mm, monthending, GETDATE()) = 1
and DATEDIFF(mm,DischDate,monthending)>2)
select *,rc.Description from rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
inner join cte_LatestPatInfo li
on p.PatNum=li.PatNum and p.UnitNum=li.UnitNum and p.LogID = li.LastPatRec
inner join rrscsql2.arhighdollar.dbo.tblarinfoHistory ah
on ah.LogID=p.LogID and ah.iplan = li.iplan and p.PatID=ah.PatID
left join rrscsql2.ARHighDollar.dbo.tblReasonCodes rc
on ah.ReasonCode=rc.ReasonCode
left join cte_EOMDenials d
on p.unitnum=d.unitnum and p.patnum = d.patnum and p.insplan=d.iplan
The statement does not seem to join two CTEs. If you want to join two ctes you can achieve it like the following example
;WITH CTE_1
as
( select field1, field2... from sometable),
CTE_2
as
(select field1, field2... from sometable),
select a.field1, a.field2, b.field1, b.field2
from CTE_1 a left join CTE_2 b on a.field1 = b.field1;
You will notice that the two ctes were defined first, separated by comma(,) then use in the select statement.
Please see http://msdn.microsoft.com/en-us/library/ms175972(v=sql.90).aspx for reference.

JOIN / LEFT JOIN conflict in SQL Server

I have a tricky query. I need to select all recent versions of 2 types of members of administrator groups. Here is the query:
SELECT refGroup.*
FROM tblSystemAdministratorGroups refGroup
JOIN tblGroup refMem ON refGroup.AttributeValue = refMem.ObjectUID
This query will return all the administrator groups. The next step will be getting the members of these groups. Since I have 2 types of memberships (Explicit, Computed), I will have to use a LEFT JOIN to make sure that I am not excluding any rows.
SELECT refGroup.*
FROM tblSystemAdministratorGroups refGroup
-- The JOIN bellow can be excluded but it is here just to clarify the architecture
JOIN tblGroup refMem ON refGroup.AttributeValue = refMem.ObjectUID
LEFT JOIN tblGroup_ComputedMember cm ON refMem.ObjectUID = cm.GroupObjectID
LEFT JOIN tblGroup_ExplicitMember em ON refMem.ObjectUID = em.GroupObjectID
The last piece in the puzzle is to get the latest version of each member. For that I will have to use JOIN to exclude older versions:
JOIN (
SELECT MAX([ID]) MaxId
FROM [OmadaReporting].[dbo].tblGroup_ComputedMember
GROUP BY ObjectID
) MostRecentCM ON MostRecentCM.MaxId = cm.Id
and
JOIN (
SELECT MAX([ID]) MaxId
FROM [OmadaReporting].[dbo].tblGroup_ExplicitMember
GROUP BY ObjectID
) MostRecentEM ON MostRecentEM.MaxId = em.Id
The full query will be:
SELECT refGroup.*
FROM tblSystemAdministratorGroups refGroup
JOIN tblGroup refMem ON refGroup.AttributeValue = refMem.ObjectUID
LEFT JOIN tblGroup_ComputedMember cm ON refMem.ObjectUID = cm.GroupObjectID
JOIN (
SELECT MAX([ID]) MaxId
FROM [OmadaReporting].[dbo].tblGroup_ComputedMember
GROUP BY ObjectID
) MostRecentCM ON MostRecentCM.MaxId = cm.Id
LEFT JOIN tblGroup_ExplicitMember em ON refMem.ObjectUID = em.GroupObjectID
JOIN (
SELECT MAX([ID]) MaxId
FROM [OmadaReporting].[dbo].tblGroup_ExplicitMember
GROUP BY ObjectID
) MostRecentEM ON MostRecentEM.MaxId = em.Id
The issue is clear: The 2 JOIN to exclude old versions are also applied to the select statement and clearly no rows are returned. What would be the best solution to escape such situation and to return the intended values?
SELECT refGroup.*
FROM tblSystemAdministratorGroups refGroup
JOIN tblGroup refMem ON refGroup.AttributeValue = refMem.ObjectUID
LEFT JOIN (
select GroupObjectID, ID, max(ID) over (partition by ObjectID) as maxID
from tblGroup_ComputedMember
) cm ON refMem.ObjectUID = cm.GroupObjectID and cm.ID = cm.maxID
LEFT JOIN (
select GroupObjectID, ID, max(ID) over (partition by ObjectID) as maxID
from tblGroup_ExplicitMember
) em ON refMem.ObjectUID = em.GroupObjectID and em.ID = em.maxID
where cm.ID = cm.MaxID
What about using LEFT join in your last two joins?
LEFT JOIN (
SELECT MAX([ID]) MaxId
FROM [OmadaReporting].[dbo].tblGroup_ComputedMember
GROUP BY ObjectID
) MostRecentCM ON MostRecentCM.MaxId = cm.Id
And then in Where clause filter values as:
WHERE MostRecentCM.MaxId IS NOT NULL
OR
MostRecentEM.MaxId IS NOT NULL

Sum record data into one

I have this query which returns qty in each of my branch. now the branch has two WH_subType as you see in the attached diagram i have attached. I want to sum the 2 subtype and show its available qty. how can i do it.
my select query is like this
SELECT
dbo.WarehouseType.name AS Section,
dbo.WarehouseSubType.name AS WH_Type,
dbo.WarehouseSubType1.name AS WH_SubType,
dbo.Branch.name AS Branch,
(dbo.WarehouseProductQuantity.actualQuantity - dbo.WarehouseProductQuantity.reservedQuantity) AS AvailQty,
dbo.WarehouseProductQuantity.tafsilId AS Tafsil,
dbo.Tafsil.description AS Product_Name
FROM
dbo.WarehouseSubType
INNER JOIN
dbo.WarehouseType
ON
(
dbo.WarehouseSubType.warehouseTypeId = dbo.WarehouseType.id)
INNER JOIN
dbo.WarehouseSubType1
ON
(
dbo.WarehouseSubType.id = dbo.WarehouseSubType1.warehouseSubTypeId)
INNER JOIN
dbo.Warehouse
ON
(
dbo.WarehouseSubType1.id = dbo.Warehouse.warehouseSubType1Id)
INNER JOIN
dbo.Branch
ON
(
dbo.Warehouse.branchId = dbo.Branch.id)
INNER JOIN
dbo.WarehouseProductQuantity
ON
(
dbo.Warehouse.id = dbo.WarehouseProductQuantity.warehouseId)
INNER JOIN
dbo.TafsilLink
ON
(
dbo.WarehouseProductQuantity.tafsilId = dbo.TafsilLink.sourceId)
INNER JOIN
dbo.Tafsil
ON
(
dbo.TafsilLink.targetId = dbo.Tafsil.id)
INNER JOIN
dbo.FinishProduct
ON
(
dbo.Tafsil.id = dbo.FinishProduct.tafsilId)
INNER JOIN
dbo.Supplier
ON
(
dbo.FinishProduct.supplierId = dbo.Supplier.tafsilId)
WHERE
WarehouseSubType1.warehouseSubTypeId IN (1,4)
group by dbo.WarehouseProductQuantity.tafsilId
Have you tried a group by
SELECT SubType, SUM(qty) AS QtySum
GROUP BY SubType
Every grouped by column should be in your select. Note: for every column you group by it further sub divides the data
Update based on OP comment:
If you want other columns you need to do something like
SELECT s.WH_SubType,s.AvailQty, t.other_cols
from
(SELECT
dbo.WarehouseSubType1.name AS WH_SubType,
sum(dbo.WarehouseProductQuantity.actualQuantity - dbo.WarehouseProductQuantity.reservedQuantity) AS AvailQty
FROM
table
GROUP BY
dbo.WarehouseSubType1.name) s
left join table t on t.dbo.WarehouseSubType1.name = s.WH_SubType;
For reference see this question: How do I use "group by" with three columns of data?
UPDATE 2:
SELECT
dbo.WarehouseType.name AS Section,
dbo.WarehouseSubType.name AS WH_Type,
dbo.WarehouseSubType1.name AS WH_SubType,
dbo.Branch.name AS Branch,
SumTable.AvailQty,
SumTable.Tafsil,
dbo.Tafsil.description AS Product_Name
FROM
dbo.WarehouseSubType
INNER JOIN
dbo.WarehouseType
ON
(
dbo.WarehouseSubType.warehouseTypeId = dbo.WarehouseType.id)
INNER JOIN
dbo.WarehouseSubType1
ON
(
dbo.WarehouseSubType.id = dbo.WarehouseSubType1.warehouseSubTypeId)
INNER JOIN
dbo.Warehouse
ON
(
dbo.WarehouseSubType1.id = dbo.Warehouse.warehouseSubType1Id)
INNER JOIN
dbo.Branch
ON
(
dbo.Warehouse.branchId = dbo.Branch.id)
INNER JOIN
dbo.WarehouseProductQuantity
ON
(
dbo.Warehouse.id = dbo.WarehouseProductQuantity.warehouseId)
INNER JOIN
dbo.TafsilLink
ON
(
dbo.WarehouseProductQuantity.tafsilId = dbo.TafsilLink.sourceId)
INNER JOIN
dbo.Tafsil
ON
(
dbo.TafsilLink.targetId = dbo.Tafsil.id)
INNER JOIN
dbo.FinishProduct
ON
(
dbo.Tafsil.id = dbo.FinishProduct.tafsilId)
LEFT JOIN (SELECT
sum(dbo.WarehouseProductQuantity.actualQuantity - dbo.WarehouseProductQuantity.reservedQuantity) AS AvailQty,
dbo.WarehouseProductQuantity.tafsilId AS Tafsil
FROM
dbo.WarehouseProductQuantity
group by dbo.WarehouseProductQuantity.tafsilId) SumTable on dbo.Tafsil.id = SumTable.Tafsil
WHERE
WarehouseSubType1.warehouseSubTypeId IN (1,4)
You need to do something like
SELECT SUM(AvailQty), ... FROM ... WHERE ... GROUP BY WH_SubType
http://www.w3schools.com/sql/sql_func_sum.asp
http://www.w3schools.com/sql/sql_groupby.asp

Choose the greater of either left or right side of 2 queries

I have the following union query that queries for the most recent date of a column if it exists:
SELECT TOP 1 m.sentdate AS 'calltreelastsignedoff'
FROM Incidents i
INNER JOIN Plans p ON i.planuid = p.uid
INNER JOIN IncidentMessages im ON i.uid = im.incidentuid
INNER JOIN Messages m ON im.messageuid = m.uid
WHERE p.uid = '031E3346-2921-426E-9494-1111111111'
UNION
SELECT TOP 1 m.sentdate AS 'calltreelastsignedoff'
FROM Incidents i
INNER JOIN PlanExercises pe ON i.planexerciseuid = pe.uid
INNER JOIN IncidentMessages im ON i.uid = im.incidentuid
INNER JOIN Messages m ON im.messageuid = m.uid
WHERE pe.planuid = '031E3346-2921-426E-9494-1111111111'
This will return 2 values if each query returns a top 1 result.
What I really want is to select the top 1 of the combined query.
How can I perform a select on the unioned query?
try this:
You could do this with a derived table
select top 1 from
(
SELECT TOP 1 m.sentdate AS 'calltreelastsignedoff'
FROM Incidents i
INNER JOIN Plans p ON i.planuid = p.uid
INNER JOIN IncidentMessages im ON i.uid = im.incidentuid
INNER JOIN Messages m ON im.messageuid = m.uid
WHERE p.uid = '031E3346-2921-426E-9494-1111111111'
UNION
SELECT TOP 1 m.sentdate AS 'calltreelastsignedoff'
FROM Incidents i
INNER JOIN PlanExercises pe ON i.planexerciseuid = pe.uid
INNER JOIN IncidentMessages im ON i.uid = im.incidentuid
INNER JOIN Messages m ON im.messageuid = m.uid
WHERE pe.planuid = '031E3346-2921-426E-9494-1111111111'
)a
order by <col>