SQL Query for Month wise count - sql-server

I have table as below for package and its monthly count, like..
Package1 - 1 dollor per search - 50 searches (PER MONTH) included in package and extra search FOR MONTH will be counted separately.
I am passing data in my table like..
User package table has user's package information with its detail.
UserpackageId Userid PackageId SearchPerMonth StartDatePackage EndDatePackage
1 1 1 50 25/02/2012 25/02/2013 (1 YEAR)
Detail table
Userpackageid SearchDate SearchCost
1 26/02/2012 1 Dollor
Now I want to summarize data like
monthly package search count - as I have search per month 50 for that package.. and if extra count happens over 50 than It will show other count for extra IN MONTHLY WISE..

I agree with #Smarty that your question is pretty tough to read so I'll have to make some assumptions on your requirements.
See if this help you.
create table UserPackage(ID int identity(1,1), UserID int, PackageID int,
SearchPerMonth int, StartDate datetime, EndDate datetime)
create table Detail(ID int identity(1,1), UserPackageID int, SearchDate datetime,Cost money)
insert into UserPackage(UserID, PackageID, SearchPerMonth, StartDate, EndDate)
values(1,1,3,'2/15/12','2/15/13')
insert into Detail(UserPackageID,SearchDate,Cost) values(1,'2/16/12',1)
insert into Detail(UserPackageID,SearchDate,Cost) values(1,'2/17/12',2)
insert into Detail(UserPackageID,SearchDate,Cost) values(1,'2/18/12',3)
insert into Detail(UserPackageID,SearchDate,Cost) values(1,'2/19/12',4)
insert into Detail(UserPackageID,SearchDate,Cost) values(1,'2/20/12',5)
select
UserPackageID,
CostInLimit=sum(CostInLimit),
CostAboveLimit=sum(CostAboveLimit)
from
(
select
z.UserPackageID,
z.DetailID,
IsInLimit=case when z.RankInMonth > up.SearchPerMonth then 1 else 0 end,
CostInLimit=case when z.RankInMonth > up.SearchPerMonth then Cost else 0 end,
CostAboveLimit=case when z.RankInMonth > up.SearchPerMonth then 0 else Cost end,
z.Cost,
z.RankInMonth,
z.SearchDate
from
UserPackage up
inner join
(
select
DetailID=ID,
UserPackageID,
Cost,
RankInMonth=RANK() OVER (PARTITION BY UserPackageID ORDER BY SearchDate),
SearchDate
from
Detail
) z on z.UserPackageID = up.ID
) y
where
y.SearchDate between '2/15/12' and '3/15/12'
group by
UserPackageID][1]
Here's the result from the outer select:
Here's the result from the inner select (table y by itself):

Related

dynamic SQL - Allocate count of midnights to correct month

This is a fairly common issue that I've seen a lot of people duck tape together solutions for, but it's never quite right. Hoping this forum can get it ironed out. I have a table:
create table temp
( PatientID varchar(12),
AdmitDate datetime,
DischargeDate datetime
)
insert into temp values ('Patient1','1/30/2020 13:23:44', '2/2/2020 15:12:52')
What I'd like to count is the number of midnights the patient was admitted in the correct month. So in the example above the patient would be admitted at midnight on 1/31, 2/1, and 2/2 dates. So my output in sql should look something like:
01-2020 02-2020
------- --------
1 2
I know it has to be dynamic SQL, because the columns need to be created with respect to the date range queried. Although, I'm pretty stumped as to next steps.
create table #temp
( PatientID varchar(12),
AdmitDate datetime,
DischargeDate datetime
)
insert into #temp values ('Patient1','1/30/2020 13:23:44', '2/2/2020 15:12:52')
--Virtually creates a dates table
;with dates(thedate) as (
select dateadd(yy,years.number,0)+days.number
from master..spt_values years
join master..spt_values days
on days.type='P' and days.number < datepart(dy,dateadd(yy,years.number+1,0)-1)
where years.type='P' and years.number between 100 and 150
-- note: 100-150 creates dates in the year range 2000-2050
-- adjust as required
)
select dateadd(m,datediff(m, 0, d.thedate),0) [Month], count(1) PatientDays
from dates d
join #temp t on d.thedate between t.[AdmitDate] and t.[DischargeDate]
group by datediff(m, 0, d.thedate)
order by [Month];

Repeat last value of a column, when it is empty -SSRS

Can we repeat last value of a column in SSRS? As in attachment, all blank rows in
the last column should be filled with the latest value 702
I used Previous, Last functions but nothing helped
That's achievable if you do this:
Step 1. For your source, you build a sql query where you group the data by Year, AbsoluteMonth, etc.
So for each Year / AbsoluteMonth pair the report has only ONE value.
Step 2. Use below formula:
=IIf(IsNothing(Sum(Fields!Amt.Value)), Last(Fields!Amt.Value, "Year"), Sum(Fields!Amt.Value))
Here "Year" is group name, and Amt - your field name, which is probably R_Pax
Step3. (optional) Sort the data if it's not naturally sorted to provide the correct last value.
Step 1 is very important. Otherwise the cell with empty value will not show the last total, it will show the last value for a month, so if month (1) has values 30, 50, 60, and month (2) doesn't have any values, then it will show 60 for month(2), month(3), etc..., not sum(30+50+60).
You better insert the remaining blank records with last value into your dataset before pass the data to report.I assume your table is matrix.
DECLARE #Today DATETIME
SET #Today = GETDATE()
DECLARE #MatrixData TABLE (
Month1 INT
, Year1 INT
, Value INT
)
INSERT INTO #MatrixData (Month1, Year1, Value)
SELECT MONTH(DATEADD(MONTH, Id * -1, #Today)) AS Date1Month, YEAR(DATEADD(MONTH, Id * -1, #Today)) AS Date1Year, Id * 10 AS Value1
FROM (
SELECT TOP 60 ROW_NUMBER() OVER (ORDER BY Id) AS Id
FROM SysObjects
) A
ORDER BY Date1Year, Date1Month
SELECT * FROM #MatrixData
-- Insert blank month of last year with last value
INSERT INTO #MatrixData (Month1, Year1, Value)
SELECT A.RunningMonth, A1.MaxYear, A1.LastValue
FROM (
SELECT TOP 12 ROW_NUMBER() OVER (ORDER BY Id) AS RunningMonth
FROM SysObjects
) A
INNER JOIN (
-- Get Last Value in #MatrixData
SELECT A.MinMonth, A.MaxMonth, A.MaxYear, A1.Value AS LastValue
FROM (
-- Get Max Month Last Year in #MatrixData
SELECT MAX(A1.Month1) AS MinMonth, A.MaxMonth, A.MaxYear
FROM (
-- Get Max Month & Max Year
SELECT MAX(Month1) AS MaxMonth, MAX(Year1) AS MaxYear
FROM #MatrixData
) A
INNER JOIN #MatrixData A1 ON A.MaxYear = A1.Year1
GROUP BY A.MaxMonth, A.MaxYear
) A
INNER JOIN #MatrixData A1 ON A.MinMonth = A1.Month1 AND A.MaxYear = A1.Year1
) A1 ON A.RunningMonth > A1.MinMonth AND A.RunningMonth <= A1.MaxMonth
SELECT * FROM #MatrixData
We can do it at SQL end and fetch data to SSRS
Steps:
Do pivot if needed
Get the data at granularity column. Here it is Absolute Month
Then use the SQL method to replcae the Nulls/ last values which are empty with the last highest value
Ref:
`select a.AbsoluteMonth,Mon
,first_value(a.S1_pax)over(partition by a.v1_p order by num ) as S_Pax
,first_value(a.S2_pax)over(partition by a.v2_p order by num ) as S2_Pax`
from
(select *
,sum(case when S1_pax is null then 0 else 1 end) over (order by num) as v1_p
,sum(case when S2_pax is null then 0 else 1 end) over (order by num) as v2_p
from X_Table
)a
And fill all places respectively. Plz refer below output
In Oracle it is done like this. SQL Server has both COALESCE and LAG functions. So this must be possible with SQL Server also. There is also another stackoverflow question similar to this. Just could not locate it.
create table mytab(n number, m number);
insert into mytab values(1,null);
insert into mytab values(2,null);
insert into mytab values(3,44949);
insert into mytab values(4,null);
insert into mytab values(5,null);
insert into mytab values(6,null);
insert into mytab values(7,null);
insert into mytab values(8,null);
insert into mytab values(9,null);
insert into mytab values(10,null);
insert into mytab values(11,74631);
insert into mytab values(12,null);
insert into mytab values(13,null);
select t.*, coalesce(m, lag(m ignore nulls) over (order by n))
from mytab t;

SQL stored procedure for picking a random sample based on multiple criteria

I am new to SQL. I looked for all over the internet for a solution that matches the problem I have but I couldn't find any. I have a table named 'tblItemReviewItems' in an SQL server 2012.
tblItemReviewItems
Information:
1. ItemReviewId column is the PK.
2. Deleted column will have only "Yes" and "No" value.
3. Audited column will have only "Yes" and "No" value.
I want to create a stored procedure to do the followings:
Pick a random sample of 10% of all ItemReviewId for distinct 'UserId' and distinct 'ReviewDate' in a given date range. 10% sample should include- 5% of the total population from Deleted (No) and 5% of the total population from Deleted (Yes). Audited ="Yes" will be excluded from the sample.
For example – A user has 118 records. Out of the 118 records, 17 records have Deleted column value "No" and 101 records have Deleted column value "Yes". We need to pick a random sample of 12 records. Out of those 12 records, 6 should have Deleted column value "No" and 6 should have Deleted column value "Yes".
Update Audited column value to "Check" for the picked sample.
How can I achieve this?
This is the stored procedure I used to pick a sample of 5% of Deleted column value "No" and 5% of Deleted column value "Yes". Now the situation is different.
ALTER PROC [dbo].[spItemReviewQcPickSample]
(
#StartDate Datetime
,#EndDate Datetime
)
AS
BEGIN
WITH CTE
AS (SELECT ItemReviewId
,100.0
*row_number() OVER(PARTITION BY UserId
,ReviewDate
,Deleted
order by newid()
)
/count(*) OVER(PARTITION BY UserId
,Reviewdate
,Deleted
)
AS pct
FROM tblItemReviewItems
WHERE ReviewDate BETWEEN #StartDate AND #EndDate
AND Deleted in ('Yes','No')
AND Audited='No'
)
SELECT a.*
FROM tblItemReviewItems AS a
INNER JOIN cte AS b
ON b.ItemReviewId=a.ItemReviewId
AND b.pct<=6
;
WITH CTE
AS (SELECT ItemReviewId
,100.00
*row_number() OVER(PARTITION BY UserId
,ReviewDate
,Deleted
ORDER BY newid()
)
/COUNT(*) OVER(PARTITION BY UserId
,Reviewdate
,Deleted
)
AS pct
FROM tblItemReviewItems
WHERE ReviewDate BETWEEN #StartDate AND #EndDate
AND deleted IN ('Yes','No')
AND audited='No'
)
UPDATE a
SET Audited='Check'
FROM tblItemReviewItems AS a
INNER JOIN cte AS b
ON b.ItemReviewId=a.ItemReviewId
AND b.pct<=6
;
END
Any help would be highly appreciated. Thanks in advance.
This may assist you in getting started. My idea is, you create the temp tables you need, and load the specific data into the (deleted, not deleted etc.). You then run something along the lines of:
IF OBJECT_ID('tempdb..#tmpTest') IS NOT NULL DROP TABLE #tmpTest
GO
CREATE TABLE #tmpTest
(
ID INT ,
Random_Order INT
)
INSERT INTO #tmpTest
(
ID
)
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10 UNION ALL
SELECT 11 UNION ALL
SELECT 12 UNION ALL
SELECT 13 UNION ALL
SELECT 14 UNION ALL
SELECT 15 UNION ALL
SELECT 16;
DECLARE #intMinID INT ,
#intMaxID INT;
SELECT #intMinID = MIN(ID)
FROM #tmpTest;
SELECT #intMaxID = MAX(ID)
FROM #tmpTest;
WHILE #intMinID <= #intMaxID
BEGIN
UPDATE #tmpTest
SET Random_Order = 10 + CONVERT(INT, (30-10+1)*RAND())
WHERE ID = #intMinID;
SELECT #intMinID = #intMinID + 1;
END
SELECT TOP 5 *
FROM #tmpTest
ORDER BY Random_Order;
This assigns a random number to a column, that you then use in conjunction with a TOP 5 clause, to get a random top 5 selection.
Appreciate a loop may not be efficient, but you may be able to update to a random number without it, and the same principle could be implemented. Hope that gives you some ideas.

Clone a void refund transaction record to cancel each other out

Account_ID Amount
123 200
Result
Account_ID Amount
123 200
123 -200
Typically, our database will have two transactions for a void refund payment, but somehow few records only have one transaction.
I know I can manually insert a same record into the table.
Is there any other ways to clone a record and set the amount to negative without using insert statment?
Even though as M.Ali said it is not good to clone record but we can achieve but i didn't exactly know if it suits your requirement or not
DECLARE #T TABLE
([Account_ID] int, [Amount] int)
;
INSERT INTO #T
([Account_ID], [Amount])
VALUES
(123, 200)
;
;WITH CTE AS (select Account_ID,Amount,row_number()OVER(PARTITION BY Amount ORDER BY (Select NULL))RN from #T
CROSS APPLY(values('Account_ID',Account_ID),('Amount',Amount))M(v,s))
Select Account_ID,
CASE WHEN RN = 1 THEN cast(Amount as varchar) ELSE
'-' + cast(Amount as varchar)END
from CTE

SQL Newbie Needs Assistance w/ Query

Below is what I am trying to do in SQL Server 2012. I want to Update Table 2 with the % of total that each AMT value is to the total in Table 1. But the denominator to get the % should only be the total of the rows that have the same MasterDept. I can use this SELECT query to get the correct percentages when I load the table with only one MasterDept but do not know how to do it when there are multiple MasterDept. The first 2 columns in each table are identical both in structure and the data within the columns.
SELECT ABCID,
[AMT%] = ClientSpreadData.AMT/CONVERT(DECIMAL(16,4),(SELECT SUM(ClientSpreadData.AMT)
FROM ClientSpreadData))
FROM ClientSpreadData
Table data
TABLE 1 (MasterDept varchar(4), ABCID varchar(20), AMT INT)
Sample Data (4700, 1, 25),
(4300, 2, 30),
(4700, 3, 50),
(4300, 4, 15)
TABLE 2 (MasterDept varchar(4), ABCID varchar(20), [AMT%] INT)
Sample Data (4700, 1, AMT%)
AMT% should equal AMT / SUM(AMT). SUM(AMT) should only be summing the values where the MasterDept on Table 1 matches the MasterDept from the record on Table 2.
Does that make sense?
You can use a window to get a partitioned SUM():
SELECT MasterDept, ABCID, AMT, SUM(AMT) OVER(PARTITION BY MasterDept)
FROM #Table1
You can use that to get the percentage for each row to update your second table (this assumes 1 row per MasterDept/ABCID combination):
UPDATE A
SET A.[AMT%] = B.[AMT%]
FROM Table2 A
JOIN (SELECT MasterDept
, ABCID
, AMT
, CASE WHEN SUM(AMT) OVER(PARTITION BY MasterDept) = 0 THEN 0
ELSE AMT*1.0/SUM(AMT) OVER(PARTITION BY MasterDept)
END 'AMT%'
FROM #Table1
) B
ON A.MasterDept = B.MasterDept
AND A.ABCID = B.ABCID
As you can see in the subquery, a percent of total can be added to your Table1, so perhaps you don't even need Table2 as it's a bit redundant.
Update: You can use a CASE statement to handle a SUM() of 0.

Resources