Outline ·
[ Standard ] ·
Linear+
MS SQL store multiple result in variables
|
TSXaxas
|
Jul 9 2014, 01:28 AM, updated 12y ago
|
Getting Started

|
Hi, I'm having trouble with getting the result I want from my query. I have written a query like so. CODE select country from Itinerary where schedule between '2014-08-01' and '2014-08-20' group by country having sum(participant) > 20 The query will be using the sample data from this table. » Click to show Spoiler - click again to hide... « When executed, it returns 2 rows of result : row 1 = Malaysia, row 2 = South Korea. How do i set them in a separate variable like a = Malaysia and b = South Korea as I still want to filter the result more.? This post has been edited by Xaxas: Jul 9 2014, 01:38 AM
|
|
|
|
|
|
narf03
|
Jul 9 2014, 02:32 AM
|
|
why dont u just tell us what do u expect after "filter the result more". I believe u chosen the wrong method.
This post has been edited by narf03: Jul 9 2014, 02:32 AM
|
|
|
|
|
|
TSXaxas
|
Jul 9 2014, 02:48 AM
|
Getting Started

|
Once I've gotten the country name, I'm planning to filter the location based on the most selected.
|
|
|
|
|
|
ketnave
|
Jul 9 2014, 03:08 AM
|
|
QUOTE(Xaxas @ Jul 9 2014, 02:48 AM) Once I've gotten the country name, I'm planning to filter the location based on the most selected. How bout this ? CODE select country,location,attraction from Itinerary where schedule between '2014-08-01' and '2014-08-20' group by country,location,attraction having sum(participant) > 20
|
|
|
|
|
|
TSXaxas
|
Jul 9 2014, 03:19 AM
|
Getting Started

|
QUOTE(ketnave @ Jul 9 2014, 04:08 AM) How bout this ? CODE select country,location,attraction from Itinerary where schedule between '2014-08-01' and '2014-08-20' group by country,location,attraction having sum(participant) > 20
Hmmm it doesn't work as location and attraction has different values in it. I'll probably have to make my scope smaller by letting the user selects only one location.
|
|
|
|
|
|
ketnave
|
Jul 9 2014, 03:37 AM
|
|
QUOTE(Xaxas @ Jul 9 2014, 03:19 AM) Hmmm it doesn't work as location and attraction has different values in it. I'll probably have to make my scope smaller by letting the user selects only one location. can try the following query ? not the most efficient one. what's the dataset that you have ? CODE SET ROWCOUNT 1
select country,location,attraction from Itinerary where schedule between '2014-08-01' and '2014-08-20' where country = <the country you wanna select> group by country,location,attraction having sum(participant) > 20 order by sum(participant) desc
|
|
|
|
|
|
TSXaxas
|
Jul 9 2014, 04:20 AM
|
Getting Started

|
QUOTE(ketnave @ Jul 9 2014, 04:37 AM) can try the following query ? not the most efficient one. what's the dataset that you have ? CODE SET ROWCOUNT 1
select country,location,attraction from Itinerary where schedule between '2014-08-01' and '2014-08-20' where country = <the country you wanna select> group by country,location,attraction having sum(participant) > 20 order by sum(participant) desc
The data from the table in the first post. Was planning to create a procedure for it to run in asp.net when a page load or at a given time without having to select a country. This post has been edited by Xaxas: Jul 9 2014, 04:20 AM
|
|
|
|
|
|
ketnave
|
Jul 9 2014, 04:30 AM
|
|
QUOTE(Xaxas @ Jul 9 2014, 04:20 AM) The data from the table in the first post. Was planning to create a procedure for it to run in asp.net when a page load or at a given time without having to select a country. what is your intended output ? can you draft a sample output that you expect ?
|
|
|
|
|
|
TSXaxas
|
Jul 9 2014, 04:45 AM
|
Getting Started

|
QUOTE(ketnave @ Jul 9 2014, 05:30 AM) what is your intended output ? can you draft a sample output that you expect ? I would like the result to show up as Location Perak after checking for the country, schedule and participant. In this case, it should be itineraryID 1/3/4 with location showing only Perak. Basically it suppose to produce an output of a similar result where all three id have it, which is Perak. This post has been edited by Xaxas: Jul 9 2014, 04:47 AM
|
|
|
|
|
|
ketnave
|
Jul 9 2014, 05:00 AM
|
|
QUOTE(Xaxas @ Jul 9 2014, 04:45 AM) I would like the result to show up as Location Perak after checking for the country, schedule and participant. In this case, it should be itineraryID 1/3/4 with location showing only Perak. Basically it suppose to produce an output of a similar result where all three id have it, which is Perak. Depending on your dataset, you will have to use wildcard to get the location. Just need to be wary of sql injection though. Try this out: CODE DECLARE @country VARCHAR(50) DECLARE @location VARCHAR(50)
SET @country = 'Malaysia' SET @location = '%Perak%'
SELECT itineraryID,,country,location,schedule,participant FROM Itinerary WHERE Country = @Country AND Location LIKE @Location
|
|
|
|
|
|
TSXaxas
|
Jul 9 2014, 05:20 AM
|
Getting Started

|
QUOTE(ketnave @ Jul 9 2014, 06:00 AM) Depending on your dataset, you will have to use wildcard to get the location. Just need to be wary of sql injection though. Try this out: CODE DECLARE @country VARCHAR(50) DECLARE @location VARCHAR(50)
SET @country = 'Malaysia' SET @location = '%Perak%'
SELECT itineraryID,,country,location,schedule,participant FROM Itinerary WHERE Country = @Country AND Location LIKE @Location
Is it possible to like use a while loop to check if all the result shown have the same location without having to specify it?
|
|
|
|
|
|
ketnave
|
Jul 9 2014, 05:46 AM
|
|
QUOTE(Xaxas @ Jul 9 2014, 05:20 AM) Is it possible to like use a while loop to check if all the result shown have the same location without having to specify it? Don't get what you are trying to do here ? Perhaps you can do it from the application end. The only loop I know in SQL is thru the use of CURSOR, but again, I am not sure what you are actually trying to achieve here
|
|
|
|
|
|
TSXaxas
|
Jul 9 2014, 06:29 AM
|
Getting Started

|
QUOTE(ketnave @ Jul 9 2014, 06:46 AM) Don't get what you are trying to do here ? Perhaps you can do it from the application end. The only loop I know in SQL is thru the use of CURSOR, but again, I am not sure what you are actually trying to achieve here  Well it's okay, thanks for the help though. I'll probably change the data to all have only one single location. I was actually trying to generate from the database the most chosen location based on the country selected, the schedule time and the no. participant.
|
|
|
|
|
|
ketnave
|
Jul 9 2014, 07:02 AM
|
|
QUOTE(Xaxas @ Jul 9 2014, 06:29 AM) Well it's okay, thanks for the help though. I'll probably change the data to all have only one single location. I was actually trying to generate from the database the most chosen location based on the country selected, the schedule time and the no. participant. That can be done as long as you are clear on what you wanted to achieve. I try to do most of it in SQL if possible. Anyway, the choice is yours.
|
|
|
|
|
|
TSXaxas
|
Jul 9 2014, 12:42 PM
|
Getting Started

|
QUOTE(ketnave @ Jul 9 2014, 08:02 AM) That can be done as long as you are clear on what you wanted to achieve. I try to do most of it in SQL if possible. Anyway, the choice is yours. Haha okay, but partly because of time constraint, I'll have to make it a bit simpler but still show what it is suppose to do. Although it isn't a complete product of what I want to do
|
|
|
|
|
|
ketnave
|
Jul 9 2014, 10:30 PM
|
|
QUOTE(Xaxas @ Jul 9 2014, 12:42 PM) Haha okay, but partly because of time constraint, I'll have to make it a bit simpler but still show what it is suppose to do. Although it isn't a complete product of what I want to do  wookiee ... all the best !
|
|
|
|
|
|
Lord Tiki Mick
|
Jul 10 2014, 01:38 AM
|
|
CODE select * into #tmpTable from Itinerary where schedule between '2014-08-01' and '2014-08-20' group by country having sum(participant) > 20
select * from #tmpTable where <conditions>
This post has been edited by Lord Tiki Mick: Jul 10 2014, 01:39 AM
|
|
|
|
|