| Author |
Topic |
|
maevr
Posting Yak Master
169 Posts |
Posted - 2007-04-18 : 14:47:41
|
| I need help writing the query below. ss1_oil has a column storing values in the decimal format (1, 2 and 3) and ss1_uppmatvardelista har the corresponding text for these stored in description column.My problem is that I have to get the text and insert it into a view to be able to build an Analysis cube. ss1_uppmatvardelistavalue decimal(14,4)description varchar(50)Examplevalues:value description0 invalid1 text12 text23 text3null text missing--------------------------------------ss1_oilid intvalue decimal(14,4)Examplevalues:id value1 12 3--------------------------------------I want to create a view that presents the following values taken from ss1_uppmatvardelista and ss1_oil:ss1_view_oilid intdescription varchar(50)examplevalues:id description1 text12 text3Can this be done and how? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-04-18 : 14:59:49
|
| I think this does it:CREATE VIEW ss1_view_oilASSELECT ss1_oil.id, ss1_uppmatvardelista.descriptionFROM ss1_oilINNER JOIN ss1_uppmatvardelistaON ss1_oil.value = ss1_uppmatvardelista.idTara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2007-04-18 : 15:39:34
|
| Thanks!But how do you get the null values to get the text? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-04-18 : 15:41:46
|
| I don't understand what you mean.The query that I posted will give you the output that you requested. If that isn't the output that you want, then please provide a better example.Here is what you requested:examplevalues:id description1 text12 text3Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
sshelper
Posting Yak Master
216 Posts |
Posted - 2007-04-18 : 15:43:24
|
| If I understand your question correctly, try this:CREATE VIEW ss1_view_oilASSELECT ss1_oil.id, ss1_uppmatvardelista.descriptionFROM ss1_oilINNER JOIN ss1_uppmatvardelistaON ss1_oil.value = ss1_uppmatvardelista.id OR(ss1_oil.value IS NULL AND ss1_uppmatvardelista.id IS NULL)SQL Server Helperhttp://www.sql-server-helper.com |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2007-04-18 : 16:01:34
|
| Thank you both tkizer and sshelper, it works fine. Any idéa how to handle values that have been inserted that do not match any of the values from the ss1_uppmatvardelista, for example 1.7568. I would want to give these a description like "fault registered" or something like that. My first thought was a case inside the select queryYour help is much appreciated |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-04-18 : 16:05:03
|
| You would use an OUTER JOIN for the ones that don't have matches. Just replace the NULL value (which you get from the outer join for the table that doesn't have a match) with the value you want. If you could provide a clear example of what you want, then we could write the query.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2007-04-18 : 16:19:35
|
| Ok I give it a try!ss1_oil has the column value that only should have the values null, 1, 2, 3 but there exists som values that should belong there such as 1.6788, 0.3455 for example. null, 1, 2 and 3 have descriptions specified but how can yoy take care of the others, I would want to place a description on those to.ss1_oil1, 1.7892, 23, 1ss1_uppmatvardelista0, invalid1, text12, text23, text3null, text missingOthers, fault registeredview1, fault registered2, text23, text1Hope this will describe better what I'm looking for. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-04-18 : 16:23:39
|
| SELECT ss1_oil.id, CASE WHEN ss1_uppmatvardelista.description IS NOT NULL THEN ss1_uppmatvardelista.description ELSE 'fault registered' ENDFROM ss1_oilLEFT OUTER JOIN ss1_uppmatvardelistaON ss1_oil.value = ss1_uppmatvardelista.idTara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-04-18 : 18:42:20
|
| Cross Post:[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=82287[/url] |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2007-04-19 : 08:32:10
|
| Hi I have added a column in ss1_uppmatvardelista, it looks like this now:----------------------------------ss1_uppmatvardelistauppmatkod varchar(15) not null,value decimal(14,4)description varchar(50)inserted values:isolfangtyp, 1, text1isolfangtyp, 2, text2isolavser, 1, text11isolavser, 2, text22----------------------------------When I try to add the last row (look query below), the query does not work for values that is not specified in the ss1_uppmatvardelista table, why?----------------------------------SELECT o.id, u.[description]FROM @ss1_oil oLEFT OUTER JOIN@ss1_uppmatvardelista uON COALESCE(o.[value], -1) = COALESCE(u.[value], -1)where u.uppmatkod like 'ISOLFANGTYP'---------------------------------- |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-19 : 08:56:42
|
You should place that table on the LEFT side of LEFT JOIN for which you want all records to be returned irrespective of whether there is matching record in the joined table. Run the example below to get the better idea on LEFT JOINS:declare @emp table( empid int, ename varchar(20), deptid int)declare @dept table( deptid int, dname varchar(20))insert @empselect 1, 'aaa', 1 union allselect 2, 'bbb', 2 union allselect 3, 'ccc', 6 union allselect 4, 'ddd', 8insert @deptselect 1, 'Accounts' union allselect 3, 'Sales' union allselect 5, 'HR' union allselect 8, 'IT'-- To see all employees irrespective of related departmentsselect e.*, d.*from @emp e left join @dept d on e.deptid = d.deptid-- To see all depts irrespective of related employeesselect d.*, e.*from @dept d left join @emp e on e.deptid = d.deptid Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2007-04-19 : 14:31:16
|
| I dont really understand the left outer joins could anyone please redo this so it fits the solution above since that is what I've been doing the last couple of days without success. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-04-19 : 14:33:41
|
| Which solution are you referring to?If you don't understand outer joins, then you need to learn them rather than come up with a different solution that doesn't use outer joins.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2007-04-19 : 14:46:25
|
| I think I understand tham but what I dont understand is why the resultset becomes different when I add the line below:where u.uppmatkod like 'ISOLFANGTYP' (look at previous post), when the 17315.0000 value seems to disappear, it should get the value fault registered.Also when I add a new row in ss1_uppmatvardelista containing values below and run the query against isolavser then I the above appear again.ss1_uppmatvardelistaisolfangtyp, 1, text1isolfangtyp, 2, text2 isolavser, 1, text11 |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-04-19 : 17:16:12
|
quote: Originally posted by maevr I think I understand tham but what I dont understand is why the resultset becomes different when I add the line below:where u.uppmatkod like 'ISOLFANGTYP' (look at previous post), when the 17315.0000 value seems to disappear, it should get the value fault registered.Also when I add a new row in ss1_uppmatvardelista containing values below and run the query against isolavser then I the above appear again.ss1_uppmatvardelistaisolfangtyp, 1, text1isolfangtyp, 2, text2 isolavser, 1, text11
I'm not sure I understand what is happening. Could you provide sample data, expected results and actual results to help us trouble shoot?maybe the issue is the WHERE clause? Try this:SELECT o.id, u.[description]FROM @ss1_oil oLEFT OUTER JOIN@ss1_uppmatvardelista uON COALESCE(o.[value], -1) = COALESCE(u.[value], -1)AND u.uppmatkod like 'ISOLFANGTYP' |
 |
|
|
|