| Author |
Topic |
|
UnathiM
Starting Member
8 Posts |
Posted - 2006-11-14 : 01:21:07
|
| Hi GuysI have two queries that I want to join. I have tried using UNION but then some of the data comes as a row instead of a column. Any ideas and suggestions will be most welcome |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-14 : 01:23:52
|
| Post the queries here.I have never heard of a UNION converting a row to a column. You most probably have a syntax problem in your query.Peter LarssonHelsingborg, Sweden |
 |
|
|
UnathiM
Starting Member
8 Posts |
Posted - 2006-11-14 : 02:12:06
|
| select datepart(yy, date) as 'Year', datepart(mm, date) as 'Month', case when Btype in ('A','B', 'C') then 'DA' when Btype in ('D','E', 'F') then 'DS' when Btype in ('G','H','I') then 'DC' else 'other ' end,count(*) as 'Total Approvals'from Table A join Table B on A.dealer = B.branchjoin Table C on A.refno = C.refnowhere date >= '14 Jan 2006'and scoredatetime = (select min (scoredatetime) from Table C where refno = a.refno )and decision = 'A APPROVE'group by datepart(yy, date), datepart(mm, date),case when Btype in ('A','B', 'C') then 'DA' when Btype in ('D','E', 'F') then 'DS' when Btype in ('G','H','I') then 'DC' else 'other ' endunion select datepart(yy, date), datepart(mm, date), case when Btype in ('A','B', 'C') then 'DA' when Btype in ('D','E', 'F') then 'DS' when Btype in ('G','H','I') then 'DC' else 'other ' end,count(*)as 'TotalApps'from Table A join Table B on A.dealer = B.branchjoin Table C on A.refno = C.refnowhere date >= '14 Jan 2006'and scoredatetime = (select min (scoredatetime) from Table C where refno = a.refno )group by datepart(yy, date), datepart(mm, date),case when Btype in ('A','B', 'C') then 'DA' when Btype in ('D','E', 'F') then 'DS' when Btype in ('G','H','I') then 'DC' else 'other ' endorder by datepart(yy, date), datepart(mm, date),case when Btype in ('A','B', 'C') then 'DA' when Btype in ('D','E', 'F') then 'DS' when Btype in ('G','H','I') then 'DC' else 'other ' end |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-14 : 02:15:57
|
| Do you self-join table "TABLE" two times?Or is it different tables?Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-14 : 02:21:15
|
Your query can be somewhat simplified like this. YOU NEED TO LEARN TO PREFIX YOUR COLUMNS WHEN USING MORE THAN ONE TABLE IN A QUERY.select datepart(yy, xx.date) as 'Year', datepart(mm, xx.date) as 'Month', case when xx.Btype in ('A','B', 'C') then 'DA' when xx.Btype in ('D','E', 'F') then 'DS' when xx.Btype in ('G','H','I') then 'DC' else 'other ' end, sum(case when xx.decision = 'A APPROVE' then 1 else 0 end) as 'Total Approvals', count(*) as 'TotalApps'from Table Ainner join Table B on A.dealer = B.branchinner join Table C on A.refno = C.refnowhere xx.date >= '14 Jan 2006' and xx.scoredatetime = (select min (d.scoredatetime) from Table D where d.refno = a.refno )group by datepart(yy, xx.date), datepart(mm, xx.date), case when xx.Btype in ('A','B', 'C') then 'DA' when xx.Btype in ('D','E', 'F') then 'DS' when xx.Btype in ('G','H','I') then 'DC' else 'other ' endSubstitute "xx." with the tablename or table alias you want to use.Peter LarssonHelsingborg, Sweden |
 |
|
|
UnathiM
Starting Member
8 Posts |
Posted - 2006-11-14 : 03:13:00
|
| THANK YOU VERY MUCH! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-11-14 : 09:44:54
|
| >>where xx.date >= '14 Jan 2006'I prefer using YYYYMMDD or YYYY-MM-DD format because DD MMM YYYY may not work properly in non-English ServersMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|