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.

Similar Messages

  • Last Record Values for a given Date Range

    Hi, I have a unique requirement to process/report only the values of the last record found in a date range for a given set of characteristics.  The order would be by FISCPER.
    For example:  I have some data with 3 characteristics.
    STORE001 MATN001 2004001 100 PCS
    STORE001 MATN001 2004005 75  PCS
    STORE001 MATN001 2004006 80  PCS
    STORE002 MATN002 2003008 10  PCS
    STORE002 MATN002 2004001 15  PCS
    STORE002 MATN002 2004004 40  PCS
    A selected date range of 2003008 to 2004001 would result in the following records being processed.
    STORE001 MATN001 2004001 100  PCS
    STORE002 MATN002 2004001 15   PCS
    A selected date range of 2004002 to 2004006 would result in the following records being processed.
    STORE001 MATN001 2004006 80  PCS
    STORE002 MATN002 2004004 40  PCS
    How can I accomplish this at runtime?  Any ideas?

    Nope, it's not an inventory scenario.  It could be say the last status of a gas meter within a selected date range. 
    Any other ideas? 
    I've considered using VKF, but haven't figured out how this would work yet.  If I could modify the selected query data prior to query processing I might be able to number the records and then filter on that.....  Anyway I'm open to any suggestions or leads!
    I won't forget the points for anyone with a solution!
    Thanks!

  • Query to get possible values for each segment

    Hi Gurus,
    Give the flex value set id, what is the query to get all possible values for each segment (just like what is shown in the accounting flex window)?
    Thank you,
    Beibei

    Hi Beibei,
    The table FND_FLEX_VALUES_VL can give you details of values available in the Value set i.e. 1 segment at a time.
    If you are looking for a concatenated view of Accounting Flexfield values, that would be available in GL_CODE_COMBINATIONS table.
    Regards,
    Ivruksha

  • SQL Query to find cumulative values for a Financial Year

    Dear users,
    My requirement is to create a SQL query for a table/view where I have day-wise data. I want to find out cumulative values for financial year by giving any date. It should add the values from start of financial year till that date in the financial year.
    I think creating a view of such type will post heavy burden on resources since accuimulation will be done for each day upto that day.
    Thanks

    Dear users,
    My requirement is to create a SQL query for a
    table/view where I have day-wise data. I want to
    find out cumulative values for financial year by
    giving any date. It should add the values from start
    of financial year till that date in the financial
    year.
    I think creating a view of such type will post heavy
    burden on resources since accuimulation will be done
    for each day upto that day.
    ThanksKumar's solution will serve yours purpose but m not agreed
    I think creating a view of such type will post heavy
    burden on resources since accuimulation will be done
    for each day upto that day. Khurram

  • Query to get a value for each day in a month

    Hi All,
    I'm needing a query (preferably without a loop or function) that can get a number representing each day of a given month.
    For example for the month of February, the result would be 1,2,3,....28
    Surely this can be done?
    Thanks!

    SQL> with a as (
      2  select trunc(sysdate,'Month') mon from dual)
      3  SELECT mon + LEVEL - 1 AS dates
      4  FROM a
      5  CONNECT BY LEVEL <= ADD_MONTHS (mon, 1)  - mon;
    DATES
    01-FEB-11
    02-FEB-11
    03-FEB-11
    04-FEB-11
    05-FEB-11
    06-FEB-11
    07-FEB-11
    08-FEB-11
    09-FEB-11
    10-FEB-11
    11-FEB-11
    DATES
    12-FEB-11
    13-FEB-11
    14-FEB-11
    15-FEB-11
    16-FEB-11
    17-FEB-11
    18-FEB-11
    19-FEB-11
    20-FEB-11
    21-FEB-11
    22-FEB-11
    DATES
    23-FEB-11
    24-FEB-11
    25-FEB-11
    26-FEB-11
    27-FEB-11
    28-FEB-11
    28 rows selected.or if u want it in one query,Then
    SQL> SELECT trunc(sysdate,'Month') + LEVEL - 1 AS dates
      2  FROM dual
      3  CONNECT BY LEVEL <= ADD_MONTHS (trunc(sysdate,'Month'), 1)  - trunc(sysdate,'Month');
    DATES
    01-FEB-11
    02-FEB-11
    03-FEB-11
    04-FEB-11
    05-FEB-11
    06-FEB-11
    07-FEB-11
    08-FEB-11
    09-FEB-11
    10-FEB-11
    11-FEB-11
    DATES
    12-FEB-11
    13-FEB-11
    14-FEB-11
    15-FEB-11
    16-FEB-11
    17-FEB-11
    18-FEB-11
    19-FEB-11
    20-FEB-11
    21-FEB-11
    22-FEB-11
    DATES
    23-FEB-11
    24-FEB-11
    25-FEB-11
    26-FEB-11
    27-FEB-11
    28-FEB-11
    28 rows selected.
    SQL> Regards
    Umesh

  • Selecting last record in a date range

    Post Author: Alien8n
    CA Forum: Data Connectivity and SQL
    I need to extract the last record for a group and display it. Normally this would simply mean selecting the max date in the SQL but...I need the last record from within a specified date range. By using the max date it won't pull out any records for a group that has a record after my date range. ie I have 2 group items and they both have a record for each day of the weekBy leaving the date open it pulls out the last record, Friday, for each item in the group.I want to set a range saying give me the last record from Mon - Wed.What I want returned is Wed's records and if there's no records on Wednesday the next last record for that itemSo If I have...Item Date    RecordA     MON     1A     TUE      2A     FRI       3B     MON    4B     TUE      5B     WED    6B     THUR    7B     FRI       8I get A     TUE      2 B     WED    6FYI CR XI with SQL 2005

    Post Author: Alien8n
    CA Forum: Data Connectivity and SQL
    The database actually contains sample tests by date with each sample being tested multiple times over several months.I need to be able to pull a date range from the whole dataset, but I only want the last record in each date range. Samples can be tested every week or every 2 weeks and then the last record is used to show how the sample is doing over time. So you would run the report for say 3 weeks and show the last record for each sample. This wouldn't be a problem if you were only interested in the last 3 weeks data, but if you want to look at historical records you have to be able to pull from that date range. In effect the query has to work regardless of whether you ask for Mon-Fri (using my example above) or Mon-Wed or even just Mon alone. I'll give your example above a try and see how it goes

  • Sql query to find the balances for a customer account wise.

    Hi,
    Could someone help me with the sql query to find the balances for each customer account wise. This is need to generate the report.
    presently we are using this query, but the output doesnot return the expected result.
    SELECT sum(nvl(ps.acctd_amount_due_remaining,0)) "Balance"
    FROM      ra_cust_trx_line_gl_dist_all gld,
              gl_code_combinations c,
              ar_payment_schedules_all ps,
              RA_CUSTOMER_TRX_ALL rat,
              ra_customers rc
    WHERE      c.CHART_OF_ACCOUNTS_ID = 101
    and gld.code_combination_id = c.code_combination_id
         and rat.CUSTOMER_TRX_ID=gld.CUSTOMER_TRX_ID
         and rat.CUSTOMER_TRX_ID=ps.CUSTOMER_TRX_ID
    and ps.customer_id=rc.customer_id
         and ps.status='OP'
         and ps.gl_date <= :PDATE
         and ps.org_id=:PORGID
         and ps.class in ('GUAR','INV','DM','DEP')
    and c.SEGMENT4=:Account_id
    and ps.customer_id=:Customer_id
    Thanks in advance.
    Kalyan.

    Can someone help us with this.

  • Creation of a Query to show the values for the current month and the last 12 months data.

    Dear All,
    Good day!
    I have to create a Query with the below requirement.
    I have to create a Query to show the values for the current month and the last 12 months data.
    Can you please guide me how to achieve this ??
    thank you,
    Regards,
    Hema

    Hema
    explain the exact problem..? as you mentioned you want to create query to show values for current month and last 12 months.. so I think you want to show values for 12 months from current data.. you can achive this by multiple way..
    you can have selection screen and field with date .. and restrict based on system current date and 12 months before or you can handle this at your target.. .. I mean there are multiple ways to restrict data by date range..
    for some more hints..
    http://www.forumtopics.com/busobj/viewtopic.php?t=34393&sid=7fba465d0463bf7ff5ec46c128754ed6
    http://businessintelligence.ittoolbox.com/groups/technical-functional/cognos8-l/how-to-display-last-12-months-in-report-based-on-todays-date-3231850
    http://scn.sap.com/thread/3217381
    search on SDN you will get many other ways..
    Thanks,
    Bhupesh

  • How to have a unique value for each record??

    could any 1 help me out in this...
    I want to have a column name 'Order No' which should be unique.
    How to generate a unique value for each record.??

    could any 1 help me out in this...
    I want to have a column name 'Order No' which should be unique.
    How to generate a unique value for each record.?? If you are using SQL PLUS to create the table try something like
    this:
    CREATE TABLE ORDER_TEST (
    ORD_NO NUMBER (8) NOT NULL PRIMARY KEY,
    ORDERDATE DATE,
    CUSTID NUMBER (8) NOT NULL,
    SHIPDATE DATE,
    TOTAL NUMBER (8,2) CONSTRAINT TOTAL_ZERO CHECK
    (TOTAL >= 0),
    CONSTRAINT ORD_FOREIGN_KEY FOREIGN KEY (CUSTID) REFERENCES
    CUSTOMER (CUSTID),
    CONSTRAINT ORD_UNIQUE UNIQUE (ORD_NO)
    -- or a simpler table example
    DROP TABLE ORDER_TEST;
    CREATE TABLE ORDER_TEST (
    ORD_NO NUMBER (8),
    ORDERDATE DATE,
    CUSTID NUMBER (8) NOT NULL,
    SHIPDATE DATE,
    TOTAL NUMBER (8,2) CONSTRAINT TOTAL_ZERO CHECK
    (TOTAL >= 0),
    CONSTRAINT ORD_UNIQUE UNIQUE (ORD_NO)
    note: ORD_NO can also be a primary key
    If you are doing the INSERT during runtime from a form first
    create a sequence in SQL PLUS to handle the ORD_NO value:
    Create SEQUENCE ORDERNO_UNIQUEVAL_sqnc
    START WITH 000001
    NOMAXVALUE
    NOCACHE;
    and reference it as the ORD_NO parameter in your INSERT
    statement:
    ORDERNO_UNIQUEVAL_sqnc.NEXTVAL
    note: to maintain data integrity you must use the sequence
    everytime you insert a new order to table. To start a new
    sequence drop the sequence and re-create it with whatever "START
    WITH" value you want.
    Hope this helps
    Kevin

  • TSQL Get Previous values for each group by

    Dear Friends,
    I have a problem with my TSQL statment. 
    I need to get previous value for each day and specific unit. Everything goes fine if I have always 2 unit in each day. But if same day has just one unit or less, I cannot have the previous value in the current record.
    The Output is this one:
    SK_DAY SK_UNIT
    VALUE VALUE_PREVIUS_DAY
    20131112 2
    30.00 NULL
    20131112 3
    34.00 NULL
    20131113 2
    40.00 30.00
    20131113 3
    45.00 34.00
    20131114 2
    50.00 40.00
    I dont have the second record for 2013-11-14, because in this date I just have value for unit 2.
    The final output should include the record:
    20131114 3
    0 45.00
    The Statment I have is:
    SELECT MAIN.SK_DAY, MAIN.SK_UNIT, SUM(MAIN.VALUE) AS VALUE
     SELECT SUM(VND1.VALUE) AS VALUE
     FROM FCT_TEST VND1
     WHERE CONVERT(DATE,CONVERT(VARCHAR(10),VND1.SK_DAY))>=DATEADD(dd,-1,CONVERT(DATE,CONVERT(VARCHAR(10),MAIN.SK_DAY)))
    AND CONVERT(DATE,CONVERT(VARCHAR(10),VND1.SK_DAY))<CONVERT(DATE,CONVERT(VARCHAR(10),MAIN.SK_DAY))
    AND VND1.SK_UNIT=MAIN.SK_UNIT
    ) AS VALUE_PREVIUS_DAY
    FROM FCT_TEST MAIN
    GROUP BY SK_DAY, MAIN.SK_UNIT
    SQL CREATE SCRIPT:
    CREATE TABLE [dbo].[FCT_TEST](
    [SK_DAY] [int] NOT NULL,
    [SK_UNIT] [int] NOT NULL,
    [VALUE] [decimal](18, 2) NULL,
     CONSTRAINT [PK_FCT_TEST] PRIMARY KEY CLUSTERED 
    [SK_DAY] ASC,
    [SK_UNIT] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    Thank you!!!
    PPSQL

    Thank you Kalman,
    But my problem is not with date! Fortunately I have dates for all days.
    The problem is for dimension tables like UNITS that I dont have values for all days, and when I try to get the previous value for previous day and each unit I dont have records. 
    20131112
    2
     30.00
    NULL
    20131112
    3
     34.00
    NULL
    20131113
    2
     40.00
    30.00
    20131113
    3
     45.00
    34.00
    20131114
    2
     50.00
    40.00
    20131114
    3
     0
    45.00 [I NEED THIS RECORD AS IS]
    Could you help me??
    Thank you!!

  • [Microsoft][ODBC SQL Server Driver]Invalid character value for cast specifi

    [Microsoft][ODBC SQL Server Driver]Invalid character value for cast specification
    An you help me with this error?
    This is the problemativ query:
    Search QUERY : SELECT DISTINCT theK_files.fileid, theK_files.name, theK_files.t
    itle, theK_files.description, theK_users1.name AS AuthorName, theK_users1.surn
    ame AS AuthorSurname, [theK_file-extensions].name AS Extension, theK_files.publ
    ished, theK_files.updated FROM theK_users INNER JOIN [theK_users-to-groups] ON
    theK_users.userid = [theK_users-to-groups].userid INNER JOIN theK_usergroups
    ON [theK_users-to-groups].groupid = theK_usergroups.groupid INNER JOIN [theK_g
    roups-to-files] ON theK_usergroups.groupid = [theK_groups-to-files].groupid INN
    ER JOIN theK_files ON [theK_groups-to-files].fileid = theK_files.fileid INNER
    JOIN theK_users theK_users1 ON theK_files.authorid = theK_users1.userid INNER
    JOIN [theK_file-extensions] ON theK_files.extensionid = [theK_file-extensions]
    .extensionid WHERE theK_users.userid = '1' AND theK_files.extensionid = '1' AN
    D ( theK_files.name LIKE '%a%' OR theK_users1.name LIKE '%a%' OR theK_files.de
    scription LIKE '%a%' )

    are these columns character/string/varchar:
    theK_users.userid = '1'
    theK_files.extensionid = '1'
    if these are number columns, change them to
    theK_users.userid = 1
    theK_files.extensionid = 1
    Jamie

  • Write the sql query to find largest value in row wise without using   great

    write the sql query to find largest value in row wise without using
    greatest fuction?

    Another not so good way, considering you want greatest of 4 fields from a single record:
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (Select 100 col1,200 col2,300 col3,400 col4 from dual
      2  union select 500,600,700,800 from dual
      3  union select 900,1000,1100,1200 from dual
      4  union select 1300,1400,1500,1600 from dual
      5  union select 1700,1800,1900,2000 from dual
      6  union select 2100,2200,2300,2400 from dual
      7  union select 2800,2700,2600,2500 from dual
      8  union select 2900,3000,3100,3200 from dual)
      9  SELECT (CASE WHEN col1 > col2 THEN col1 ELSE col2 END) Max_value
    10  FROM
    11  (SELECT (CASE WHEN col1_col2 > col2_col3 THEN col1_col2 ELSE col2_col3 END) col1,
    12         (CASE WHEN col2_col3 > col3_col4 THEN col2_col3 ELSE col3_col4 END) col2,
    13         (CASE WHEN col3_col4 > col4_col1 THEN col3_col4 ELSE col4_col1 END) col3
    14  FROM
    15  (SELECT (CASE WHEN col1 > col2 THEN col1 ELSE col2 END) col1_col2,
    16         (CASE WHEN col2 > col3 THEN col2 ELSE col3 END) col2_col3,
    17         (CASE WHEN col3 > col4 THEN col3 ELSE col4 END) col3_col4,
    18         (CASE WHEN col4 > col1 THEN col4 ELSE col1 END) col4_col1
    19* FROM t))
    SQL> /
    MAX_VALUE
           400
           800
          1200
          1600
          2000
          2400
          2800
          3200
    8 rows selected.
    SQL> Edited by: AP on Sep 21, 2010 6:29 AM

  • How to create an array of ring with a different items/values for each

    Hi All,
    i want an array of text ring with different items and values for each text ring. Do you have other solution if it does not work?
    thanks by advance

    0utlaw wrote:
    Hello Mnemo15,
    The properties of elements in an array are shared across all controls or indicators in an array, so there is no way to specify unique selectable values for different text rings in an array.  It sounds like what you are looking for is a cluster of ring controls, where each control can be modified independently.  
    Could you provide a more descriptive overview of the sort of behavior you are looking for?  Are these ring elements populated at run time?  Will the values be changed dynamically? Will the user interact with them directly?
    Regards,
    But the selection is not a property, it is a value... I just tried it and you can have different selections.  Just not different items.
    Bill
    (Mid-Level minion.)
    My support system ensures that I don't look totally incompetent.
    Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.

  • How to get Privacy Policy value for each userprofile in sharepoint 2010?

    In userprofile application, we have defined Privacy Policy of mobilephone feild as optional, so every user has option to choose visibility scope of this property to "Everyone/My Manager/my colegues/Only Me" .
    Now I am trying to get mobilephone value and their selected visibility option for each user.
    I am able to get mobilephone value but I could not get "what each user has chosen as their visibility scope"?

    Hi,
    According to your post, my understanding is that you want to get Privacy Policy value for each userprofile in sharepoint 2010.
    You need to use RunWithElevatedPrivileges method to impersonate user.
    To get the get mobilephone policy, you can use user["CellPhone "].Privacy.
    For more information, you can refer to:
    c# - Getting property privacy with Sharepoint 2010
    How to Programmatically Impersonate Users in SharePoint
    Managing Sharepoint 2010 Profiles Programmatically
    Thanks,
    Linda Li                
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Linda Li
    TechNet Community Support

  • Planned depreciation value for each period

    Hi,
    Where can I get the planned depreciation value for each period? I need the fields for customised report.
    Thank you

    Hi,
    You can get the dep planned values for the each period by using SAP standard report S_ALR_87012936.
    In the selection screen of that report, click on All selections button on application tool bar. Then you will be able to see the all the available fields for selection.
    Choose evaluation period as Month.
    Now execute it. After getting output, you need to go for change layout and adopt fileds like Dep 01-2010, Dep 02-2010, Dep 03-2010....Etc till Dep 12-2010.
    This will fix your requirement.
    Thanks,
    Srinu

Maybe you are looking for

  • How do I make PDFs OPEN and fit my screen?

    Every time I open a PDF, the document opens larger than my screen. How do I make the document fit the screen I have? This is absolutely NOT a zoom problem. I have a 15 inch laptop. It has a screen that is about 12 inches wide x 7.5 inches high. I ope

  • Simple editor for Apple TV video?

    I'm looking for something simple for editing video for the Apple TV, just to chop off the ends and some interior segments (eliminate commercials). The editor that's part of Eye TV works very well for this, I can control on a frame level where the cut

  • Internal & External No. Range....................

    1.I have created GL master with selection of open item management and i have posted business transactions to that GL. Now client wants to change that GL  deselect open item management. is it possible...how? 2.What is exactly diff between External and

  • Is it necessary to use this. on objects or is it just good practice?

    Is it necessary to use this. on objects or is it just good practice?

  • Turn screen off?

    why is there not an option under turn screen off to not automatically turn off? Shouldn't I have the option on my Touchpad for the screen to not turn itself off? I use splashtop often to access my desktop so I can watch Netflix and I hate that after