Calculate the sum of workdays until today using SQL - sql

I have the Base Table and I am trying to get the Result Table.
I am using this code to get the two first columns of the Result Table.
The Overdue column should give the SUM of the WorkingDays until today's date.
How can I get all 3 columns in the same select statement?
;With cte as (
Select [Id] ,[Event_Id] ,[StartDate] ,[EndDate] ,[WorkingDay] From BaseTable
)
Select
Id,
SUM(WorkingDays) as Planned
From cte
Group By Id, Event_Id

Related

Using Derby SQL to calculate value for histogram

I have a table with various SKU in totes.
The table is totecontents with below columns:
ToteID
SKU
Each Tote can contain a maximum of 6 SKUs. (programmatically constrained)
select toteid, count(*) as qtypertote
from totecontents
group by toteid;
gives me a list of totes with the number of skus in each.
I now want to get to a table with following result
SkuCount Occurences where each row would have the ordinal value (1 through 6 ) and then the number of occurences of that value.
My efforts included the following approach
select count(*)
from
( select toteid, count(*) as qtypertote
from totecontents
group by toteid)
group by qtypertote;
Stung by the comments I performed more research. This works:
SELECT CountOfskus, COUNT(1) groupedCount
FROM
( SELECT COUNT(*) as countofskus, toteid
FROM totecontents
Group By toteid
) MyTable
GROUP BY countofskus;

How to add the values of two similar dates which is in a column

how can i add the values of two similar dates values into one
for example:
2019-15-07 120
2019-16-07 152
2019-16-07 120
2019-17-07 100
I want to add the date values of 2019-17-07 into one for example 2019-17-07 152+120. That is 2019-17-07 272. Thank you.
to check i use the code:
SELECT date, COUNT(*)
FROM getProcess
GROUP BY Date
HAVING ( COUNT(*) > 1 )
which shows which is repeated date.
Instead of count just perform SUM:
SELECT date, SUM([myvalue])
FROM getProcess
GROUP BY Date
If yo want to materialize the data and clear duplicates, you can store the above result in temporary table, then delete the found records and insert the them with the aggregate value:
SELECT date, SUM([myvalue]) [myvalue]
INTO #datasource
FROM getProcess
GROUP BY Date
HAVING ( COUNT(*) > 1 )
DELETE [dbo].[mytable]
FROM [dbo].[mytable] MT
INNER JOIN #datasource DS
ON MT.[date] = DS.[date]
INSERT INTO [dbo].[mytable]
SELECT *
FROM #datasource

How to determine difference in month between rows in a way like crossing

I'm creating a report where I need to calculate the difference between two dates in different rows in a way like cross to cross. How can I achieve this result with the following data:
CREATE TABLE #Customers (
customerid INT,
issuedate DATE,
statusdate date
)
INSERT INTO #Customers
SELECT 928, '2017-07-24', '2018-01-22'
union
SELECT 928, '2018-04-05', '2018-10-05'
union
SELECT 928, '2019-02-21', '2019-01-21'
--The Result should be like this "Displaying the difference between '2018-01-22' and '2018-04-05'
--And difference between '2018-20-05' and '2019-02-21'
DROP TABLE #Customers
I expect the result from the query to display the difference in months between columns 'statusdate' and 'issuedate' giving an output of
3 and
4
SELECT DATEDIFF(MONTH,'2018-01-22','2018-04-05')
UNION
SELECT DATEDIFF(MONTH,'2018-10-05','2019-02-21')
Use the LAG() function to get the value from the previous row, and check where that value is not null. We use a sub-select to fetch the values, as the LAG() functions cannot be used in a WHERE clause.
SELECT DATEDIFF(MONTH, T.PreviousStatusDate, T.issuedate) as difference
FROM (
SELECT LAG(c.statusdate) OVER(ORDER BY c.statusdate) as PreviousStatusDate,
c.issuedate
FROM Customers c
) AS T
WHERE T.PreviousStatusDate IS NOT NULL
Result,
difference
----------------
3
4
See live demo at http://www.sqlfiddle.com/#!18/d1169/10
If I understand correctly and you want to get the difference between values from current and subsequent rows, next approach using LEAD() may help:
SELECT DATEDIFF(MONTH, statusdate, nextissuedate) AS [Difference]
FROM (
SELECT
customerid,
issuedate,
statusdate,
LEAD(issuedate) OVER (PARTITION BY customerid ORDER BY issuedate) AS nextissuedate
FROM #Customers
) t
WHERE nextissuedate IS NOT NULL
Output:
----------------
Difference
----------------
3
4

Loop through dates

We have a table to keep track of items in a warehouse:
-------------------------
ID DATEIN DATEOUT
-------------------------
What we need is a list of the amount of items in the warehouse per week.
This is how far we've gotten:
SELECT
COUNT (ID)
FROM
table
WHERE
table.DATEIN<=#somedate1#
AND
(
table.DATEOUT>#somedate1#
OR
table.DATEOUT IS NULL
)
This gives us the amount of items in the warehouse on date #somedate1#. However, we need a list of the amount per week. (keeping in mind that it's possible that some items stay in the warehouse for months)
The tricky part isn't the week, we can use DATEPART ("ww", #somedate1#), but looping through the dates.
Any ideas?
Have you tried?
SELECT
DATEPART("ww","D"),COUNT (ID)
FROM
(
SELECT Table.DATEIN as D
FROM table
WHERE table.DATEIN<=#somedate1# AND ( table.DATEOUT>#somedate1# OR table.DATEOUT IS NULL)
UNION ALL
SELECT Table.DATEOUT as D
FROM table
WHERE table.DATEIN<=#somedate1# AND ( table.DATEOUT>#somedate1# OR table.DATEOUT IS NULL)
)
GROUP BY DATEPART("ww","D")
First you need to make an UNION to make sure you have all DATEIN and DATEOUT, then you can GROUP BY
Here's what we've come up with so far.
This code gives a list of all dates where an item came into the warehouse or left. These are the dates we need to apply the code in my first post to. I.e. we need to loop through these dates where for each iteration the next date is used as #somedate1#
SELECT
table.DATEIN AS DATE
FROM
table
GROUP BY
table.DATEIN
UNION
SELECT
table.DATEOUT AS DATE
FROM
table
WHERE
table.DATEOUT NOT IN (
SELECT
table.DATEIN
FROM
table
GROUP BY
table.DATEIN
)
GROUP BY
table.DATEOUT
We're stuck at how we can loop through them though. Everything we try gives weird results that we're not expecting.
We got it! The answer was staring us right in the face the whole time. This does the trick:
SELECT
COUNT (table1.ID),
table2.DATE
FROM
table1,
(
SELECT
table1.DATEIN AS DATE
FROM
table1
GROUP BY
table1.DATEIN
UNION
SELECT
table1.DATEOUT AS DATE
FROM
table1
WHERE
table1.DATEOUT NOT IN (
SELECT
table1.DATEIN
FROM
table1
GROUP BY
table1.DATEIN
)
GROUP BY
table1.DATEOUT
) table2
WHERE
table1.DATEIN<=table2.DATE
AND
(
table1.DATEOUT>table2.DATE
OR
table1.DATEOUT IS NULL
)
GROUP BY
table2.DATE

SQL Percentage Count Query By Date

I am able to calculate the percentage count on a particular date in a Microsoft Access 2007 SQL query using:
SELECT Date, Val, (Count(Val) / (SELECT Count(*) From Table HAVING Date=#7/31/2012#) as PercentVal
FROM Table
GROUP BY Date, Val
HAVING Date=#7/31/2012#
However, I would like to make this same calculation over every date using the count totals . For instance, the query:
SELECT Date, Val, Count(*) AS CountVal
FROM Table
GROUP BY Date, Val
finds the counts in every period. I would like to add an additional column with the percent counts. However, I can't seem to figure out how to calculate count percentage in every period without using the above block of text and setting up queries for each individual period.
You can subquery it like this:
SELECT A.ADate, A.Val, COUNT(A.Val) / B.DateCount
FROM Table1 AS A
INNER JOIN (
SELECT C.ADate, COUNT(*) AS DateCount
FROM Table1 C
GROUP BY C.ADate
) AS B ON A.ADate = B.ADate
GROUP BY A.ADate, A.Val, B.DateCount