StartDate and EndDate values are multiplied, Can't get the right values

Building: Im trying to build a report where I can calculate the difference between two dates (Example between 1-1-2001 and 31-12-2001) but also showing (per name per identifier) the current balance of a customer per date(As shown in the Report attachment).
Problem: The problem is that the totals are not correct, they get multiplied on random numbers (Checking it with Count and CountDistinct I learned that I probably made a mistake in my SQL, so I copied SQL in this question as well).
I have tried plenty myself and tried to find solutions on the interwebs, but none of them seem to work.  So I am desperate enough to ask for help :).
SELECT
CASE WHEN t1.identifier IS NULL THEN t2.identifier ELSE t1.identifier
END as identifier
, CASE WHEN t1.firstName IS NULL THEN t2.firstName ELSE t1.firstName
END as firstName
, CASE WHEN t1.lastName IS NULL THEN t2.lastName ELSE t1.lastName
END as lastName
, CASE WHEN t1.initials IS NULL THEN t2.initials ELSE t1.initials
END as initials
, CASE WHEN t1.name IS NULL THEN t2.name ELSE t1.name
END as name
, CASE WHEN t1.employmentNumber IS NULL THEN t2.employmentNumber ELSE t1.employmentNumber
END as employmentNumber
, CASE WHEN t1.externalIdentifier IS NULL THEN t2.externalIdentifier ELSE t1.externalIdentifier
END as externalIdentifier
, t1.balance AS S_balance
, t1.unvested AS S_unvested
, t2.balance AS V_unvested
, t2.unvested AS V_balance
, SUM(t1.TotalBalance + t1.TotalUnvested) Total1
FROM
SELECT
par.identifier
,par.firstName
,par.lastName
,par.initials
,pos.name
,pos.balance
,pos.unvested
,par.employmentNumber
,SUM(pos.balance) TotalBalance
,sum(pos.unvested) TotalUnvested
,Right(par.externalIdentifier,(Len(par.externalIdentifier)-4)) as externalIdentifier
FROM
participant par
INNER JOIN position pos
ON par.identifier = pos.participantIdentifier
WHERE
(pos.balance != 0 OR pos.unvested != 0)
AND pos.name NOT LIKE (@Euro)
AND par.Metadata_start_validity <=@Datum
AND (par.Metadata_end_validity >=@Datum
OR par.Metadata_end_validity IS NULL)
AND pos.Metadata_start_validity <=@Datum
AND (pos.Metadata_end_validity >=@Datum
OR pos.Metadata_end_validity IS NULL)
GROUP BY
par.identifier
,par.firstName
,par.lastName
,par.initials
,pos.name
,pos.balance
,pos.unvested
,par.employmentNumber
,par.externalIdentifier
) as t1
INNER JOIN
SELECT
par.identifier
,par.firstName
,par.lastName
,par.initials
,pos.name
,pos.balance
,pos.unvested
,par.employmentNumber
,Right(par.externalIdentifier,(Len(par.externalIdentifier)-4)) as externalIdentifier
FROM
participant par
INNER JOIN position pos
ON par.identifier = pos.participantIdentifier
WHERE (pos.balance != 0 OR pos.unvested != 0)
AND pos.name NOT LIKE (@Euro)
AND par.Metadata_start_validity <=@V_Datum
AND (par.Metadata_end_validity >=@V_Datum
OR par.Metadata_end_validity IS NULL)
AND pos.Metadata_start_validity <=@V_Datum
AND (pos.Metadata_end_validity >=@V_Datum
OR pos.Metadata_end_validity IS NULL)
) as t2
ON
t1.identifier = t2.identifier
GROUP BY
CASE WHEN t1.identifier IS NULL THEN t2.identifier ELSE t1.identifier
END
, CASE WHEN t1.firstName IS NULL THEN t2.firstName ELSE t1.firstName
END
, CASE WHEN t1.lastName IS NULL THEN t2.lastName ELSE t1.lastName
END
, CASE WHEN t1.initials IS NULL THEN t2.initials ELSE t1.initials
END
, CASE WHEN t1.name IS NULL THEN t2.name ELSE t1.name
END
, CASE WHEN t1.employmentNumber IS NULL THEN t2.employmentNumber ELSE t1.employmentNumber
END
, CASE WHEN t1.externalIdentifier IS NULL THEN t2.externalIdentifier ELSE t1.externalIdentifier
END
, t1.balance
, t1.unvested
, t2.balance
, t2.unvested
, t1.TotalBalance
, t1.TotalUnvested

Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules (you have no idea,
do you?). Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. 
This is minimal polite behavior on SQL forums. Did you know that camelCase does not work? Google the research. There are no generic “<nothing in particular>_name”, <magic universal>_identifier”, etc in a valid data model. 
This codes tells us you have attribute splitting in the schema:
 T1.balance AS S_balance, T1.unvested AS S_unvested, 
 T2.balance AS V_unvested, T2.unvested AS V_balance, 
I am sorry you have only one “Participant” and one “Position”, which is what your singular table name tell the world. 
Your “vested” and “unvested” are a status value, but you put them into separate tables. This is as silly as having “Male_Personnel” and “Female_Personnel” tables instead of the correct “Personnel” table. This why you are re-creating the name-address columns
every time! 
What kind of entity is an “external”? You have an “external_identifier”, so there has to be such an entity. Even worse you have:
RIGHT(PAR.external_identifier, (LEN(PAR.external_identifier)-4))
This tells us that this vague column is a concatenation of two or more data elements. This is not a valid schema design. And when you write:
AR.identifier = POS.participant_identifier
Your magical generic “identifier” changes to a participant. In RDBMS, a data element has one and only one name, and doer not change from table to table.  Can it also become a squid? An automobile? 
I see you are an old punch card programmer. Back in the old days, we put one expression per card (line) so we could re-arrange the deck. This also meant that the comma went at the front line. And we even parentheses and keywords on single cards! WOW! 
You do not know about COALESCE() and use CASE the way that you would use IF-THEN in COBOL. Look at this code. Why did you think that “@datum” was a clear, precise data element name? It is the most generic name for a single data element value (not even the element
itself). 
 AND PAR.metadata_start_validity <= @vague_generic_something
 AND (PAR.metadata_end_validity >= @vague_generic_something
 OR PAR.metadata_end_validity IS NULL)
COBOL, C and the other procedural languages that you are trying to write in SQL do no have the SQL shorthand, so you use Boolean operators. We would write:
@vague_generic_something
 BETWEEN PAR.metadata_start_validity 
   AND COALESCE(PAR.metadata_end_validity, @vague_generic_something)
But we never put metadata into a schema. NEVER. “Validity” makes no sense. Is it a date? A physical location? What? It has to be on a scale on which theta operators apply. Your validity hs no ordering! 
You do not know that SQL uses <> and not != from the C family. 
We do not use bit flags; that was assembly language, not SQL. The term “Euro” is a monetary unit of measurement, not value. 
You are getting garbage data because you have a garbage schema. Post the DDL and someone might be able to help you replace it. 
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL

Similar Messages

  • I downloaded bundle for ms office only to realise its just a template.my questions are can i be refunded and how can i get the right msoffice because am in need of it badly

    i downloaded bundle for ms office only to realise its just a template.my questions are can i be refunded and how can i get the right msoffice because am in need of it badly

    You can request a Mac App Store refund at reportaproblem.apple.com
    You can buy Microsoft Office at office.microsoft.com

  • I downloaded microsoft office to my MBP and my question is how do i get the right file or operating system to open it and so that i can use it?

    i downloaded microsoft office to my MBP and my question is how do i get the right file or operating system to open it and so that i can use it?

    Welcome to the Apple Support Communities
    There are two Office versions: Office for Windows, and Office for Mac.
    I suspect that you have downloaded Office for Windows, and you can use it if you install Windows, but a cheaper and easiest way to use Office is to use Office for Mac, so you won't have to install Windows. See > http://www.microsoft.com/mac

  • I am trying to set up a co-worker's email on her iPhone and I don't know what her password for Thunderbird is and neither does she. Can I get the password?

    I am trying to set up a co-worker's email on her iPhone and I don't know what her password for Thunderbird is and neither does she. Can I get the password somehow, or what do you recommend?

    There is no password to Thunderbird. The password you need is to the email account. Contact the email provider for password reset.

  • HT2712 Network ip address keeps changing and for some reason I can't find the right settings. The diagnostics says to check with my network  administrator,  but idk whom that is being at a motel and all. I have made. New locations, & changed the ipv4 addr

    Network ip address keeps changing and for some reason I can't find the right settings. The diagnostics says to check with my network  administrator,  but idk whom that is being at a motel and all. I have made. New locations, &amp; changed the ipv4 address. Help plz

    Hmmm, is Network set to using DHCP?
    Go to System Preferences
    Click Network
    Highlight AirPort and click Configure...
    Choose “By default, join: Preferred networks”
    Select your access point and Remove your access point with the minus button.
    Launch your keychain access in Utilities and delete your access point keychain entry.
    Reboot
    Go back to the “By default, join:” page and click the plus this time to add your access point. Enter the correct password, save, reboot.
    Instead of joining your network from the list, click the WiFi icon at the top, and click join other network. Fill in everything as needed

  • Hello, trying to download the latest iTunes software version, a pop up comes with a "invalid signature" message. Try to do download only with the same results. I can buy and download from iTunes but can't  get the latest version?

    Hello, trying to download the latest iTunes software version, a pop up comes with a "invalid signature" message. Try to do download only with the same results. I can buy and download from iTunes but can't  get the latest version?

    Transfer Purchases  = iTunes > File > Transfer Purchases
    http://support.apple.com/kb/HT1848

  • Remember only the icloud email address, forget the password, birthdate, and verification questions. How can i get the icloud email back?

    Remember only the icloud email address, forget the password, birthdate, and verification questions. How can i get the icloud email back?

    Go to https://getsupport.apple.com ; click 'See all products and services', then 'More Products and Services, then 'Apple ID', then 'Other Apple ID Topics' then 'Forgotten Apple ID security questions'.

  • I think I goofed when running disk utility, and unmounted at the end and restarted. Now I can't get the disk to mount. What am I missing?

    I think I goofed when running disk utility, and unmounted at the end and restarted. Now I can't get the disk to mount. What am I missing?

    Which disk won't mount? What version of OS X? Your post is impossible to answer with no useful information.

  • The new ibook app and ibook update is designed for iPhone 5 operating system.   When my iPhone4 recently updated the ibook app, it created many problems using the ibook to download and read my books.    Can I get the previous ibook app back on my phone.

    The new ibook app and ibook update is designed for iPhone 5 operating system.   When my iPhone4 recently updated the ibook app, it created many problems using the ibook to download and read my books.    Can I get the previous ibook app back on my phone.

    Check your trash can on the desktop. The old version is automatically moved to the trashcan. You should be able to move it from the trashcan back to the Apps folder within the iTunes folder. The update that was pushed out was for iBooks to work with iOS 7 not iPhone 5. iOS 7 was released today.

  • How can I get the right click to work in firefox -- it works in internet explorer

    how can I get the right click to work in firefox -- it works in interne

    Try the Firefox SafeMode to see how it works there. <br />
    ''A troubleshooting mode, which disables most Add-ons.'' <br />
    ''(If you're not using it, switch to the Default Theme.)''
    * You can open the Firefox 4/5/6/7 SafeMode by holding the '''Shft''' key when you use the Firefox desktop or Start menu shortcut.
    * Or use the Help menu item, click on '''Restart with Add-ons Disabled...''' while Firefox is running. <br />
    ''Don't select anything right now, just use "Continue in SafeMode."''
    ''To exit the Firefox Safe Mode, just close Firefox and wait a few seconds before using the Firefox shortcut (without the Shft key) to open it again.''
    If it is good in the Firefox SafeMode, your problem is probably caused by an extension, and you need to figure out which one. <br />
    http://support.mozilla.com/en-US/kb/troubleshooting+extensions+and+themes

  • HT4759 how can i get the right version to install icloud on my mac

    how can i get the right version to install icloud on my mac

    Open the Mac App Store and buy Mountain Lion.
    (90636)

  • Airport I just can use one device to connect with the internet, PPoE setup problem. Can't get the right ip

    My airport exrreme can't get the right ip if I try to make a new network
    I use also the apps
    I have 3 devices but just can used always one
    My ip and my user name is from the university
    I need to fix the ip, username and password in the airport extrem, but he always copy a wrong ip in the system
    Thanks for help
    Elias

    You're only allowed to authorize 5 devices with your apple id.
    How many have you got authorized?
    Maybe deauthorize one of them.

  • I'm looking for some help connecting linksys IP Cameras to my home network to monitor my property when I'm travelling. I used to do this with linksys WAPS, but since I've discarded all my old linksys networking and standardized on airport, I can't get the

    I'm looking for some help connecting linksys IP Cameras to my home network to monitor my property when I'm travelling.  I used to do this with linksys WAPS, but since I've discarded all my old linksys networking and standardized on airport, I can't get these things working.  I know that I have to identify my camera through the DHCP table and set up port forwarding and there is the problem. 
    My network consists of 4  base stations set up in a roaming network - same network name and passwords.  I need to do it this way so I don't have to switch network when I move from one side of the house to the other, go to the cabana, or my shop in the barn.  The network works pretty well since I went to a roaming set up.  Good performance, yata, yata, yata.
    However, the roaming network requires the AEBS's to be set up in bridge mode, rather than sharing an ip address.  When the AEBS is set to  bridge mode, you don't see a DCHP table or have the ability to identify your IP Cam through the AEBS - and hence, no port forwarding. 
    I am able to identify and set up my Linksys IP Cam by locating the ip address on my FIOS router, even though, it's plugged into an AEBS.  I set it up, see the video, remove the ethernet cable from the IP Cam, restart - and I can't get to it from an AEBS.  In researching this, it appears, I should be setting up the AEBS to "share an IP Address", going to the DHCP table and identifying the camera's IP address and setting up port forwarding.  However, you don't see any of the DHCP or port forwarding options in Airport Network Utility when configuring in bridge mode. 
    I'm hoping I'm missing something here and that the solution isn't to set it up at the FIOS router level, but I'm beginning to think that's my only hope.  What concerns me there is that I should be able to see the IP cam on the network without port forwarding since I'm not coming from outside, and I can't even do this unless it's connected hard wire.
    I'd appreciate any insight into this that anyone might have.  I've hit the wall with what I know.
    Thanks.

    In a roaming network, your "main" router is the device that would require port mapping/forwarding to be configured in order to access the IP camera from the Internet. This router is also the one that would be provide the private IP address for the camera which you will want to be a static one.
    So as you described your network, the IP cameras should be getting an IP address or you assigned it a static one and this is the address that you would enter in the Private IP address (or equivalent depending on the router used) field when setting up port mapping.
    If you are not able to access this camera from the local network, then this should be troubleshot first.

  • I've read two solutions to my problem and neither worked. I can't get the repeat option to show in some events, though it does appear in others.

    I can't get the "repeat" option to show in some events, though it does appear in others. How can i get it to show when I need it? (I read two "soluntions" that supposedly worked, but neither worked for me.)

    Greetings,
    I can't say I've seen that behavior before.  I' be curious to see a screen shot of it in the edit mode where you edit the fields.
    Beyond that I'll offer generic troubleshooting steps:
    Go to Apple Menu > System Preferences > Language & Text > Formats > Region. Region should be set to the country of your choice. Do not have it set to "Custom" as this can have unexpected results in the iCal window.
    If that doesn't resolve the issue:
    1. First make an iCal backup:  Click on each calendar on the left hand side of iCal one at a time highlighting it's name and then going to File Export > Export and saving the resulting calendar file to a logical location for safekeeping.
    2. Go to iCal > Quit iCal
    3. Remove the following to the trash and restart your computer:
    Home > Library > Caches > com.apple.ical
    Home > Library > Calendars > Calendar Cache, Cache, Cache 1, 2, 3, etc. (Do not remove Sync Cache or Theme Cache if present)
    --- NOTE: To get to "Home > Library" in Lion: Click anywhere on the desktop and then click on the "Go" menu at the top of the computer screen while holding down the "option" key on the keyboard.  You will see "Library" in the menu.
    4. Launch iCal and test.
    If the issue persists:
    1. Go to iCal > Quit iCal
    2. Remove the following to the trash and restart your computer:
    Home > Library > Caches > com.apple.ical
    Home > Library > Calendars > Calendar Cache, Cache, Cache 1, 2, 3, etc. (Do not remove Sync Cache or Theme Cache if present)
    Home > Library > Preferences > com.apple.ical (There may be more than one of these. Remove them all.)
    --- NOTE: Removing these files may remove any shared (CalDAV) calendars you may have access to. You will have to re-add those calendars to iCal > Preferences > Accounts.
    3. Launch iCal and test.
    Hope that helps!

  • I can not get the right frequency out of my NI-DAQ 6062E card from Matlab

    Hi.
    My name is Johan and I want to generate an analog square waveform with 5.76 kb/s in frequency with my NI DAQCard-6062E from Matlab.
    When I run in Matlab the program below, I get the frequency of 5.747 kb/s on the output channel (even thou I have chosen 5.76 kb/s). When I try to change the sample rate, to higher values in Matlab, I get the same frequency until I choose the sample rate to 5.79 kb/s. The output signal is in that case is 5.813 kb/s.
    So my problem is that I can't get the exact frequency of 5.76 kb/s.
    How do I solve this.
    Thankful for help.
    Best regards, Johan
    Program in Matlab:
    clear all
    openDAQ=daqfind; %Detect any open DAQ channels
    for i=1:length(openDAQ), %Close any open DAQ channels
      stop(openDAQ(i));
    end
    ao = analogoutput('nidaq',1); %Create an object for analog output
    addchannel(ao, [0 1]);
    set(ao, 'SampleRate', 2*5760); %Here the sample rate is specied. 2*, because to get right samplerate
    y=[80,-80] %Sets theoutput values to 80 and -80 (As -1 and 1)
    putdata(ao, [y' y']); %Sends the values to out buffer
    set(ao,'RepeatOutput',inf) %Repeats sending in infinity
    set(ao, 'TriggerType', 'Immediate'); %Choose trigger to immediate, because sending values shall occur driect after start command.
    start(ao);
    %stop(ao)

    Hello Johan!
    Bad news I am afraid….
    I assume you are using the Data Acquisition Toolbox from the Mathworks and when it comes to support on that interface I have to advice you to contact the Mathworks for assistance. We don't provide any support on their data acquisition interface since we have nothing to do with it.
    Sorry for the inconvenience this might cause you.
    Regards,
    Jimmie A.
    Applications Engineer, National Instruments
    Regards,
    Jimmie Adolph
    Systems Engineer Manager, National Instruments Northern Region
    Bring Me The Horizon - Sempiternal

Maybe you are looking for

  • Printing a PDF back to back

    Hi I have this weird problem. I am trying to print this 2 page pdf back to back, however I am unable to do so. All my office printers are set by default to do back to back printing. My other word and txt documents all get printed back to back except

  • Exported KN loop has a delay in it that doesn't exist in the KN file!

    I've exported my KN file to QT. There are only 4 slides with some transitions and magic moves. The transition from the third to the fourth slide (in the KN file) is a dissolve with a .2 second delay. In the QT version of the file the delay is closer

  • I have a macbook pro md318 late 2011 i want to know can i change hard drive from 500GB to 2TB????

    i have a macbook pro md318 late 2011 i want to know can i change hard drive from 500GB to 2TB????

  • Passing Internal Tables

    I have a function ZMYFUNCTION belonging to ZMYFUNCTIONGRP function group.  In the TOP include of the group I have the following type declared: TYPES: BEGIN OF bundle_struc,                   exidv  LIKE vekp-exidv,                   vhilm  LIKE vekp-

  • Re: Product Costing

    Hi All, I am having some doubt in product costing side of production. I am having a scenario,where I do create a Process order for 10 pc & I do issue Raw material (manually,using GI-261).Then I do confirmation for 9 pc. Now 1st.: What should be the W