473,495 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sql Query for finding count of records which have say firstId=1 andSecondId=1

Hi All,
I want the query in sql which gives count of records for 2 conditions.
Say i have table Category which has CategoryID and SubCatID as 2
columns.

Then suppose i want count for the records which has CategoryID=1 and
SubCatID =1 .
But I have SubCatID field as comma separated means it may has 1,2,3 or
1 or 11,2,3
I have used query like

select count(*) from Category where CategoryID=1 and SubCatID like
'%1%'

but it take 11 as subcatid which i donot want .
So is there any solution to this problem?

Thanks

Feb 19 '08 #1
1 1424
Not quite sure how this relates to dotnet...
So is there any solution to this problem?
How about: don't store relational data as csv if you want to query it? Or
more usefully, how about:

WHERE ',' + SubCatId + ',' LIKE '%,1,%'

Obviously this is simpler if your csv starts and ends with ',' as you don't
need the extra concatenation (on the left-hand-side). However! This approach
should only be used if your CategoryID can limit the results fairly well;
LIKE with an open start can't really use the index (especially since we are
using a computed [concatenated] value, not the raw column value).

Personally I'd be inclined to store the data out into another table, or
(perhaps) use sqlxml which provides /a/ mechanism (perhaps not the best) to
store multiple values in a single field while retaining the ability to query
and index.

Marc
Feb 19 '08 #2

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

Similar topics

20
10080
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
4
2961
by: DBNovice | last post by:
I have a database that keeps records on the issue and failure of an item. Currently, the database is poorly desisned; therefore I'm performing queries to break the data into normalized tables and...
9
5445
by: (Pete Cresswell) | last post by:
I've got some SQL that works as far as returning a recordset from a series of UNION statements. viz: SELECT whatever UNION this UNION that UNION other
3
2991
by: Dan | last post by:
Hello, Am trying to construct a query that'll both compute elapsed time based on certain criteria and sum this up for a subset of records. Sample Data that I'm working with: Row Ev_Time ...
4
2642
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I...
6
11477
by: Nicolae Fieraru | last post by:
Hi All, I have a query, Select Count(BoolField) from tblMyTable, Where BoolField = true. If I run the query by itself, it returns the number of true records I want to use the result of that...
2
1769
by: Justin Koivisto | last post by:
In PHP, I do the following (pseudo code for clarity)... Query: SELECT distinct(web_contacts.zip) FROM web_contacts WHERE web_contacts.zip <> '' Query: SELECT DISTINCT(contacts.zip) FROM contacts...
6
2546
by: geronimo_me | last post by:
I have 20 queries that compare fields in one table with fields in another table - the query results are the records that do not match in Table1 and Table2. ie Table1 DOB 28/02/78 Table2 DOB...
5
5005
by: vonclausowitz | last post by:
Repost from an VB group. Hi All, I'm looking for a way to search for multiple words in a database. There is however one but. The words have to be within a certain range of each other. For...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4583
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.