Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 MS SQL store multiple result in variables

views
     
TSXaxas
post Jul 9 2014, 01:28 AM, updated 12y ago

Getting Started
**
Junior Member
207 posts

Joined: Oct 2010
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
post Jul 9 2014, 02:32 AM

Look at all my stars!!
*******
Senior Member
4,547 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


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
post Jul 9 2014, 02:48 AM

Getting Started
**
Junior Member
207 posts

Joined: Oct 2010
Once I've gotten the country name, I'm planning to filter the location based on the most selected.
ketnave
post Jul 9 2014, 03:08 AM

Enthusiast
*****
Senior Member
868 posts

Joined: Sep 2009
From: douchistan, pekopon


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
post Jul 9 2014, 03:19 AM

Getting Started
**
Junior Member
207 posts

Joined: Oct 2010
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
post Jul 9 2014, 03:37 AM

Enthusiast
*****
Senior Member
868 posts

Joined: Sep 2009
From: douchistan, pekopon


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
post Jul 9 2014, 04:20 AM

Getting Started
**
Junior Member
207 posts

Joined: Oct 2010
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
post Jul 9 2014, 04:30 AM

Enthusiast
*****
Senior Member
868 posts

Joined: Sep 2009
From: douchistan, pekopon


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
post Jul 9 2014, 04:45 AM

Getting Started
**
Junior Member
207 posts

Joined: Oct 2010
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
post Jul 9 2014, 05:00 AM

Enthusiast
*****
Senior Member
868 posts

Joined: Sep 2009
From: douchistan, pekopon


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
post Jul 9 2014, 05:20 AM

Getting Started
**
Junior Member
207 posts

Joined: Oct 2010
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
post Jul 9 2014, 05:46 AM

Enthusiast
*****
Senior Member
868 posts

Joined: Sep 2009
From: douchistan, pekopon


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 rclxub.gif
TSXaxas
post Jul 9 2014, 06:29 AM

Getting Started
**
Junior Member
207 posts

Joined: Oct 2010
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  rclxub.gif
*
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
post Jul 9 2014, 07:02 AM

Enthusiast
*****
Senior Member
868 posts

Joined: Sep 2009
From: douchistan, pekopon


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
post Jul 9 2014, 12:42 PM

Getting Started
**
Junior Member
207 posts

Joined: Oct 2010
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 sweat.gif
ketnave
post Jul 9 2014, 10:30 PM

Enthusiast
*****
Senior Member
868 posts

Joined: Sep 2009
From: douchistan, pekopon


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 sweat.gif
*
wookiee ... all the best !
Lord Tiki Mick
post Jul 10 2014, 01:38 AM

Regular
******
Senior Member
1,020 posts

Joined: Jul 2012
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

 

Change to:
| Lo-Fi Version
0.0196sec    0.90    5 queries    GZIP Disabled
Time is now: 18th December 2025 - 12:14 AM