Query for aggregates for each date in a date range
Hi,
I want to generate a Trend report with a T-SQL proc, which needs following logic.
Input -
Date Range say '10/10/12' to '20/10/12' (Say to check the trend of Size of account in 20 days of Trend report)
Account balance is captured randomly, (i mean not every day)
Table with date looks like this..
--Account Balance Table
CREATE TABLE AccBanalce (
BranchId SMALLINT
NOT NULL,
AccId CHAR(9)
NOT NULL,
Amount DECIMAL(9,3)
NOT NULL,
SnapShotDate DATETIME
NOT NULL
CONSTRAINT PK_AccBanalce PRIMARY KEY NONCLUSTERED (AccId, SnapShotDate) )
GO
Create CLUSTERED INDEX CIx_AccBanalce ON AccBanalce (SnapShotDate)
GO
--Date Range table
CREATE TABLE DateRange ( StartDate DATETIME, EndDate DATETIME)
GO
--Date for the Account Balance Table
INSERT INTO AccBanalce (BranchId, AccId, Amount, SnapShotDate)
VALUES (1, 'C1-100', 10.4, '10/11/2010' ),
(1, 'G1-110', 20.5, '10/11/2010' ),
(2, 'GC-120', 23.7, '10/11/2010' ),
(2, 'Gk-130', 78.9, '10/13/2010' ),
(3, 'GH-150', 23.5, '10/14/2010'),
(1, 'C1-100', 31.8, '10/16/2010' ),
(1, 'G1-110', 54.8, '10/16/2010' ),
(2, 'GC-120', 99.0, '10/16/2010' ),
(3, 'Gk-130', 110.0, '10/16/2010' ),
(3, 'G5-140', 102.8, '10/16/2010' ),
(2, 'GC-120', 105, '10/18/2010' ),
(2, 'Gk-130', 56.7, '10/18/2010' ),
(1, 'C1-100', 84.3, '10/18/2010' ),
(1, 'G1-110', 75.2, '10/19/2010' ),
(2, 'GC-120', 64.9, '10/20/2010' ),
(3, 'GH-150', 84.0, '10/20/2010' ),
(1, 'C1-100', 78.0, '10/20/2010' ),
(1, 'G1-110', 89.5, '10/20/2010' )
GO
--Date for DateRange Table
INSERT INTO DateRange (StartDate, EndDate) VALUES
('2010-10-11 00:00:00.000', '2010-10-11 23:59:59.997'),
('2010-10-12 00:00:00.000', '2010-10-12 23:59:59.997'),
('2010-10-13 00:00:00.000', '2010-10-13 23:59:59.997'),
('2010-10-14 00:00:00.000', '2010-10-14 23:59:59.997'),
('2010-10-15 00:00:00.000', '2010-10-15 23:59:59.997'),
('2010-10-16 00:00:00.000', '2010-10-16 23:59:59.997'),
('2010-10-17 00:00:00.000', '2010-10-17 23:59:59.997'),
('2010-10-18 00:00:00.000', '2010-10-18 23:59:59.997'),
('2010-10-19 00:00:00.000', '2010-10-19 23:59:59.997'),
('2010-10-20 00:00:00.000', '2010-10-20 23:59:59.997')
GO
Question -
I want TOTAL Balance of all Accounts in a Branch per each day between 10/11/2010 to 10/20/2010
If the Snapshotdate (date) on which the account was not made an entery to AccBalance table, last available balance to be considered for that account.
like for account [C1-100] on 10/15/2010 the balance should be [10.4]
--Group By Branch
--Last valid Account balance to be considered.
I know, this is long solution, but any one who is expert in T-SQL can help me in this solution.
Thanks,
Krishna
Thanks Himanshu You almost solved my issue...but can you provide the final output as following...
Actually you are aggregating the Amount, which is not required, as it is the total available in that account.
But the missing pint is I need the SUM of all the accounts for each DAY in a BRANCH.
The 3rd Result Query modified to get DAILY balances for each account as following...
--*RESULT*
SELECT a.AccId, a.StartDate,
(SELECT TOP 1 b.Amount
FROM #InterimOutput b
WHERE b.AccId = a.AccId and b.Amount > 0
AND B.StartDate<=A.StartDate ORDER BY B.StartDate DESC) as ToDateBal
FROM #InterimOutput a
ORDER BY a.AccId
go
Now I need SUM of all Account Balances AT each BRANCH on DAILY basics. Can you help on that?
Thanks again
Krishna
Similar Messages
-
Hi folks,
hope everyone had a good weekend. Have a problem at hand and hoping to find a solution. Here is what I need to do. I need to find out the following based on Dates.
Currently, we have Students who pay certain kinds of fees and that is detrmined based on their Fee Codes. I will listing the table structure in a bit.
Query 1:
1. I need to find out Students who did not have a Fee Code back in September 1st, 2009 and had a Fee Code on September 1st, 2010.
Query 2:
2. Find out students who had a Fee Code back in September 01st, 2009 and do not have a Fee Code in September 01,2010.
Now, by saying that a Student no longer has a Fee Code in September 01,2009/2010, it's not just the fact that record does not exist but also that there is an Expiry Date. So, basically, when a new record is created in the table, the previous record's GRAD_EXP_EFF_DATE gets stamped with SYSDATE.
Table GRAD_FEE_STRUCTURE_HIST
GRAD_STU_ID NOT NULL VARCHRA2(6)
GRAD_STU_FEE_CODE NOT NULL VARCHAR2(6)
GRAD_EFF_FEE_DATE DATE
GRAD_EXP_FEE_DATE DATE
GRAD_CREATE_DATE DATE
GRAD_CREATE_USERID VARCHAR2(10)Any help is appreciated. I am using Oracle 9i.Hi,
Try something like this:
SELECT grad_stu_id
FROM grad_fee_structure_hist
GROUP BY grad_stu_id
HAVING COUNT ( CASE
WHEN DATE '2010-09-01' BETWEEN grad_eff_fee_date
AND grad_exp_fee_date
THEN 1
END
) > 0
AND COUNT ( CASE
WHEN DATE '2009-09-01' BETWEEN grad_eff_fee_date
AND grad_exp_fee_date
THEN 1
END
) = 0
;If a NULL grad_fee_exp_date means that the row is currently in force, then use something like
NVL ( grad_fee_exp_date
, DATE '9999-12-31'
)instead of the raw date.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data. Explain how you get those results from that data, giving specific examples.
Always say what version of Oracle you're using.
Edited by: Frank Kulash on Sep 13, 2010 11:08 AM
Changed date comparisons to BETWEEN. -
'Repeat Subform for Each Data Item' is greyed out
Hi there,
I need to make a certain subform repeatable but the 'Repeat Subform for Each Data Item' is greyed out. It's parent subform is not greyed out so I can make this repeatable but this is not the requirement.
Is there another setting that prevents a subform from being repeatable?
Many thanks,
Kieran KellyI guess you need to set the set the content flowed instead of positioned for which subform you want to repeat.
Click on the subform you want to repeat in the hierarchy panel , then in the object panel click on the content and set in the flowed format.
Hope it helps .
Thanks.
Bibhu -
Not all my titles are showing for each data set
Hello,
I have made a column graph for 4 different data sets, I need the titles to show for each data set along the X axis. I highlight the titles in my data when making the graph, however, only two of the four are showing. Is there any way I can get all 4 to show?
^^^^ Need the title after Baseline and after Numb Hand
Thank youTry clicking the chart then the blue 'Edit Data References' rectangle, and then in the lower left of the window switch from 'Plot Columns as Series' to 'Plot Rows as Series'. Then switch back if needed. All four should then show up.
SG -
Hi,
My end user requires me to develop a report that will accept the following parameters:
Customer Code : ABC
From Period : 01-Jan-2005
To Period : 31-Dec-2010
Output must be as follows :
Customer Jan-05 Feb-05 Mar-05 Apr-05 May-05 ......................................Dec-10 Total Order
Code
ABC 10 15 5 20 10 ...................................... 15 X value
I have told my end user that it is not possible to create an Oracle report that will give data for any period range that the user specifies. First the number of columns for the date range must be fixed like say at a time only data for 12 months will be displayed in the report. Then they can run the report for any year and the parameter has to be
Customer Code : ABC
Year :2005
The output will be :
Customer Jan-05 Feb-05 Mar-05 Apr-05 May-05 ......................................Dec-05 Total Order
Code
ABC 10 15 5 20 10 ...................................... 15 X value
Am I right or wrong ? Please advise. This is very urgent.I don't see why this should not be possible.
Remember that you can have repeating frames in every direction (ie. right and down) and can combine these.
What I would do is look for (or create) a type of calender-table where I could select the date values from (ie. JAN 05) qualified by the from- and to-Parameters (aka query1),
then have a second, dependent query which uses the date from the calender (aka query2).
Then stack a down repeating frame for query2 into the right repeating frame for query1 and you should be almost there... Use the date from query1 as heading...
Cheers,
Jens Rettig -
How to pull records only for particular date range in Flex frm SAP wd table
Hi,
Can anyone help me with databing for datefield.
I am using two datefields in Flex for Start Date and End Date. When I click the Execute button, it should pull only the records for that date range from SAP wd table and display in my Flex datagrid.
Thanks,
Sri
Edited by: rmsridevi on May 17, 2011 4:38 PMHi,
Your query has mistakes as well. I corrected them.
Check this two different ways were in first you can define the period (month) you want and in second you have the option to select from the drop drown list :
SELECT T0.DocNum, T0.DocDate, T0.CardName,T0.DocTotal,T1.whsCode
FROM OINV T0 INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
WHERE t0.docdate >= '2011.01.01' and t0.docdate <='2011.01.31'
OR
SELECT T0.DocNum,T0.DocDate,T0.CardName,T0.DocTotal,T1.whsCode
FROM OINV T0 INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
WHERE t0.docdate >= [%1] and t0.docdate <= [%2]
Kind Regards,
Jitin
SAP Business One Forum Team -
Devolped an ALV report for daily cash receipts for selected date range
hi,
how to devlop an ALV report for daily cash receipts for selected date range.for this report what are the tables and fields we have to use.what is the selectionscreen&what is logic.give me sample report.hi,
how to devlop an ALV report for daily cash receipts for selected date range.for this report what are the tables and fields we have to use.what is the selectionscreen&what is logic.give me sample report. -
Devloped an ALV report for daily cash receipts for selected date range
hi,
how to devlop an ALV report for daily cash receipts for selected date range.for this report what are the tables and fields we have to use.what is the selectionscreen&what is logic.give me sample report.Hi,
You can develop simple reports using Report Painter.
You may be also interested in:
Check report SAPMF05A for credit memo
See the following Std reports on Payment Advices execute the Tcodes:
S_ALR_87009888
S_ALR_87009889
S_ALR_87009890
S_ALR_87009891
S_ALR_87009892
S_ALR_87009893
S_ALR_87009978
S_ALR_87009979
S_ALR_87009980
S_ALR_87009981
S_ALR_87009982
S_ALR_87009983
S_ALR_87010056
S_ALR_87010057
S_ALR_87010058
S_ALR_87010059
S_ALR_87010060
S_ALR_87010061
S_ALR_87010066
S_ALR_87010067
S_ALR_87012106
S_ALR_87012107
S_ALR_87012108
S_ALR_87012109
S_ALR_87012110
S_ALR_87012111
S_ALR_87012116
S_ALR_87012117
S_ALR_87012200
S_ALR_87012201
S_ALR_87012202
S_ALR_870122
S_ALR_87012204
S_ALR_87012205
S_ALR_87012350
S_ALR_87012351
S_ALR_87012352
S_ALR_87012353
S_ALR_87012354
S_ALR_87012355
sample ALV report:
tables:
marav. "Table MARA and table MAKT
Data to be displayed in ALV
Using the following syntax, REUSE_ALV_FIELDCATALOG_MERGE can auto-
matically determine the fieldstructure from this source program
Data:
begin of imat occurs 100,
matnr like marav-matnr, "Material number
maktx like marav-maktx, "Material short text
matkl like marav-matkl, "Material group (so you can test to make
" intermediate sums)
ntgew like marav-ntgew, "Net weight, numeric field (so you can test to
"make sums)
gewei like marav-gewei, "weight unit (just to be complete)
end of imat.
Other data needed
field to store report name
data i_repid like sy-repid.
field to check table length
data i_lines like sy-tabix.
Data for ALV display
TYPE-POOLS: SLIS.
data int_fcat type SLIS_T_FIELDCAT_ALV.
select-options:
s_matnr for marav-matnr matchcode object MAT1.
start-of-selection.
read data into table imat
select * from marav
into corresponding fields of table imat
where
matnr in s_matnr.
end-of-selection.
Now, we start with ALV
To use ALV, we need a DDIC-structure or a thing called Fieldcatalogue.
The fieldcatalouge can be generated by FUNCTION
'REUSE_ALV_FIELDCATALOG_MERGE' from an internal table from any
report source, including this report.
The only problem one might have is that the report and table names
need to be in capital letters. (I had it )
Store report name
i_repid = sy-repid.
Create Fieldcatalogue from internal table
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = sy-repid
I_INTERNAL_TABNAME = 'IMAT' "capital letters!
I_INCLNAME = sy-repid
CHANGING
CT_FIELDCAT = int_fcat
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = i_repid
I_STRUCTURE_NAME = 'marav'
I_DEFAULT = 'X'
I_SAVE = 'A'
TABLES
T_OUTTAB = imat.
IF SY-SUBRC <> 0.
WRITE: 'SY-SUBRC: ', SY-SUBRC .
ENDIF.
Hope this will help.
Regards,
Naveen. -
How to take unreconcilled transactions report for a date range ?
hi all,
How to take unreconcilled transactions report for a
data range ?
we have taken unreconcilled transactons from
external reconcillation using filter option mentioning
range of dates,But when we take print out using PLD,
it showing unreconcilled transactions for all dates.
But our client requires it as a standard report from SAP ?
Our client is using SAP B1 2005B PL39.
JeyakanthanHi
Financials -> Financial Reports -> Accounting -> General Ledger.
In the 'Display' dropdown select, 'Unreconciled' .
Hope this should help you. -
Workday calculation for specified date range
Hi All,
How to count number of workdays available for given date range. And I tried below statement which is
not working.
SUM(CASE [Calendar].[Calendar Date]
WHEN [Calendar].[Calendar Date].[Year] = '2005' AND [Calendar].[Calendar Date].[Month]
= 1
THEN [Calendar].[Calendar Date].CURRENTMEMBER.CHILDREN
END
, [Measures].[Work Day Flag])Hi bpbhhaskar,
According to your description, you want to count the weekdays in MDX. Right?
In this scenario, we can just aggregation days from Monday to Friday. The key should be 2 to 6. Please use the expression below:
with
MEMBER [Measures].[DayCount] AS
Count
Descendants
[Date].[Calender].CurrentMember
,[Date].[Calender].[Date]
* {[Date].[Day Of Week].&[2]:[Date].[Day Of Week].&[6]}
Best Regards,
Simon Hou
TechNet Community Support -
Get table partition name dynamically for given date range
Dear All,
Could you please tell me how to get the partition name dynamicaly for given date range ?
Thank you.SQL> select table_name,
partition_name,
to_date (
trim (
'''' from regexp_substr (
extractvalue (
dbms_xmlgen.
getxmltype (
'select high_value from all_tab_partitions where table_name='''
|| table_name
|| ''' and table_owner = '''
|| table_owner
|| ''' and partition_name = '''
|| partition_name
|| ''''),
'//text()'),
'syyyy-mm-dd hh24:mi:ss')
high_value_in_date_format
from all_tab_partitions
where table_name = 'SALES' and table_owner = 'SH'
TABLE_NAME PARTITION_NAME HIGH_VALUE_IN_DATE_FORMAT
SALES SALES_1995 01-JAN-96
SALES SALES_1996 01-JAN-97
SALES SALES_H1_1997 01-JUL-97
SALES SALES_H2_1997 01-JAN-98
SALES SALES_Q1_1998 01-APR-98
SALES SALES_Q2_1998 01-JUL-98
SALES SALES_Q3_1998 01-OKT-98
SALES SALES_Q4_1998 01-JAN-99
SALES SALES_Q1_1999 01-APR-99
SALES SALES_Q2_1999 01-JUL-99
SALES SALES_Q3_1999 01-OKT-99
SALES SALES_Q4_1999 01-JAN-00
SALES SALES_Q1_2000 01-APR-00
SALES SALES_Q2_2000 01-JUL-00
SALES SALES_Q3_2000 01-OKT-00
SALES SALES_Q4_2000 01-JAN-01
SALES SALES_Q1_2001 01-APR-01
SALES SALES_Q2_2001 01-JUL-01
SALES SALES_Q3_2001 01-OKT-01
SALES SALES_Q4_2001 01-JAN-02
SALES SALES_Q1_2002 01-APR-02
SALES SALES_Q2_2002 01-JUL-02
SALES SALES_Q3_2002 01-OKT-02
SALES SALES_Q4_2002 01-JAN-03
SALES SALES_Q1_2003 01-APR-03
SALES SALES_Q2_2003 01-JUL-03
SALES SALES_Q3_2003 01-OKT-03
SALES SALES_Q4_2003 01-JAN-04
28 rows selected. -
SD pricing extract for given date range
Hi,
Is any one knows any FM where you can able to extract pricing for given date range.
FM Pricing works for one date. I don't want to loop at this
FM for the given date rage. It takes very long time.
Thanks for any suggestion.
Kind Regards
NirHi,
Is any one knows any FM where you can able to extract pricing for given date range.
FM Pricing works for one date. I don't want to loop at this
FM for the given date rage. It takes very long time.
Thanks for any suggestion.
Kind Regards
Nir -
BDC to tick checkbox for some date range?
Hi experts.
I want to write one BDC which will mark checkbox for some field..
I have some date range from 1.4.2004-1.4.2008,
I want to check in my transaction that if document date falls in this range then tick PR field checkbox..
Can you plz help me? How can I do this?
Thanks & RegardsHi,
Check the document date for your date range and according to that condition execute the BDC recording.
Eg,
If doc_date gt 20040104 and doc_date lt 20080104.
perform bdc_field using 'your_check_box_name'
'X'.
endif.
Hope this will help you. -
How to put condition for one date range should not interfear with another ?
hi friends,
how to put condition for one date range should not interfear with another date range.
my data base table has two fields
from date
to date.
when we enter the date range in the data base , new date range means from date and to date should not interfear.
can anybody help me.
thanks &Regards,
Revanth
Edited by: rk.kolisetty on Jul 1, 2010 7:18 PMDo it the SAP way....
First entry...from is today, to is 99991231.
New dates entered, now we have two rows...:
from is original date to becomes yesterday.
From is today to is 99991231 -
Sql query - Selecting last recorded values for each date in specified period
Hello,
Can someone please help me with my problem.
I'm trying to get last recorded balance for each day for specific box (1 or 2) in specified period of days from ms access database using ADOTool.
I'm trying to get that information with SQL query but so far unsuccessfully...
My table looks like this:
Table name: TestTable
Date Time Location Box Balance
20.10.2014. 06:00:00 1 1 345
20.10.2014. 12:00:00 1 1 7356
20.10.2014. 18:45:00 1 1 5678
20.10.2014. 23:54:00 1 1 9845
20.10.2014. 06:00:02 1 2 35
20.10.2014. 12:00:04 1 2 756
20.10.2014. 18:45:06 1 2 578
20.10.2014. 23:54:10 1 2 845
21.10.2014. 06:00:00 1 1 34
21.10.2014. 12:05:03 1 1 5789
21.10.2014. 15:00:34 1 1 1237
21.10.2014. 06:00:00 1 2 374
21.10.2014. 12:05:03 1 2 54789
21.10.2014. 15:00:34 1 2 13237
22.10.2014. 06:00:00 1 1 8562
22.10.2014. 10:00:00 1 1 1234
22.10.2014. 17:03:45 1 1 3415
22.10.2014. 22:00:00 1 1 6742
22.10.2014. 06:00:05 1 2 562
22.10.2014. 10:00:16 1 2 123
22.10.2014. 17:03:50 1 2 415
22.10.2014. 22:00:10 1 2 642
23.10.2014. 06:00:00 1 1 9876
23.10.2014. 09:13:00 1 1 223
23.10.2014. 13:50:17 1 1 7768
23.10.2014. 19:47:40 1 1 3456
23.10.2014. 21:30:00 1 1 789
23.10.2014. 23:57:12 1 1 25
23.10.2014. 06:00:07 1 2 976
23.10.2014. 09:13:45 1 2 223
23.10.2014. 13:50:40 1 2 78
23.10.2014. 19:47:55 1 2 346
23.10.2014. 21:30:03 1 2 89
23.10.2014. 23:57:18 1 2 25
24.10.2014. 06:00:55 1 1 346
24.10.2014. 12:30:22 1 1 8329
24.10.2014. 23:50:19 1 1 2225
24.10.2014. 06:01:00 1 2 3546
24.10.2014. 12:30:26 1 2 89
24.10.2014. 23:51:10 1 2 25
Let's say the period is 21.10.2014. - 23.10.2014. and I want to get last recorded balance for box 1. for each day. The result should look like this:
Date Time Location Box Balance
21.10.2014. 15:00:34 1 1 1237
22.10.2014. 22:00:00 1 1 6742
23.10.2014. 23:57:12 1 1 25
So far I've managed to write a query that gives me balance for ONLY ONE date (date with highest time in whole table), but I need balance for EVERY date in specific period.
My incorrect code (didn't manage to implement "BETWEEN" for dates...):
SELECT TestTable.[Date], TestTable.[Time], TestTable.[Location], TestTable.[Box], TestTable.[Balance]
FROM TestTable
WHERE Time=(SELECT MAX(Time)
FROM TestTable
WHERE Location=1 AND Box=1 );
Tnx!
Solved!
Go to Solution.For loop
following query keep day (here 24 in below query) Variable from ( 1 to 28-29/30/31 as per month)
SELECT TOP 1 TestTable.[Date], TestTable.[Time], TestTable.[Location], TestTable.[Box], TestTable.[Balance]
FROM Test Table.
WHERE Time=(SELECT MAX(Time) FROM TestTable WHERE Location=1 AND Box=1 )
AND DATE = "2014-10-24";
PBP (CLAD)
Labview 6.1 - 2014
KUDOS ARE WELCOMED.
If your problem get solved then mark as solution.
Maybe you are looking for
-
The optic exit doesn't work correctly K8N Neo Platinium
Hello I am French and I have just acquired a Mother Board MSI K8N Neo Platiniums Of that someone uses the exit Optic Spidf. There is a problem with, she is restores sounds of Windows as well as the reading of the MP3 well. But if one connects t
-
Count of updated rows after an UPDATE command
Hi, is there any way to know how many rows have been updated after an UPDATE statement in a PL/SQL program (i know it is possible in Pro*C. Thanks.
-
Our DB has 4 'audit' columns : create_date, created_by, last_update_date, last_updated_by. We're currently not using ejbs, just J2SE, servlets & jsps, but I'm looking into using ejbs. Is there any kind of design pattern for this? I've been searching
-
How Schedule line date is determmined
Hi Experts, In my present issue i want to understand how schedule line date is determined for a document bcoz schedule date is for each item in a document but not at document level Please Help Thanks in Advance , Nitya
-
Mail account offline I can not go online
habe gerade mountain lion installiert jetzt kann ich keine Mails mehr versenden. Es kommt immer die Meldung " offline " was nicht stimmt da ich mails empfangen kann.