Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
SkyJoggeR
Starting Member
1 Post |
Posted - 2010-10-18 : 00:36:49
|
| Hii have a table that has a list of samples in it, each samples has about 7 elements to it and are put into this table as separate entries. what i want to run a query that puts all the elements onto one line for each sample. now i can do this to a point with a self-join where the samples have the a common ID for this table, but this only happens for about 4 of the elements the other 3 elements are created from another test and therefore come out with a different ID which is linked to a second table. in this second table there is a column sampleindent which is not the primary key for this table and does not link to the first table, but all elements in this sample i want out putted on 1 row have the same sampleindentthis is what i have so farSELECT s.SAMPLEIDENT ,s.EXTERNALIDENT ,s.ANALYSED ,a.FORMATTEDVALUE AS CU ,b.Formattedvalue as FE ,p.formattedvalue as PB ,z.formattedvalue as ZN from SAMPLE s join ANALYTE a on s.ID=a.ID join ANALYTE b on a.ID=b.ID join ANALYTE p on a.ID=p.ID join ANALYTE z on a.ID=z.ID where s.pro_job in (select PRO_JOB from CHD.dbo.CHD_JOB where CLI_CODE like '%geo%') and a.ANALYTECODE like '%CU%' and b.ANALYTECODE like '%FE%' and p.ANALYTECODE like '%PB%' and z.ANALYTECODE like '%ZN%'but then i dont know how to get the values that dont share the ID in the Analyte table to out put into this valuethanks |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-18 : 04:20:35
|
 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2010-10-18 : 15:54:19
|
| 1) There is no such thing AS a magical generic vague "id" in RDBMS. There are only particular identifiers. It would also help if you follow the ISO-11179 naming rules2) Do not reserved words in lowercase and object names in uppercase. It does not work. Read the research. 3) Here is a guess made without any DDL:SELECT sample_id, external_something_id, analyzed_flg, MAX (CASE WHEN analyte_code LIKE '%cu%' THEN formatted_something_value ELSE '' END) AS cu, MAX (CASE WHEN analyte_code LIKE '%fe%' THEN formatted_something_value ELSE '' END) AS fe, MAX (CASE WHEN analyte_code LIKE '%pb%' THEN formatted_something_value ELSE '' END) AS pb, MAX (CASE WHEN analyte_code LIKE '%zn%' THEN formatted_something_value ELSE '' END) AS zn FROM Samples AS S WHERE S.pro_job IN (SELECT pro_job FROM CHD.chd_job) AND cli_code LIKE '%geo%' GROUP BY sample_id, external_something_id, analyzed_flg;--CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
|
|
|
|
|