473,411 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Help with this query, please

Hi. I have a table, let's name it TableA, containing a list of PINS
with some other data. I have another table, "Comments". A PIN in
TableA might have 0, 1, or many records in the Comments table. I need
to produce a data set for a report. My query would be:
__________________________________________
Select *

from dbo.[TableA] LEFT OUTER JOIN (SELECT EmployeePIN,
Count(EmployeePIN) AS OldComments FROM dbo.Comments GROUP BY
EmployeePIN ) X

ON ([TableA].PIN = X.EmployeePIN)
__________________________________________

which works fine. The only problems is that I would like to show some
string, like 'No Records' , if the field OldComments returns NULL.

So I try:
__________________________________________

Select [TableA].*, CASE X.OldComments WHEN NULL THEN 'No Records' END
AS Comments

from dbo.[TableA] LEFT OUTER JOIN (SELECT EmployeePIN,
Count(EmployeePIN) AS OldComments FROM dbo.Comments GROUP BY
EmployeePIN ) X

ON ([TableA].PIN = X.EmployeePIN)
__________________________________________

which works just as well, except that it does not return 'No
Comments', or 0, or... whatever, when it is null. I need to show
something NOT NULL in the dataset, so I can easily build a link with
that something. A NULL linking to another page works, but you, as a
user, have to KNOW that you need to click there - I would like to
show, say, an underlined 'No Records' instead...

What should I do, please ?

Thank you for reading this.
Alex.
Dec 10 '07 #1
3 1123
Hi Alex,

There are a couple of ways you could do this, but your first step
should be to look up the ISNULL function in BOL :)

Another thing - as you are returning a COUNT value (Integer), you will
never be able to display 'No Comments' (String) as a result for this
column as it is not the same type. You'll have to settle for 0, or
convert the valid counts to strings which is probably not a good idea
for your downstream logic.

So sticking as close to your supplied query as possible, try:
/* NOTE: All queries untested as no DDL supplied */
SELECT
[TableA].*
, CASE
WHEN X.OldComments IS NULL
THEN 0
ELSE X.OldComments
END AS Comments
FROM dbo.[TableA]
LEFT OUTER JOIN (
SELECT
EmployeePIN
, COUNT(EmployeePIN) AS OldComments
FROM dbo.Comments
GROUP BY EmployeePIN
) X
ON [TableA].PIN = X.EmployeePIN

Another way using ISNULL:
SELECT
[TableA].*
, ISNULL(X.OldComments, 0) AS Comments
FROM dbo.[TableA]
LEFT OUTER JOIN (
SELECT
EmployeePIN
, COUNT(EmployeePIN) AS OldComments
FROM dbo.Comments
GROUP BY EmployeePIN
) X
ON [TableA].PIN = X.EmployeePIN

My preferred way:
SELECT
[TableA].*
, COUNT(c.EmployeePIN) AS Comments
FROM dbo.[TableA]
LEFT OUTER JOIN dbo.Comments c
ON [TableA].PIN = c.EmployeePIN
GROUP BY <[TableA].*-- Column list not supplied

Good luck!
J
Dec 11 '07 #2
Thank you very much, "J", you have been very kind. Problem solved, and
I have learned something.
Thanks again ! Alex.

On Dec 11, 7:14 am, jhofm...@googlemail.com wrote:
Hi Alex,

There are a couple of ways you could do this, but your first step
should be to look up the ISNULL function in BOL :)

Another thing - as you are returning a COUNT value (Integer), you will
never be able to display 'No Comments' (String) as a result for this
column as it is not the same type. You'll have to settle for 0, or
convert the valid counts to strings which is probably not a good idea
for your downstream logic.

So sticking as close to your supplied query as possible, try:
/* NOTE: All queries untested as no DDL supplied */
SELECT
[TableA].*
, CASE
WHEN X.OldComments IS NULL
THEN 0
ELSE X.OldComments
END AS Comments
FROM dbo.[TableA]
LEFT OUTER JOIN (
SELECT
EmployeePIN
, COUNT(EmployeePIN) AS OldComments
FROM dbo.Comments
GROUP BY EmployeePIN
) X
ON [TableA].PIN = X.EmployeePIN

Another way using ISNULL:
SELECT
[TableA].*
, ISNULL(X.OldComments, 0) AS Comments
FROM dbo.[TableA]
LEFT OUTER JOIN (
SELECT
EmployeePIN
, COUNT(EmployeePIN) AS OldComments
FROM dbo.Comments
GROUP BY EmployeePIN
) X
ON [TableA].PIN = X.EmployeePIN

My preferred way:
SELECT
[TableA].*
, COUNT(c.EmployeePIN) AS Comments
FROM dbo.[TableA]
LEFT OUTER JOIN dbo.Comments c
ON [TableA].PIN = c.EmployeePIN
GROUP BY <[TableA].*-- Column list not supplied

Good luck!
J
Dec 11 '07 #3
You're welcome :)

If you're using SQL Server 2005, you should also do a quick search on
Common Table Expressions (CTE's) in BOL.

J
Dec 11 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: sah | last post by:
I need some help with the following query: DECLARE @SRV VARCHAR(20), @date smalldatetime SET @SRV = (select @@servername) SET @date = '20040901' select Srv_Name = @SRV, DB_Name = 'DB_NAME',...
2
by: pratchaya | last post by:
This is my sample error in my MySQL Log New value of fp=(nil) failed sanity check, terminating stack trace! Please read http://www.mysql.com/doc/en/Using_stack_trace.html and follow instructions...
6
by: Martin Hampl | last post by:
Hi, I am using PostgreSQL 7.4, but I did have the same problem with the last version. I indexed the column word (defined as varchar(64)) using lower(word). If I use the following query,...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
6
by: John Baker | last post by:
Hi: As those who have looked at this newsgroup recently will realize, I am a neophyte with Access, although I have experienced with Approach (the Lotus product). There are things I could easily...
9
by: hope | last post by:
Hi Access 97 I'm lost on this code please can you help ================================= Below is some simple code that will concatenate a single field's value from multiple records into a...
5
by: Rated R1 | last post by:
I wrote this before in the NGs, so I am going to paste the responses that I got and see if someone can please help me. Email me and we can set something up as Id even be willing to pay for your...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
0
by: alritedonthaveacow | last post by:
please can someone please help me. i have mad a database in access, and its pretty amature. i have a product table and a stock table. the products and the stock are related through the product id....
0
by: uno7031 | last post by:
Help Please!!! Adding 5 Days to another Date in an access query Good Morning, Help please…. I am new to access and trying to write a query that will add 5 days between a RecDate and a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.