| Author |
Topic |
|
satish.gorijala
Posting Yak Master
182 Posts |
Posted - 2009-04-20 : 08:31:56
|
| HI, i have two tablesTable1-------Zcode INDI JPAN ASIM Total165-34 7 2 1 10 165-64 9 3 2 14165-76 3 3 6 12165-90 7 2 1 10567-34 1 9 2 12567-09 3 4 4 11435-09 2 5 8 15435-96 3 4 5 12768-99 4 4 5 13145-90 4 2 1 7Table2------Zipcode165567768Now get first row from table2 and the value is 165. select all the rows from table1 having zcode as like '165%'. Now insert new row to the table1 having Zcode as 165 and for the remining columns data.Add the rows of 165 which having different totals(i.e 10,14,12 here. Take only one row having total 10).The final insertion rows should be as below. How can we do this??Newly inserted rows in table1.------Zcode INDI JPAN ASIM Total165 19 8 9 36 567 4 13 6 23768 4 4 5 13G. Satish |
|
|
whitefang
Enterprise-Level Plonker Who's Not Wrong
272 Posts |
Posted - 2009-04-20 : 08:38:32
|
| This should work:SELECT SubString(ZCode, 0, 3) as subZCode, SUM(INDI), SUM(JPAN), SUM(ASIM), SUM(Total) FROM [Table1] INNER JOIN [Table2] ON Table1.subZCode = Table2.ZipCode GROUP BY subZCodeI haven't tested it (don't have an SQL editor installed) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-20 : 08:47:23
|
WhiteFang, you problably meant to first DISTINCT all totals as per OP request?INSERT Table1 ( Zcode, INDI, JPAN, ASIM, Total )SELECT LEFT(d.Zcode, 3) AS Zcode, SUM(d.INDI), SUM(d.JPAN), SUM(d.ASIM), SUM(d.Total)FROM ( SELECT Zcode, INDI, JPAN, ASIM, Total, ROW_NUMBER() OVER (PARTITION BY Zcode, Total ORDER BY Zcode) AS recID FROM Table1 ) AS dINNER JOIN Table2 AS t2 ON t2.Zipcode = d.ZcodeWHERE d.recID = 1GROUP BY LEFT(d.Zcode, 3) E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-20 : 09:17:38
|
We have already posted suggestions to you.There is no need to edit your original post. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
satish.gorijala
Posting Yak Master
182 Posts |
Posted - 2009-04-20 : 09:27:00
|
Sorry, i have given total values as wrong in previous post. The query is ok, its working fine. But it adding all the rows for '165'. I need this as add the rows of having total distinct. Here i need to consider only one row for total having data '10'. Now i am getting output as 165 30 9 6 45But i need it as 165 23 7 5 35quote: Originally posted by Peso WhiteFang, you problably meant to first DISTINCT all totals as per OP request?INSERT Table1 ( Zcode, INDI, JPAN, ASIM, Total )SELECT LEFT(d.Zcode, 3) AS Zcode, SUM(d.INDI), SUM(d.JPAN), SUM(d.ASIM), SUM(d.Total)FROM ( SELECT Zcode, INDI, JPAN, ASIM, Total, ROW_NUMBER() OVER (PARTITION BY Zcode, Total ORDER BY Zcode) AS recID FROM Table1 ) AS dINNER JOIN Table2 AS t2 ON t2.Zipcode = d.ZcodeWHERE d.recID = 1GROUP BY LEFT(d.Zcode, 3) E 12°55'05.63"N 56°04'39.26"
G. Satish |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-20 : 09:34:31
|
Small mistake of me. Please see this corrected versionDECLARE @Table1 TABLE ( Zcode VARCHAR(10), INDI INT, JPAN INT, ASIM INT, Total INT )INSERT @Table1SELECT '165-34', 7, 2, 1, 10 UNION ALL SELECT '165-64', 9, 3, 2, 14 UNION ALLSELECT '165-76', 3, 3, 6, 12 UNION ALLSELECT '165-90', 7, 2, 1, 10 UNION ALLSELECT '567-34', 1, 9, 2, 12 UNION ALLSELECT '567-09', 3, 4, 4, 11 UNION ALLSELECT '435-09', 2, 5, 8, 15 UNION ALLSELECT '435-96', 3, 4, 5, 12 UNION ALLSELECT '768-99', 4, 4, 5, 13 UNION ALLSELECT '145-90', 4, 2, 1, 7DECLARE @Table2 TABLE ( Zipcode VARCHAR(10) )INSERT @Table2SELECT '165' UNION ALLSELECT '567' UNION ALLSELECT '768'--INSERT Table1-- (-- Zcode,-- INDI,-- JPAN,-- ASIM,-- Total-- )SELECT LEFT(d.Zcode, 3) AS Zcode, SUM(d.INDI), SUM(d.JPAN), SUM(d.ASIM), SUM(d.Total)FROM ( SELECT LEFT(Zcode, 3) AS Zcode, INDI, JPAN, ASIM, Total, ROW_NUMBER() OVER (PARTITION BY LEFT(Zcode, 3), Total ORDER BY Zcode) AS recID FROM @Table1 ) AS dINNER JOIN @Table2 AS t2 ON t2.Zipcode = d.ZcodeWHERE d.recID = 1GROUP BY LEFT(d.Zcode, 3) E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|