|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, November 23, 2012 9:48 AM
Points: 15,
Visits: 56
|
|
Hi, I have a table that, among other columns, there are three in particular; qualifier, segment, and value. the combination of data in the segment and qualifier columns produce some sort of "value". If the combination of qualifier column (PL) and the segment column (SLN), the resulting value in column "VALUE" should be "Line Number" If the combination of qualifier column (DV) and the segment column (SLN), the resulting value in column "VALUE" should be "Deptartment" If the combination of qualifier column (91) and the segment column (PID), the resulting value in column "VALUE" should be "Size" I just can't think of a way to create a query. Please help. Thank you
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 6:25 AM
Points: 838,
Visits: 2,197
|
|
You could use a case to populate the values eg
Select CASE When Qualifer='PL' and Segment='SLN' Then 'Line Number' When Qualifer='DV' and Segment='SLN' Then 'Department' When Qualifer='91' and Segment='PID' Then 'Size' ELSE NULL END VALUE From aTable
Edit :wrong code.
_________________________________________________________________________ SSC Guide to Posting and Best Practices
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 4:22 AM
Points: 141,
Visits: 558
|
|
Hi,
Not entirely sure I understand your question, but I think you need to look at the CASE statement.
e.g.:
SELECT col1 , col2 , CASE WHEN (col1 + col2) = 42 THEN 'Department' ELSE 'somethingelse' END FROM myTable
See http://msdn.microsoft.com/en-us/library/ms181765.aspx
HTH,
B
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, November 23, 2012 9:48 AM
Points: 15,
Visits: 56
|
|
Thank you for the suggestions. So this is what I have: SELECT zzoordrh.BILL_NUM, zzedatawhse.value CASE WHEN zzedatawhse.QUALIFIER = 'PL' AND zzedatawhse.SEGMENT = 'SLN' THEN 'Line No' WHEN zzedatawhse.QUALIFIER = 'DV' AND zzedatawhse.SEGMENT = 'SLN' THEN 'Department' WHEN zzedatawhse.QUALIFIER = '91' AND zzedatawhse.SEGMENT = 'PID' THEN 'Line No' END
FROM zzedatawhse INNER JOIN zzoordrh ON zzedatawhse.DOC_NUM = zzoordrh.edi_doc_num WHERE zzoordrh.BILL_NUM = '113111'
But I am getting: Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'CASE'.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 4:22 AM
Points: 141,
Visits: 558
|
|
you need a comma before the word CASE and you might as well add AS [myColumnName] after END to give a column name to the third column.
B
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, November 23, 2012 9:48 AM
Points: 15,
Visits: 56
|
|
Humm, Thank you B, I added both and I am still getting: Incorrect syntax near '='.
This is what it looks like now: SELECT zzoordrh.BILL_NUM, zzedatawhse.value, CASE zzedatawhse.VALUE WHEN zzedatawhse.QUALIFIER = 'PL' AND zzedatawhse.SEGMENT = 'SLN' THEN 'Line No' WHEN zzedatawhse.QUALIFIER = 'DV' AND zzedatawhse.SEGMENT = 'SLN' THEN 'Department' WHEN zzedatawhse.QUALIFIER = '91' AND zzedatawhse.SEGMENT = 'PID' THEN 'Line No' END AS zzedatawhse.value
FROM zzedatawhse INNER JOIN zzoordrh ON zzedatawhse.DOC_NUM = zzoordrh.edi_doc_num
WHERE zzoordrh.BILL_NUM = '113111'
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 7:30 AM
Points: 11,791,
Visits: 28,070
|
|
it's just a syntax issue; CASE for SQL has two forms :
CASE ValuetoTest WHEN SomeValue THEN SomeOtherValue ELSE DefaultValue END --OR CASE WHEN {SomeExpression} (ie SomeColumn = SomeValue) THEN SomeOtherValue ELSE DefaultValue END
you werejust mixing them together
I think this is your final query:
SELECT zzoordrh.BILL_NUM, zzedatawhse.value, CASE WHEN zzedatawhse.QUALIFIER = 'PL' AND zzedatawhse.SEGMENT = 'SLN' THEN 'Line No' WHEN zzedatawhse.QUALIFIER = 'DV' AND zzedatawhse.SEGMENT = 'SLN' THEN 'Department' WHEN zzedatawhse.QUALIFIER = '91' AND zzedatawhse.SEGMENT = 'PID' THEN 'Line No' END AS [zzedatawhse.value] FROM zzedatawhse INNER JOIN zzoordrh ON zzedatawhse.DOC_NUM = zzoordrh.edi_doc_num WHERE zzoordrh.BILL_NUM = '113111'
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, November 23, 2012 9:48 AM
Points: 15,
Visits: 56
|
|
Thank you Lowell!!!!!!! It works!!!! I just not sure this this is what I was expecting. See, I am getting three columns: BILL_NUM, VALUE, and zzedatawhse.value. The table shows all records from table zzedatawhse as "NULL" except the ones in the CASE statement. Each with their appropriate label. I want to only shows the records called or mentioned in the CASE statement on their own columns. That is; BILL_NUM, LINE_NO, DEPARTMENT, SIZE
It gets more complicated as these records must match the line number of an order. I have this part done but I just can't figure out how to get these values on their own column. I hope I am explaining myself clearly. Thank you
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Today @ 7:32 AM
Points: 8,957,
Visits: 8,524
|
|
reliableitservice (11/6/2012) Thank you Lowell!!!!!!! It works!!!! I just not sure this this is what I was expecting. See, I am getting three columns: BILL_NUM, VALUE, and zzedatawhse.value. The table shows all records from table zzedatawhse as "NULL" except the ones in the CASE statement. Each with their appropriate label. I want to only shows the records called or mentioned in the CASE statement on their own columns. That is; BILL_NUM, LINE_NO, DEPARTMENT, SIZE
It gets more complicated as these records must match the line number of an order. I have this part done but I just can't figure out how to get these values on their own column. I hope I am explaining myself clearly. Thank you
Your post was pretty clear...or so I thought until this last post. It is very unclear what you are trying to do here. You might take a look at the first link in my signature for best practices when posting questions.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Moden's splitter.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
|
|
>> I have a table that, among other columns, there are three in particular; qualifier [of what?], segment[of what?], and value [of what on what scale?]. <<
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. If you know how, follow ISO-11179 data element naming conventions and formatting rules [you do not]. This is minimal polite behavior on SQL forums.
Sample data is also a good idea, along with clear specifications. It is very hard to debug code when you do not let us see it.
RDBMS is based on Logic. The first law of Western Logic is the Law of Identity, falsely attributed to Aristotle (actually due to Parmenides the Eleatic, “a thing is itself”, “A is A” or “to be is to be something in particular; to be nothing in particular or to be everything in general is to be nothing at all” as you learn in freshman Logic). Your column names in this nameless table that you did not bother to show us are vague and therefore useless as well as violate ISO-11179 rules.
>> the combination of data in the segment and qualifier columns produce some sort of "value". <<
I hope not! That would violate First Normal Form (1NF), the foundation of the relational model. Then your narrative proceeds to describe a total nightmare so bad I have to use it in a book.
We make fun of designs like this be calling it a “Automobiles, Squids and Lady Gaga” schema. Each column should be a single attribute, measured with scalar values drawn from a domain. A real column stands by itself, not dependent for its meaning on the context of each row. It is itself, by the Law of Identity.
Nobody really helped you. Please be smart enough to learn to do it right and not kludge crap just to get it to run. Look at my credentials; look at the kludgers; read and learn. Then make a GOOD decision.
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
|
|
|
|