Table 1 : Employee
EmpId CreatedAt
100 2015-11-09 07:21:02
200 2017-01-24 18:24:01
300 2016-08-20 06:55:35
Table 2 : Account
AccId EmpID Currency CreatedAt
9000 100 USD 2017-04-20 19:40:55
9001 200 USD 2017-04-20 19:40:55
9002 100 EUR 2017-05-20 19:40:55
9003 200 USD 2017-04-20 19:40:55
9004 100 USD 2017-04-20 19:40:55
Table 3 : Transaction
TrnsId AccId Amount CreatedAt
10 9000 3000 2017-04-25 19:40:55
11 9001 500 2017-05-25 19:40:55
12 9000 -200 2017-05-30 19:40:55
13 9000 -500 2017-06-11 19:40:55
Create a table that provides the day end balance (at midnight) for each account since it was first created, i.e. there should be a single entry in the table for each day an account exists, and its balance at the end of that day.
Can anybody help me in writing query to above scenario?
Thanks.
Since you haven't posted any attempt to solve this yourself, I will assume you need an initial nudge in the right direction. Hopefully this will help.
Outer-Join your Account table to a table (create it if you don't have one) that has one row for every day in the calendar (this is often referred to as a "tally table"). Filter out the days in the calendar that were before the date the account was created.
That will produce a result of one row for every Account-Date combination, which is all the rows you want in your result.
From there it's just a matter of adding the column with the End-Of-Day balance for that Account on that Date. There are lots of ways to do this. Google "SQL Running Total" and pick a method you like.
Related
I have an Access table (Logs) like this:
pk
modID
relID
DateTime
TxType
1
1234
22.3
10/1/22 04:00
1
2
1234
23.1
10/10/22 06:00
1
3
1234
23.1
10/11/22 07:00
2
4
1234
23.1
10/12/22 08:00
3
5
4321
22.3
10/2/22 06:00
7
6
4321
23.1
10/10/22 06:00
1
7
4321
23.1
10/11/22 07:30
3
Trying to write a query as part of a function that searches this table:
for all records matching a given modID and relID (e.g. 1234 and 23.1),
picks the most recent one (the MAX of DateTime),
returns the TxType for that record.
However, a bit new to Access and its query structure is vexing me. I landed on this but because I have to include a Total/Aggregate function for TxType I had to either choose Group By (not what I want) or Last (closer, but returns junk results). The SQL for my query is currently:
SELECT Last(Logs.TxType) AS LastOfTxType, Max(Logs.DateTime) AS MaxOfDateTime
FROM Logs
GROUP BY Logs.dmID, Logs.relID
HAVING (((Logs.dmID)=[EnterdmID]) AND ((Logs.relID)=[EnterrelID]));
It returns the TxType field when I pass it the right parameters, but not the correct record - I would like to be rid of the Last() bit but if I remove it Access complains that I don't have it as part of an aggregate function.
Anyone that can point me in the right direction here?
Have you tried
SELECT TOP 1 TxtType
FROM Logs
WHERE (((Logs.dmID)=[EnterdmID]) AND ((Logs.relID)=[EnterrelID]))
ORDER BY DateTime DESC;
That will give you the latest single data row based on your DateTime field and other criteria.
I have related two custom sql query in Tableau (via making relationship)
The outcome of the queries looks like :
Q1 : (It shows the starting time of the valid budget.If a user has multiple rows in this table, it means his/her budget has been updated with new amount)
id_user
budgete_start_date
budget_amount
1234
06-11-2021
120
1234
06-07-2022
200
56789
06-01-2022
1200
56789
06-07-2022
2000
643
05-05-2022
30
Q2 :(It shows the budget usage)
id_user
budgete_usage_date
amount_usage
1234
01-12-2021
50
1234
05-08-2022
100
56789
10-02-2022
60
56789
07-08-2022
500
643
05-07-2022
17
I need to find a way to create the following view to know what was the valid budget at
budgete_usage_date.
id_user
budgete_usage_date
amount_usage
valid budget
1234
01-12-2021
50
120
1234
05-08-2022
100
200
56789
10-02-2022
60
1200
56789
07-08-2022
500
2000
643
05-07-2022
17
30
How can I do that with calculated field in Tableau (with db made by relationship)?
If that's not possible, how can I do that directly in query? (changing the relationship to a single query)
I have a table as :
Serveur
Total
Date
#01
200
2022-01-01 14:11:50.000
#01
100
2022-01-01 22:19:33.000
#02
150
2022-01-01 14:55:35.000
#02
300
2022-01-01 21:45:28.000
#03
500
2022-01-01 21:43:21.000
But I want the output as follows(I want to use this output to inner join with some other table) :
Serveur
Total_1
Total_2
#01
200
100
#02
150
300
#03
500
0
I guess you are using Microsoft SQL
Please follow the link below for syntax and examples on how to pivot the data within tables
https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-ver16
Or you can use sun queries to pivot which is time consuming and dirty way of doing it.
I have a scenario on following tables:
SampleData: (has one hour periodic values of a Prod.)
TST_DATE CLK_LITRE_WT
---------------------------------
09/15/2019 17:15 1280 <-- current time value
09/15/2019 16:15 1300
09/15/2019 15:15 1190
09/15/2019 14:15 1200
09/15/2019 13:15 1200
CLK_LITRE_WT is prod name out of 13 products. So totally 14 columns are there. But it doesn't matter. Note that no of row/hour is given user.
SettingMaster:
UserCode LastRunDate
------------------------------------
aa 2019-09-15 15:18:01.350
LastRunDate is nothing but Last DB reached time of a User. So I need query which should be like and Expected Result:
TST_DATE CLK_LITRE_WT_Real CLK_LITRE_WT_Seen
-----------------------------------------------------------
09/15/2019 17:15 1280 1190 <-- value of previous live record.
09/15/2019 16:15 1300 1190 <-- value of previous live record.
09/15/2019 15:15 1190 1190 <-- Last seen record by User.
09/15/2019 14:15 1200 1200
09/15/2019 13:15 1200 1200
I tried Self Join, LEAD-LAG (will share the query soon). But I did not achieve the expected result. So I need your help for how can I get the Expected result.
Edit 1:
I recreate LEAD-LAG what I tried yesterday.
select TST_DATE, CLK_LITRE_WT CLK_LITRE_WT_Real, lead (CLK_LITRE_WT) over (order by convert(datetime,sd.TST_DATE + ':15.00') desc) CLK_LITRE_WT_Seen
from SettingMaster sm left join SampleData sd
on convert(datetime,sd.TST_DATE + ':15.00') between dateadd (hour, -(4), '2019-09-15 17:20:02.733') and '2019-09-15 17:20:02.733'
where sm.UserCode = 'aa'
order by convert(datetime,sd.TST_DATE + ':15.00') desc
Here yesterday date and time was passed static. Because, given data are yesterday data. And result is:
TST_DATE CLK_LITRE_WT_Real CLK_LITRE_WT_Seen
09/15/2019 17:15 1280 1300
09/15/2019 16:15 1300 1190 <-- value of previous live record
09/15/2019 15:15 1190 1200 <-- here should be '1190' as Real
09/15/2019 14:15 1200 NULL
I have a single table in Powerpivot.
My columns are Account, Amount and Date. I want to calculate PrevYearAmount, but I can't fin the correct formula.
Sample data:
Account Amount Date PrevYearAmount
1 100 01/01/2016 90
1 120 02/01/2016 200
2 130 01/01/2016 108
2 103 01/01/2015
2 105 01/01/2015
1 90 01/01/2015
1 200 02/01/2015
tried
=CALCULATE(SUM(Hoja1[Amount]);FILTER(Hoja1;DATEADD(Hoja1[Date];-1;YEAR));FILTER(Hoja1;Hoja1[Account]))
But this returns 350 for all rows.
Also tried:
=CALCULATE(SUM(Hoja1[Importe]);DATESYTD(SAMEPERIODLASTYEAR(Hoja1[Fecha])))
but returns blank
this should do the trick:
calculate(sum('Table1'[Amount]);SAMEPERIODLASTYEAR('Table1'[Date]))
Hope this helps.
But, please consider to create a date table, as it is always a good idea, to use relationships, this makes the expanding/collapsing part of DAX much easier.