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 |
|
mic_lch83
Starting Member
5 Posts |
Posted - 2007-02-01 : 10:44:53
|
| I need to make a "select" statement that will create two columns with different requirements.My Database is like this.PopTotal Variance2 13 14 15 26 2I need to create a table from an sql statement that will show.PopTotal1 PopTotal22 5 3 64Meaning two columns from the same column with different requirements. How can I do that with sql statements without CHANGING the database? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-01 : 11:08:49
|
Is your objective to cut the PopTotal in half, and create two columns with the divied PopTotal in both, starting with lowest number?-- prepare sample datadeclare @sample table (PopTotal int, Variance int)insert @sampleselect 2, 1 union allselect 6, 1 union allselect 9, 1 union allselect 25, 2 union allselect 3433, 2-- show the resultSELECT c1.PopTotal, c2.PopTotalFROM ( SELECT c1.PopTotal, ROW_NUMBER() OVER (ORDER BY c1.PopTotal) AS Row FROM ( SELECT PopTotal, NTILE(2) OVER (ORDER BY PopTotal) AS Col FROM @Sample ) as c1 WHERE c1.Col = 1 ) AS c1LEFT JOIN ( SELECT c2.PopTotal, ROW_NUMBER() OVER (ORDER BY c2.PopTotal) AS Row FROM ( SELECT PopTotal, NTILE(2) OVER (ORDER BY PopTotal) AS Col FROM @Sample ) as c2 WHERE c2.Col = 2 ) AS c2 ON c2.Row = c1.RowORDER BY c1.Row Peter LarssonHelsingborg, Sweden |
 |
|
|
mic_lch83
Starting Member
5 Posts |
Posted - 2007-02-01 : 11:13:55
|
| Thank you for your quick response. My objective is to cut PopTable into whatever the VarID is. But, It do not need to be sorted.Thanks. |
 |
|
|
mic_lch83
Starting Member
5 Posts |
Posted - 2007-02-01 : 11:13:57
|
| Thank you for your quick response. My objective is to cut PopTable into whatever the VarID is. But, It do not need to be sorted.Thanks. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2007-02-01 : 11:17:50
|
| So Value that has a variance goes into Col1, and variance goes into Col2?What is the rlationship between the values in Column 1 and Column 2?It makes no senseI could "make something up" to produce these results. Before I do, you really need to explain to us why? Because I can tell you right now, the result set is meaninglessBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-01 : 11:24:50
|
Or it is as simple as Variance 1 is the first column, Variance 2 is the second column?-- prepare sample datadeclare @sample table (PopTotal int, Variance int)insert @sampleselect 2, 1 union allselect 3, 1 union allselect 4, 1 union allselect 5, 2 union allselect 6, 2-- Show the resultSELECT MAX(CASE WHEN Variance = 1 THEN PopTotal END) AS Col1, MAX(CASE WHEN Variance = 2 THEN PopTotal END) AS Col2FROM ( SELECT PopTotal, Variance, ROW_NUMBER() OVER (PARTITION BY Variance ORDER BY PopTotal) AS RowID FROM @Sample ) AS sGROUP BY RowIDORDER BY RowID Peter LarssonHelsingborg, Sweden |
 |
|
|
mic_lch83
Starting Member
5 Posts |
Posted - 2007-02-01 : 11:27:25
|
| No, the value with different variance goes into different columns. Let my show you my table 'tblTotPopulation'.LocID VarID PopTotal1 2 1.21 3 3.4 2 2 5.62 3 3.23 2 3.53 3 3.9I need to create a table where it shows 2 columns of Poptotal with Different VarID. The results should BeLocID PopTable1 PopTable21 1.2 3.42 5.6 3.23 3.5 3.9As you can see that the PopTables have different VarID. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-01 : 11:28:03
|
| [code]-- prepare sample datadeclare @sample table (LocID int, VarID int, PopTotal money)insert @sampleselect 1, 2, 1.2 union allselect 1, 3, 3.4 union all select 2, 2, 5.6 union allselect 2, 3, 3.2 union allselect 3, 2, 3.5 union allselect 3, 3, 3.9-- Show the resultSELECT MAX(CASE WHEN s.Col = 1 THEN s.PopTotal END) AS Col1, MAX(CASE WHEN s.Col = 2 THEN s.PopTotal END) AS Col2, MAX(CASE WHEN s.Col = 3 THEN s.PopTotal END) AS Col3FROM ( SELECT PopTotal, DENSE_RANK() OVER (ORDER BY VarID) AS Col, DENSE_RANK() OVER (ORDER BY LocID) AS Row FROM @Sample ) AS sGROUP BY s.RowORDER BY s.Row[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
mic_lch83
Starting Member
5 Posts |
Posted - 2007-02-01 : 11:35:33
|
| Peso,You really seems to be a great help. However, I am currently using SQL 2000 and some functions like ROW_NUMBER() does not exists.Do you know how to avoid the functions in SQL 200? Thanks. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-01 : 13:45:45
|
| [code]-- prepare sample datadeclare @sample table (LocID int, VarID int, PopTotal money)insert @sampleselect 1, 2, 1.2 union allselect 1, 3, 3.4 union all select 2, 2, 5.6 union allselect 2, 3, 3.2 union allselect 3, 2, 3.5 union allselect 3, 3, 3.9-- Show the result SQL 2005 styleSELECT MAX(CASE WHEN s.Col = 1 THEN s.PopTotal END) AS Col1, MAX(CASE WHEN s.Col = 2 THEN s.PopTotal END) AS Col2, MAX(CASE WHEN s.Col = 3 THEN s.PopTotal END) AS Col3FROM ( SELECT PopTotal, DENSE_RANK() OVER (ORDER BY VarID) AS Col, LocID AS Row FROM @Sample ) AS sGROUP BY s.RowORDER BY s.Row-- Show the result SQL 2000 styleSELECT MAX(CASE WHEN s.Col = 1 THEN s.PopTotal END) AS Col1, MAX(CASE WHEN s.Col = 2 THEN s.PopTotal END) AS Col2, MAX(CASE WHEN s.Col = 3 THEN s.PopTotal END) AS Col3FROM ( SELECT s.PopTotal, (SELECT COUNT(DISTINCT sc.VarID) FROM @Sample AS sc WHERE sc.VarID <= s.VarID) AS Col, s.LocID AS Row FROM @Sample AS s ) AS sGROUP BY s.RowORDER BY s.Row[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-01 : 14:22:17
|
VarID of 2 and 3 are fixed?-- Show the result SQL 2000 styleSELECT MAX(CASE WHEN VarID = 2 THEN PopTotal END) AS Col, MAX(CASE WHEN VarID = 3 THEN PopTotal END) AS Co2FROM @SampleGROUP BY LocIDORDER BY LocID If they are not fixed, my previous suggestion will translate the VarID's into 1-2-3 with the same order as original.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|