| Author |
Topic |
|
steveUsingSQL
Starting Member
6 Posts |
Posted - 2012-02-24 : 08:38:43
|
| Hi,Hoping someone can help.1. This piece of SQL populates a table:INSERT INTO tblECHO SELECT * FROM qrytblXYZ2. The query (qrytblXYZ) code looks like:SELECT DISTINCT tblALPHA.one, tblALPHA.Surname AS two, tblBRAVO.three,tblBRAVO.fourFROM tblCHARLIEINNER JOIN tblALPHA ON tblCHARLIE.one = tblALPHA.oneINNER JOIN vwDELTA ON vwDELTA.five = tblALPHA.fiveINNER JOIN tblBRAVO ON tblBRAVO.four = vwDELTA.four3. Then this runs:SELECT tblCHARLIE.six, tblCHARLIE.one, tblCHARLIE.[seven],tblCHARLIE.[eight], tblCHARLIE.nine, tblECHO.two, tblCHARLIE.tenFROM tblCHARLIE LEFT JOIN tblECHO ON tblCHARLIE.one = tblECHO.oneGROUP BY tblCHARLIE.six, tblCHARLIE.one, tblCHARLIE.[seven],tblCHARLIE.[eight], tblCHARLIE.nine, tblECHO.two, tblCHARLIE.tenORDER BY tblCHARLIE.[seven];I need to make this 1 SQL statement. Please can anyone help with this complex question. Many thanks in advance for todays conundrum.Thanks, Steve |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-24 : 09:10:15
|
| [code]INSERT INTO tblECHOSELECT required columns here....FROM(SELECT DISTINCT tblALPHA.one, tblALPHA.Surname AS two, tblBRAVO.three,tblBRAVO.fourFROM tblCHARLIEINNER JOIN tblALPHA ON tblCHARLIE.one = tblALPHA.oneINNER JOIN vwDELTA ON vwDELTA.five = tblALPHA.fiveINNER JOIN tblBRAVO ON tblBRAVO.four = vwDELTA.four)tINNER JOIN (SELECT tblCHARLIE.six, tblCHARLIE.one, tblCHARLIE.[seven],tblCHARLIE.[eight], tblCHARLIE.nine, tblECHO.two, tblCHARLIE.tenFROM tblCHARLIE LEFT JOIN tblECHO ON tblCHARLIE.one = tblECHO.oneGROUP BY tblCHARLIE.six, tblCHARLIE.one, tblCHARLIE.[seven],tblCHARLIE.[eight], tblCHARLIE.nine, tblECHO.two, tblCHARLIE.ten)t1ON t1.one = t.oneAND t1.two = t.two[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
steveUsingSQL
Starting Member
6 Posts |
Posted - 2012-02-24 : 09:26:56
|
| Thank you.1. I am now no longer using table tblECHO. This is now a SELECT statement. So the SELECT statement needs to do everything without tblECHO.The SQL code above in my original post(part 1 and 2) populated a temporary table, and then part 3 ran against that temporary table. I hope you can still assist. Thank you.Steve |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-24 : 09:56:22
|
| so without using tblECHO how are tables related?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
steveUsingSQL
Starting Member
6 Posts |
Posted - 2012-02-24 : 10:04:52
|
| the secret to this is tblECHO and the the column "one" i believe.There is a join in part 3 SQL:FROM tblCHARLIE LEFT JOIN tblECHO ON tblCHARLIE.one = tblECHO.onein SQL part 3, tblECHO needs to be replaced with the results from the part 2 SQL. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-24 : 10:39:12
|
| [code]INSERT INTO tblECHOSELECT tblCHARLIE.six, tblCHARLIE.one, tblCHARLIE.[seven],tblCHARLIE.[eight], tblCHARLIE.nine, t.two, tblCHARLIE.tenFROM tblCHARLIE LEFT JOIN (SELECT DISTINCT tblALPHA.one, tblALPHA.Surname AS two, tblBRAVO.three,tblBRAVO.fourFROM tblCHARLIEINNER JOIN tblALPHA ON tblCHARLIE.one = tblALPHA.oneINNER JOIN vwDELTA ON vwDELTA.five = tblALPHA.fiveINNER JOIN tblBRAVO ON tblBRAVO.four = vwDELTA.four)tON tblCHARLIE.one = t.oneGROUP BY tblCHARLIE.six, tblCHARLIE.one, tblCHARLIE.[seven],tblCHARLIE.[eight], tblCHARLIE.nine, t.two, tblCHARLIE.ten[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
steveUsingSQL
Starting Member
6 Posts |
Posted - 2012-02-24 : 10:54:18
|
quote: Originally posted by visakh16
INSERT INTO tblECHOSELECT tblCHARLIE.six, tblCHARLIE.one, tblCHARLIE.[seven],tblCHARLIE.[eight], tblCHARLIE.nine, t.two, tblCHARLIE.tenFROM tblCHARLIE LEFT JOIN (SELECT DISTINCT tblALPHA.one, tblALPHA.Surname AS two, tblBRAVO.three,tblBRAVO.fourFROM tblCHARLIEINNER JOIN tblALPHA ON tblCHARLIE.one = tblALPHA.oneINNER JOIN vwDELTA ON vwDELTA.five = tblALPHA.fiveINNER JOIN tblBRAVO ON tblBRAVO.four = vwDELTA.four)tON tblCHARLIE.one = t.oneGROUP BY tblCHARLIE.six, tblCHARLIE.one, tblCHARLIE.[seven],tblCHARLIE.[eight], tblCHARLIE.nine, t.two, tblCHARLIE.ten ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Thank you for this, but i can now no longer use the temporary table - tblECHO - So i need to make this 1 SELECT statement and NOT an INSERT statement.** EDIT **Some results came back - i will now test them vigourously.Many thanks! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-25 : 13:02:52
|
| welcome...if you check my latest suggestion i'm not using tblECHO so i think this is what you're looking for------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
steveUsingSQL
Starting Member
6 Posts |
Posted - 2012-02-29 : 06:35:43
|
quote: Originally posted by visakh16 welcome...if you check my latest suggestion i'm not using tblECHO so i think this is what you're looking for------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Hello again,This solution works great. I just need to modify it a bit. In the solution, the item: t.two, can have many values. It would be ideal to have the first row with all the fields, and then t.two listd by itself under this row. e.g.________________________________value | value | t.two | value |_____|______| t.two |_____|_____|______| t.two |_____| Please can you help refine this.Steve. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-29 : 11:48:49
|
| this is a presntation issue and you should be handling it in your front end application. It is not all recommended to do this in sqlif you're using a reporting tool as front end, then this property is called suppress duplicates or remove duplicates------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
steveUsingSQL
Starting Member
6 Posts |
Posted - 2012-03-01 : 05:19:39
|
quote: Originally posted by visakh16 this is a presntation issue and you should be handling it in your front end application. It is not all recommended to do this in sqlif you're using a reporting tool as front end, then this property is called suppress duplicates or remove duplicates------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/Thank You,I think i am coming to that conclusion also.I will have a look at Visual Studio and see if it can do that.Steve
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-01 : 10:50:11
|
quote: Originally posted by steveUsingSQL
quote: Originally posted by visakh16 this is a presntation issue and you should be handling it in your front end application. It is not all recommended to do this in sqlif you're using a reporting tool as front end, then this property is called suppress duplicates or remove duplicates------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/Thank You,I think i am coming to that conclusion also.I will have a look at Visual Studio and see if it can do that.Steve
are you working on .NET app of reporting services?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|