How to get column value of join query in SSRS? - sql-server

I have select join query for generating report in SSRS. Query is working fine in SQL Server but as I add same query as dataset in SSRS and try to get rows count using CountRows() function it always return 0 (Zero). I'm not getting where my query is going wrong.
SQL Query
SELECT PR.NAME
FROm innovator.PROJECT PR
INNER JOIN innovator.PROJECT_RISK LPR ON LPR.SOURCE_ID = Pr.ID
INNER JOIN innovator.RISK_MANAGEMENT LR ON LR.id = LPR.RELATED_ID
Inner join innovator.PROGRAM_PROJECT P ON PR.ID = P.RELATED_ID
Inner Join innovator.PROGRAM PP ON P.SOURCE_ID = PP.ID
WHERE pp.ID = #Id
Fetching total count using CountRows() for Textbox
=CountRows(Fields!NAME.Value, "DataSetRisk")
DataSetRisk is Dataset name and Name is column name of Project Table

Use the CountRows function. For example
=CountRows("MyDataset")
Example : =CountRows("DataSetRisk")
will give you the number of rows in MyDataSet.

Try something a little simpler: Count(Fields!NAME.Value) as a column. This assumes, of course, that field name actually is populated. If the column is in separate groups, it will provide a count for each group, otherwise it will count for the entire report.

Related

Trying to sum a joined subquery

For my sql class I am trying to write a query that sums a column in a sub query. The Sub query has a calculated value in it and I am trying to total that calculated value for particular zipcodes.
the query runs without errors but the output is null in the sum column
Tried running the sub query on its own and the value is calculated for the column properly
select c.CustomerState
,c.CustomerZip
,sum (b.TotalSales)as Sales
from [DBM460LearningTeam].[dbo].[TBLcustomer]as c
Left Join (select v.CustomerNumber
,sum (v.QuantitySold * i.ItemPrice) as TotalSales
from TBLinvoice as v
inner join TBLitem as i
on v.ItemNumber = right(i.ItemNumber,3)
group by v.CustomerNumber) as b
on c.CustomerNumber = b.CustomerNumber
group by c.CustomerZip , c.CustomerState;
I want to sum the total sales by zipcode so i expect to see column for zipcode and column for state and column for sales and thats what I get but the sales column is Null all the way down.
Your CustomerNumbers aren't lining up on your join on. I would verify that that part if you say you have a value for TotalSales when you just run the subquery. I bet if you changed that left join to an inner join you would get nothing. So you are getting all values from TBLCustomer but nothing matches on customernumber when you do the left join.

Need query to determine number of attachments for each issue

I have a database in SQL Server that has three tables: Issues, Attachments, and Requestors. I need a single query that returns all the columns contained in the "Issues" and "Attachments" tables. Listed below is the query that I've created, but it's not working as expected:
SELECT A.*,
B.*,
SubQuery.attachmentcount
FROM [DB].[dbo].[issues] AS A
FULL OUTER JOIN [DB].[dbo].[requestors] AS B
ON A.issue_id = B.issue_id,
(SELECT Count(attachments.attachment_id) AS AttachmentCount
FROM issues
LEFT OUTER JOIN attachments
ON issues.issue_id = attachments.issue_id
WHERE attachments.attachment_status = 1
GROUP BY issues.issue_id) AS SubQuery;
Pictures describing the three tables are listed below:
Any ideas on how to fix my query?
Thanks,
"I need a single query that returns all the columns contained in the "Issues" and "Attachments" tables".
Based on this sentence try this:
SELECT A.Issue_ID, I.Issue_Name,r.Name, COUNT(A.attachment_id) AS Count
FROM Attachments as A
INNER JOIN Issues I on I.issue_id = A.issue_id
INNER JOIN requestors as R on A.issue_id = R.requestor_id
WHERE A.attachment_status = 1
GROUP BY A.Issue_ID, I.Issue_Name, r.Name
--Specify all columns by name (don't use *)
Keep It Simple and Try This!
SELECT i.Issue_ID, i.Issue_Name, COUNT(a.attachment_id) AS AttachmentCount
FROM attachments a JOIN
issues i ON
i.issue_id = a.issue_id
WHERE a.attachment_status = 1
GROUP BY i.Issue_ID, i.Issue_Name
Add your Desired Columns in Both Select List and Group By Clause and you are done.

Inner Join to Same Table Twice on same column

I'm having a problem with a SQL Server query trying to join a view with another view twice
SELECT
FAC.*
FROM
ViewFacturacionDiaria_Test AS FAC
INNER JOIN
ViewInformacionRepresentantes AS REP
ON REP.RepIDTabacal = FAC.Vendedor
INNER JOIN
ViewInformacionRepresentantes AS REP2
ON REP2.RepIDCtayOrden = FAC.Vendedor
WHERE
FecCpbte BETWEEN '2015-11-28' AND '2015-11-30'
In the "FAC" view I have sales information, in the other one I have a specific group of sales person which I want to filter from the main view.
I would like to understand why the query is returning an empty resultset.
Sorry, I cannot comment. But I believe Peter is right in his comment. Since you are using 2 inner joins they both need to return results. Are you expecting both joins to find a match?
Try this and see which column is null. That is the join that is resulting in no returned rows.
SELECT
FAC.Vendedor
,REP.RepIDTabacal
,REP2.RepIDCtayOrden
FROM
ViewFacturacionDiaria_Test AS FAC
LEFT JOIN
ViewInformacionRepresentantes AS REP ON
REP.RepIDTabacal = FAC.Vendedor
LEFT JOIN
ViewInformacionRepresentantes AS REP2 ON
REP2.RepIDCtayOrden = FAC.Vendedor
WHERE
FecCpbte BETWEEN '2015-11-28' AND '2015-11-30'

left join not showing null values

I need to find the items that exist in table A but not in table B. Now that would be really simple in MySQL doing a join like this
select * from A
left join B on A.key=B.key
where B.key is null
However for some reason this is not working in MSSQL. I have created the query without the where clause to see all the results and I only see matches, not null values. Do you have any idea why this is not working?
I know I can alternatively use "when not exists" but I want to know the reason as to why with a join is not working.
I am adding the code for your review
select Absences.CustomerID, b.*
from (
select * from openquery(JUAN_INTERFACE,'select cmp_wwn from Planet_Customers where i_outcome =4')) b
left join Absences on Absences.CustomerID = b.cmp_wwn
where Absences.Type = 3223
Your where clause is filtering out null values:
where Absences.Type = 3223
You are left-joining from the openquery subquery to Absences; and then filtering only rows that have a specific (non-null) value in an Absences column.
Did you mean to join the other way around, from Absenses to openquery?

SQL Server JOIN adds results?

I have a table which basically contains a Person_ID, Action, TimeStamp and using Microsoft ReportBuilder I have a report which tables each Person and the COUNT for their Actions.
Person Action X ActionY
1 3 5
2 0 4
Now I need to filter the results to only show people in a certain group which is defined by another table containing Person_ID, Group_ID.
When I do a JOIN and filter results based on the Group_ID = x the counts are very high, although it does filter correctly.
I run the query manually in SQL Server Manager and it is returning the same row multiple times?
EDIT:
My current SQL is
select t1.personid, t1.action, t2.personid, t2.groupid
from t1
inner join t2 on t1.personid = t2.personid
where t2.groupid = 1
This returns each line multiple times, forgetting the count part as this is in the report builder I would like to understand why the same row is returned multiple times as this is what breaks the report.
Do a DISTINCT
SO..
SELECT PersonID, ActionX = COUNT(distinct varname), ActionY = SUM(distint varname)
FROM tblName1 a
INNER JOIN tblName2 b ON a.PersonID = b.PersonID
WHERE b.Group_ID = 'groupvar'

Resources