SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query 2 tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rjrferreira
Starting Member

13 Posts

Posted - 03/15/2007 :  10:52:10  Show Profile  Reply with Quote
I all,

i have a problem with a query.

I have two tables (Software and Licenses), and one software can have one or more licenses.

So what i want to get from this query is:

- all info of the software table
- and, if the software have or not, attributed licenses.

How can i do that?

snSQL
Flowing Fount of Yak Knowledge

USA
1837 Posts

Posted - 03/15/2007 :  10:54:25  Show Profile  Reply with Quote
Use an outer join

SELECT *
FROM Software
LEFT OUTER JOIN Licenses ON Software.PKID = Licenses.FKID
Go to Top of Page

rjrferreira
Starting Member

13 Posts

Posted - 03/15/2007 :  10:59:09  Show Profile  Reply with Quote
sorry snSQL but doesn't work that query!
Go to Top of Page

snSQL
Flowing Fount of Yak Knowledge

USA
1837 Posts

Posted - 03/15/2007 :  11:01:45  Show Profile  Reply with Quote
So you just tried to run that query and it didn't work - huh! You did not give the names of the columns in your tables, so of course it won't work, you'll need to replace the names in the query I gave you with your names. That was just an example!
Go to Top of Page

rjrferreira
Starting Member

13 Posts

Posted - 03/15/2007 :  11:10:09  Show Profile  Reply with Quote
quote:
Originally posted by snSQL

So you just tried to run that query and it didn't work - huh! You did not give the names of the columns in your tables, so of course it won't work, you'll need to replace the names in the query I gave you with your names. That was just an example!



I know that!!! :D
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 03/15/2007 :  11:15:46  Show Profile  Reply with Quote
then post your table DDL, some sample data and the expected result.


KH

Go to Top of Page

rjrferreira
Starting Member

13 Posts

Posted - 03/15/2007 :  11:28:06  Show Profile  Reply with Quote
My table Software have this info:
- id
- softname
- version
- state

My table License have this info:
- id
- softID (FK)
- serial
- username


In table License in field username i put the name of the user that have the licence.


So what i want with the query is something like this:

ID|softname|version|state|allLicencesFree
-----------------------------------------
1|microsoft|200|A|1
2|oracle|9.0|A|0


In field allLicencesFree "1" is if all Licences are Free, and "0" if not
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 03/15/2007 :  11:33:41  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT s.ID, s.SoftName, s.Version, s.State, l.ID, l.Serial, l.UserName
FROM Software AS s
LEFT JOIN Licenses AS l ON l.SoftID = s.ID


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

rjrferreira
Starting Member

13 Posts

Posted - 03/15/2007 :  11:39:55  Show Profile  Reply with Quote
quote:
Originally posted by Peso

SELECT s.ID, s.SoftName, s.Version, s.State, l.ID, l.Serial, l.UserName
FROM Software AS s
LEFT JOIN Licenses AS l ON l.SoftID = s.ID


Peter Larsson
Helsingborg, Sweden

Go to Top of Page

rjrferreira
Starting Member

13 Posts

Posted - 03/15/2007 :  11:41:56  Show Profile  Reply with Quote
quote:
Originally posted by Peso

SELECT s.ID, s.SoftName, s.Version, s.State, l.ID, l.Serial, l.UserName
FROM Software AS s
LEFT JOIN Licenses AS l ON l.SoftID = s.ID


Peter Larsson
Helsingborg, Sweden



With that query all licenses appears, but i don't want that!!!

I want that one colunm of the result say to me:

"This software don't have any attributed license, or have."


and i just want one line per software.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 03/15/2007 :  11:50:18  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT s.SoftName, s.Version, case when l.softid is null then 'No license' else 'License' end
FROM Software AS s
LEFT JOIN Licenses AS l ON l.SoftID = s.ID


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

snSQL
Flowing Fount of Yak Knowledge

USA
1837 Posts

Posted - 03/15/2007 :  13:09:21  Show Profile  Reply with Quote
quote:
With that query all licenses appears, but i don't want that!!!
I want that one colunm of the result say to me:
"This software don't have any attributed license, or have."


That's not what you said, you said you wanted the allLicensesFree column, but you didn't tell us how you know if a license is free. So here is what you want without the allLicensesFree column
SELECT Q.[ID], Q.softname, Q.version, Q.state, 
    CASE WHEN Q.Licensed = 0
    THEN 'This software don''t have any attributed license'
    ELSE 'This software do have any attributed license' as licensed
(SELECT S.[ID], S.softname, S.version, S.state, MAX(ISNULL(L.SoftID, 0)) AS Licensed
FROM Software S
LEFT OUTER JOIN Licenses L ON S.[ID] = L.SoftID
GROUP BY S.[ID], S.softname, S.version, S.state) AS Q

Edited by - snSQL on 03/15/2007 13:16:10
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000