I have a data set that contains the following items:
Month-Yr
Account Nbr
Revenue Total
If an account does not have a revenue total for a given month there is no record for that account / month.
I would like to know per account - what is the maximum # of consecutive months that account had no revenue for?
For example if account 123 had revenue in every month in the last 12 their count would be 0.
If account 987 had revenue for 4 consecutive months then no revenue for 5 consecutive months and then revenue for the remaining months their count would be 5.
If account 657 had activity every other month then their count would be 1, b/c 1 is the max # of consecutive months with no activity.
Thanks in advance
SAMPLE DATA
**Accnt** |**Month-Yr** |**Revenue**
123 |8/1/2016 |1000
123 |9/1/2016 |1500
123 |10/1/2016 |500
123 |11/1/2016 |100
123 |12/1/2016 |250
123 |1/1/2017 |750
123 |2/1/2017 |900
123 |3/1/2017 |700
123 |4/1/2017 |1200
123 |5/1/2017 |250
123 |6/1/2017 |750
123 |7/1/2017 |900
123 |8/1/2017 |700
123 |9/1/2017 |1200
987 |8/1/2016 |100
987 |9/1/2016 |250
987 |10/1/2016 |750
987 |11/1/2016 |900
987 |5/1/2017 |700
987 |6/1/2017 |1300
987 |7/1/2017 |250
987 |8/1/2017 |750
987 |9/1/2017 |900
657 |8/1/2016 |700
657 |10/1/2016 |1200
657 |12/1/2016 |100
657 |2/1/2017 |250
657 |4/1/2017 |750
657 |6/1/2017 |900
657 |8/1/2017 |700
Accnt 123 would be 0 months
Accnt 987 would be 5 months
Accnt 657 would be 1 month
How about this one? (Because you posted minimal info, I reserve the right to create my own DDL as I interpret).
IF OBJECT_ID ('tempdb..#') IS NOT NULL
DROP TABLE #sales
GO
CREATE TABLE #sales
(Dte datetime
,amt int
,id int
,rownum int identity PRIMARY KEY
)
INSERT INTO #sales
SELECT '9/1/17',1200, 987
INSERT INTO #sales
SELECT '10/1/17',1100, 987
INSERT INTO #sales
SELECT '11/1/17',1000, 987
INSERT INTO #sales
SELECT '5/1/18',1900, 987
INSERT INTO #sales
SELECT '6/1/18',120, 987
INSERT INTO #sales
SELECT '4/1/17',100, 657
INSERT INTO #sales
SELECT '5/1/17',190, 657
INSERT INTO #sales
SELECT '7/1/17',12, 657
GO
SELECT MAX(DATEDIFF(dd,a.dte,b.dte)), a.id
FROM #sales a
INNER JOIN #sales b
ON a.rownum = b.rownum-1
AND a.id =b.id
GROUP BY a.id
Related
Below is my table:
id order_number order_date order_details
---------------------------------------------
1 222 01-01-2020 44
2 222 02-01-2020 66
3 222 03-01-2020 20
4 223 03-01-2020 33
5 224 04-01-2020 55
6 225 02-01-2020 77
I want to have sum(order_details) where order_number = 222
like this table
order_date sum_order_details
----------------------------------
01-01-2020 130
02-01-2020 130
03-01-2020 130
I tried the below but it doesn't work
select order_number , order_date , sum(order_details) sum_orders from ex
group by order_number
having order_number = 222
It seems like you're after this is a windowed SUM:
SELECT order_date,
SUM(order_details) OVER () AS sum_orders
FROM YourTable
WHERE order_number = 222;
I have a Employee Wages table like this, with their EmpID and their wages.
EmpId | Wages
================
101 | 1280
102 | 1600
103 | 1400
104 | 1401
105 | 1430
106 | 1300
I need to write a Stored Procedure in SQL Server, to group the Employees according to their wages, such that similar salaried people are in groups together and the deviations within the group is as minimum as possible.
There are no other conditions or rules mentioned.
The output should look like this
EmpId | Wages | Group
=======================
101 | 1280 | 1
106 | 1300 | 1
103 | 1400 | 2
104 | 1401 | 2
105 | 1430 | 2
102 | 1600 | 3
You can use a query like the following:
SELECT EmpId, Wages,
DENSE_RANK() OVER (ORDER BY CAST(Wages - t.min_wage AS INT) / 100) AS grp
FROM mytable
CROSS JOIN (SELECT MIN(Wages) AS min_wage FROM mytable) AS t
The query calculates the distance of each wage from the minimum wage and then uses integer division by 100 in order to place records in slices. So all records that have a deviation that is between 0 - 99 off the minimum wage are placed in the first slice. The second slice contains records off by 100 - 199 from the minimum wage, etc.
You can for +-30 deviation as the below:
DECLARE #Tbl TABLE (EmpId INT, Wages INT)
INSERT INTO #Tbl
VALUES
(99, 99),
(100, 101),
(101, 1280),
(102, 1600),
(103, 1400),
(104, 1401),
(105, 1430),
(106, 1300)
;WITH CTE AS ( SELECT *, ROW_NUMBER() OVER (ORDER BY Wages) AS RowId FROM #Tbl )
SELECT
A.EmpId ,
A.Wages ,
DENSE_RANK() OVER (ORDER BY MIN(B.RowId)) [Group]
FROM
CTE A CROSS JOIN CTE B
WHERE
ABS(B.Wages - A.Wages) BETWEEN 0 AND 30 -- Here +-30
GROUP BY A.EmpId, A.Wages
ORDER BY A.Wages
Result:
EmpId Wages Group
----------- ----------- --------------------
99 99 1
100 101 1
101 1280 2
106 1300 2
103 1400 3
104 1401 3
105 1430 3
102 1600 4
Can you PLEASE help me on following?
We have payroll software that runs on SQL Server. I have to update certain payroll category from the SQL Server so that it can reflect on the software.
This is my Excel file:
Employee Number Payroll Category Rate
------------------------------------------
111111 011 32.21
111111 012 56.23
111111 013 12.52
111111 021 45.21
111112 011 36.21
111112 012 56.23
111112 013 42.54
111112 021 85.21
These are the current values in my database table Masterpaycard
Employee Number Payroll Category Rate
-------------------------------------------
111111 011 0.00
111111 012 0.00
111111 013 10.25
111112 011 36.21
111112 012 12.50
111112 013 41.25
111112 021 85.21
So if you see following record is not present in the database, but present in the .CSV, then I have to insert it.
111111 021 45.21
Here Employee Number and Payroll Category are FKs from the Employee and Payroll Category tables.
So my final results should look like in the database and in the front end something like this.
Employee Number Payroll Category Rate
--------------------------------------------
111111 011 32.21
111111 012 56.23
111111 013 12.52
111111 021 45.21
111112 011 36.21
111112 012 56.23
111112 013 42.54
111112 021 85.21
I guess in simple words if payroll category match in MASTERPAYCARD table then just update the category with value from .CSV, and if we can not find Payrollcategory than insert that as new category for that employee and add value too from CSV.
Please help.
This is the type of approach you need to take...
create table #Table1 (
id int,
value varchar(10)
)
create table #Table2 (
id int,
value varchar(10)
)
insert into #Table1 values (1, 'AAA')
insert into #Table1 values (2, 'BBB')
insert into #Table2 values (1, 'ZZZ')
insert into #Table2 values (3, 'CCC')
select * from #Table1
select * from #Table2
--insert data from Table2 into Table1 if it doesn't already exist in Table1
insert into #Table1
select #Table2.* from #Table2
left join #Table1 on #Table2.id = #Table1.id
where #Table1.id is null
--update data in Table1 from Table2 if it does already exist in Table1
update #Table1 set value = #Table2.value
from #Table2
left join #Table1 on #Table2.id = #Table1.id
where #Table1.id is not null
select * from #Table1
I am trying to retrieve the most recent entry of a record in an Access report. The query I have gives me the results in SQL-Server but row_number is not compatible with Access. Its been suggested that I use the max function in Access. Can you assist me in generating this report?
SELECT cID, CName, Address, Project#, JobOwner, SubStatusID, Status, JNJobID, JNNote
FROM (
SELECT
cID, CName, Address, Project#, JobOwner, SubStatusID, Status, JNJobID, JNNote
, ROW_NUMBER() OVER (PARTITION BY JNJobID ORDER BY JNDate DESC) AS r
FROM [JobNotes]
Left JOIN Jobs ON [JobNotes].JNJobID = Jobs.JobID
Left JOIN Addresses ON Jobs.JobAddressID = Addresses.AddressID
Left JOIN Customers ON Jobs.JobCustomerID = Customers.CID
Left JOIN Status ON Jobs.JobSubStatusID = Status.StatusID
) x
WHERE r = 1 and customerID = 134 and jobsubstatusid <> 14 and jobsubstatusid <> 15 and jobsubstatusid <> 16 and jobsubstatusid <> 42 and jobsubstatusid <>38 and jobsubstatusid <>75
Jobs Table
JobID Project# JobOwner JobStatusID AddressID JobCustomerID
6972 PN1 John 1 333 222
6973 PN2 Sarah 3 444 666
6974 PN3 James 6 555 777
Address Table
AddressID Address
333 1333 Janes Ln
444 5555 Davis Blvd
555 888 Post Rd
Customer Table
CID CName
222 Builder
666 HomeOwner
777 HOA
JobNotes Table
JobNotesID JNJobID JNDate JNNote
11800 6972 2016-03-15 00:00:00.000 Example 1
11874 6972 2016-03-17 00:00:00.000 Example 2
12181 6972 2016-03-25 00:00:00.000 Example 3
12006 6973 2016-03-21 00:00:00.000 Example 4
11961 6974 2016-03-18 00:00:00.000 Example 5
11924 6974 2016-03-17 00:00:00.000 Example 6
JobNotes Table
CID CName Address Project# JobOwner SubStatusID Status JNJobID JNNote
222 Builder 1333 Janes Ln PN1 John 1 Sales 6972 Example 3
666 HomeOwner 5555 Davis Blvd PN2 Sarah 3 Design 6973 Example 4
777 HOA 888 Post Rd PN3 James 6 Construction 6974 Example 6
i have a table which has sales at day level
sales_day
loc_id day_id sales
124 2013-01-01 100
124 2013-01-02 120
124 2013-01-03 140
124 2013-01-04 160
124 2013-01-05 180
124 2013-01-06 200
124 2013-01-07 220
there is weekly table which is the aggregate of all the days
loc_id week_id sales
123 201401 1120
Now i need all of the above in table as below
loc_id day_id sales week_sales
124 2013-01-01 100 1120
124 2013-01-02 120 1120
124 2013-01-03 140 1120
124 2013-01-04 160 1120
124 2013-01-05 180 1120
124 2013-01-06 200 1120
124 2013-01-07 220 1120
there are so many loactions and so many weeks,days.
How to get the data exactly without cross join.
Have you tried this:
select loc_id, day_id, sales, week_sales
from table
cross join (
select sum(sales) as week_sales from table
) t
Window analytical function should help you here...
select loc_id,
day_id,
sales,
sum(sales) over(partition by loc_id,date_part('week', day_id)) as week_total_sales
from <table name>
It will sum the sales by location id and the week of the year to give you the total you are looking for.
In your example, 2013-01-07 was included with the other dates, but it isn't actually part of the same calendar week.
It wasn't clear which DBMS you were referring to. The above is for Netezza. For SQL Server etc try changing date_part('week',day_id) to datepart(ww,day_id).