| Author |
Topic |
|
stillme67
Starting Member
4 Posts |
Posted - 2007-12-11 : 16:28:10
|
| i am completely new to sql but have a need to create a sort of check registry.the data comes from two separate files likechecksdate num name acct amount 09/17/07 1747 companyA tmz1 87.5009/18/07 1748 companyB tmz1 99.2409/18/07 9326 company1 tmz2 9.6509/18/07 1749 companyC tmz1 103.5409/20/07 9327 company2 tmz2 55.0109/20/07 1750 companyD tmz1 87.12deposits date type acct amount09/17/07 deposit tmz1 100.0009/17/07 deposit tmz2 200.0009/19/07 deposit tmz1 300.0009/19/07 deposit tmz2 400.00i would like something like this as a result(1st group - tmz1)09/17/07 1747 companyA tmz1 87.5009/17/07 deposit tmz1 100.0009/18/07 1748 companyB tmz1 99.2409/18/07 1749 companyC tmz1 103.5409/19/07 deposit tmz1 300.0009/20/07 1750 companyD tmz1 87.12(2nd group - tmz2)09/17/07 deposit tmz2 200.0009/18/07 9326 company1 tmz2 9.6509/19/07 deposit tmz2 400.0009/20/07 9327 company2 tmz2 55.01but when i form my statement with an inner join, i miss records, with the outer, i get a ton of records (more than both tables combined).i have been working with sql for about week now and can do simple queries but this is beyond me at this point. any help what so ever would be greatly appreciated. |
|
|
stillme67
Starting Member
4 Posts |
Posted - 2007-12-12 : 19:39:09
|
| no one? can this not be done? am i inquirying in the wrong area?or do i need to explain this better?any help would be awesome... thanksBill |
 |
|
|
singularity
Posting Yak Master
153 Posts |
Posted - 2007-12-12 : 19:59:36
|
| I think all you need to do is union the two tables together. No joins necessary.select date, num, name, NULL as type, acct, amount from checksunionselect date, NULL as num, NULL as name, type, acct amount from depositsIf you need to split the result set into separate tables based on the account name, you can do:select date, num, name, NULL as type, acct, amount from checkswhere acct = tmz1unionselect date, NULL as num, NULL as name, type, acct amount from depositswhere acct = tmz1 |
 |
|
|
stillme67
Starting Member
4 Posts |
Posted - 2007-12-12 : 21:56:35
|
| hey thank you much ... i will try it out! |
 |
|
|
stillme67
Starting Member
4 Posts |
Posted - 2007-12-19 : 16:46:04
|
| this is my resulting sql. both parts work fine separately but fails once i put it together with the union statement. select t0.company_id t0company_id, t0.bank_account_id t0bank_account_id, t0.check_number t0check_number, t0.check_date t0check_date, t0.payee_name t0payee_name, t0.amount t0amount, t0.clear_date t0clear_date, t0.void_date t0void_date, NULL AS t0bank_trx_code_id,'t0amountsub' = case when t0.void_date > 01/01/1990 then 0 when t0.void_date < 01/01/1900 then t0.amount else t0.amount end, 't0comp_name' = case when t0.company_id = 'TMS' then 'Van' when t0.company_id = 'TMS2' then 'Tanker' when t0.company_id = 'TMS3' then 'TMS3' when t0.company_id = 'TMS4' then 'TMS4' when t0.company_id = 'TMS5' then 'TMS5' end from ap_check t0 where t0.check_date >= 12/01/2007 order by t0.company_id ASC, t0.check_number ASCunionselect t1.company_id t0company_id, t1.bank_account_id t0bank_account_id, t1.amount t0amount, t1.transaction_date t0check_date, t1.bank_trx_code_id t0bank_trx_code_id, NULL AS t0check_number, NULL AS t0payee_name, NULL AS t0clear_date, NULL AS t0void_date, NULL AS t0amountsub,'t0comp_name' = case when t1.company_id = 'TMS' then 'Van' when t1.company_id = 'TMS2' then 'Tanker' when t1.company_id = 'TMS3' then 'TMS3' when t1.company_id = 'TMS4' then 'TMS4' when t1.company_id = 'TMS5' then 'TMS5' end from bank_rec_trx t1where t1.bank_trx_code_id = 'DEP' and t1.transaction_date >= 12/01/2007order by t1.company_id ASCcould this be due to my use of aliases? or possibly the use of the same alias for two different fields? ie t1.transaction_date t0check_date (2nd table) & t0.check_date t0check_date (1st table) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-19 : 17:06:59
|
[code]DECLARE @Checks TABLE (Date DATETIME, Num INT, Name VARCHAR(10), Acct VARCHAR(10), Amount MONEY)SET DATEFORMAT MDYINSERT @ChecksSELECT '09/17/07', 1747, 'companyA', 'tmz1', 87.50 UNION ALLSELECT '09/18/07', 1748, 'companyB', 'tmz1', 99.24 UNION ALLSELECT '09/18/07', 9326, 'company1', 'tmz2', 9.65 UNION ALLSELECT '09/18/07', 1749, 'companyC', 'tmz1', 103.54 UNION ALLSELECT '09/20/07', 9327, 'company2', 'tmz2', 55.01 UNION ALLSELECT '09/20/07', 1750, 'companyD', 'tmz1', 87.12DECLARE @Deposits TABLE (Date DATETIME, Type VARCHAR(10), Acct VARCHAR(10), Amount MONEY)INSERT @DepositsSELECT '09/17/07', 'deposit', 'tmz1', 100.00 UNION ALLSELECT '09/17/07', 'deposit', 'tmz2', 200.00 UNION ALLSELECT '09/19/07', 'deposit', 'tmz1', 300.00 UNION ALLSELECT '09/19/07', 'deposit', 'tmz2', 400.00SELECT Acct, Date, Num, Type, Name, AmountFROM ( SELECT Acct, Date, Num, NULL AS Type, Name, Amount, 0 AS SortOrder FROM @Checks UNION ALL SELECT Acct, Date, NULL, Type, NULL, Amount, 1 FROM @Deposits ) AS dORDER BY Acct, Date, SortOrder, Num[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|