views:

110

answers:

2

I am writing a foxpro sql & need to combine three date columns from three different tables into a new date column. As an example if the three tables are A,B,& C and the date Columns are Adate, Bdate and Cdate, how do I combine them as distinct and separate dates into a column called TransDate and in the SQL? I would appreciate samples of the code if possible, as I am very new at this

A: 

Yeah, more date clarification is needed. You can't combine 3 into 1... or did you mean you want 1 result row that shows all 3 dates associated with a given transaction? Or, you want (for example) the EARLIEST of 3 dates to identify when the first transaction date started? In any event, please clarify in your question, but here is a possible options for you without knowing more of your table's structures... Also, if you are doing it directly from foxpro (VFP) tables, you'll have to obviously add the ";" as line continuation to the following statements

select 
      T1.ADate,
      T2.BDate,
      T3.CDate,
      iif( T1.ADate > T2.BDate and T1.ADate > T3.CDate, T1.ADate,
           iif( T2.BDate > T1.ADate and T2.BDate > T3.CDate, 
               T2.BDate, T3.CDate ) ) as LargestDate,
      iif( T1.ADate < T2.BDate and T1.ADate < T3.CDate, T1.ADate,
           iif( T2.BDate < T1.ADate and T2.BDate < T3.CDate, 
               T2.BDate, T3.CDate ) ) as EarliestDate
   from 
      YourTableA T1,
      YourTableB T2,
      YourTableC T3
   where 
          T1.IDKey = T2.IDKey
      AND T1.IDKey = T3.IDKey
   into 
      cursor C_SomeTestResultCursor

Again, I don't know what your "Key" is that would be common between your three tables, so you'll obviously have to adjust the query on whatever that element(s) is(are).

DRapp
I wish to show each type of transaction (Purchase, stock adjustment,or sale) against its date where all the individual dates from the three separate source tables are combined into a single date column.Thanks for your help so far. I shall try your suggestion
Tass-man
Can you explain what you mean by combine dates? If table A says Jan st, table B says Feb 2nd, table C says March 3rd, what would you like in your result table? – Andomar 2 days ago.Using the above as an example, what I would like to show is each of the individual dates in a "combined" date column ieJan 1Feb2March 3with all the other columns being pulled from the three tables along side the "combined" date column. The idea is to show the purchases, stock adjustments and sales of any particular stock line by its transaction date
Tass-man
Looking at how the above comment reads, I can see this may cause confusion: I want separate lines for each date in the combined date column.
Tass-man
A: 

New answer option... from what your comment was about having a purchase, or stock adjustment or sale, and without any clear description or sample of the output you would LIKE to see, I am thinking you want something like...

Table A = Purchases Table B = Adjustments Table C = Sales

Not sure I would agree with this construct, but can only imply from brief descriptions. I think you want more of a "ledger" style summary

Purchase Jan 10 Purchase Jan 15 Sale Feb 2 Adjustment Feb 10 Purchase Feb 15 etc...

Then, I would do a UNION

select  
      "Purchase   " as TransType,
      T1.ADate as TransDate
   from
      YourTableA T1
   where 
      T1.IDKey = ?SomeValueSuchAsAccount
   order by
      TransDate
UNION ALL
select
      "Adjustments" as TransType,
      T2.BDate as TransDate
   from
       YourTableB T2
   where 
      T2.IDKey = ?SomeValueSuchAsAccount
UNION ALL
select
      "Sales      " as TransType,
      T3.CDate as TransDate
   from
       YourTableB T3
   where 
      T3.IDKey = ?SomeValueSuchAsAccount

If I'm incorrect on my assumptions, please provide myself and others a little more samples of such transactions, data layout, etc... Live data doesn't need be required, nor do full table structures or actual table names. However, we DO need more to work with, especially if you want assistance in the future, otherwise, people may just blow by your requests.

DRapp
DRapp thanks for your help so far.I would like to show all the info for a given product on the same date line if possible. That is if the product is purchased on 1st Jan, adjusted on 1st Jan and then sold on 1st Jan, one line is shown, whereas if it is purchased on 1st Jan, adjusted on 2nd Jan and sold on 3rd Jan three lines will be shown. I was hoping that this may be achievable by creating a new column into which the three other dates could be combined inserted and used to sort the data into the output of the tables.
Tass-man
Unfortunately ther is no key thqt links the various dates nor any of the purchase info to the likes of adjustment info or sales info.The only thing common would be the dates from the three separate tables.
Tass-man
Then you are almost completely out of luck. There has to be something between the tables that you can relate the tables. I would assume there is some sort of product ID (stock) between the 3 tables. Additionally, dealing with stocks, it sounds like your tring to manage some sort of mutual fund and not specific to an individual investor. You need common ground between tables.
DRapp