Show count in SSRS using SQL Server - sql-server

Below is my sql query
USE AdventureWorks2012
Select EDH.*,
case when EDH.DepartmentID between 1 and 5 then 'DEPT-A'
when EDH.DepartmentID between 5 and 9 then 'DEPT-B'
when EDH.DepartmentID between 9 and 30 then 'DEPT-C' end as [DEPT TYPE]---, count(EDH.DepartmentID)
from HumanResources.EmployeeDepartmentHistory EDH
left outer join HumanResources.Employee EM on EM.BusinessEntityID = EDH.BusinessEntityID
group by EDH.DepartmentID, EDH.BusinessEntityID, EDH.ShiftID, EDH.StartDate, EDH.EndDate, EDH.ModifiedDate
I'm bringing the Dept Type column either as Dept-A, B or C based on Case when Condition on Department ID.
Now in the SSRS report I want to show the count of the Department Column in the footer such as
Dept Count[A/B/C]: 49/195/46
How to show the count in the report? Should I modify the query or there is some change required in the report

You can use an expression like:
="Dept Count [A/B/C]: "
& Sum(IIf(Fields!DeptType.Value = "DEPT-A", 1, 0)) & "/"
& Sum(IIf(Fields!DeptType.Value = "DEPT-B", 1, 0)) & "/"
& Sum(IIf(Fields!DeptType.Value = "DEPT-C", 1, 0))
This will work as you have a set amount of department types. Use a conditional Sum with IIf to get the numbers in each account.
Works on some simplified data:

Related

SQL Server "No column name was specified" - But it was

I have a query that runs in SQL Server (using Visual Studio 19), but when I try to run it using Microsoft Query in Excel, I get the error
"No column name was specified for column 5 of 'bv'.
"No column name was specified for column 6 of 'bv'.
"No column name was specified for column 7 of 'bv'.
Statement(s) could not be prepared.
As you can see below, I alias columns 5, 6, and 7 (Pieces, BatchValue, Multiplier). In addition, the fact that this query runs in Visual Studio indicates I haven't gotten this egregiously wrong.
WITH bv AS
(
SELECT
b.BatchID, b.BatchDate, b.BatchName, b.Price AS Cost,
SUM(bh.QtyFound) AS Pieces,
SUM(ps.StrategyPrice * QtyFound) AS BatchValue,
IIF(b.Price = 0, 0, SUM(ps.StrategyPrice * QtyFound) / b.Price) AS Multiplier,
b.Active
FROM
Inventory.Batches b
JOIN
Inventory_Item_History_Summary bh ON b.BatchID = bh.BatchID
JOIN
Selling.Price_Strategy_Table ps ON LEFT(bh.ItemType, 1) = LEFT(ps.ItemType, 1)
AND bh.ItemNum = ps.ItemNo
AND bh.ColorID = ps.Color
AND bh.BLCond = ps.Cond
GROUP BY
b.BatchID, b.BatchDate, b.BatchName, Price, Active
)
SELECT
SUM(p.StrategyPrice * h.QtyFound) / SUM(p.StrategyPrice * IIF(bv.Multiplier = 0, 0, h.QtyFound / bv.Multiplier)) AvgMultiplier
FROM
Inventory.Locations l
JOIN
Inventory_Item_History_Summary h ON h.LocationID = l.LocationID
JOIN
Selling.Price_Strategy_Table p ON LEFT(h.ItemType, 1) = LEFT(p.ItemType, 1)
AND h.ItemNum = p.ItemNo
AND h.ColorID = p.Color
AND h.BLCond = p.Cond
JOIN
bv ON h.BatchID = bv.BatchID
WHERE
l.Cond = 'U'
AND bv.cost > 0
AND bv.Active = 0
What have I done wrong to cause it not to run in Microsoft Query? It's worth noting that I have another query on the same worksheet that uses the exact same bv subquery with no issues

SQL Subquery with a COUNT

I have a table of our communications containing; Created User, Created Date & Sub Code. I want the output to have 4 columns in SSRS, showing communications from the previous month;
Comms logged Dealt With % Dealt With
Created User 1
Created User 2
So far I've got;
SELECT
[EM-COMMUNICATION].[CRT-USER]
,COUNT([EM-COMMUNICATION].[CRT-USER]) AS LOGGED
,(SELECT COUNT([EM-COMMUNICATION].[CRT-USER]) FROM [EM-COMMUNICATION] WHERE [EM-COMMUNICATION].[SUB-CODE] = N'DEALTWITH' AND DateDiff(MONTH,[EM-COMMUNICATION].[CRT-DATE],GetDate()) = 1) AS Dealt
FROM
[EM-COMMUNICATION]
WHERE
DateDiff(MONTH,[EM-COMMUNICATION].[CRT-DATE],GetDate()) = 1
GROUP BY
[EM-COMMUNICATION].[CRT-USER]
The problem I'm having is that the sub query is returning a count of comms for all users, instead of matching the groupings of the main query, i.e., each row has the same count in 'Dealt With'
You need to relate your subquery with your main query using AND [EM-COMMUNICATION].[CRT-USER] = T.[CRT-USER]
SELECT
[EM-COMMUNICATION].[CRT-USER]
,COUNT([EM-COMMUNICATION].[CRT-USER]) AS LOGGED
,(SELECT COUNT([EM-COMMUNICATION].[CRT-USER]) FROM [EM-COMMUNICATION] as T WHERE T.[SUB-CODE] = N'DEALTWITH' AND DateDiff(MONTH,T.[CRT-DATE],GetDate()) = 1 AND [EM-COMMUNICATION].[CRT-USER] = T.[CRT-USER]) AS Dealt
FROM
[EM-COMMUNICATION]
WHERE
DateDiff(MONTH,[EM-COMMUNICATION].[CRT-DATE],GetDate()) = 1
GROUP BY
[EM-COMMUNICATION].[CRT-USER]

Change the Grouping Sub Total row title into other name in SQL Server 2008

I can calculate the Sub total for each Item Type but the problem is intead of display "Total QTY:" , It only displays the Item Type Name.
Please see my expected result:
And here is the query:
SELECT
[GOODS_TYPE] = COALESCE([_TYPE_NAME], 'GRAND_TOTAL'),
b.GOODS_CODE,b.GOODSNAME,a.CURRENCY,a.CUSTOMER_CD, a.INVOICE_DATE,[QUANTITY] = SUM([QTY])
From [DVHead] a
inner join [DVRecords] b
on a.DELIVERYNO=b.DELIVERYNO
WHERE a.INVOICE_DATE='2018-05-04'
GROUP BY GROUPING SETS(([_TYPE_NAME],[GOODS_CODE],[GOODSNAME],
[CURRENCY],[CUSTOMER_CD],[INVOICE_DATE]),([_TYPE_NAME]),());
use CASE ... WHEN condition
[GOODS_TYPE] = CASE WHEN [_TYPE_NAME] IS NULL
THEN 'GRANT_TOTAL'
WHEN [GOODS_CODE] IS NULL
THEN 'TOTAL ' + [_TYPE_NAME]
END

What is the query for table 3?

I have extracted these two tables from a SQL Database:
query for table 1:
SELECT POM_DOCNO,
POM_DATE,
SUP_CODE,
POM_CREATEDBY
FROM SI_PURORDERMASTER
WHERE POM_YEAR = 2012
AND POM_PERIOD = 6
query for table 2:
SELECT POM_DOCNO,
ITM_ITEMCODE,
ITM_ITEMDESC,
POD_QTY,
POD_RATE
FROM SI_PURORDERDETAIL
WHERE POM_YEAR = 2012
AND POM_PERIOD = 6
query to get table 3 ?
I have tried using joins but always end up with wrong result :/
Is there anyway to get table 3 with just table 1 & 2 ?
Both table 1 and 2 have "POM_DOCNO" column in common.
Try something like that;
SELECT TOP 5
SIR.POM_DOCNO,
SIR.POM_DATE,
SIR.SUP_CODE,
SIL.ITM_ITEMCODE,
SIL.POD_QTY,
SIL.POD_RATE,
SIR.POM_CREATEDBY
FROM SI_PURORDERMASTER SIR inner join SI_PURORDERDETAIL SIL ON SIR.POM_DOCNO = SIL.POM_DOCNO
WHERE SIR.POM_YEAR = 2012
AND SIR.POM_PERIOD = 6
Also, just use TOP to limit results. If you want to specift order you should use order by

SQL Server LEFT JOIN

This query has been keeping me busy for the last couple of days. I tried to rewrite it with different ideas but I keep having the same problem. To simplify the problem I put part of my query in a view, this view returns 23 records. Using a left join I would like to add fields coming from the table tblDatPositionsCalc to these 23 records. As you can see I have an additional condition on the tblDatPositionsCalc in order to only consider the most recent records. With this condition it would return 21 records. The join should be on two fields together colAccount and colId.
I simply want the query to return the 23 records from the view and where possible have the information from tblDatPositionsCalc. There is actually only 2 records in the view without corresponding id and account in tblDatPositionsCalc, that means out of the 23 records only 2 will have missing values in the fields coming from the table tblDatPositionsCalc.
The problem with my query is that it only returns the 21 records from tblDatPositionsCalc. I don't understand why. I tried to move the condition on date in just after the JOIN condition but that did not help.
SELECT TOP (100) PERCENT
dbo.vwCurrPos.Account,
dbo.vwCurrPos.Id,
dbo.vwCurrPos.TickerBB,
dbo.vwCurrPos.colEquityCode,
dbo.vwCurrPos.colType,
dbo.vwCurrPos.colCcy,
dbo.vwCurrPos.colRegion,
dbo.vwCurrPos.colExchange,
dbo.vwCurrPos.[Instr Type],
dbo.vwCurrPos.colMinLastDay,
dbo.vwCurrPos.colTimeShift,
dbo.vwCurrPos.Strike,
dbo.vwCurrPos.colMultiplier,
dbo.vwCurrPos.colBetaVol,
dbo.vwCurrPos.colBetaEq,
dbo.vwCurrPos.colBetaFloor,
dbo.vwCurrPos.colBetaCurv,
dbo.vwCurrPos.colUndlVol,
dbo.vwCurrPos.colUndlEq,
dbo.vwCurrPos.colUndlFut,
tblDatPositionsCalc_1.colLots,
dbo.vwCurrPos.[Open Positions],
dbo.vwCurrPos.colListMatShift,
dbo.vwCurrPos.colStartTime,
tblDatPositionsCalc_1.colPrice,
tblDatPositionsCalc_1.colMktPrice,
dbo.vwCurrPos.colProduct,
dbo.vwCurrPos.colCalendar,
CAST(dbo.vwCurrPos.colExpiry AS DATETIME) AS colExpiry,
dbo.vwCurrPos.colEndTime,
CAST(tblDatPositionsCalc_1.colDate AS datetime) AS colDate,
dbo.vwCurrPos.colFund,
dbo.vwCurrPos.colExchangeTT,
dbo.vwCurrPos.colUserTag
FROM dbo.vwCurrPos
LEFT OUTER JOIN dbo.tblDatPositionsCalc AS tblDatPositionsCalc_1
ON tblDatPositionsCalc_1.colId = dbo.vwCurrPos.Id
AND tblDatPositionsCalc_1.colAccount = dbo.vwCurrPos.Account
WHERE (tblDatPositionsCalc_1.colDate =
(SELECT MAX(colDate) AS Expr1 FROM dbo.tblDatPositionsCalc))
ORDER BY
dbo.vwCurrPos.Account,
dbo.vwCurrPos.Id,
dbo.vwCurrPos.colEquityCode,
dbo.vwCurrPos.colRegion
Any idea what might cause the problem?
(Option 1) DrCopyPaste is right so your from clause would look like:
...
FROM dbo.vwCurrPos
LEFT OUTER JOIN dbo.tblDatPositionsCalc AS tblDatPositionsCalc_1
ON tblDatPositionsCalc_1.colId = dbo.vwCurrPos.Id
AND tblDatPositionsCalc_1.colAccount = dbo.vwCurrPos.Account
and (tblDatPositionsCalc_1.colDate =
(SELECT MAX(colDate) AS Expr1 FROM dbo.tblDatPositionsCalc))
...
reason: the where clause restriction of left joined to column = some expression with fail to return for "null = something" so the row will be removed.
(Option 2) As oppose to pushing code in to additional views where it is harder to maintain you can nest sql select statements;
select
X.x1,X.x2,
Y.*
from X
left join
(select Z.z1 as y1, Z.z2 as y2, Z.z3 as y3
from Z
where Z.z1 = (select max(Z.z1) from Z)
) as Y
on x.x1 = Y.y1 and X.x2 = Y.y2
The advantage here is you check each nested sub query a move out quickly. Although if you still building up more logic check out common table expressions (CTE's) http://msdn.microsoft.com/en-us/library/ms175972.aspx

Resources