2 queries should return the same result (but they dont...)

hello
i have a following query:
select col1,extract(year from datum) yr, COUNT(*)
from tableA@dblink where
DATUM between '1-jan-1985' and '31-dec-2012'
and col2 > 100 and col2 not in ('999999')
and TRIM(TO_CHAR(col1)) in ('0','1')
group by col1,extract(year from DATUM);the above query returns the count: 143 982 for year 1991
however when i put the filter directly into this year the query returns a different number: 143 917
select col1,extract(year from datum) yr, COUNT(*)
from tableA@dblink  where
DATUM between '1-jan-1991' and '31-dec-1991'
and col2 > 100 and col2 not in ('999999')
and TRIM(TO_CHAR(col1)) in ('0','1')
group by col1,extract(year from DATUM);please can you help me understand why is this happening and why these 2 counts are different?
id appreciate any tips
thanks very much
rgds

UserMB wrote:
please can you help me understand why is this happening and why these 2 counts are different?
id appreciate any tipsThis is tricky. All others of cause are right. You must compare date columns with date values.
If you don't explicitly compare as a date value, then it might happen that a string comparison is made.
And if that happens both of your counts would return a wrong result. For example all dates that start with
'4' are not counted. Like '4-jan-1991'. The string '4-jan-1991' is greater then the string '31-dec-1991'. Therefore it would not be included in the count.
I don't think this happened. But the danger is there.
However I have a problem seeing, what the difference between your two counts can be.
The two important parts are the date between filter and the group by condition.
DATUM between '1-jan-1985' and '31-dec-2012'
DATUM between '1-jan-1991' and '31-dec-1991'
group by col1,extract(year from DATUM);The second option returns less results than the first option. Some rows must be missing because of this different filter condition.
If this would be a text comparison then there shouldn't be a difference. Only if it is a date comparison then this could be explained.
Here is an example:
Dates like 31-dec-1991 17:53:14 are included in the first count+group, but not included in the second count.
Why? Because the string '31-dec-1991' is converted into a date. This date is 31-dec-1991 00:00:00 (midnight). Everything from this day, that is not exactly on midnight is greater than this value and therefore not included in your second query.
So as others already pointed out you need to either truncate the date or to compare it a little differently.
select col1,extract(year from datum) yr, COUNT(*)
from tableA@dblink 
where DATUM >= to_date('01-01-1991','dd-mm-yyyy')
and  DATUM < to_date('01-01-1992','dd-mm-yyyy')
and col2 > 100 and col2 not in ('999999')
and TRIM(TO_CHAR(col1)) in ('0','1')
group by col1,extract(year from DATUM);This should give the same result as your first query.
Note that I changed the month from a month name to a number. This makes it independent from national language settings. E.g. DEZ = German, DEC = American
Edited by: Sven W. on Oct 9, 2012 2:04 PM

Similar Messages

  • The same selection doesn't return the same result in 2 identical systems

    Hi,
    After a copy we have 2 systems with same data.
    In a program the same selection doesn't returns the same result, it seems that the sort is different.
    I have checked indexes but they are ok in both systems.
    Here is the selection:
    SELECT but000~partner
    INTO TABLE lt_partner
    *UP TO 50 ROWS
    FROM but000
    JOIN but020
      ON but020partner = but000partner
    JOIN adrc
      ON adrcaddrnumber = but020addrnumber
    JOIN but100
      ON but100partner = but000partner
      WHERE but020~addr_valid_from LE '20100727000000'
      AND   but020~addr_valid_to   GE '20100727000000'.
    Is there a customizing point to define how the database must be sorted ?
    Thanks.
    Edited by: julien schneerberger on Jul 27, 2010 4:18 PM

    Hi,
    Thank you for your answer.
    Result is the same and I don't sort data after the selection.
    Do you think the system copy has "broke" the records order in database ?
    If it is the case it will be impossible to reproduce the same sort, is it right ?
    Edited by: julien schneerberger on Jul 27, 2010 5:19 PM
    Edited by: julien schneerberger on Jul 27, 2010 5:20 PM

  • How to find a selection of photos taken at different dates but imported together. I want them in the same album but they're no longer in the "last import" album.

    I imported a bunch of photos taken at different dates that i wanted to group together in the same album but they are not in the "last import" album and are all just mixed in with "all photos" according to the dates they were taken and it's impossible to sift through them all to select the ones i wanted.

    Sorry I'm terrible at explaining and even using my macbook haha. Let me try again. I imported some photos that I wanted to group together into an album. I forgot to create the album before importing a new set of photos which then took the place of the previous ones in the "last import" album. Is there anywhere or any way I can easily find the selection of photos I forgot to put into an album together without having to select them one by one from the "all photos" section? These would be the only pictures in my Photos application that haven't been organized into an album yet if that helps; however, these photos were not taken on the same date.

  • I have music files by the same artist but they are listed in itunes in more than one album hod can i add then to just one album pf that artist

    I have music files by the same artist but they are listed in Itunes in more than one album hod can i add then to just one album of that artist I am using windows 8.1 .

    iTunes will group songs into an album when:
    All songs have the same value for the Album and Artist field, or
    All songs have the same value for the Album and Album Artist field
    All songs have the same value for the Albumfield and the Part of a Compilation flag is checked.
    If you want to group all the songs by one artist into a single "album" you can do so by following one of these rules.  However, it may be more useful to create Playlists to group music like this and retain the original release information.  One Smart Playlist for each artist you're interested in would meet this need without over-writing information about the source albums.

  • I have photos with the same numer but they are different photos how can I fix that?

    I have photos with same names but they are different photos. Can I rename these photos ? How can I do this?

    you can save a file with the same name by dragging and dropping from iphoto to a Finder folder
    but (and there are always buts) you can only do up to 4 photos at a time
    any more and you are not given the option
    This is the error message you get:
    An item named “IMG_8770.JPG” already exists in this location. Do you want to replace it with the one you’re moving?
    4 or lesss  your options are:
    Keep Both       Stop       Replace
    5 or more your options change
    Skip     Stop    Replace
    it will add a space and a 2 to the name
    IMG_8816.JPG  turns to IMG_8816 2.JPG
    this is the solution I was looking for; let the computer do the name change
    Cheers!

  • Hello! Pls can anyone help. I am new to Lab view, having three(3) input variable entering a tank at the same time, but they not mixed?

    Pls, i really need help. I am having three (3) input variables entering a tank at the same time,but not mixed, they mixed only inside the tank. I assumed the tank to be an indicator then used bundle/unbundle didn't solve. I tried build array but still no solution. Thanks

    Abul, I would recommend posting in that other thread to follow up on your issues from before.  Also your problem is not very clear.  You may want to expand some more on what your issue, and a post a screen shot or a VI.
    Good Luck,
    Paul
    Paul Davidson
    Sound and Vibration Software Staff Product Support Engineer
    National Instruments

  • Where does Sting to IP get the net address if the computer is disconnected from the net? It should return the same I

    P/Net address� it did not change. Internet Protocal(TCP/IP) Properties did not change. When the network is physically disconnected, my VI after several runs returns 127.0.0.1. So, how do I get my IP address whether it�s connected to the network or not?Thanks in advance for your help.

    P/Net address� it did not change. Internet Protocal(TCP/IP) Properties did not change. When the network is physically disconnected, my VI after several runs returns 127.0.0.1. So, how do I get my IP address whether it�s connected to the network or not?If you're getting a dynamically assigned IP address from the network, then String to IP will return that IP but it may not be constant. See if TCP/IP properties are set to "Obtain an IP address automatically". If you disconnect from the network, then the IP address will default to something and I wouldn't be surprised if the default is 127.0.0.1. If you want to always have the same IP, then the ICP/IP properties should be changed to use a fixed IP but you'll certainly need to check with your IS department before doing so.

  • Shouldn't using WITH return the same results as if you'd put the results in a table first?

    First off, here's my version info:
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
    PL/SQL Release 11.1.0.7.0 - Production
    CORE 11.1.0.7.0 Production
    TNS for HPUX: Version 11.1.0.7.0 - Production
    NLSRTL Version 11.1.0.7.0 - Production
    I just reread the documentation again on the subquery factoring clause of SELECT statement, and I didn't see any restrictions that would apply.
    Can someone help me understand why I'm getting different results?  I'd like to be able to use the statement that creates MAT3, but for some reason it doesn't work.  However, when I break it up and store the last TMP subquery in a table (MAT1), I'm able to get the expected results in MAT2.
    Sorry if the example seems a little esoteric.  I was trying to put something together to help illustrate another problem, so it was convenient to use the same statements to illustrate this problem.
    drop table mat1;
    create table mat1 as
    with skus as (
      select level as sku_id
      from dual
      connect by level <= 1000
      tran_dates as (
      select to_date('20130731', 'yyyymmdd') + level as tran_date
      from dual
      connect by level <= 31
      sku_dates as (
      select s.sku_id,
      t.tran_date,
      case when dbms_random.value * 5 < 4
      then 0
      else 1
      end as has_changes,
      round(dbms_random.value * 10000, 2) as unit_cost
      from skus s
      inner join tran_dates t
      on 1 = 1
    select d.sku_id,
      d.tran_date,
      d.unit_cost
      from sku_dates d
      where d.has_changes = 1
    drop table mat2;
    create table mat2 as
    select m.sku_id,
      m.tran_date,
      m.unit_cost,
      min(m.tran_date) over (partition by m.sku_id order by m.tran_date rows between 1 following and 1 following) as next_tran_date
      from mat1 m
    drop table mat3;
    create table mat3 as
    with skus as (
      select level as sku_id
      from dual
      connect by level <= 1000
      tran_dates as (
      select to_date('20130731', 'yyyymmdd') + level as tran_date
      from dual
      connect by level <= 31
      sku_dates as (
      select s.sku_id,
      t.tran_date,
      case when dbms_random.value * 5 < 4
      then 0
      else 1
      end as has_changes,
      round(dbms_random.value * 10000, 2) as unit_cost
      from skus s
      inner join tran_dates t
      on 1 = 1
      tmp as (
      select d.sku_id,
      d.tran_date,
      d.unit_cost
      from sku_dates d
      where d.has_changes = 1
    select m.sku_id,
      m.tran_date,
      m.unit_cost,
      min(m.tran_date) over (partition by m.sku_id order by m.tran_date rows between 1 following and 1 following) as next_tran_date
      from tmp m
    select count(*) from mat2;
    select count(*) from mat3;
      from tmp m
    select count(*) from mat2;
    select count(*) from mat3;
    select count(*) from mat2;
      COUNT(*)
         31000
    Executed in 0.046 seconds
    select count(*) from mat3;
      COUNT(*)
             0
    Executed in 0.031 seconds

    I think there's something else going on.
    I made the change you suggested, with a slight modification to retain the same functionality of flagging ~80% of the rows as not having changes.  I then copied that section of my script - included below - and pasted it into my session twice.  Unfortunately, I got different results each time.  I have had a number of strange problems when using the WITH clause, which is one of the reasons I jumped at posting something here when I encountered it again in this context.
    Can you help me understand why this would happen?
    drop table mat3;
    create table mat3 as
    with skus as (
      select level as sku_id
      from dual
      connect by level <= 1000
      tran_dates as (
      select to_date('20130731', 'yyyymmdd') + level as tran_date
      from dual
      connect by level <= 31
      sku_dates as (
      select s.sku_id,
      t.tran_date,
      case when dbms_random.value(1,100) * 5 < 400
      then 0
      else 1
      end as has_changes,
      round(dbms_random.value * 10000, 2) as unit_cost
      from skus s
      inner join tran_dates t
      on 1 = 1
      tmp as (
      select d.sku_id,
      d.tran_date,
      d.unit_cost
      from sku_dates d
      where d.has_changes = 1
    select m.sku_id,
      m.tran_date,
      m.unit_cost,
      min(m.tran_date) over (partition by m.sku_id order by m.tran_date rows between 1 following and 1 following) as next_tran_date
      from tmp m
    select count(*) from mat2;
    select count(*) from mat3;
    152249 < mattk > drop table mat3;
    Table dropped
    Executed in 0.016 seconds
    152249 < mattk > create table mat3 as
                             2  with skus as (
                             3   select level as sku_id
                             4   from dual
                             5   connect by level <= 1000
                             6   ),
                             7   tran_dates as (
                             8   select to_date('20130731', 'yyyymmdd') + level as tran_date
                             9   from dual
                            10   connect by level <= 31
                            11   ),
                            12   sku_dates as (
                            13   select s.sku_id,
                            14   t.tran_date,
                            15   case when dbms_random.value(1,100) * 5 < 400
                            16   then 0
                            17   else 1
                            18   end as has_changes,
                            19   round(dbms_random.value * 10000, 2) as unit_cost
                            20   from skus s
                            21   inner join tran_dates t
                            22   on 1 = 1
                            23   ),
                            24   tmp as (
                            25   select d.sku_id,
                            26   d.tran_date,
                            27   d.unit_cost
                            28   from sku_dates d
                            29   where d.has_changes = 1
                            30   )
                            31  select m.sku_id,
                            32   m.tran_date,
                            33   m.unit_cost,
                            34   min(m.tran_date) over (partition by m.sku_id order by m.tran_date rows between 1 following and 1 following) as next_tran_date
                            35   from tmp m
                            36  ;
    Table created
    Executed in 0.53 seconds
    152250 < mattk > select count(*) from mat2;
      COUNT(*)
             0
    Executed in 0.047 seconds
    152250 < mattk > select count(*) from mat3;
      COUNT(*)
         31000
    Executed in 0.047 seconds
    152250 < mattk >
    152251 < mattk > drop table mat3;
    Table dropped
    Executed in 0.016 seconds
    152252 < mattk > create table mat3 as
                             2  with skus as (
                             3   select level as sku_id
                             4   from dual
                             5   connect by level <= 1000
                             6   ),
                             7   tran_dates as (
                             8   select to_date('20130731', 'yyyymmdd') + level as tran_date
                             9   from dual
                            10   connect by level <= 31
                            11   ),
                            12   sku_dates as (
                            13   select s.sku_id,
                            14   t.tran_date,
                            15   case when dbms_random.value(1,100) * 5 < 400
                            16   then 0
                            17   else 1
                            18   end as has_changes,
                            19   round(dbms_random.value * 10000, 2) as unit_cost
                            20   from skus s
                            21   inner join tran_dates t
                            22   on 1 = 1
                            23   ),
                            24   tmp as (
                            25   select d.sku_id,
                            26   d.tran_date,
                            27   d.unit_cost
                            28   from sku_dates d
                            29   where d.has_changes = 1
                            30   )
                            31  select m.sku_id,
                            32   m.tran_date,
                            33   m.unit_cost,
                            34   min(m.tran_date) over (partition by m.sku_id order by m.tran_date rows between 1 following and 1 following) as next_tran_date
                            35   from tmp m
                            36  ;
    Table created
    Executed in 0.078 seconds
    152252 < mattk > select count(*) from mat2;
      COUNT(*)
             0
    Executed in 0.031 seconds
    152252 < mattk > select count(*) from mat3;
      COUNT(*)
             0
    Executed in 0.047 seconds

  • I pre-ordered and album, it took my money and now the date has come and is asking for the same amount but i dont have it because it already took it... what can i do? or am i just already screwed

    i have no clue what to do, i'm thinking i just totally got screwed over with these products again. but i waited so long for this album and not i cant get it. so i'm a little angry. i just would really like to know if i could do anything.. at all. and i physically have a song from the album i bought a long time ago but that doesnt even show in my purchase history...

    Check your purchase and bank/credit card history. I preordered the Rival Sons' Great Western Valkyrie a couple of months ago and was charged for three individual songs at £0.79 each as they were released one-by-one and then the balance of £5.62 when the album was finally published last week. You should not have been charged twice.
    tt2

  • Two iphoto albums - should be the same, but extra images reported in one

    I have two iphoto albums which in theory should contain the same images - but one is reporting around 5500 and the other around 5570 - is there a simple way to identify the additional 70 images ?
    cheers for any help
    macbook air 6,2
    os x 10.10.1
    iphoto 9.6

    I guess at some point I must have added some files which are not on the phone - I can't think what and it probably doesn't matter - its just the not knowing that bugs me
    You could create a smart album in your library on the 2009 iMac and search for photos that have not been taken by the iPhone camera:
    This will show all photos you imported from other sources:
    Try the command:
         File > New Smart album
    Then set the Smart rule to "Camera Model is" and select your iPhone.
    After selecting the phone, switch the rule to "Camera Model is not", and you will see all photos that did not come from your iPhone.

  • My old computer crashed a few days ago. And my backup external drive apparently did not save my old Firefox profile, but my IP address should be the same. Any way to recover my old Bookmarks?

    When I set up my new computer, my internet connection & modem for DSL is the same, so my IP address should be the same. But I had to reinstall Firefox and I can't find the file for my old Firefox profile. If my IP address is the same, is there any way Firefox can recover my old bookmarks?

    See this support article: <br />
    https://support.mozilla.com/en-US/kb/Recovering+important+data+from+an+old+profile
    Your old Profile is located here in Win XP and W2K: <br />
    ''drive'':\Documents and Settings\''Windows login user name''\Application Data\Mozilla\Firefox\Profiles\''profile_name''

  • I recently purchased FaceTime from the Application Store. After approx. ±10 meg, the application resets and starts downloading again. I have tried to download it at different times of the day but with the same results. I have been in contact with Apple su

    I recently purchased FaceTime from the Application Store. After approx. ±10 meg, the application resets and starts downloading again. I have tried to download it at different times of the day but with the same results. I have been in contact with Apple support and have tried different options with the same result. I cannot download a new Version because in my Purchased List the applicaiton (FaceTime) is still there, and because I have halted it it tells me to resume. I can’t, because the same thing is going to happen. I have tried all the various troubleshooting tips supplied by my ISP and also Apple Support with no luck. I am currently working on a IMac OS 10.6.7. Has anybody got any ideas? Any suggestion would help. Thanks

    Amol Soni wrote:
    As I said, I tried everything what the manual says, but hard luck. I have taken an appointment for today, lets see what happens.
    Also there is no activity on unit when I use the remote. The unit is stuck on the first page itself where we have to choose the language. The unit seems to be fine to me but the real problem is remote does'nt have activity. The IR is not responding while pressing any of the button.
    Good luck with your appointment.
    Not much consolation but even new products can have issues and might need a trip to a store to return/replace.
    The reason i asked about the LED on the AppleTV is that occasionally it thinks it's paired with a specific remote and ignores others - the unpair keypress I described is quick and free.
    AC

  • My iphone 6 connects to the car via bluetooth, the music works good, but the phone calles does not work.  It looks like it is working but doesn't.  I have tried in my Hyundai and a Dodge rent car and get the same results.  I updated the last 8.0.2.

    My iphone 6 connects to the car via bluetooth, the music works good, but the phone calls does not work.  It looks like it is working but doesn't.  I have tried in my Hyundai Sonata and a Dodge Dart rent car and get the same results.  I updated the last 8.0.2.  It worked the first day i had the phone, and then i updated to Ios 8.0.2 and it quit working.
    Now when i get in the car, it acts like it is connected and makes the same call it was on after syncing to bluetooth, but it really isn't on a call.  This is happening on both cars.
    Does anyone know if this is the phone and i need to take it to Apple or if there is an issue that Apple is working on getting a fix for?
    My son in law has the exact same phone as me, we both got the on 10/6, he had a Dodge Dart and his is working via bluetooth.
    Someone HELP please, as i consider this a safety issue by not having my calls go to bluetooth.

    We had the same problem, but figure out the solution.
    You MUST have at least 1 song added to your ITUNE!  After you add a free song, then everything else should work as normal!
    Hope this helps!

  • I'm playing galaxy empire online game,when I want to buy any dark matter,but can not,n ask me to see I tunes support,do you know why?I have try many times,n have the same result..thx

    I'm playing galaxy empire game,when I try to buy dark matter,It cannot,n ask me to see iTunes support,...what should I do?I have try for many times,but I've got the same result...plz help me,thanks

    Hi Julius,
    As you can see in my profile section, my MBP is older by 5 years, 2GB RAM and with regards to question 1 (Q1), has no problems with having all those programs open at the same time with the possible exception of Photoshop since I use Gimp instead. With twice the RAM and much overall much better specs, Photoshop shouldn't be an issue. But you listed some other activities that might need that additional RAM. But the advise most give is to not get it from Apple but from a third party.
    Q5 - I always get Apple Care. Only had to use it once but it paid for itself that one time.
    Sorry, can't help you much with the questions you really want answered since I won't have a new MBP until (hopefully) next week but for Q4, I believe it depends on the peripheral. My current monitors are too old to allow them to be hooked up in a chain. Supposedly, if the monitors do have the ports, you could via the MBP Thunderbolt port. The speeds are also dependent upon the peripheral. If the external hard drive is using a USB 2 port, you are limited by that speed (480 Mb/s). If it has a Thunderbolt port, then from what I understand, we, my friend, are in for some great times because I believe your RAM will have the ability to access data as fast as it can take it at 10Gb/s (and if will need those kinds of speeds, the 8GB of RAM may be the way to go). The Thunderbolt demos on YouTube where they are accessing four video files from a Raid drive, then sending them back out via Final Cut Pro to a monitor are amazing. I'll probably never push this machine to anywhere near it's limits but I can't wait to see what others do.
    Peace,
    Jeremiah

  • HI ALL SINCE TWO DAYS I AM DOWNLOADING VIBER BUT I AM NOT HAVING MY ACCESS CODE AND EVERY TIME I DELETE THE APPLICATION AND DOWNLOADING IT AGAIN BUT ALWAYS THE SAME RESULT PLEASE I NEED YOUR HELP FOR THE ACCESS CODE.MY PHONE NUMBER IS 0022996969896.THANKS

    HI ALL SINCE TWO DAYS I AM DOWNLOADING VIBER BUT I AM NOT HAVING MY ACCESS CODE AND EVERY TIME I DELETE THE APPLICATION AND DOWNLOADING IT AGAIN BUT ALWAYS THE SAME RESULT PLEASE I NEED YOUR HELP FOR THE ACCESS CODE.MY PHONE NUMBER IS 0022996969896.THANKS IN ADVANCE

    try this website this should help you http://helpme.viber.com/index.php?/Knowledgebase/List/Index/1/iphone

Maybe you are looking for

  • Error7 (windows error 127) when launching iTunes 11.1.4

    I tried the complete uninstall of iTunes (HT1923), but get the same error after re-install.  This really blows...and all for a "minor" update.  Any other suggestions?

  • Photo page won't load thumbnails, but slideshow works (PC:IE)

    hi everyone, problem with the thumbnail on the photo page, it shows 3-5 out of 20s thumbnails(only show photo's names) on other browser except Safair. on mac, safari works, thumbnail, slideshow work. on mac, Firefox 1.5, thumbnail won't works, but sl

  • Can i take a picture of a video in iPhoto?

    If it's possible, how is it?

  • Cs3 error loading

    hi ...i have done alot of reading trying to remedy this situation.. i know it can be traced to when i upgraded my zonealarm to extreme.... 7 did a pc tuneup... it found alot of errors in registies...& probably reversed the fix i did the last time thi

  • ICal subscription never shows new events

    Hello, Whenever I create a new event in iCal it doesn't appear in the iCals on machines that are only subscribing, rather than syncing. For example, my wife's subscription to my calendars never updates. All the iCals that are syncing rather than subs