I have 2 datasets within a report, 1 holds the total population and the 2nd holds reviews. What I need to do within a field is show the % done. e.g. dataset1.[Reviews] where Status = "incomplete" and Status = "high risk" / dataset2.[Population] where type = "high risk"
Population Data example:
NAME, RISK LEVEL
Client123, HIGH RISK
Client124, MEDIUM RISK
Client125, HIGH RISK
Client126, HIGH RISK
Client127, HIGH RISK
Client128, HIGH RISK
Review Data example
CLIENT NAME, REVIEW STATUS
Client123, INCOMPLETE
Client124, COMPLETE
Client125, COMPLETE
Client126, INCOMPLETE
Client127, INCOMPLETE
Client128, INCOMPLETE
So 5 HIGH RISK Population, with 4 reviews incomplete = 80% delinquent
I need to try and do this in an expression and for the life of me I'm struggling! Maybe it's Friday brain!?
Any help appreciated.
Check below screenshots and steps to get desired output
Sample Output
Expression Details
Row Group Details - 1
Row Group Details - 2
DataSet1
select 'Client123' as [NAME],'HIGH RISK' as [RISK LEVEL]
union all
select 'Client124' as [NAME],'MEDIUM RISK' as [RISK LEVEL]
union all
select 'Client125' as [NAME],'HIGH RISK' as [RISK LEVEL]
union all
select 'Client126' as [NAME],'HIGH RISK' as [RISK LEVEL]
union all
select 'Client127' as [NAME],'HIGH RISK' as [RISK LEVEL]
union all
select 'Client128' as [NAME],'HIGH RISK' as [RISK LEVEL]
DataSet2
select 'Client123' as [CLIENT NAME] , 'INCOMPLETE' as [REVIEW STATUS]
union all
select 'Client124' as [CLIENT NAME] , 'COMPLETE' as [REVIEW STATUS]
union all
select 'Client125' as [CLIENT NAME] , 'COMPLETE' as [REVIEW STATUS]
union all
select 'Client126' as [CLIENT NAME] , 'INCOMPLETE' as [REVIEW STATUS]
union all
select 'Client127' as [CLIENT NAME] , 'INCOMPLETE' as [REVIEW STATUS]
union all
select 'Client128' as [CLIENT NAME] , 'INCOMPLETE' as [REVIEW STATUS]
Expression used in this report
1. Lookup(Fields!NAME.Value, Fields!CLIENT_NAME.Value, Fields!REVIEW_STATUS.Value, "DataSet2")
2. count(Lookup(Fields!NAME.Value, Fields!CLIENT_NAME.Value, Fields!REVIEW_STATUS.Value, "DataSet2"))
3. count(Lookup(Fields!NAME.Value, Fields!CLIENT_NAME.Value, Fields!REVIEW_STATUS.Value, "DataSet2"))/Count(Fields!NAME.Value,"RISK_LEVEL")
4. Count(Fields!NAME.Value, "RISK_LEVEL2")
I have created two sample report. I mentioned group properties of sample report1. For sample report2 the group property like as first one.
Related
I need count how many suppliers have status "CLOSED" or "READY FOR AUDIT", but in the same time I don't want to count those suppliers that have status "NULL".
supplier
status
JUSTRITE MANUFACTURING COMPANY
CLOSED
JW SPEAKER CORPORATION
CLOSED
KLEIN TOOLS INC
NULL
KLEIN TOOLS INC
CLOSED
KLEVER INNOVATIONS
CLOSED
LA-CO INDUSTRIES INC
CLOSED
As we can see in this example, there are 5 different customers, but the results will be "4" because KLEINT contains "NULL" and it shouldn't be counted in.
Let's say table is defined like the following:
Table name: your_table with two fields
supplier status
---------------- ------
Then you can get the result by aggregation function count.
SELECT
supplier,
SUM(IFNULL((SELECT 1 FROM your_table WHERE a. supplier = supplier LIMIT 1), 0)) AS cnt
FROM your_table a
WHERE `status` IN ('CLOSED', 'READY')
GROUP BY supplier
HAVING cnt > 0
;
I have made a fiddle for you with the solution as an example
https://www.db-fiddle.com/f/rW914i1yUA5GBK2d9j6PGB/0
also the code should look like this
SELECT count(*)
FROM test
WHERE STATUS IS NOT NULL
AND STATUS IN (
'CLOSED'
,'READY FOR AUDIT'
)
this code ignores NULL values, (but they have to be empty values, not someone typing NULL in the fields. that makes a diffrence)
You can:
select all suppliers which have "CLOSED" or "READY FOR AUDIT" status
remove from the selection all those that have a "NULL" status
SELECT COUNT(DISTINCT supplier)
FROM tab
WHERE status IN ('CLOSED', 'READY FOR AUDIT')
AND supplier NOT IN (SELECT supplier FROM tab WHERE status IS NULL)
Check the demo here.
WITH CTE(COMPANY,STATUSS) AS
(
SELECT 'JUSTRITE MANUFACTURING COMPANY', 'CLOSED' UNION ALL
SELECT'JW SPEAKER CORPORATION', 'CLOSED' UNION ALL
SELECT'KLEIN TOOLS INC' , NULL UNION ALL
SELECT'KLEIN TOOLS INC' , 'CLOSED' UNION ALL
SELECT'KLEVER INNOVATIONS', 'CLOSED' UNION ALL
SELECT'LA-CO INDUSTRIES INC' , 'CLOSED'
)
SELECT COUNT(C.COMPANY)
FROM CTE AS C
WHERE C.STATUSS IN('CLOSED','READY FOR AUDIT')
AND NOT EXISTS
(
SELECT 1 FROM CTE AS X WHERE C.COMPANY=X.COMPANY AND X.STATUSS IS NULL
)
CTE is an example of your data. Please replace it with your table name
I have a query
I have to calculate monthly sales per branch and customer (Data coming from one table)
Data should look like below
I can write the query for Jan_2019 total sales:
I create a temp table for Feb_2019. I can use the join and combine the 2 tables, but in Feb_2019 if there are new customers added, then when joining the tables I am missing new customers, and due to this the total sales for that month are not matching.
Can any one help?
I have written the query like this below
;with a as
(
select branchid, customer, sum(totalsales) as jan_totalsales from tableA
where year = 2019 and month = 1
group by customer, branched
), feb as
(
select branchid, customer, sum(totalsales) as feb_totalsales from tableA
where year = 2019 and month = 2
group by customer, branched
)
select a.branchid, feb.branchid, a.jan_totalsales, feb.feb_totalsales
from a
left join feb on feb.branchid = a.branchid
I have to create this in a temp table and do it for march_2019
Again, I am not getting new customers as I am joining from Jan data.
Can anyone help me to make this simple?
What you are after here is a conditional aggregate. This should get you on the right path:
SELECT branchid,
customer,
SUM(CASE WHEN [Year] = 2019] AND [Month] = 1 THEN totalsales ELSE 0 END) AS JanSales,
SUM(CASE WHEN [Year] = 2019] AND [Month] = 2 THEN totalsales ELSE 0 END) AS FebSales,
....
FROM YourTable
GROUP BY branchid,
customer;
If you don't undertstand how this works, please do ask. At the end of the day, it's you who has to support the SQL, not myself or other volunteers on Stack Overflow.
I am specifically targeting SQL Server, but this information would be helpful if it exists for MySQL also. I have found how to create a column that calculates the difference between columns of two consecutive rows, but what I am interested in is how do I calculate the difference between one column in two distinctly identifiable rows?
I know of one way to do it, but it requires deep query nesting and repeating the same base queries. Suppose I have a large query joining multiple tables that ultimately boils down to this:
SELECT
projectStatus AS [Project Status],
COUNT(projectStatus) AS [# of Projects]
FROM
projectList
GROUP BY projectStatus
And let's say it gives the following result:
Project Status | # of Projects
------------------------------
Delayed | 167
Delayed Known | 83
On Time | 92
Ahead | 86
What I would like to do is append a row that calculates the difference between the # of Projects value of rows Delayed and Delayed Known, then omits the Delayed row, like so:
Project Status | # of Projects
------------------------------
Delayed | 167
Delayed Known | 83
On Time | 92
Ahead | 86
Delayed Unknown| 84
Based on the first query, the way I have figured out how to do this looks like:
SELECT
projectStatus AS [Project Status],
COUNT(projectStatus) AS [# of Projects]
FROM
projectList
GROUP BY projectStatus
UNION
SELECT
'Delayed Unknown' AS [Project Status],
SUM([Sum Val]) AS [# of Projects]
FROM (
SELECT
[# of Projects] *
CASE
WHEN [Project Status] = 'Delayed' THEN 1
WHEN [Project Status] = 'Delayed Known'] THEN -1
ELSE 0
END AS [Sum Val]
FROM (
SELECT
projectStatus AS [Project Status],
COUNT(projectStatus) AS [# of Projects]
FROM
projectList
WHERE
projectStatus IN ('Delayed', 'Delayed Known')
GROUP BY projectStatus
) AS queryC
) AS queryB
Keep in mind that the inner query that this is based on is simplified for this post, but it actually is a larger query that is composed of its own UNION. Therefore, this gets ugly very quickly and approaches hard-to-maintain status.
The target for this query is to act as a dataset for SQL Server Reporting Services, so my constraint is to do this all in one query (unless it is possible to use temp tables within SSRS datasets). So, in one query, is there a less invasive way to do this calculation?
Please try this, may be you are looking for something like the below.
;with cte as(
select Projectstatus, count(*) as [# of Projects]
from #Table
where projectStatus IN ('Delayed', 'Delayed Known')
group by ProjectStatus
), cte2 as(
select
(Select [# of Projects] From cte Where Projectstatus = 'Delayed')
-(Select [# of Projects] From cte Where Projectstatus = 'Delayed Known') as DelayedUnKownProjects
)
select Projectstatus, [# of Projects]
From cte
UNION ALL
SELECT 'Delayed UnKnown' as Projectstatus, DelayedUnKownProjects as [# of Projects]
From cte2
i have 3 tables
supplier(id_supp, name, adress, ...)
Customer(id_cust, name, adress, ...)
Order(id_order, ref_cust, ref_supp, date_order...)
I want to make a job that counts the number of orders by Supplier, for last_week, last_two_weeks with Talend
select
supp.name,
(
select
count(*)
from
order
where
date_order between sysdate-7 and sysdate
nd ref_supp=id_supp
) as week_1,
(
select
count(*)
from
order
where
date_order between sysdate-14 and sysdate-7
nd ref_supp=id_supp
) as week_2
from supplier supp
the resaon for what i'm doing this, is that my query took to much time
You need a join between supplier and order to get supplier names. I show an inner join, but if you need ALL suppliers (even those with no orders in the order table) you may change it to a left outer join.
Other than that, you should only have to read the order table once and get all the info you need. Your query does more than one pass (read EXPLAIN PLAN for your query), which may be why it is taking too long.
NOTE: sysdate has a time-of-day component (and perhaps the date_order value does too); the way you wrote the query may or may not do exactly what you want it to do. You may have to surround sysdate by trunc().
select s.name,
count(case when o.date_order between sysdate - 7 and sysdate then 1 end)
as week_1,
count(case when o.date_order between sysdate - 14 and sysdate - 7 then 1 end)
as week_2
from supplier s inner join order o
on s.id_supp = o.ref_supp
;
I am trying to build a lapsed payment report which displays the data in a quarter by Quarter Basis, but evaluated based on the year.
E.g. I would like to return all the Names of people who have payed in previous years, but not this year and display the results by quarter.
I have this so far, however getting it to return the correct names continues to elude me.
SELECT
IDNo,
Year(now()) as CurrentYear,
concat(year(pdate),'-Q',quarter(pdate)) as YrQtr,
Concat(IDNo,' - ',Fname,' ',Lname) as Fullname,
sum(amt) as Amt
FROM Payments
GROUP BY fname, lname, YrQtr
Having Sum(if(year(pdate)=Year(now()),1,0)) < 1 and (Sum(if(year(pdate)=Year(now())-1 ,1,0)) >0 or Sum(if(year(pdate)=Year(now())-2 ,1,0)) >0)
Any Help, direction, of even letting me know this is impossible would be appreciated.
thanks,
Edit:
The results would show Payer 1, who has paid in 2012 or 2013 but not 2014, with a Breakdown by Quarter - year
2012-Q1 2013-Q3
$xxx $xxx
But not payer 2, who has paid in 2012 and 2014. (or any payer which has paid in 2014)
I solved the issue using the following solution and temporary tables;
DROP temporary table IF EXISTS LapsedRPT;'
Create temporary table LapsedRPT
SELECT IDno,
Concat(IDNo,' - ',Fname,' ',Lname) as Fullname
FROM Payments
GROUP BY FullName
having year(max(ddate)) < year(now());
Select
year(now()) as CurrentYear,
Year(pdate) as PYr,
quarter(pdate) as PQTR,
concat(year(pdate),'-Q',quarter(ddate)) as YrQtr,
Fullname,
sum(amt) as Amt
from LapsedRPT
Where year(pdate) > Year(now())-3
Group by FullName, pyr, yrQtr;