Showing posts with label contains. Show all posts
Showing posts with label contains. Show all posts

Monday, March 26, 2012

question please

please i hav a question..can uoy answer me?

i have a workers table which contains 4 records such as (id,name,category, status) ...

category like : a,b,c

for example :

id - name - category - status
1 - Jone - A - free
2 - Tom - B - busy
3 - Adm - c - free
4 - Raul - B - free
5 - Sami -A - busy

i want to write query to obtain 3 workers that their status = free and belong to each category (a,b and c )

such as here :
1 - Jone - A - free
3 - Adm - c - free
4 - Raul - B - free

can you help me ?Hope this work for you.
Good luck!

SELECT id,
name,
category
FROM WORKERS
WHERE status = 'free'

|||

Use this query...

select Workers.* From Workers Join
(
Select Category,Max(main.id) id From Workers main Where 3 =
(Select Count(*) From Workers Sub Where Main.id <= Sub.ID And Main.Category = Sub.Category And Main.Status=Sub.Status) And Status='Free'
Group BY
Category
Union All
Select Category,Max(main.id) From Workers main Where 2 =
(Select Count(*) From Workers Sub Where Main.id <= Sub.ID And Main.Category = Sub.Category And Main.Status=Sub.Status) And Status='Free'
Group BY
Category
Union All
Select Category,Max(main.id) From Workers main Where 1 =
(Select Count(*) From Workers Sub Where Main.id <= Sub.ID And Main.Category = Sub.Category And Main.Status=Sub.Status) And Status='Free'
Group BY
Category
) as Data On Data.id = Workers.Id
Order By 3

It will exactly obtain 3 workers that their status = free and belong to each category

|||

-- --
-- Kanjo:
--
-- The things that I understand from your request are:
-- 1. You do want to filter out any status other
-- than the 'free' status.
-- 2. You want to select the worker information
-- for 'free' workers -- especially the category
-- of the worker.
--
-- The things that I am not sure of are:
-- 1. Do you want to filter out any worker that is
-- not in catories 'A', 'B' or 'C'?
-- 2. Do you want all free users in any of the
-- categories or
-- 3. Do you only want the 'Top' free worker of
-- each of the categories?
-- 4. Are you running SQL Server 2000 or 2005?
-- (In this case it might not matter.)
--
-- Each of these isues has an impact on the way that
-- the query needs to be written.
-- --
set nocount on
declare @.workers table
( id integer not null,
name varchar (20) not null,
category char (1) not null,
status varchar (10) not null
)

insert into @.workers values (1, 'Jone', 'A', 'free')
insert into @.workers values (2, 'Tom' , 'B', 'busy')
insert into @.workers values (3, 'Adm', 'C', 'free')
insert into @.workers values (4, 'Raul', 'B', 'free')
insert into @.workers values (5, 'Sami', 'A', 'busy')
insert into @.workers values (6, 'Roni', 'A', 'free')
insert into @.workers values (7, 'Suzy', 'D', 'free')

--select * from @.workers

-- --
-- If there is no need to filter based on category
-- Vincent's query will work just fine:
-- --
select id,
name,
category
from @.workers
where status = 'free'
-- and category in ('A','B','C')

-- Sample Output:

-- id name category
-- -- -- --
-- 1 Jone A
-- 3 Adm C
-- 4 Raul B
-- 6 Roni A
-- 7 Suzy D

-- --
-- However, if you do need to filter based on the
-- category then the where clause will need to be
-- modified to reflect that need
-- --
select id,
name,
category,
status
from @.workers
where status = 'free'
and category in ('A','B','C')

-- - Sample Output --

-- id name category status
-- -- -- -- -
-- 1 Jone A free
-- 3 Adm C free
-- 4 Raul B free
-- 6 Roni A free


-- --
-- There are other ways of obtaing the "top" list if
-- that is the requirement; this is just one of the
-- ways.
-- --
select a.id,
a.name,
a.category,
a.status
from @.workers a
inner join
( select category,
min (id) as min_id
from @.workers
where category in ('A','B','C')
group by category
) b
on a.id = b.min_id


-- -- Sample Output:

-- id name category status
-- -- -- -- -
-- 1 Jone A free
-- 2 Tom B busy
-- 3 Adm C free

-- --
-- Mani:
--
-- HELP! I am not getting the expected results and
-- I am still trying to figure out why. Can you spot
-- where I went wrong?
-- --
select workers.* From @.workers workers Join
(
Select Category,Max(main.id) id From @.workers main Where 3 =
(Select Count(*) From @.workers Sub Where Main.id < Sub.ID And Main.Category = Sub.Category And Main.Status=Sub.Status) And Status='Free'
Group BY
Category
Union All
Select Category,Max(main.id) From @.workers main Where 2 =
(Select Count(*) From @.workers Sub Where Main.id < Sub.ID And Main.Category = Sub.Category And Main.Status=Sub.Status) And Status='Free'
Group BY
Category
Union All
Select Category,Max(main.id) From @.workers main Where 1 =
(Select Count(*) From @.workers Sub Where Main.id < Sub.ID And Main.Category = Sub.Category And Main.Status=Sub.Status) And Status='Free'
Group BY
Category
) as Data On Data.id = workers.Id
Order By 3


-- Unexpected Output: -

-- id name category status
-- -- -- -- -
-- 1 Jone A free

|||

Fixed... ThanQ!

select workers.* From @.workers workers Join
(

Select Category,Max(main.id) id From @.workers main Where 3 =
(Select Count(*) From @.workers Sub Where Main.id <= Sub.ID And Main.Category = Sub.Category And Main.Status=Sub.Status) And main.Status='Free'
Group BY
Category
Union All
Select Category,Max(main.id) From @.workers main Where 2 =
(Select Count(*) From @.workers Sub Where Main.id <= Sub.ID And Main.Category = Sub.Category And Main.Status=Sub.Status) And main.Status='Free'
Group BY
Category
Union All
Select Category,Max(main.id) From @.workers main Where 1 =
(Select Count(*) From @.workers Sub Where Main.id <= Sub.ID And Main.Category = Sub.Category And Main.Status=Sub.Status) And main.Status='Free'
Group BY
Category
) as Data On Data.id = workers.Id
Order By 3

Tuesday, March 20, 2012

Question on SELECT * TOP 100 FROM TheTable

I have a question about how the TOP n clause works on the SELECT
statement.
Assume I have a table called TheTable that contains 10,000,000 rows
and no indexes.
Assume I execute the following SQL...
SELECT * TOP 100 FROM TheTable WHERE TheField = "12345"
Will SQL Server first do a TableScan to find ALL the rows that meet
the WHERE clause and THEN give me the TOP 100, or is it clever enough
to stop the TableScan when 100 matching rows have been found?
Thanks for any help.
RichardF> Assume I have a table called TheTable that contains 10,000,000 rows
> and no indexes.
Why would you ever have such a table?

> Will SQL Server first do a TableScan to find ALL the rows that meet
> the WHERE clause and THEN give me the TOP 100, or is it clever enough
> to stop the TableScan when 100 matching rows have been found?
Run your query, turn on execution plan, and check it out...|||Thanks for your 'help'.
It is a hypothetical table.
My question is about how SQL Server acts on the TOP n clause when one
of the fields involved in the WHERE clause is not indexed.
RichardF
On Thu, 10 Mar 2005 12:53:14 -0500, "Aaron [SQL Server MVP]"
<ten.xoc@.dnartreb.noraa> wrote:

>Why would you ever have such a table?
>
>Run your query, turn on execution plan, and check it out...
>|||> Thanks for your 'help'.
I'm trying to teach you how to analyze these things yourself. Teach someone
to fish, rather than just handing them a fish, that kind of thing. Sorry if
that help isn't good enough for you.
If it is, then create a smaller table, select a smaller percentage, and the
direction the engine takes (which you can view in Query Analyzer if you turn
execution plan on) should be roughly equivalent.
Without more information, I think it is impossible for anyone to give you a
black and white answer. There are dozens of variables aside from index
existence that contribute to whether a scan is used and when the engine
knows to stop searching. So you are probably better off testing against the
kind of data you are working with anyway.|||If the query is as simple as this, then SQL-Server will start a table
scan. During the table scan the matching rows will be returned. If a
100th row is found, the table scan will stop.
You can test this by creating this hypothetical table, and filling it
with an ascending value for "TheField". Then you can measure the
performance difference between
SELECT TOP 1 * FROM TheTable WHERE TheField=1 -- Should be fast
and
SELECT TOP 1 * FROM TheTable WHERE TheField=10000000 -- Could be slow
Hope this helps,
Gert-Jan
RichardF wrote:
> I have a question about how the TOP n clause works on the SELECT
> statement.
> Assume I have a table called TheTable that contains 10,000,000 rows
> and no indexes.
> Assume I execute the following SQL...
> SELECT * TOP 100 FROM TheTable WHERE TheField = "12345"
> Will SQL Server first do a TableScan to find ALL the rows that meet
> the WHERE clause and THEN give me the TOP 100, or is it clever enough
> to stop the TableScan when 100 matching rows have been found?
> Thanks for any help.
> RichardF|||Like Gert-Jan and Aaron said: You have to experiment. From what I could
tell, SQL Server stops searching the table when it finds enough rows to
satisfy the TOP X requirement. If you leave off of TOP requirement (or
there aren't enough rows to fulfill the TOP X) then the entire table will be
scanned.
Of course if this was a production table that was being queried quite often,
you could want an index. :)
-- Create temp table
-- =================
CREATE TABLE #test (col int NOT NULL)
GO
-- This will take awhile... (5 million rows in my test database)
-- ========================
INSERT INTO #test
SELECT CHECKSUM(NEWID()) FROM sysobjects S1,sysobjects S2,sysobjects
S3,sysobjects S4,sysobjects S5
GO
-- Start experimenting. Turn on "Show Execution Plan' and "Show Server
Trace"
-- ========================================
==============================
SELECT TOP 0 col FROM #test WHERE col = '779668530'
SELECT TOP 1 col FROM #test WHERE col = '779668530'
SELECT TOP 2 col FROM #test WHERE col = '779668530'
SELECT col FROM #test WHERE col = '779668530'
-- Find out which values of "col" are repeated
-- For use in the previous "experimenting" calls
-- ========================================
=====
SELECT *, count(*) FROM #test
group by col having count(*) > 1
"RichardF" <noone@.nowhere.com> wrote in message
news:sm1131tr8v8muj14huhtrtvnjevdkkvirf@.
4ax.com...
>I have a question about how the TOP n clause works on the SELECT
> statement.
> Assume I have a table called TheTable that contains 10,000,000 rows
> and no indexes.
> Assume I execute the following SQL...
> SELECT * TOP 100 FROM TheTable WHERE TheField = "12345"
> Will SQL Server first do a TableScan to find ALL the rows that meet
> the WHERE clause and THEN give me the TOP 100, or is it clever enough
> to stop the TableScan when 100 matching rows have been found?
> Thanks for any help.
> RichardF

Friday, March 9, 2012

question on join

I have a database with a many to many relation ...

table REFERENCE contains data about library items
table TOPIC contains the topics covered by these items
table REFERENCE_TOPIC contains the relation between the items in both tables.

suppose i want all the fields from the table REFERENCE but only where the topic is a certain value (i'm doing an ASP page, so it the querystring passed by the page)

would this select statement do the job?

"SELECT * FROM REFERENCE WHERE ([REFERENCE_TOPIC].ref_id=[REFERENCE].ref_id AND [REFERENCE_TOPIC].topic_id=" & Request.QueryString("topic_id")

i'm gonaa try ... hope to get some feed backThere are many methods to accomplish this.

1)
Select R.*
From REFERENCE R
INNER JOIN
REFERENCE_TOPIC RT
ON R.ID = RT.R_ID
INNER JOIN
TOPIC T ON
T.ID = RT.T_ID AND T.value = 'x'

2)
Select R.*
From REFERENCE R
where R.ID IN
(Select RT.R_ID
From REFERENCE_TOPIC RT
Where RT.T_ID IN
(Select T.ID
From TOPIC T
Where T.value ='X'))

Saturday, February 25, 2012

Question on cube write back

Hi, all experts here,

I encountered a problem with cube writing back which contains measures with count aggregation rather than sum which said write back can not be allowed on measure groups with other aggregations rather than sum? Is it saying we can only enable write back on cubes with measure groups all based on sum aggregations?

But in my case, the measures needed to be counted rather than sum. What can I try to enable write back on this cube?

I am looking forward to hearing from you for your advices and thank you very much in advance.

With best regards,

Yours sincerely,

I'm having trouble thinking of a situation where you would need to writeback to a count measure, but that is beside the point.

One work around might be to create a new column in your fact table for this measure and pre-populate it with a value of 1 during ETL. Then you would be able to use a sum aggregation and get the same value as you would with a count measure. It would then also be possible to write back to this measure.

|||

Hi,

In my data, the facts are bit data, which only with values of 1 and 0 meaning different things respectively. Therefore I need to count these different situations happened in the fact table across different levels. There are no numeric data types at the moment which are for sum.

Thanks for your advices and I am looking forward to hearing from any of you here more ideas in this situation. Maybe you would give me some better ideas on how to deal with it.

With best regards,

Yours sincerely,

|||

Helen,

Explain me better what is the goal, I didnt understood... Explain me as I a dummy!

:-)

Regards!

|||

HI, PedroCGD,

The facts in the facts tables are with 0 or 1 values only. (0 and 1 therefore represent different meanings), therefore I dont see any point to sum these facts in any cube. Instead, count aggregation makes more sense which can count the numbers for the fact being 0 or 1.

Hope my explanation is clear for your help.

With best regards,

Yours sincerely,

|||It sounds like you have some flags which should modelled as separate attributes, not as measures. You would have one count measure and set either a single dimension or a combined dimension (also known as a 'junk' dimension). In this way you should be able to get the count by any combination of attributes.|||

Hi, Darren,

Thanks for your kind advices.

As you kindly advised, I will then need to update the underlying relational database (as the underlying data warehouse is designed by others)?Since at the moment, the data residing in the data warehouse does not have any junk dimensions at all. All those flags attributes are residing in the fact tables.

I would need to create single dimension for each flag attribute or combine some of the flag attributes together into one single dimension? But only one count measure for all these dimension attributes?

Will it be any good to complete these in data source view of the cube? Thanks again. And hope you can give me more further advices on it.

With best regards,

Yours sincerely,