I have a SALES table with Person, Date and Qty:
Person Date Qty
Bob 2016-08-01 5
Bob 2016-08-02 2
Bob 2016-08-03 6
Bob 2016-08-04 4
Jim 2016-08-01 1
Jim 2016-08-02 3
Jim 2016-08-03 2
Jim 2016-08-04 2
Sheila 2016-08-03 9
Sheila 2016-08-04 12
I would like to produce 3 outputs
1) The Daily change in total Qty for the Persons who were selling the prior day:
Date Qty Change Pct Change
2016-08-01 0 0 0.00
2016-08-02 5 -1 -16.66
2016-08-03 8 3 60.00
2016-08-04 18 1 5.88
Note that 8/1/16 is the first day in my dataset, so total = 0 since no SalesPerson was selling the prior day. Also note that Sheila started selling on 8/3, which means here 8/3 sales do not figure into the 8/3 qty or change. However, when determining the 8/4 change, Sheila's 8/3 sales of 9 units are used to determine the correct total change of 1 unite for 8/4.
2) I want to break out the totals for new SalesPersons each day on one line per day. If no new Salespersons are added, then the date would show zeros.
Date New Qty
2016-08-01 6
2016-08-02 0
2016-08-03 9
2016-08-04 0
Since 8/1 was the first day of selling for anyone, both sales for Bob and Jim are included in the New Qty of 6 for 8/1.
3) The final output shows detail for #2, such as the date that a new SalesPerson started selling and the quantity they sold on that day.
Date Person New Qty
2016-08-01 Bob 5
2016-08-01 Jim 1
2016-08-03 Sheila 9
Are these 3 outputs possible in SQL Server?
You can use LAG() if you are using sql server 2012 or above. You got the "change". You can figure out the rest.
Query #1:
SELECT
dt.DateVal,
SUM(CASE WHEN dt.PreviousQuota = 0 THEN 0 ELSE dt.PreviousQuotadd END) Change
FROM
(
SELECT
Id,
Person,
DateVal,
Qty,
LAG(Qty, 1,0) OVER ( PARTITION BY Person ORDER BY DateVal) AS PreviousQuota ,
LEAD(Qty, 1,0) OVER ( PARTITION BY Person ORDER BY DateVal) AS NextQuota ,
LAG(Qty, 1,0) OVER ( PARTITION BY Person ORDER BY DateVal) AS PreviousQuotad ,
(Qty - LAG(Qty, 1,0) OVER ( PARTITION BY Person ORDER BY DateVal)) AS PreviousQuotadd
FROM Table1
) AS dt
Here is the Fiddlle link.
I solved the following queries:
Query #2:
WITH CTE_1 AS
(Select DISTINCT Date FROM [master].[dbo].[Sales] GROUP BY Date)
, CTE_2 AS
(select Person,Date,ROW_NUMBER() OVER(Partition By Person Order By Date) as RowNum, SUM(Qty) as [PersonDailySum]
FROM [master].[dbo].[Sales]
GROUP BY Person,Date)
,CTE_3 AS(
SELECT T0.date, SUm([PersonDailySum]) As [New Qty]
FROM CTE_1 T0
LEFT JOIN CTE_2 T1
ON T0.Date = T1.date AND T1.RowNum = 1
GROUP BY T0.Date)
SELECT Date, ISNULL([New Qty],0) AS [New Qty] FROM CTE_3
Query #3
SELECT Date,Person,SUM([PersonDailySum]) AS [New Qty] FROM (
select Person,Date,ROW_NUMBER() OVER(Partition By Person Order By Date) as RowNum, SUM(Qty) as [PersonDailySum]
FROM [master].[dbo].[Sales]
GROUP BY Person,Date) T0
WHERE T0.RowNum = 1
GROUP BY Date,Person
Related
I have query to SUM and COUNT my table, but i have trouble, i want to SUM column name after COUNT it.
This is my table...
id no_reg name date qty
1 REG01 T-212-BS 2019-05-03 1
2 REG01 T-212-BS 2019-05-03 1
3 REG01 T-212-BS 2019-05-03 1
4 REG01 T-212-BA 2019-05-03 1
5 REG02 T-111-AA 2019-05-04 1
6 REG03 T-111-AB 2019-05-04 1
I create query....
SELECT no_reg, COUNT(DISTINCT name) AS Name_qty, date, SUM(qty) AS qty
FROM part
GROUP BY no_reg, name, date, qty
and result of query after execution...
no_reg Name_qty date qty
REG01 1 2019-05-03 1
REG01 1 2019-05-03 3
REG02 1 2019-05-04 1
REG03 1 2019-05-04 1
But, I want results like this...
no_reg Name_qty date qty
REG01 2 2019-05-03 4
REG02 1 2019-05-04 1
REG03 1 2019-05-04 1
No need to group by name, even if you're using it in your distinct statement.
SELECT no_reg, COUNT(DISTINCT name) AS Name_qty, date, SUM(qty) AS qty
FROM part
GROUP BY no_reg, date
You're grouping by qty, so any rows that do not have the same qty will be aggregated separately. Since qty is used in an aggregate function, you can remove it from the group by and it should give you the expected results
SELECT no_reg, COUNT(DISTINCT name) AS Name_qty, date, SUM(qty) AS qty
FROM part
GROUP BY no_reg, date
EDIT:
I also noticed that name was included in the group by. You can remove it too since it is used in the count aggregate
I have a table with some records about fuel consumption. The important columns in the table are: CONSUME_DATE_FROM and CONSUM_DATE_TO.
I want to calculate average fuel consumption per cars on a monthly basis but some rows are not in the same month. For example some have a three month difference between them and the total of gas per litre is aggregated in a single row.
Now I should find records that have difference more than a month between CONSUME_DATE_FROM and CONSUM_DATE_TO, and duplicate them in current or second table per count of month and divide the total gas per litre between related rows.
I've this table with the following data:
ID VehicleId CONSUME_DATE_FROM CONSUM_DATE_TO GAS_PER_LITER
1 100 2018-10-25 2018-12-01 600
2 101 2018-07-19 2018-07-24 100
3 102 2018-12-31 2019-01-01 400
4 103 2018-03-29 2018-05-29 200
5 104 2018-02-05 2018-02-09 50
The expected output table should be as below
ID VehicleId CONSUME_DATE_FROM CONSUM_DATE_TO GAS_PER_LITER
1 100 2018-10-25 2018-12-01 200
1 100 2018-10-25 2018-12-01 200
1 100 2018-10-25 2018-12-01 200
2 101 2018-07-19 2018-07-24 100
3 102 2018-12-31 2019-01-01 200
3 102 2018-12-31 2019-01-01 200
4 103 2018-03-29 2018-05-29 66.66
4 103 2018-03-29 2018-05-29 66.66
4 103 2018-03-29 2018-05-29 66.66
5 104 2018-02-05 2018-02-09 50
Or as below
ID VehicleId CONSUME_DATE_FROM CONSUM_DATE_TO GAS_PER_LITER DATE_RELOAD_GAS
1 100 2018-10-25 2018-12-01 200 2018-10-01
1 100 2018-10-25 2018-12-01 200 2018-11-01
1 100 2018-10-25 2018-12-01 200 2018-12-01
2 101 2018-07-19 2018-07-24 100 2018-07-01
3 102 2018-12-31 2019-01-01 200 2018-12-01
3 102 2018-12-31 2019-01-01 200 2019-01-01
4 103 2018-03-29 2018-05-29 66.66 2018-03-01
4 103 2018-03-29 2018-05-29 66.66 2018-04-01
4 103 2018-03-29 2018-05-29 66.66 2018-05-01
5 104 2018-02-05 2018-02-09 50 2018-02-01
Can someone please help me out with this query?
I'm using oracle database
Your business rule treats the difference between CONSUME_DATE_FROM and CONSUM_DATE_TO as absolute months. So you expect the difference between 2018-10-25 and 2018-12-01 to be three months whereas the difference in days actually equates to about 1.1 months. So we can't use simple date arithmetic to get your desired output, we need to do some additional massaging of the dates.
The query below implements your desired logic by deriving the first day of the month for CONSUME_DATE_FROM and the last day of the month for CONSUME_DATE_TO, then using ceil() to round the difference up to the nearest whole number of months.
This is calculated in a subquery which is used in the main query with the old connect by level trick to multiply a record by level number of times:
with cte as (
select f.*
, ceil(months_between(last_day(CONSUM_DATE_TO)
, trunc(CONSUME_DATE_FROM,'mm'))) as diff
from fuel_consumption f
)
select cte.id
, cte.VehicleId
, cte.CONSUME_DATE_FROM
, cte.CONSUM_DATE_TO
, cte.GAS_PER_LITER/cte.diff as GAS_PER_LITER
, add_months(trunc(cte.CONSUME_DATE_FROM, 'mm'), level-1) as DATE_RELOAD_GAS
from cte
connect by level <= cte.diff
and prior cte.id = cte.id
and prior sys_guid() is not null
;
"what about if add a additional column "DATE_RELOAD_GAS" that display difference date for similar rows"
From your posted sample it seems like DATE_RELOAD_GAS is the first day of the month for each month bounded by CONSUME_DATE_FROM and CONSUM_DATE_TO. I have amended my solution to implement this rule.
By using connect by level structure with considering to_char(c.CONSUME_DATE_FROM + level - 1,'yyyymm') as month I was able to resolve as below :
select ID, VehicleId, myMonth, CONSUME_DATE_FROM, CONSUM_DATE_TO,
trunc(GAS_PER_LITER/max(rn) over (partition by ID order by ID),2) as GAS_PER_LITER,
'01.'||substr(myMonth,5,2)||'.'||substr(myMonth,1,4) as DATE_RELOAD_GAS
from
(
with consumption( ID, VehicleId, CONSUME_DATE_FROM, CONSUM_DATE_TO, GAS_PER_LITER ) as
(
select 1,100,date'2018-10-25',date'2018-12-01',600 from dual union all
select 2,101,date'2018-07-19',date'2018-07-24',100 from dual union all
select 3,102,date'2018-12-31',date'2019-01-01',400 from dual union all
select 4,103,date'2018-03-29',date'2018-05-29',200 from dual union all
select 5,104,date'2018-02-05',date'2018-02-09', 50 from dual
)
select ID, to_char(c.CONSUME_DATE_FROM + level - 1,'yyyymm') myMonth,
VehicleId, c.CONSUME_DATE_FROM, c.CONSUM_DATE_TO, GAS_PER_LITER,
row_number() over (partition by ID order by ID) as rn
from dual join consumption c
on c.ID >= 2
group by ID, to_char(c.CONSUME_DATE_FROM + level - 1,'yyyymm'), VehicleId,
c.CONSUME_DATE_FROM, c.CONSUM_DATE_TO, c.GAS_PER_LITER
connect by level <= c.CONSUM_DATE_TO - c.CONSUME_DATE_FROM + 1
union all
select ID, to_char(c.CONSUME_DATE_FROM + level - 1,'yyyymm') myMonth,
VehicleId, c.CONSUME_DATE_FROM, c.CONSUM_DATE_TO, GAS_PER_LITER,
row_number() over (partition by ID order by ID) as rn
from dual join consumption c
on c.ID = 1
group by ID, to_char(c.CONSUME_DATE_FROM + level - 1,'yyyymm'), VehicleId,
c.CONSUME_DATE_FROM, c.CONSUM_DATE_TO, c.GAS_PER_LITER
connect by level <= c.CONSUM_DATE_TO - c.CONSUME_DATE_FROM + 1
) q
group by ID, VehicleId, myMonth, CONSUME_DATE_FROM, CONSUM_DATE_TO, GAS_PER_LITER, rn
order by ID, myMonth;
I met an interesting issue that if I consider the join condition in the subquery as c.ID >= 1 query hangs on for huge period of time, so splitted into two parts by union all
as c.ID >= 2 and c.ID = 1
Rextester Demo
I have the following table, I am using SQL Server 2008
BayNo FixDateTime FixType
1 04/05/2015 16:15:00 tyre change
1 12/05/2015 00:15:00 oil change
1 12/05/2015 08:15:00 engine tuning
1 04/05/2016 08:11:00 car tuning
2 13/05/2015 19:30:00 puncture
2 14/05/2015 08:00:00 light repair
2 15/05/2015 10:30:00 super op
2 20/05/2015 12:30:00 wiper change
2 12/05/2016 09:30:00 denting
2 12/05/2016 10:30:00 wiper repair
2 12/06/2016 10:30:00 exhaust repair
4 12/05/2016 05:30:00 stereo unlock
4 17/05/2016 15:05:00 door handle repair
on any given day need do find the highest number of fixes made on a given bay number, and if that calculated number is repeated then it should also appear in the resultset
so would like to see the result set as follows
BayNo FixDateTime noOfFixes
1 12/05/2015 00:15:00 2
2 12/05/2016 09:30:00 2
4 12/05/2016 05:30:00 1
4 17/05/2016 15:05:00 1
I manage to get the counts of each but struggling to get the max and keep the highest calculated repeated value. can someone help please
Use window functions.
Get the count for each day by bayno and also find the min fixdatetime for each day per bayno.
Then use dense_rank to compute the highest ranked row for each bayno based on the number of fixes.
Finally get the highest ranked rows.
select distinct bayno,minfixdatetime,no_of_fixes
from (
select bayno,minfixdatetime,no_of_fixes
,dense_rank() over(partition by bayno order by no_of_fixes desc) rnk
from (
select t.*,
count(*) over(partition by bayno,cast(fixdatetime as date)) no_of_fixes,
min(fixdatetime) over(partition by bayno,cast(fixdatetime as date)) minfixdatetime
from tablename t
) x
) y
where rnk = 1
Sample Demo
You are looking for rank() or dense_rank(). I would right the query like this:
select bayno, thedate, numFixes
from (select bayno, cast(fixdatetime) as date) as thedate,
count(*) as numFixes,
rank() over (partition by cast(fixdatetime as date) order by count(*) desc) as seqnum
from t
group by bayno, cast(fixdatetime as date)
) b
where seqnum = 1;
Note that this returns the date in question. The date does not have a time component.
This is my first post here even though I am a daily reader. :)
I need to produce an MS SQL Server 2014 report that shows the clients that come back to do business with me in less than or equal to 3 days. I tried with INNER JOINS but I wasn't successful.
The way I thought of the solution is using the below Logic:
If product is same
and if userId is same
and if action was donedeal but now is new
and if date diff <= 3 days
and if type is NOT same
then show results
e.g of my Data:
id orderId userId type product date action
1 1001 654 ordered apple 01/05/2016 new
2 1002 889 ordered peach 01/05/2016 new
3 1001 654 paid apple 01/05/2016 donedeal
4 1002 889 paid peach 03/05/2016 donedeal
5 1003 654 ordered apple 03/05/2016 new
6 1004 889 ordered peach 04/05/2016 new
7 1005 122 ordered apple 04/05/2016 new
8 1006 978 ordered peach 04/05/2016 new
9 1005 122 paid apple 04/05/2016 donedeal
10 1007 122 ordered apple 10/05/2016 new
Desired results:
id orderId userId type product date Diff
3 1001 654 paid apple 01/05/2016 2 days
4 1002 889 paid peach 03/05/2016 1 day
5 1003 654 ordered apple 03/05/2016 2 days
6 1004 889 ordered peach 04/05/2016 1 day
Could you please direct me to the functions that can be useful for me to solve this?
Thanks in advance.
#
Update
Gordon Linoff gave me the suggested code below but since the Type had to be different I replicated the code and run it as per below and it worked:
select t.*
from (select t.*,
max(case when action = 'donedeal' and type='paid' then date end) over
(partition by user, product order by date) as last_donedealdate
from t
) t
where action = 'new' and type='ordered' date < dateadd(day, 3, last_donedealdate)
UNION ALL
select t.*
from (select t.*,
max(case when action = 'donedeal' and type='ordered' then date end) over
(partition by user, product order by date) as last_donedealdate
from t
) t
where action = 'new' and type='paid' date < dateadd(day, 3, last_donedealdate)
You can use window functions for this. To get the last done deal date, use max() with partition by and order by. The rest is just where clause logic:
select t.*
from (select t.*,
max(case when action = 'donedeal' then date end) over
(partition by user, product order by date) as last_donedealdate
from t
) t
where action = 'new' and date < dateadd(day, 3, last_donedealdate);
i want to join two record (from same table) into one row with multiple column.
employment history structure as follows:
StaffID StartDate EndDate DeptID
==================================================
1 2010-10-01 2011-01-19 1
1 2011-01-20 2012-12-31 2
1 2013-01-01 2013-05-29 4
how can i join the two rows into one row if same StaffID and the 2nd record startdate is 1 day after the enddate of 1st record (continuous employment)
the output should like this
StaffID EffectiveDate New_DeptID Prev_DeptID
==================================================
1 2011-01-20 2 1
1 2013-01-01 4 2
the following is my sql statement but it doesn't work
select distinct
ca1.StaffID,
ca1.ProjectDepartment as Prev_DeptID, ca1.StartDate, ca1.EndDate,
ca2.ProjectDepartment as New_DeptID, ca2.StartDate, ca2.EndDate
from
emp_hist as ca1,
emp_hist as ca2
where
(ca1.StaffID = ca2.StaffID)
and ca1.StartDate<>ca2.StartDate
and ca1.EndDate <>ca2.EndDate
and ca2.startdate= DATEADD(day, 1, ca1.enddate)
for example,
two records (true data) in the table:
StaffID StartDate EndDate DeptID
===========================================================================
1 2010-04-12 12:00:00.000 2013-02-28 00:00:00.000 1
1 2013-03-01 12:00:00.000 2013-08-29 11:02:59.877 2
i cannot retrieve this record by using my sql statement
Your problem is that the dates have a time component. You appear to be using SQL Server. You can fix your query by doing this:
select ca1.StaffID,
ca1.ProjectDepartment as Prev_DeptID, ca1.StartDate, ca1.EndDate,
ca2.ProjectDepartment as New_DeptID, ca2.StartDate, ca2.EndDate
from emp_hist as ca1 join
emp_hist as ca2
on ca1.StaffID = ca2.StaffID and
cast(ca1.StartDate as date) <> cast(ca2.StartDate as date) and
cast(ca1.EndDate as date) <> cast(ca2.EndDate as date) and
cast(ca2.startdate as date) = DATEADD(day, 1, cast(ca1.enddate as date));
I also replaced the implicit join with improved join syntax.
If you're using SQL 2012 try the lag functions.
select distinct
ca1.StaffID,
ca1.EndDate,
ca1.ProjectDepartment as New_DeptID,
LAG(ca1.ProjectDepartment) OVER (PARTITION BY ca1.StaffId ORDER BY ca1.EndDate) as Prev_DeptID
from
emp_hist as ca1
If you're not, use the RANK function and a subquery
select
eh.StaffID,
eh.EndDate,
eh.ProjectDepartment as New_DeptID,
eh1.ProjectDepartment as Prev_DeptID
from
(select *, RANK(EndDate) OVER (PARTITION BY StaffId ORDER BY EndDate) as Rank
from emp_hist) eh left join (
select distinct
StaffID,
EndDate,
ProjectDepartment,
RANK(EndDate) OVER (PARTITION BY StaffId ORDER BY EndDate) as Rank
from
emp_hist) eh1 on eh1.staffid=a.staffid and eh1.rank=eh.rank-1