all groups > sql server programming > october 2005 >
You're in the

sql server programming

group:

SQL query question


Re: SQL query question Jerry Spivey
10/6/2005 2:07:10 PM
sql server programming: GB,

Gonna have to give us more than that. DDL and criteria please.

HTH

Jerry
[quoted text, click to view]

Re: SQL query question Jerry Spivey
10/6/2005 2:35:50 PM
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]

Re: SQL query question Jerry Spivey
10/6/2005 4:42:40 PM
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]

SQL query question GB
10/6/2005 8:19:42 PM
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

Re: SQL query question GB
10/6/2005 9:32:21 PM
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]

Re: SQL query question GB
10/6/2005 10:32:48 PM
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]

Re: SQL query question David Portas
10/6/2005 10:44:55 PM
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]

Re: SQL query question Hugo Kornelis
10/8/2005 12:53:22 AM
[quoted text, click to view]

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
--

AddThis Social Bookmark Button