Showing posts with label trouble. Show all posts
Showing posts with label trouble. Show all posts

Saturday, February 25, 2012

Can anyone help with this query...

Hi,I'm moving across to SQL Server after using MySQL and having trouble with one particular query. My tables are outlined below. I'm trying to get hold of the names of bands playing the most recently added shows but can't quite get there.


Shows table = Id.. some other columns.. DateAdded
Bands table = Id, Name
Playing table = Id, Show_Id, Band_Id

My query at the moment is

SELECT Bands.Name FROM Shows
INNER JOIN Playing ON Shows.Id = Playing.Show_Id
INNER JOIN Bands ON Bands.Id = Playing.Band_Id
AND Playing.Headlining = 1
ORDER BY Shows.DateAdded DESC

Which gives me the right results but will return duplicate band names if there is more than one new show for that band. I've tried doing SELECT DISTINCT Bands.Name but get an error saying "ORDER BY items must appear in the select list if SELECT DISTINCT is specified". I also tried using a GROUP BY clause but I can't seem to combine it with the order by clause without getting an error.

I've tried creating a view which handles the joining and selecting distinct Names from the view but this is ignoring the ordering and just returns band names in alphabetical order.

I hope you understand my problem and I really HOPE someone has a solution even if it means returning some extra columns which I can just ignore in my page logic. Thanks.

SELECT Bands.Name, MAX(Shows.DateAdded) FROM Shows
INNER JOIN Playing ON Shows.Id = Playing.Show_Id
INNER JOIN Bands ON Bands.Id = Playing.Band_Id
AND Playing.Headlining = 1

GROUP BY Bands.Name
ORDER BY Shows.DateAdded DESC

|||

Thanks for the reply but that doesn't work either. The error I get the same I error I was getting when I tried using GROUP BY...

Msg 8127, Level 16, State 1, Line 1

Column "Shows.DateAdded" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

|||

You could try a slight variation of that:

SELECT Bands.Name, MAX(Shows.DateAdded) FROM Shows
INNER JOIN Playing ON Shows.Id = Playing.Show_Id
INNER JOIN Bands ON Bands.Id = Playing.Band_Id
AND Playing.Headlining = 1

GROUP BY Bands.Name
ORDER BY 2 DESC

or

SELECT Bands.Name, MAX(Shows.DateAdded) FROM Shows
INNER JOIN Playing ON Shows.Id = Playing.Show_Id
INNER JOIN Bands ON Bands.Id = Playing.Band_Id
AND Playing.Headlining = 1

GROUP BY Bands.Name
ORDER BY MAX(Shows.DateAdded) DESC

|||They both work. Thank you.

Can anyone help with help?

I guess I shouldn't expect Microsoft to go to any trouble to make our lives easier but my goodness they've out done themselves with the latest help system.

Can anyone tell me how to actually find help on anything? I open SQL Server Managment studio and click help How do I and get help on how to use help... Boy how useful... Next I click on search and tell it to search for Autoinc, or increment, and tell it to search products for SQL Server. Boy how nice to bring me all these useful topics on articles written on Fox Pro in the late 90's...

In fact, not a single help entry on SQL Server anywhere in my list... Great!

This is normal. Every search I've done I've had to resort to google. I've even tried turning off this feature to go to the Internet for all the help and its still useless.

All I started out wanting was to find out how to create an AutoInc field type or at least provide that type of feature with SQL Server and 30 minutes later I find myself complaining about help on help!

Is anyone finding this help system with Visual Studio 2005 and SQL Server 2005 even remotely helpful? They should rename it the ConfuseQLater or something...

what i understand from this is u have not installed Books Online (BOL) which is the bible for SQL Server... While installing you should have opted this option ...

download and install BOL from this link...

http://www.microsoft.com/downloads/details.aspx?familyid=a6f79cb1-a420-445f-8a4b-bd77a7da194b&displaylang=en

Madhu

Friday, February 10, 2012

calling user-defined functions in another DB

I have a number of databases that require a set of common functions. I'd like to place all those functions in a central DB. I'm having trouble calling them using the syntax FunctionDB.GetParamLength() for example, which works within the FunctionDB database.

Any ideas/suggestions? I really don't want to maintain seperate copies of the functions across 5+ databases.

You have to qualify the function name with owner name, as in

FunctionDB.dbo.GetParamLength()

|||I've tried that, unfortunately no luck.|||Should work for you like this here:

USE Master

GO

CREATE FUNCTION dbo.DisplaySomething()

RETURNS VARCHAR(10)

AS

BEGIN

RETURN('Something')

END

GO

USE AdventureWorks

GO

SELECT master.dbo.DisplaySomething()

USE Master

GO

DROP FUNCTION dbo.DisplaySomething

Is the database case sensitive ? Then you have use the right case sensitive name. Is the owner of the function dbo ? Otherwise you have to name the original owner.


HTH, jens Suessmeyer.

http://www.sqlserver2005.de

|||There was an error within the function I was calling which confused me, thanks for putting me on the right track :)