Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dakeyras
Starting Member
4 Posts |
Posted - 2007-03-22 : 09:59:25
|
| Hi there, I'm new here and quite green when it comes to SQL in general - I havent used it in any depth for some years and, in the absence of our DBA I've been asked to produce a report... I'm just using query analyser to extract some data as I don't have access to Crystal or anything similar...I have a table laid out as follows - the PK is REC_ID. It basically stores all the contract start dates for each client we deal with.RECID------Company-----Product-----------Date445as------ABCLtd------ICT-Hardware------2007-01-14 00:00:00.000253s1------ABCLtd------SOFT-Mainstream---2007-01-18 00:00:00.000567o8------ABCLtd------SOFT-Maintenance--2007-01-18 00:00:00.000809b9------Gen4Ltd-----ICT-Hardware------2007-01-14 00:00:00.000098xc------Gen4Ltd-----SOFT-Maintenance--2007-01-18 00:00:00.000551df------SteetsPLC---ICT-Hardware------2007-01-14 00:00:00.000919sd------SteetsPLC---SOFT-Upgrade------2007-01-18 00:00:00.000010qr------SteetsPLC---SOFT-Maintenance--2007-01-18 00:00:00.000124vv------SteetsPLC---PERS-Allocations--2007-01-18 00:00:00.000I can easily extract, for example, how many companies have taken up individual services, or many times individual services have been employed... what I'm having difficulty with is trying to extract, for example, all those companies who have had say taken up ICT AND SOFT services, whether explicit or using a LIKE statement.For instance, if I wanted to find all those companies that had taken up both 'ICT-Hardware' AND 'Soft-Maintenance' and literally just get back the results...CompanyABCLtdSteetsPLCOr a numeric result(in this case 2) would be ok...How would I do that? Sorry, this may seem to be very obvious to you but I'm at a loss...Hope you can help. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-22 : 10:04:51
|
[code]select Companyfrom yourtablewhere Product in ('ICT-Hardware', 'SOFT-Mainstream')group by Companyhaving count(*) = 2[/code] KH |
 |
|
|
dakeyras
Starting Member
4 Posts |
Posted - 2007-03-22 : 10:06:41
|
| Hey Khtan! Thanks for the fast response!if I wanted to find those that are like '%SOFT%' for example, would I still be able to use the same syntax? (as in the IN statement)? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-22 : 10:13:20
|
[code]select Companyfrom yourtablewhere Product = 'ICT-Hardware'or Product like '%SOFT%'group by Companyhaving count(*) = 2[/code] KH |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-22 : 10:15:00
|
quote: Originally posted by khtan
select Companyfrom yourtablewhere Product = 'ICT-Hardware'or Product like '%SOFT%' -- If SOFT is going to be prefixgroup by Companyhaving count(*) = 2 KH
Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
dakeyras
Starting Member
4 Posts |
Posted - 2007-03-22 : 10:28:54
|
| Hmmm, they seem to be bringing back innacuracies.For example - in the original post, I want to isolate all companies that have been provided with BOTH services, not either / or. It's essential that I bring back those that have only had ALL the services specified in the query.The initial query is bringing back companies that have sometimes had only one of these services? Any ideas? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-22 : 11:22:57
|
| select company from (select 1 as t, Company from yourtable where Product = 'ICT-Hardware'unionselect 0, Company from yourtable where Product = 'SOFT-Mainstream') AS xgroup by Companyhaving count(*) = 2Peter LarssonHelsingborg, Sweden |
 |
|
|
dakeyras
Starting Member
4 Posts |
Posted - 2007-03-22 : 13:23:54
|
| Thank you Peso! |
 |
|
|
|
|
|
|
|