Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Urgent Query Help


Urgent Query Help

Author
Message
DBA12345
DBA12345
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 240
The below query has to give Y if it's equals to zero but it gives always zero. can you modify this one



UPDATE T1 SET [coumnname] = CASE WHEN T2.[Application ID] IS NOT NULL AND T2.[ServerName] IS NOT NULL AND [colname2] <> 0 THEN 'Y' ELSE 'N' END
FROM tablename T1
LEFT JOIN tablename2 T2 ON (T1.[Application ID] = T2.[Application ID] AND T1.[ServerName] = T2.[ServerName]) WHERE T2.[Data Period] = 'September 2012'
David Webb-CDS
David Webb-CDS
SSC Eights!
SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)

Group: General Forum Members
Points: 916 Visits: 8584
This one says if the tested column is NOT = 0, then it will update with a 'y'. Your post isn't really clear about what the actual results of the update are.



And then again, I might be wrong ...
David Webb
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8484 Visits: 18082
There's not much we can do without DDL and sample data.
To audit the problem, you could do a simple select instead of the update.

Another thing to watch is the datatype. You can't expect to return 'Y' if the data type is int.


--This first line will return an error
--UPDATE tablename SET    = 'N'


--Commenting the UPDATE to transform into SELECT
--UPDATE T1 SET
-- [coumnname] = CASE WHEN T2.[Application ID] IS NOT NULL AND T2.[ServerName] IS NOT NULL AND [colname2] <> 0 THEN 'Y' ELSE 'N' END
SELECT T2.[Application ID], --It's recommended not to use spaces for column/object names
T2.[ServerName],
colname2
FROM tablename T1
LEFT JOIN tablename2 T2 ON (T1.[Application ID] = T2.[Application ID] AND T1.[ServerName] = T2.[ServerName])
WHERE T2.[Data Period] = 'September 2012'
--Uncomment to be sure there're records that comply
--AND T2.[Application ID] IS NOT NULL
--AND T2.[ServerName] IS NOT NULL
--AND [colname2] <> 0




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
DBA12345
DBA12345
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 240
Please check this below one more time ...i give clearly


UPDATE T1 SET [September 2012 Billing File] = CASE WHEN T2.[Application ID] IS NOT NULL AND T2.[ServerName]
IS NOT NULL AND [TB Billed This Month] <> 0 THEN 'Y' ELSE 'N' END
FROM TableName1 T1
LEFT JOIN TableName2 T2 ON (T1.[Application ID] = T2.[Application ID]
AND T1.[ServerName] = T2.[ServerName]) WHERE T2.[Data Period] = 'September 2012'
DBA12345
DBA12345
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 240
Please check this below one more time ...i give clearly

It is taking the one first row it should be take entire rows.


UPDATE T1 SET [September 2012 Billing File] = CASE WHEN T2.[Application ID] IS NOT NULL AND T2.[ServerName]
IS NOT NULL AND [TB Billed This Month] <> 0 THEN 'Y' ELSE 'N' END
FROM TableName1 T1
LEFT JOIN TableName2 T2 ON (T1.[Application ID] = T2.[Application ID]
AND T1.[ServerName] = T2.[ServerName]) WHERE T2.[Data Period] = 'September 2012'
Ray M
Ray M
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1491 Visits: 1076
Were trying to point out that since we do not have your tables or data we cannot tell you why the case is evaluating to N

Before running your update,
Run a version of the query as a select statement.
Are the data elements in your query evaluating as you suspect?
Is T2.Application ID Not Null? Is ServerName Not Null?
and does TB Billed This Month Not equal 0?
SELECT 
   T2.[Application ID]
   , T2.[ServerName]
   ,[TB Billed This Month]
   ,CASE WHEN T2.[Application ID] IS NOT NULL AND T2.[ServerName] IS NOT NULL AND [TB Billed This Month] <> 0 THEN 'Y' ELSE 'N' END
FROM TableName1 T1
LEFT JOIN TableName2 T2 ON (T1.[Application ID] = T2.[Application ID]
AND T1.[ServerName] = T2.[ServerName]) WHERE T2.[Data Period] = 'September 2012'


David Webb-CDS
David Webb-CDS
SSC Eights!
SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)

Group: General Forum Members
Points: 916 Visits: 8584
OK, this code:

UPDATE T1 SET [September 2012 Billing File] = CASE WHEN T2.[Application ID] IS NOT NULL AND T2.[ServerName] 
IS NOT NULL AND [TB Billed This Month] <> 0 THEN 'Y' ELSE 'N' END
FROM TableName1 T1
LEFT JOIN TableName2 T2 ON (T1.[Application ID] = T2.[Application ID]
AND T1.[ServerName] = T2.[ServerName]) WHERE T2.[Data Period] = 'September 2012'



will set [September 2012 Billing File] = 'y' if [TB Billed This Month] IS NOT = 0. If you want it to set [September 2012 Billing File] = 'y' if [TB Billed This Month] = 0 the the '<> 0' in your code needs to change to '= 0'.



And then again, I might be wrong ...
David Webb
Ray M
Ray M
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1491 Visits: 1076
2 possible things
The query is returning multiple rows, and the 'N' is being returned because of the last row.
OR/AND
Your performing a Left Join,
But you referencing T2.[Date Period] in your Where clause effectively makings it an inner join.
Unless the the value for Application ID and Server Name are NULL in the T2 table It will never evaluate to 'Y'

This is why we need sample data and DDL which would allow us to see these things and provide a better answer.

SELECT 
   T2.[Application ID]
   , T2.[ServerName]
   ,[TB Billed This Month]
   ,CASE WHEN T2.[Application ID] IS NOT NULL AND T2.[ServerName] IS NOT NULL AND [TB Billed This Month] <> 0 THEN 'Y' ELSE 'N' END
FROM TableName1 T1
LEFT JOIN TableName2 T2 ON (T1.[Application ID] = T2.[Application ID]
AND T1.[ServerName] = T2.[ServerName]) WHERE T2.[Data Period] = 'September 2012'


DBA12345
DBA12345
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 240
Awesome......I use inner join instead of Left join
Now its working fyn...Thank you very much
DBA12345
DBA12345
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 240
Hi..Once again thanks for all your support. but my lead ask me to modify the query like below. I tried it but it giving error. Could any one modify it plzzz

Msg 8120, Level 16, State 1, Line 1
Column 'AEG_DB.dbo.MRO_TBL_EMCData.Application ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


select [September 2012 Billing File] = CASE WHEN T2.[Application ID] IS NOT NULL AND T2.[ServerName]
IS NOT NULL AND Sum([Billed This Month]) <> 0 THEN 'Y' ELSE 'N' END
FROM [AEG_DB].[dbo].[TBL_Work] T1
LEFT JOIN [TBL_Data] T2 ON (T1.[Application ID] = T2.[Application ID] AND T1.[ServerName] = T2.[ServerName])
WHERE T2.[Data Period] = 'September 2012'
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search