473,324 Members | 2,257 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,324 software developers and data experts.

T-SQL question

I'm fairly new to mySQL and T-SQL in general and am looking for what I
thought would be a fairly straightforward query. Here are some sample
tables and data.

Let's say I have a table of authors and a table of books. I want to
bring back a result set with a row for each author showing the total
number of books they have of a particular genre in the books table. If
an author has no books in the books table of that genre they would
have row in the resultset with a zero for their number of books.

Here is some sample data. The resultset I'm looking for is at the
bottom. In this example I'm looking for the number of books of the
scifi genre each author has in this table.

I assumed it would be a fairly straightforward left outer join but
when I add the "WHERE b.genre = 'scifi' " it removes the authors from
the result set with no books of that particular genre.

Please reply to the newsgroup.
Authors
----------------------------------
a_id a_name a_publishing_co
----------------------------------
1 fred McMillon
2 sally BooksRUs
3 george Streetscape

Books
-------------------------------
b_id a_id b_genre
-------------------------------
1 1 scifi
2 1 history
3 1 scifi
4 2 scifi
resultset I'd like
----------------------
totals a_id a_name
----------------------
2 1 fred
1 2 sally
0 3 george

The following queries are what I have attempted but they both bring
back incorrect data (ie, no row for george)

SELECT IF(b.a_id IS NULL,0,count(1)) as 'totals', a.a_name,
a_publishing_co
FROM authors a LEFT OUTER JOIN books b ON b.a_id = a.a_id
WHERE b.b_genre = 'scifi'
GROUP BY b.a_id;

SELECT count(1) as 'totals', a.a_name, a_publishing_co
FROM authors a LEFT OUTER JOIN books b ON b.a_id = a.a_id
WHERE b.b_genre = 'scifi'
GROUP BY b.a_id;
Jul 19 '05 #1
4 4972
If this is helpful, I think the reason the 0 row doesn't show up is that you eliminate it by your restriction to rows where genre = "scifi". Perhaps try expanding your criteria to include nulls under genre.

- Adam Shaw
Engwar<en*****@yahoo.com> 11/8/2003 11:23:11 AM >>>

I'm fairly new to mySQL and T-SQL in general and am looking for what I
thought would be a fairly straightforward query. Here are some sample
tables and data.

Let's say I have a table of authors and a table of books. I want to
bring back a result set with a row for each author showing the total
number of books they have of a particular genre in the books table. If
an author has no books in the books table of that genre they would
have row in the resultset with a zero for their number of books.

Here is some sample data. The resultset I'm looking for is at the
bottom. In this example I'm looking for the number of books of the
scifi genre each author has in this table.

I assumed it would be a fairly straightforward left outer join but
when I add the "WHERE b.genre = 'scifi' " it removes the authors from
the result set with no books of that particular genre.

Please reply to the newsgroup.
Authors
----------------------------------
a_id a_name a_publishing_co
----------------------------------
1 fred McMillon
2 sally BooksRUs
3 george Streetscape

Books
-------------------------------
b_id a_id b_genre
-------------------------------
1 1 scifi
2 1 history
3 1 scifi
4 2 scifi
resultset I'd like
----------------------
totals a_id a_name
----------------------
2 1 fred
1 2 sally
0 3 george

The following queries are what I have attempted but they both bring
back incorrect data (ie, no row for george)

SELECT IF(b.a_id IS NULL,0,count(1)) as 'totals', a.a_name,
a_publishing_co
FROM authors a LEFT OUTER JOIN books b ON b.a_id = a.a_id
WHERE b.b_genre = 'scifi'
GROUP BY b.a_id;

SELECT count(1) as 'totals', a.a_name, a_publishing_co
FROM authors a LEFT OUTER JOIN books b ON b.a_id = a.a_id
WHERE b.b_genre = 'scifi'
GROUP BY b.a_id;
Jul 19 '05 #2
If this is helpful, I think the reason the 0 row doesn't show up is that you eliminate it by your restriction to rows where genre = "scifi". Perhaps try expanding your criteria to include nulls under genre.

- Adam Shaw
Engwar<en*****@yahoo.com> 11/8/2003 11:23:11 AM >>>

I'm fairly new to mySQL and T-SQL in general and am looking for what I
thought would be a fairly straightforward query. Here are some sample
tables and data.

Let's say I have a table of authors and a table of books. I want to
bring back a result set with a row for each author showing the total
number of books they have of a particular genre in the books table. If
an author has no books in the books table of that genre they would
have row in the resultset with a zero for their number of books.

Here is some sample data. The resultset I'm looking for is at the
bottom. In this example I'm looking for the number of books of the
scifi genre each author has in this table.

I assumed it would be a fairly straightforward left outer join but
when I add the "WHERE b.genre = 'scifi' " it removes the authors from
the result set with no books of that particular genre.

Please reply to the newsgroup.
Authors
----------------------------------
a_id a_name a_publishing_co
----------------------------------
1 fred McMillon
2 sally BooksRUs
3 george Streetscape

Books
-------------------------------
b_id a_id b_genre
-------------------------------
1 1 scifi
2 1 history
3 1 scifi
4 2 scifi
resultset I'd like
----------------------
totals a_id a_name
----------------------
2 1 fred
1 2 sally
0 3 george

The following queries are what I have attempted but they both bring
back incorrect data (ie, no row for george)

SELECT IF(b.a_id IS NULL,0,count(1)) as 'totals', a.a_name,
a_publishing_co
FROM authors a LEFT OUTER JOIN books b ON b.a_id = a.a_id
WHERE b.b_genre = 'scifi'
GROUP BY b.a_id;

SELECT count(1) as 'totals', a.a_name, a_publishing_co
FROM authors a LEFT OUTER JOIN books b ON b.a_id = a.a_id
WHERE b.b_genre = 'scifi'
GROUP BY b.a_id;
Jul 19 '05 #3
That did the trick (and now seems so darned obvious!)

Thanks Adam.
Adam Shaw<sh**@contrapunctus.net> wrote in message news:<ZF******************@newssvr33.news.prodigy. com>...
If this is helpful, I think the reason the 0 row doesn't show up is that you eliminate it by your restriction to rows where genre = "scifi". Perhaps try expanding your criteria to include nulls under genre.

- Adam Shaw

Jul 19 '05 #4
That did the trick (and now seems so darned obvious!)

Thanks Adam.
Adam Shaw<sh**@contrapunctus.net> wrote in message news:<ZF******************@newssvr33.news.prodigy. com>...
If this is helpful, I think the reason the 0 row doesn't show up is that you eliminate it by your restriction to rows where genre = "scifi". Perhaps try expanding your criteria to include nulls under genre.

- Adam Shaw

Jul 19 '05 #5

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

Similar topics

2
by: Steve | last post by:
Hi; I'm brand spanking new to sqlserver ( nice so far ). I need to make a simple data change across a list of tables. Basically replace an old date with a new date. However, the people I am...
2
by: Steve | last post by:
Hi; I have been writing a lot of short tsql scripts to fix a lot of tiny database issues. I was wondering if a could make an array of strings in tsql that I could process in a loop, something...
18
by: mountain man | last post by:
Greetings to all database professionals and laymen, Let us make a bold assumption that we have developed a software tool for the SQL Server environment which simply acts as an interface between...
2
by: dynoweb | last post by:
I have several *.sql files with schema/data changes to be applied to our current database. Is there a way to create a TSQL script that could be run from the SQL Query Analyzer that would...
1
by: TOM GUGGER | last post by:
OMNI GROUP tgugger@aimexec.com T-SQL/ CONTRACT TO PERM/ ATLANTA
3
by: David Lozzi | last post by:
Howdy, ISSUE 1: See issue 2 below. I have a distance calculator on my site which works great. However, the users need to sort by distance, which make sense. I'm not sure how to do it other than...
16
by: David Lozzi | last post by:
Hello, I have some code that adds a new user. The new user has a checkboxlist of items which they can be associated with. I would like to send this list of items to TSQL along with the new user...
7
by: Filips Benoit | last post by:
Dear all, Tables: COMPANY: COM_ID, COM_NAME, ..... PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID, PRP_DEFAULT_VALUE ( nvarchar) COMPANY_PROPERTY: CPROP_COM_ID, CPROP_PRP_ID, CPROP_VALUE...
8
by: David Lozzi | last post by:
I'm fairly new to ASP.Net 2.0 SQLDatasource objects. It defaults using TSQL statments for the SELECT, INSERT, UPDATE, DELETE commands, which is great and it works. However, I've always been taught...
0
by: DR | last post by:
Unable to start TSQL Debugging. Could not attach to SQL Server Process on 'srvname'. The RPC server is unavailable. I get this error when I try to run a SQL Server Project with a CLR stored...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.