SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 07-22-2008, 02:02 PM
elle39 elle39 is offline
Junior Member
 
Join Date: Jul 2008
Posts: 2
elle39 is on a distinguished road
Unhappy ANY dialect question:

nested queries


I am using SQL Server 2005 don't which dialect that would be SQL 92 I am guessing !

How can I get:

1) only the Top 2 highest InvoiceTotal rows for each VENDOR in each STATE.

2) the VENDORNAMES that have supplied ONLY the highest number of Invoices for each state.


For the first query, I know this is wrong but what should i be doing??
SELECT top 2 Invoicetotals, vendorname, vendorstate
FROM vendors JOIN invoices ON vendorID
GROUP BY vendorstate, Vendorname
ORDER BY InvoiceTotal DESC

For the second query, I didn't quite get it:
SELECT vendorname, vendorstate, count(*) AS InvoiceQty
FROM vendors JOIN invoices ON vendorID
GROUP BY VendorState, VendorName
ORDER BY InvoiceTotal DESC

Help !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 07-23-2008, 05:59 AM
arun arun is offline
Junior Member
 
Join Date: Jul 2008
Posts: 9
arun is on a distinguished road
Default ANY answer. Re: nested queries

this is the correct query for ur first question
select vendors.vendorname,vendors.vendorstate,invoices.in voicetotal from vendors join invoices on vendors.vendorid=invoices.vendorid
where vendors.vendorid in(select top 2 vendorid from invoices order by invoicetotal DESC)

Last edited by arun : 07-23-2008 at 11:35 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-23-2008, 06:03 AM
arun arun is offline
Junior Member
 
Join Date: Jul 2008
Posts: 9
arun is on a distinguished road
Default ANY answer. Re: nested queries

the second scenario u said is not clear to me if u can explain it properly i may try to help u
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-23-2008, 06:51 AM
arun arun is offline
Junior Member
 
Join Date: Jul 2008
Posts: 9
arun is on a distinguished road
Default ANY answer. Re: nested queries

i think this will be the answer for ur second question

create view new as
select vendorid,"nova"=count(invoiceqty) from invoice group by vendorid

select vendorname from vendors where
vendorid in(select vendorid from new where nova=(select max(nova) from new))

Last edited by arun : 07-23-2008 at 11:36 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-23-2008, 11:41 AM
arun arun is offline
Junior Member
 
Join Date: Jul 2008
Posts: 9
arun is on a distinguished road
Default ANY answer. Re: nested queries

when copy pasting the query check for syntax error coz query in edit page and the output differ
for example in the case of first query there is a space between in and voice of the word invoice which is not possible to edit from edit page. there is a chance of misunderstanding it as the keyword in. so check for such cases. those queries are found working in sql server 2005

Last edited by arun : 07-23-2008 at 11:43 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-23-2008, 01:15 PM
elle39 elle39 is offline
Junior Member
 
Join Date: Jul 2008
Posts: 2
elle39 is on a distinguished road
Default ANY answer. Re: nested queries

Thanks a lot guys. I have not had a chance to try this yet. Will revert back in a day.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 01:00 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8