Handling Null value - sql

I am trying to generate data per sales people per week while comparing my results with last week and subtracting last week from current week. Sometimes current week doesn't have any value. I would like to handle that situation by replacing it by '0'. I am not really good at this so I am seeking help.
SELECT t2.emp_name,
t2.pipelinetarget,
Sum(t0.maxsumloc) AS SaleOpp,
Datepart(ww, t0.opendate) AS [Week #],
Isnull ((SELECT Sum(maxsumloc) AS Expr1
FROM dbo.oopr AS A
WHERE ( slpcode = t1.slpcode )
AND ( Datepart(ww, opendate) =
Datepart(ww, Getdate()) - 1 )
AND ( Datepart(yy, opendate) = Datepart(yy, Getdate()) )
GROUP BY slpcode), 0) AS LastWeek,
Isnull ((SELECT Sum(maxsumloc) AS Expr1
FROM dbo.oopr AS A
WHERE ( Datepart(ww, opendate) = Datepart(ww, Getdate()) )
AND ( Datepart(yy, opendate) = Datepart(yy, Getdate()) )
AND ( maxsumloc >= 10000.00 )
AND ( slpcode = t1.slpcode )
GROUP BY slpcode), 0) AS [SumOf-Opp-obove-10k],
Isnull ((SELECT Count(opprid) AS Expr1
FROM dbo.oopr AS A
WHERE ( Datepart(ww, opendate) = Datepart(ww, Getdate()) )
AND ( Datepart(yy, opendate) = Datepart(yy, Getdate()) )
AND ( maxsumloc >= 10000.00 )
AND ( slpcode = t1.slpcode )
GROUP BY slpcode), 0) AS [#-Opp-obove-10k],
Isnull ((SELECT Sum(maxsumloc) AS Expr1
FROM dbo.oopr AS A
WHERE ( Datepart(ww, opendate) = Datepart(ww, Getdate()) )
AND ( Datepart(yy, opendate) = Datepart(yy, Getdate()) )
AND ( maxsumloc < 10000.00 )
AND ( slpcode = t1.slpcode )
GROUP BY slpcode), 0) AS [SumOf-Opp-below-10k],
Isnull ((SELECT Count(opprid) AS Expr1
FROM dbo.oopr AS A
WHERE ( Datepart(ww, opendate) = Datepart(ww, Getdate()) )
AND ( Datepart(yy, opendate) = Datepart(yy, Getdate()) )
AND ( maxsumloc < 10000.00 )
AND ( slpcode = t1.slpcode )
GROUP BY slpcode), 0) AS [#-Opp-below-10k]
FROM dbo.oslp AS t1
LEFT OUTER JOIN dbo.oopr AS t0
ON t1.slpcode = t0.slpcode
LEFT OUTER JOIN dbo.[user_qsalestarget(salesrep)] AS t2
ON t2.emp_name = t1.slpname
LEFT OUTER JOIN dbo.ocrd AS t4
ON t4.cardcode = t0.cardcode
LEFT OUTER JOIN dbo.[user_clusters(sp)] AS t5
ON t5.emp_name = t1.slpname
WHERE ( Datepart(ww, t0.opendate) = Datepart(ww, Getdate()) )
AND ( Datepart(yy, t0.opendate) = Datepart(yy, Getdate()) )
AND ( t5.title = 'Outside Sales Rep' )
GROUP BY t2.emp_name,
t1.slpcode,
Datepart(ww, t0.opendate),
t2.pipelinetarget

Look at the coalesce function eg "SELECT COALESCE(thePossibleNullValue, 0) FROM mytable" which returns the first non-null value ie either your value or zero if it's null in this example

Related

How Can I write in my code to exclude accounts that have had a payment posted w/in last 45 days?

I'm working in SQL Server Mgmt Studio and I'm trying to identify accounts that have not had a payment posted since the last 45 days. I've tried declaring a min and max date with getdate being less than 45 days, but that didnt work. I've also tried stating "where last payment !> getdate() -45".... but this did not give me the results I was looking for. what I need the query to do is only identify accounts that truly have not had a payment post in the last 45 days. but I'm not sure how to state this in my where clause. Here's my query
DECLARE #minDate datetime;
DECLARE #maxDate datetime;
SET #minDate = GETDATE();
SET #maxDate = GETDATE() - 45;
SELECT DISTINCT
a.id,
a.FacilityCode AS [Facility],
a.accountUnit,
a.accountNum AS [Account Number],
a.amountBalance AS [Balance],
a.accountstatus AS [Status],
CONVERT(date, a.accountStatusDate) AS StatusDate,
pmtEntryDate AS [Pmt Entry Date],
v.LastLinkARpmtDate AS [Last Pmt Date],
CONVERT(date, a.accountnextactdate) AS NextActionDate,
CONVERT(date, a.rpcDate) AS RightpartyContactDate,
a.flag AS accountFlag,
pay.flag AS pmtFlag,
a.accounttype
FROM dbo.tbAccount a
JOIN dbo.tbpmtinfo pay ON a.id = pay.id
JOIN dbo.vLastPmtDate v ON v.id = a.id
FULL JOIN (SELECT id,
MAX(deDate) AS MFSLoadDate
FROM dbo.tbDataEvents (NOLOCK)
WHERE deNewVal = 'MFS'
GROUP BY id) m ON m.id = a.id
WHERE pay.flag IN ('D')
AND a.amountBalance > 0
AND a.FacilityCode IN ('PHKY', 'QANM', 'QAOH', 'QBCA', 'QBIL', 'QBTX', 'QCIL', 'QDAL', 'QEWY', 'QFAR', 'QFGA',
'QGIL', 'QHAR', 'QHIL', 'QHTN', 'QLAL', 'QLPA', 'QMIL', 'QMNC', 'QMNM', 'QMNV', 'QMOR',
'QMTN', 'QMTX', 'QMUT', 'QRIL', 'QRKY', 'QSPA', 'QTGA', 'QTKY', 'QUIL', 'QVIL', 'QWIL', 'LHFL')
AND a.amountBalance >= '1000'
AND a.accountStatus IN ('PA', 'PP', 'BPA')
AND a.flag = 'A'
AND a.accountType IN ('Resid', 'Slfpy')
--and LastLinkARpmtDate !> Getdate() -45 and accountNum = '40728597'
AND LastLinkARpmtDate BETWEEN #minDate AND #maxDate;
I would use a WHERE NOT EXISTS clause to eliminate recent activity:
SELECT DISTINCT a.id
,a.FacilityCode [Facility]
,a.accountUnit
,a.accountNum [Account Number]
,a.amountBalance [Balance]
,a.accountstatus [Status]
,convert(DATE, a.accountStatusDate) StatusDate
,pmtEntryDate [Pmt Entry Date]
,v.LastLinkARpmtDate [Last Pmt Date]
,convert(DATE, a.accountnextactdate) NextActionDate
,convert(DATE, a.rpcDate) RightpartyContactDate
,a.flag AS accountFlag
,pay.flag AS pmtFlag
,a.accounttype
FROM dbo.tbAccount AS a
JOIN dbo.tbpmtinfo AS pay ON a.id = pay.id
JOIN dbo.vLastPmtDate AS v ON v.id = a.id
FULL JOIN (
SELECT id
,Max(deDate) AS MFSLoadDate
FROM dbo.tbDataEvents(NOLOCK)
WHERE deNewVal = 'MFS'
GROUP BY id
) m ON m.id = a.id
WHERE pay.flag IN ('D')
AND a.amountBalance > 0
AND a.FacilityCode IN (
'PHKY'
,'QANM'
,'QAOH'
,'QBCA'
,'QBIL'
,'QBTX'
,'QCIL'
,'QDAL'
,'QEWY'
,'QFAR'
,'QFGA'
,'QGIL'
,'QHAR'
,'QHIL'
,'QHTN'
,'QLAL'
,'QLPA'
,'QMIL'
,'QMNC'
,'QMNM'
,'QMNV'
,'QMOR'
,'QMTN'
,'QMTX'
,'QMUT'
,'QRIL'
,'QRKY'
,'QSPA'
,'QTGA'
,'QTKY'
,'QUIL'
,'QVIL'
,'QWIL'
,'LHFL'
)
AND a.amountBalance >= '1000'
AND a.accountStatus IN (
'PA'
,'PP'
,'BPA'
)
AND a.flag = 'A'
AND a.accountType IN (
'Resid'
,'Slfpy'
)
AND NOT EXISTS (
SELECT NULL
FROM vLastPmtDate
WHERE v.id = a.id
AND CAST(v.LastLinkARpmtDate AS DATE) > CAST(dateadd(day, - 45, getdate() AS DATE))
)
AND accountNum = '40728597'
AND LastLinkARpmtDate BETWEEN #minDate
AND #maxDate;
The function dateadd() is what you need:
DATEADD (datepart , number , date)
Set #maxDate = DATEADD(DAY, -45, GETDATE());

GROUP BY & SUM of values with missing MONTHS

I have gone through a lot of examples and joined couple of them in order to come down to the following statement;
DECLARE #StartDate SMALLDATETIME, #EndDate SMALLDATETIME;
SELECT #StartDate = '20170930', #EndDate = '20180930';
;WITH d(d) AS
(
SELECT DATEADD(MONTH, n, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0))
FROM ( SELECT TOP (DATEDIFF(MONTH, #StartDate, #EndDate) + 1)
n = ROW_NUMBER() OVER (ORDER BY [object_id]) - 1
FROM sys.all_objects ORDER BY [object_id] ) AS n
)
SELECT
[Period] = CONVERT(VARCHAR(4), YEAR(d.d)) + '-' + CONVERT(VARCHAR(2), MONTH(d.d)),
QtyTotal = ISNULL(SUM(o.QEXIT),0)
FROM d LEFT OUTER JOIN VE_STOCKTRANS AS o
ON o.TRANSDATE >= d.d
AND o.TRANSDATE < DATEADD(MONTH, 1, d.d)
WHERE STOCKID = 6000 AND TRANSTYPE = 3553
GROUP BY d.d
ORDER BY d.d;
I need to get the total sales quaantity of an item for the past year. If the item does not have any sales for that particular month, 0 should be displayed next to that month. The above query does what is required unless the WHERE clause is provided. As soon as I add the WHERE clause to get the data for a specific product, the months with no sales dissappears.
I would be grateful if an experienced SQL developer can show me the right direction on this.
Thanks
You need to move condtition to ON:
-- ...
SELECT
[Period] = CONVERT(VARCHAR(4),YEAR(d.d)) +'-'+ CONVERT(VARCHAR(2), MONTH(d.d)),
QtyTotal = ISNULL(SUM(o.QEXIT),0)
FROM d LEFT OUTER JOIN VE_STOCKTRANS AS o
ON o.TRANSDATE >= d.d
AND o.TRANSDATE < DATEADD(MONTH, 1, d.d)
AND STOCKID = 6000 AND TRANSTYPE = 3553 -- here
GROUP BY d.d
ORDER BY d.d;
A more generic approach is to apply the filter before you join.
;WITH d(d) AS
(
SELECT DATEADD(MONTH, n, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0))
FROM ( SELECT TOP (DATEDIFF(MONTH, #StartDate, #EndDate) + 1)
n = ROW_NUMBER() OVER (ORDER BY [object_id]) - 1
FROM sys.all_objects ORDER BY [object_id] ) AS n
),
o AS
(
SELECT *
FROM VE_STOCKTRANS
WHERE STOCKID = 6000
AND TRANSTYPE = 3553
)
SELECT
[Period] = CONVERT(VARCHAR(4), YEAR(d.d)) + '-' + CONVERT(VARCHAR(2), MONTH(d.d)),
QtyTotal = ISNULL(SUM(o.QEXIT),0)
FROM
d
LEFT OUTER JOIN
o
ON o.TRANSDATE >= d.d
AND o.TRANSDATE < DATEADD(MONTH, 1, d.d)
GROUP BY
d.d
ORDER BY
d.d;
It's not strictly necessary here, as you've seen in the other answer. When doing FULL OUTER JOIN or other complex queries, however, it can be extremely helpful to filter your sources in one scope and join in a separate scope.
(I always filter my sources, I hate lumpy ketchup.)

Sql Sales Report

I am trying to make a report for sales. This is what I have so far but I need to break it down into stores. There is a field in the orders table for storeid. I would like to return the totals for each store. Can anyone point me in the right direction for this?
select DAY(d.date) as Day, MONTH(d.date) as Month, YEAR(d.date) as year, isnull(t.amnt, 0) as [Total Sales] from (
SELECT
sum(o.OrderTotal) amnt,
DAY(o.OrderDate) as 'Day',
YEAR(o.OrderDate) as 'Year',
MONTH(o.OrderDate) as 'Month'
FROM [Orders] o
where
o.OrderDate > #StartDate + ' 00:00:00' and o.OrderDate < '' + #EndDate + ' 23:59:59'
group by YEAR(o.OrderDate), Month(o.OrderDate), DAY(o.OrderDate)
) t
right join (
select dateadd(dd, -number, getdate()) as date
from master.dbo.spt_values
where type = 'p' and number < DATEDIFF(d, #StartDate, #EndDate) +1
) d on year(d.date) = t.[year] and month(d.date) = t.[month] and day(d.date) = t.[day]
order by YEAR(d.date), MONTH(d.date), DAY(d.date)
Add storeId to the GROUP BY clause and SELECT clause.
select DAY(d.date) as Day, MONTH(d.date) as Month, YEAR(d.date) as year, isnull(t.amnt, 0) as [Total Sales], o.storeId from (
SELECT
sum(o.OrderTotal) amnt,
DAY(o.OrderDate) as 'Day',
YEAR(o.OrderDate) as 'Year',
MONTH(o.OrderDate) as 'Month'
FROM [Orders] o
where
o.OrderDate > #StartDate + ' 00:00:00' and o.OrderDate < '' + #EndDate + ' 23:59:59'
group by YEAR(o.OrderDate), Month(o.OrderDate), DAY(o.OrderDate), o.storeId
) t
right join (
select dateadd(dd, -number, getdate()) as date
from master.dbo.spt_values
where type = 'p' and number < DATEDIFF(d, #StartDate, #EndDate) +1
) d on year(d.date) = t.[year] and month(d.date) = t.[month] and day(d.date) = t.[day]
order by YEAR(d.date), MONTH(d.date), DAY(d.date)

Group dates by week?

I'm trying to group a series of dates by week. So far I have the following:
SELECT DATEPART(week, CONVERT(VARCHAR(50), e.event_date, 107)) AS 'Date' ,
c.setting_secondary AS 'Workflow Cat' ,
d.setting_main AS 'Error Type' ,
SUM(e.event_count) AS 'Total'
FROM marlin.support_events AS e
INNER JOIN marlin.support_config AS c
ON e.event_category = c.setting_code
AND config_code = 60
INNER JOIN marlin.support_config AS d
ON e.event_type = d.setting_code
AND d.config_code = 70
WHERE e.event_date BETWEEN DATEADD(MONTH, -2, GETDATE()) AND GETDATE()
AND c.setting_secondary = 'Expenditure Voucher'
AND d.setting_main IN ( 'Unstarted' , 'Timeout' )
GROUP BY
DATEPART(week, CONVERT(VARCHAR(50), e.event_date, 107)) ,
c.setting_secondary ,
d.setting_main ,
e.event_summary
This shows me the week number but not the date that week started within, like so:
How can I show what date this week begins with?
Answer:
Answer identified below and an alternate method I also found for doing this:
DATEADD(dd, -(DATEPART(dw, e.event_date)-1), e.event_date)
You can get the year part from the date, append the first day of first month and then add the (#week - 1) to get the starting day of the week the event_date belongs to, as follows:
SELECT EventDate, WorkflowCat, ErrorType, SUM(EventCount) AS 'Total'
FROM
(
SELECT DATEADD(ww,
DATEPART(ww, e.event_date) - 1,
CONVERT(DATETIME,
CONVERT(VARCHAR(4), DATEPART(yy, e.event_date)) + '-01-01')) AS 'EventDate' ,
c.setting_secondary AS 'WorkflowCat' ,
d.setting_main AS 'ErrorType',
e.event_summary as 'EventSummary'
e.event_count AS 'EventCount'
FROM marlin.support_events AS e
INNER JOIN marlin.support_config AS c
ON e.event_category = c.setting_code
AND config_code = 60
INNER JOIN marlin.support_config AS d
ON e.event_type = d.setting_code
AND d.config_code = 70
WHERE e.event_date BETWEEN DATEADD(MONTH, -2, GETDATE()) AND GETDATE()
AND c.setting_secondary = 'Expenditure Voucher'
AND d.setting_main IN ( 'Unstarted' , 'Timeout' )
)
GROUP BY EventDate, WorkflowCat, ErrorType, EventSummary

How can I merge two rows in a result but not all results?

I have the following query:
-- Compare current period to historical data
select Name ,
avg(TimeProcessing + TimeRendering + TimeDataRetrieval) / 1000 as 'Current Month' ,
isnull(count(TimeProcessing), 0) as 'Sample' ,
min(l2.[Avg_Exec_Time_Previous_Month]) as 'Previous Month' ,
isnull(min(l2.[Executions_Last_Month]), 0) as 'Sample' ,
min(l3.[Avg_Exec_Time_Two_Months_Ago]) as 'Two Months ago' ,
isnull(min(l3.[Executions_Two_Months_Ago]), 0) as 'Sample'
from marlin.report_execution_log l
inner join marlin.report_catalog c on l.ReportID = c.ItemID
left outer join (
select
l2.ReportID ,
(
avg(l2.TimeProcessing + l2.TimeRendering
+ l2.TimeDataRetrieval) / 1000
) as 'Avg_Exec_Time_Previous_Month' ,
count(l2.TimeProcessing) as 'Executions_Last_Month'
from
marlin.report_execution_log l2
where
TimeEnd between dateadd(MONTH, -2, getdate())
and dateadd(MONTH, -1, getdate())
group by
l2.ReportID
) l2 on l.ReportID = l2.ReportID
left outer join (
select
l3.ReportID ,
(
avg(l3.TimeProcessing + l3.TimeRendering + l3.TimeDataRetrieval) / 1000
) as 'Avg_Exec_Time_Two_Months_Ago' ,
count(l3.TimeProcessing) as 'Executions_Two_Months_Ago'
from
marlin.report_execution_log l3
where
TimeEnd between dateadd(MONTH, -3, getdate())
and dateadd(MONTH, -2, getdate())
group by
l3.ReportID
) l3 on l.ReportID = l3.ReportID
group by l.ReportID ,
Name
order by 2 desc
Which brings up the following results:
Unfortunately one our reports changed names throughout the month and subsequently I need to merge these two rows. Is this possible? How can I merge two rows? For example, how could I have the first and second row show additive results using the first rows report name?
If I understood it well you just need a case statement in your select and in your group by.. something like
select case when Name = 'Project1' then 'Project1'
when Name = 'Project2' then 'Project1'
else Name
end as NAME
.......
group by case when Name = 'Project1' then 'Project1'
when Name = 'Project2' then 'Project1'
else Name
end
if your case is that now is Project 1 and one month ago was Project 2, you may need to add the date in the case statement (just in case)
select case when Name = 'Project1' and TimeEnd = getdate() then 'Project1'
when Name = 'Project2' and TimeEnd = dateadd(MONTH, -1, getdate()) then 'Project1'
else Name
end as NAME
.......
group by case when Name = 'Project1' and TimeEnd = getdate() then 'Project1'
when Name = 'Project2' and TimeEnd = dateadd(MONTH, -1, getdate()) then 'Project1'
else Name
end
That's the idea.
Edit.
I think you have an option if they get repeated but I dont really like it at all
SELECT NAME, AVG(Current Month) as Current Month, count(Sample) as Sample, min(Previous Month) as Previous Month, min(Sample2) as Sample2, min(Two Months ago) as Two Months ago,
min(Sample3) as Sample3
FROM
(
select Name ,
avg(TimeProcessing + TimeRendering + TimeDataRetrieval) / 1000 as 'Current Month' ,
isnull(count(TimeProcessing), 0) as 'Sample' ,
min(l2.[Avg_Exec_Time_Previous_Month]) as 'Previous Month' ,
isnull(min(l2.[Executions_Last_Month]), 0) as 'Sample2' ,
min(l3.[Avg_Exec_Time_Two_Months_Ago]) as 'Two Months ago' ,
isnull(min(l3.[Executions_Two_Months_Ago]), 0) as 'Sample3'
from marlin.report_execution_log l
inner join marlin.report_catalog c on l.ReportID = c.ItemID
left outer join (
select
l2.ReportID ,
(
avg(l2.TimeProcessing + l2.TimeRendering
+ l2.TimeDataRetrieval) / 1000
) as 'Avg_Exec_Time_Previous_Month' ,
count(l2.TimeProcessing) as 'Executions_Last_Month'
from
marlin.report_execution_log l2
where
TimeEnd between dateadd(MONTH, -2, getdate())
and dateadd(MONTH, -1, getdate())
group by
l2.ReportID
) l2 on l.ReportID = l2.ReportID
left outer join (
select
l3.ReportID ,
(
avg(l3.TimeProcessing + l3.TimeRendering + l3.TimeDataRetrieval) / 1000
) as 'Avg_Exec_Time_Two_Months_Ago' ,
count(l3.TimeProcessing) as 'Executions_Two_Months_Ago'
from
marlin.report_execution_log l3
where
TimeEnd between dateadd(MONTH, -3, getdate())
and dateadd(MONTH, -2, getdate())
group by
l3.ReportID
) l3 on l.ReportID = l3.ReportID
group by l.ReportID ,
Name
)
group by Name
order by 2 desc