sql server programming:
GB, Gonna have to give us more than that. DDL and criteria please. HTH Jerry [quoted text, click to view] "GB" <v7v1k3@hotmail.com> wrote in message news:yFf1f.5002$y_1.820@edtnps89... >I have a table like this: > > ID | F1 | F2 | F3| > ______________ > > 1 | A | 3 | 4 | > 1 | B | 2 | 5 | > 1 | C | 8 | 10 | > 2 | A | 25| 40 | > 2 | D | 35| 50 | > > I need a query to return the following recordset: > > ID | F1 | F2 | F3| > ______________ > > 1 | A | 3 | 4 | > 1 | B | 2 | 5 | > 1 | C | 8 | 10 | > 1 | D | 0 | 0 | > > > Thanks, > GB > >
Ok...that is a good start...now what is the criteria for the SELECT? I can hard code in that 0s be returned for D and the second A is not returned. Is that your criteria? HTH Jerry [quoted text, click to view] "GB" <v7v1k3@hotmail.com> wrote in message news:FJg1f.5013$y_1.4583@edtnps89... > CREATE TABLE [dbo].[MyTable] ( > [ID] [int] NULL , > [F1] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , > [F2] [int] NULL , > [F3] [int] NULL > ) ON [PRIMARY] > GO > > INSERT INTO MyTable VALUES (1, 'A', 3, 4); > INSERT INTO MyTable VALUES (1, 'B', 2, 5); > INSERT INTO MyTable VALUES (1, 'C', 8, 10); > INSERT INTO MyTable VALUES (2, 'A', 25, 40); > INSERT INTO MyTable VALUES (2, 'D', 35, 50); > GO > > > > "Jerry Spivey" <jspivey@vestas-awt.com> wrote in message > news:eVdnrnryFHA.3000@TK2MSFTNGP12.phx.gbl... >> GB, >> >> Gonna have to give us more than that. DDL and criteria please. >> >> HTH >> >> Jerry >> "GB" <v7v1k3@hotmail.com> wrote in message >> news:yFf1f.5002$y_1.820@edtnps89... >> >I have a table like this: >> > >> > ID | F1 | F2 | F3| >> > ______________ >> > >> > 1 | A | 3 | 4 | >> > 1 | B | 2 | 5 | >> > 1 | C | 8 | 10 | >> > 2 | A | 25| 40 | >> > 2 | D | 35| 50 | >> > >> > I need a query to return the following recordset: >> > >> > ID | F1 | F2 | F3| >> > ______________ >> > >> > 1 | A | 3 | 4 | >> > 1 | B | 2 | 5 | >> > 1 | C | 8 | 10 | >> > 1 | D | 0 | 0 | >> > >> > >> > Thanks, >> > GB >> > >> > >> >> > >
GB, Try: DECLARE @ID INT SET @ID = 1 SELECT @ID AS 'ID', F1, 0 AS F2, 0 AS F3 FROM MYTABLE WHERE F1 NOT IN (SELECT F1 FROM MYTABLE WHERE ID = @ID) UNION SELECT ID, F1, F2, F3 FROM MYTABLE WHERE ID = @ID ORDER BY ID, F1 HTH Jerry [quoted text, click to view] "GB" <v7v1k3@hotmail.com> wrote in message news:kCh1f.5017$y_1.704@edtnps89... > The criteria is : > 1. SELECT .... FROM MyTable WHERE ID = 1 ; (or 2 or any one) > 2. I need to get for ID from above (ID=1) ALL distinct values of F1 AND > 0s > for F2 and F3 where there are no values for ID=1. > > Thanks, > GB > > "Jerry Spivey" <jspivey@vestas-awt.com> wrote in message > news:Ou8Kt3ryFHA.3864@TK2MSFTNGP12.phx.gbl... >> Ok...that is a good start...now what is the criteria for the SELECT? I > can >> hard code in that 0s be returned for D and the second A is not returned. > Is >> that your criteria? >> >> HTH >> >> Jerry >> "GB" <v7v1k3@hotmail.com> wrote in message >> news:FJg1f.5013$y_1.4583@edtnps89... >> > CREATE TABLE [dbo].[MyTable] ( >> > [ID] [int] NULL , >> > [F1] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , >> > [F2] [int] NULL , >> > [F3] [int] NULL >> > ) ON [PRIMARY] >> > GO >> > >> > INSERT INTO MyTable VALUES (1, 'A', 3, 4); >> > INSERT INTO MyTable VALUES (1, 'B', 2, 5); >> > INSERT INTO MyTable VALUES (1, 'C', 8, 10); >> > INSERT INTO MyTable VALUES (2, 'A', 25, 40); >> > INSERT INTO MyTable VALUES (2, 'D', 35, 50); >> > GO >> > >> > >> > >> > "Jerry Spivey" <jspivey@vestas-awt.com> wrote in message >> > news:eVdnrnryFHA.3000@TK2MSFTNGP12.phx.gbl... >> >> GB, >> >> >> >> Gonna have to give us more than that. DDL and criteria please. >> >> >> >> HTH >> >> >> >> Jerry >> >> "GB" <v7v1k3@hotmail.com> wrote in message >> >> news:yFf1f.5002$y_1.820@edtnps89... >> >> >I have a table like this: >> >> > >> >> > ID | F1 | F2 | F3| >> >> > ______________ >> >> > >> >> > 1 | A | 3 | 4 | >> >> > 1 | B | 2 | 5 | >> >> > 1 | C | 8 | 10 | >> >> > 2 | A | 25| 40 | >> >> > 2 | D | 35| 50 | >> >> > >> >> > I need a query to return the following recordset: >> >> > >> >> > ID | F1 | F2 | F3| >> >> > ______________ >> >> > >> >> > 1 | A | 3 | 4 | >> >> > 1 | B | 2 | 5 | >> >> > 1 | C | 8 | 10 | >> >> > 1 | D | 0 | 0 | >> >> > >> >> > >> >> > Thanks, >> >> > GB >> >> > >> >> > >> >> >> >> >> > >> > >> >> > >
I have a table like this: ID | F1 | F2 | F3| ______________ 1 | A | 3 | 4 | 1 | B | 2 | 5 | 1 | C | 8 | 10 | 2 | A | 25| 40 | 2 | D | 35| 50 | I need a query to return the following recordset: ID | F1 | F2 | F3| ______________ 1 | A | 3 | 4 | 1 | B | 2 | 5 | 1 | C | 8 | 10 | 1 | D | 0 | 0 | Thanks, GB
CREATE TABLE [dbo].[MyTable] ( [ID] [int] NULL , [F1] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [F2] [int] NULL , [F3] [int] NULL ) ON [PRIMARY] GO INSERT INTO MyTable VALUES (1, 'A', 3, 4); INSERT INTO MyTable VALUES (1, 'B', 2, 5); INSERT INTO MyTable VALUES (1, 'C', 8, 10); INSERT INTO MyTable VALUES (2, 'A', 25, 40); INSERT INTO MyTable VALUES (2, 'D', 35, 50); GO [quoted text, click to view] "Jerry Spivey" <jspivey@vestas-awt.com> wrote in message news:eVdnrnryFHA.3000@TK2MSFTNGP12.phx.gbl... > GB, > > Gonna have to give us more than that. DDL and criteria please. > > HTH > > Jerry > "GB" <v7v1k3@hotmail.com> wrote in message > news:yFf1f.5002$y_1.820@edtnps89... > >I have a table like this: > > > > ID | F1 | F2 | F3| > > ______________ > > > > 1 | A | 3 | 4 | > > 1 | B | 2 | 5 | > > 1 | C | 8 | 10 | > > 2 | A | 25| 40 | > > 2 | D | 35| 50 | > > > > I need a query to return the following recordset: > > > > ID | F1 | F2 | F3| > > ______________ > > > > 1 | A | 3 | 4 | > > 1 | B | 2 | 5 | > > 1 | C | 8 | 10 | > > 1 | D | 0 | 0 | > > > > > > Thanks, > > GB > > > > > >
The criteria is : 1. SELECT .... FROM MyTable WHERE ID = 1 ; (or 2 or any one) 2. I need to get for ID from above (ID=1) ALL distinct values of F1 AND 0s for F2 and F3 where there are no values for ID=1. Thanks, GB [quoted text, click to view] "Jerry Spivey" <jspivey@vestas-awt.com> wrote in message news:Ou8Kt3ryFHA.3864@TK2MSFTNGP12.phx.gbl... > Ok...that is a good start...now what is the criteria for the SELECT? I can > hard code in that 0s be returned for D and the second A is not returned. Is > that your criteria? > > HTH > > Jerry > "GB" <v7v1k3@hotmail.com> wrote in message > news:FJg1f.5013$y_1.4583@edtnps89... > > CREATE TABLE [dbo].[MyTable] ( > > [ID] [int] NULL , > > [F1] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , > > [F2] [int] NULL , > > [F3] [int] NULL > > ) ON [PRIMARY] > > GO > > > > INSERT INTO MyTable VALUES (1, 'A', 3, 4); > > INSERT INTO MyTable VALUES (1, 'B', 2, 5); > > INSERT INTO MyTable VALUES (1, 'C', 8, 10); > > INSERT INTO MyTable VALUES (2, 'A', 25, 40); > > INSERT INTO MyTable VALUES (2, 'D', 35, 50); > > GO > > > > > > > > "Jerry Spivey" <jspivey@vestas-awt.com> wrote in message > > news:eVdnrnryFHA.3000@TK2MSFTNGP12.phx.gbl... > >> GB, > >> > >> Gonna have to give us more than that. DDL and criteria please. > >> > >> HTH > >> > >> Jerry > >> "GB" <v7v1k3@hotmail.com> wrote in message > >> news:yFf1f.5002$y_1.820@edtnps89... > >> >I have a table like this: > >> > > >> > ID | F1 | F2 | F3| > >> > ______________ > >> > > >> > 1 | A | 3 | 4 | > >> > 1 | B | 2 | 5 | > >> > 1 | C | 8 | 10 | > >> > 2 | A | 25| 40 | > >> > 2 | D | 35| 50 | > >> > > >> > I need a query to return the following recordset: > >> > > >> > ID | F1 | F2 | F3| > >> > ______________ > >> > > >> > 1 | A | 3 | 4 | > >> > 1 | B | 2 | 5 | > >> > 1 | C | 8 | 10 | > >> > 1 | D | 0 | 0 | > >> > > >> > > >> > Thanks, > >> > GB > >> > > >> > > >> > >> > > > > > >
Why don't you have a key in this table? Why are all the columns nullable? I can't guess what you want to do unless you explain how you arrived at the result you posted. -- David Portas SQL Server MVP -- [quoted text, click to view] "GB" <v7v1k3@hotmail.com> wrote in message news:FJg1f.5013$y_1.4583@edtnps89... > CREATE TABLE [dbo].[MyTable] ( > [ID] [int] NULL , > [F1] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , > [F2] [int] NULL , > [F3] [int] NULL > ) ON [PRIMARY] > GO > > INSERT INTO MyTable VALUES (1, 'A', 3, 4); > INSERT INTO MyTable VALUES (1, 'B', 2, 5); > INSERT INTO MyTable VALUES (1, 'C', 8, 10); > INSERT INTO MyTable VALUES (2, 'A', 25, 40); > INSERT INTO MyTable VALUES (2, 'D', 35, 50); > GO > > > > "Jerry Spivey" <jspivey@vestas-awt.com> wrote in message > news:eVdnrnryFHA.3000@TK2MSFTNGP12.phx.gbl... >> GB, >> >> Gonna have to give us more than that. DDL and criteria please. >> >> HTH >> >> Jerry >> "GB" <v7v1k3@hotmail.com> wrote in message >> news:yFf1f.5002$y_1.820@edtnps89... >> >I have a table like this: >> > >> > ID | F1 | F2 | F3| >> > ______________ >> > >> > 1 | A | 3 | 4 | >> > 1 | B | 2 | 5 | >> > 1 | C | 8 | 10 | >> > 2 | A | 25| 40 | >> > 2 | D | 35| 50 | >> > >> > I need a query to return the following recordset: >> > >> > ID | F1 | F2 | F3| >> > ______________ >> > >> > 1 | A | 3 | 4 | >> > 1 | B | 2 | 5 | >> > 1 | C | 8 | 10 | >> > 1 | D | 0 | 0 | >> > >> > >> > Thanks, >> > GB >> > >> > >> >> > >
[quoted text, click to view] On Thu, 06 Oct 2005 20:19:42 GMT, GB wrote: >I have a table like this: > >ID | F1 | F2 | F3| >______________ > >1 | A | 3 | 4 | >1 | B | 2 | 5 | >1 | C | 8 | 10 | >2 | A | 25| 40 | >2 | D | 35| 50 | > >I need a query to return the following recordset: > >ID | F1 | F2 | F3| >______________ > >1 | A | 3 | 4 | >1 | B | 2 | 5 | >1 | C | 8 | 10 | >1 | D | 0 | 0 |
Hi GB, Try if this does what you need: SELECT 1 AS ID, a.F1, COALESCE(a.F2, 0) AS F2, COALESCE(a.F3,0) AS F3 FROM (SELECT DISTINCT F1 FROM YourTable) AS a LEFT JOIN YourTable AS b ON b.ID = 1 AND b.F1 = a.F1 Best, Hugo --
Don't see what you're looking for? Try a search.
|