SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Proplem Put Filter ("And" or "Or") in Query


Proplem Put Filter ("And" or "Or") in Query

Author
Message
Aryafar65
Aryafar65
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 17
Hi Test This Query
Select * From
(
SELECT FirstName
, LastName
, CompanyID
, NCode
, FatherName
, nPercent
, NationalCode
, (select COUNT (*) From Tools_Information K Where K.NationalCode = R.NationalCode And K.NameTools = N'Cane')COUNT_cane
, (select COUNT (*) From Tools_Information K Where K.NationalCode = R.NationalCode And K.NameTools = N'Walker')COUNT_walker
FROM View_Report R
GROUP BY FirstName
, LastName
, CompanyID
, NCode
, FatherName
, nPercent
, NationalCode
)V
Where (Isnull(V.COUNT_cane , 0) > 0 And Isnull(V.COUNT_walker,0) > 0)

HanShi
HanShi
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8438 Visits: 3718
Instead of selecting the specific counts by a sub-select you could also accomplish this by using the CASE statement. This will give better performance because it will be calculated within the same select on the table and not by executing another select on the table.
SELECT
NationalCode, CompanyID, nPercent, NCode, FirstName, LastName, FatherName
, sum(case when NameTools = N'walker' then 1 else 0 end) as Count_walker
, sum(case when NameTools = N'Cane' then 1 else 0 end) as Count_Cane
FROM View_Report
WHERE (NameTools = N'walker') OR
(NameTools = N'Cane')
GROUP BY NationalCode, CompanyID, nPercent, NCode, FirstName, LastName, FatherName
HAVING (COUNT(NameTools) > 1)


Also notice the change in the HAVING clause to accomidate the posibility of having multiple Canes and/or Walkers.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
babak3334000
babak3334000
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 62
excuseme
Contact information table was forgotten
Plz Download New Database Attachment


SELECT FirstName, LastName, CompanyID, NCode, FatherName, nPercent, NationalCode
FROM View_Report
WHERE (NameTools = N'Walker') OR
(NameTools = N'Cane')
GROUP BY FirstName, LastName, CompanyID, NCode, FatherName, nPercent, NationalCode
HAVING (COUNT(NameTools) = 2)



I want someone who uses both(Walker and Cane ) to exhibit = Mike
But it does not display anything.
Attachments
BackupDatabaseSampleSql2008.rar (8 views, 111.00 KB)
HanShi
HanShi
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8438 Visits: 3718
You need to change the HAVING clause to get your desired results. With the code below you can check if both 'Walker' and 'Cane' exists at least once.
HAVING sum(case when NameTools = N'walker' then 1 else 0 end) > 0
AND sum(case when NameTools = N'Cane' then 1 else 0 end) > 0



** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
babak3334000
babak3334000
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 62
HanShi (9/5/2013)
You need to change the HAVING clause to get your desired results. With the code below you can check if both 'Walker' and 'Cane' exists at least once.
HAVING sum(case when NameTools = N'walker' then 1 else 0 end) > 0
AND sum(case when NameTools = N'Cane' then 1 else 0 end) > 0



Thank you so much masterw00t
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search