Solved

Return results ordered by count sort results from a select SQL Query

Posted on 2008-10-29
4
176 Views
Last Modified: 2010-04-21
I have a webapp..

A User clicks on a US State on a map, the State represents a two digit abbreviation of the state they clicked. In the database there are 1000's of locations each one has a two digit state abriviation that indicates what state they are from.  

It is easy to return a list of locations by state
(Select * from Database where State = "Clicked State" Order By Whatever)

What I need is more complicated than that...each location entry has 10 columns  (Product1 to Product10) and if that location has the product in stock, it has a "Y" in that field.

I need to return a list ordered by which location has the most "Y".  So the users would be shown who has the most products in stock.

Database: Stores
Database Table: Locations
Database fields:
Company, Address, State, Zip, Product1, Product2, Product3,  Product4, Product5, Product6, Product7, Product8, Product9, Product10, Product10

How can I do this via a select SQL Statement?
0
Comment
Question by:EGormly
  • 2
4 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 22832645
select * from locations
order by
(case when product1='Y' then 1 else 0 end +
case when product2='Y' then 1 else 0 end +
case when product3='Y' then 1 else 0 end +
case when product4='Y' then 1 else 0 end +
case when product5='Y' then 1 else 0 end +
case when product6='Y' then 1 else 0 end +
case when product7='Y' then 1 else 0 end +
case when product8='Y' then 1 else 0 end +
case when product9='Y' then 1 else 0 end +
case when product10 = 'Y' then 1 else 0 end
)

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 22832653
the above sorts from least 'Y' count to most
or, to do sort from most Y to least....

select * from locations
order by
(case when product1='Y' then 1 else 0 end +
case when product2='Y' then 1 else 0 end +
case when product3='Y' then 1 else 0 end +
case when product4='Y' then 1 else 0 end +
case when product5='Y' then 1 else 0 end +
case when product6='Y' then 1 else 0 end +
case when product7='Y' then 1 else 0 end +
case when product8='Y' then 1 else 0 end +
case when product9='Y' then 1 else 0 end +
case when product10 = 'Y' then 1 else 0 end
) desc
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22832674
Here you go:


select * from 
(select *,
case when Product1='Y' then 1 else 0 end 
+ case when Product2='Y' then 1 else 0 end 
+ case when Product3='Y' then 1 else 0 end 
+ case when Product4='Y' then 1 else 0 end 
+ case when Product5='Y' then 1 else 0 end 
+ case when Product6='Y' then 1 else 0 end 
+ case when Product7='Y' then 1 else 0 end 
+ case when Product8='Y' then 1 else 0 end 
+ case when Product9='Y' then 1 else 0 end 
+ case when Product10='Y' then 1 else 0 end as InStockCount
from Locations
where State = 'PA'
) a
order by InStockCount DESC

Open in new window

0
 

Author Closing Comment

by:EGormly
ID: 31511247
this didn't work for me no matter how I sliced it up.. but I get what it means so that is helpful
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Getting same value for every field in SQL 2 45
Addition to SQL for dynamic fields 6 38
Query Syntax 17 36
Need help constructing a conditional update query 16 46
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question