SQL: Windows function - sql-server

I'm stuck with a new problem
I have two tables:
BudgetNumber Name Remaing(h) EndDate
BU145122 Jhon 5 2018/11/15
BU145123 Jhon 20 2018/12/01
BU145124 Lewis 10 2018/11/15
BU145125 Lewis 50 2018/12/15
Actuals:
Actuals(h) Name DATE
5 Jhon 2018/11/01
6 Jhon 2018/11/10
5 Jhon 2018/11/25
5 Lewis 2018/12/10
So in my final table, I'd like something like:
Name Remaining(h) Date Budget
Jhon 5 2018/11/01 BU145122
Jhon 14 2018/11/01 BU145123
Jhon 9 2018/11/25 BU145123
Lewis 45 2018/12/10 BU145125
So if the date of my budget is lower than my actual date, my budget is lost
If it's not the case, I substract my Actuals from my remaing(Table B)
I can get a table with a windows table but I'm stuck with the date
Is there another way to do that than a loop?
Thanks for your help

Related

How to convert an array of multiple columns and rows into 2 columns in Excel in the following format?

Name Canada US Euro
John 10 50 60
Mindy 5 60 100
Joe 20 15 55
Alan 10 30
Visual of original table
Into The below Format (effectively a concatenation of the first column and the first row into one column and then taking the array of number values corresponding to the person and geography into one column).
Visual of Desired Table
Name Fields
John Canada 10
John US 50
John Euro 60
Mindy Canada 5
Mindy US 60
Mindy Euro 100
Joe Canada 20
Joe US 15
Joe Euro 55
Alan Canada 10
Alan US 30
I am able to get it to work without the concatenation of the first column and first row.

Sum values of each parcels by months

I'm stucked here. Someone please help meeee.
I need sum some values, grouped by month. But I don't have this values on DB. Just the start date, end date, value of the parcels and number of months.
Ex.
Expense
Date of buy: 2022-01-15
Value: 500.00
Parceled: 10x
Each parcel: 50.00
End of parcels: 2022-11-15
Instead of inserting 10 lines in DB with each installment of each month. I would like to get the purchase date until the end of the installments and show in a graph the amount to be paid each month.
If I've another expense with the same value at month 03, for example. On month 04 my value by month of parcels will be 100.00, until the end of this new expense.
here is what i made:
$parceled_expenses = Expense::whereYear('end_parcels', '>=', $thisYear)
->whereMonth('end_parcels', '>=', $thisMonth)
->select(
DB::raw('sum(parcel_vl) as total'),
DB::raw('month(end_parcels) as month')
)
->groupBy('month');
This return (value of parcels => month):
items: array:3 [
126 => 5
75 => 6
50 => 12
With this data on my DB:
date_pc value parcels parcel_vl end_parcelsĀ 
15/03/2022 500.00 10 50.00 15/01/2023
16/04/2022 150.22 2 75.11 16/06/2022
16/02/2022 150.00 3 50.00 16/05/2022
02/03/2022 153.11 2 76.56 02/05/2022
09/03/2022 125.00 2 62.50 09/05/2022
Thanks Advance!

how to distribute a number into numbers of cells?

I have a sheet for the employees leaves so for example if:
Employee A takes Annual leave for 4 days starting 1/1/2021 I want to distribute the days into cells for these days to be like this:
A | 4 | 1/1/2021 1/1/2021 2/1/2021 3/1/2021 4/1/2021
I want to have each day date into a single cell.
any idea how to achieve that, please
or:
=INDEX(TEXT(SEQUENCE(1, A2, B2), "d/m/yyyy"))
try:
=INDEX(TEXT(TRANSPOSE(ROW(INDIRECT(B2&":"&B2+A2-1))), "d/m/yyyy"))
Or:
=ArrayFormula(B2+SEQUENCE(1,A2,0))

Displaying differences with PowerPivot

Based on the following Data:
Location Salesperson Category SalesValue
North Bill Bikes 10
South Bill Bikes 90
South Bill Clothes 250
North Bill Accessories 20
South Bill Accessories 20
South Bob Bikes 200
South Bob Clothess 400
North Bob Accesories 40
I have the following Sales PivotTable in Excel 2016
Bill Bob
Bikes 100 200
Clothes 10 160
Accessories 40 40
I would now like to diplay the difference between Bill and Bob and, importantly, be able to sort the table by difference. I have tried adding the Sales a second time and displaying it as a difference to "Bill". This gives me the correct values but sorts according to the underlying sales value and not the computed difference.
Bill Bob Difference
Bikes 100 200 100
Clothes 10 160 150
Accessories 40 40 0
I am fairly sure I need to use some form of DAX calculation but am having difficulty finding out exactly how. Can anyone give me a pointer?
Create a measure for that calculation:
If Bill and Bob are columns in your table.
Difference = ABS(TableName[Bill] - TableName[Bob])
If Bill and Bob are measures:
Difference = ABS([Bill] - [Bob])
UPDATE: Expression to only calculate difference between Bob and Bill.
Create a measure (in this case DifferenceBillAndBob) and use the following expression.
DifferenceBillAndBob =
ABS (
SUMX ( FILTER ( Sales, Sales[SalesPerson] = "Bob" ), [SalesValue] )
- SUMX ( FILTER ( Sales, Sales[SalesPerson] = "Bill" ), [SalesValue] )
)
It is not tested but should work.
Let me know if this helps.

How to generate a SQL query to for this data?

I have a SQL Server 2005 table in which I store the book exchanges that take place between two students.
ExchangeID BookID ExchangeDate FromPersName ToPersName
1 23 23.12.2011 John Matt
2 22 15.02.2012 Billy Ken
3 23 27.12.2011 Matt Riddley
5 23 05.03.2012 Riddley Josh
6 22 08.03.2012 Ken Rachel
7 23 19.03.2012 Josh Laura
8 23 15.01.2013 Laura Mike
9 22 17.01.2013 Rachel Stephanie
I want to generate a report for a specified year that looks like this:
Year:2012
BookID PersonName ReceivingDate DeliveryDate
23 Matt 01.01.2012 27.02.2012
23 Riddley 27.02.2012 05.03.2012
23 Josh 05.03.2012 19.03.2012
23 Laura 19.03.2012 31.12.2012
22 Ken 01.01.2012 08.03.2012
22 Rachel 08.03.2012 31.12.2012
This is half solution.You need to change it a litle bit to work. I will not make whole query for Your homework. Put some effort!
http://sqlfiddle.com/#!3/39d73/17
table can join it self
select ex1.BookID,ex1.toPersName,ex1.ExchangeDate as DeliveryDate,Exchanges.ExchangeDate
as ReceivingDate from Exchanges as ex1 inner join Exchanges on
ex1.toPersName=Exchanges.FromPersName

Resources