Showing posts with label write. Show all posts
Showing posts with label write. Show all posts

Friday, March 30, 2012

Query Help

Help, please!
I have 3 columns with numeric values. i want to write a query that returns
the following:
If all 3 columns are populated, then the middle value (not the average);
If one colunn has a null, then the lesser value,
If 2 are null, then the remaining value.
For example: if ColumnA = 5 and ColumnB = 10 and ColumnC = 12, then I want
the result to be "10". If ColumnB were null, then the result would be "5".
Making sense?
Is there a way to do this, without having to create a bazillion Case When
statements? (case when columnA > ColumnB and ColumnB < ColumnC then ColumnB
else case when columA < ColumnB and ColumnB > ColumnC then ColumnB else . .
... and so on).
Just wondering.
Thank you!
Kyra
Financial Systems Analyst
CCNA, MCSE, MCSA, MCDBA
Kyra,
I think the bazillion CASEs is the only way to go. Here's one attempt:
CREATE TABLE T1 (T1ID INT NOT NULL IDENTITY, Col1 int, Col2 int, Col3 int)
GO
INSERT T1 VALUES (NULL, NUll, 1)
INSERT T1 VALUES (NULL, 2, NULL)
INSERT T1 VALUES (3, NUll, NULL)
INSERT T1 VALUES (NULL, 4, 5)
INSERT T1 VALUES (6, NULL, 7)
INSERT T1 VALUES (8,9, NULL)
INSERT T1 VALUES (10,11,12)
GO
SELECT
CASE
WHEN Col1 IS NULL AND Col2 IS NULL THEN Col3
WHEN Col2 IS NULL AND Col2 IS NULL THEN Col1
WHEN Col1 IS NULL AND Col3 IS NULL THEN Col2
WHEN Col1 IS NULL AND Col2 IS NOT NULL AND Col3 IS NOT NULL THEN
CASE WHEN Col2 < Col3 THEN Col2 Else Col3 END
WHEN Col2 IS NULL AND Col1 IS NOT NULL AND Col3 IS NOT NULL THEN
CASE WHEN Col1 < Col3 THEN Col1 Else Col3 END
WHEN Col3 IS NULL AND Col1 IS NOT NULL AND Col2 IS NOT NULL THEN
CASE WHEN Col1 < Col2 THEN Col1 Else Col2 END
WHEN Col1 <= Col2 AND Col2 <= Col3 THEN Col2
WHEN Col2 <= Col3 AND Col3 <= Col1 THEN Col3
WHEN Col3 <= Col1 AND Col1 <= Col2 THEN Col1
END
FROM T1
One thing your conditions left out: what if they're all NULL?
Hope this helps,
Ron
Ron Talmage
SQL Server MVP
"Ysandre" <Ysandre@.discussions.microsoft.com> wrote in message
news:3147EC21-B5A3-4725-B156-6A40834E5561@.microsoft.com...
> Help, please!
> I have 3 columns with numeric values. i want to write a query that returns
> the following:
> If all 3 columns are populated, then the middle value (not the average);
> If one colunn has a null, then the lesser value,
> If 2 are null, then the remaining value.
> For example: if ColumnA = 5 and ColumnB = 10 and ColumnC = 12, then I want
> the result to be "10". If ColumnB were null, then the result would be "5".
> Making sense?
> Is there a way to do this, without having to create a bazillion Case When
> statements? (case when columnA > ColumnB and ColumnB < ColumnC then
> ColumnB
> else case when columA < ColumnB and ColumnB > ColumnC then ColumnB else .
> .
> .. and so on).
> Just wondering.
> Thank you!
> Kyra
> --
> Financial Systems Analyst
> CCNA, MCSE, MCSA, MCDBA
|||Ysandre,
Here is an alternative to Ron's solution. In general, you
would have an easier time if all the values were in one
column..
SELECT
T1ID,
CASE cntC
WHEN 1 THEN maxC
WHEN 2 THEN minC
WHEN 3 THEN sumC - maxC - minC
END AS C
FROM (
SELECT
T1ID,
SUM(C) as sumC,
MIN(C) as minC,
MAX(C) as maxC,
COUNT(C) as cntC
FROM (
SELECT T1ID, Col1 AS C FROM T1
UNION ALL
SELECT T1ID, Col2 FROM T1
UNION ALL
SELECT T1ID, Col3 FROM T1
) T
GROUP BY T1ID
) T
or
SELECT T1ID, MIN(C) FROM (
SELECT T1ID, Col1 AS C FROM T1
UNION ALL
SELECT T1ID, Col2 FROM T1
UNION ALL
SELECT T1ID, Col3 FROM T1
) T1
WHERE C IN (
SELECT TOP 2 C FROM (
SELECT T1ID, Col1 AS C FROM T1
UNION ALL
SELECT T1ID, Col2 FROM T1
UNION ALL
SELECT T1ID, Col3 FROM T1
) AS T1x
WHERE T1x.T1ID = T1.T1ID
AND C IS NOT NULL
ORDER BY C DESC
)
GROUP BY T1ID
If your table were in the form I used for the derived table above (one
value column instead of three), you could write
SELECT T1ID, MIN(C) FROM T1
WHERE C IN (
SELECT TOP 2 C FROM T1 AS T1x
WHERE T1x.T1ID = T1.T1ID
AND C IS NOT NULL
ORDER BY C DESC
)
GROUP BY T1ID
Steve Kass
Drew University
Ysandre wrote:

>Help, please!
>I have 3 columns with numeric values. i want to write a query that returns
>the following:
>If all 3 columns are populated, then the middle value (not the average);
>If one colunn has a null, then the lesser value,
>If 2 are null, then the remaining value.
>For example: if ColumnA = 5 and ColumnB = 10 and ColumnC = 12, then I want
>the result to be "10". If ColumnB were null, then the result would be "5".
>Making sense?
>Is there a way to do this, without having to create a bazillion Case When
>statements? (case when columnA > ColumnB and ColumnB < ColumnC then ColumnB
>else case when columA < ColumnB and ColumnB > ColumnC then ColumnB else . .
>.. and so on).
>Just wondering.
>Thank you!
>Kyra
>
>
|||Thank you Ron and Steve, that was very helpful!!!!
Thanks,
ysandre
"Steve Kass" wrote:

> Ysandre,
> Here is an alternative to Ron's solution. In general, you
> would have an easier time if all the values were in one
> column..
> SELECT
> T1ID,
> CASE cntC
> WHEN 1 THEN maxC
> WHEN 2 THEN minC
> WHEN 3 THEN sumC - maxC - minC
> END AS C
> FROM (
> SELECT
> T1ID,
> SUM(C) as sumC,
> MIN(C) as minC,
> MAX(C) as maxC,
> COUNT(C) as cntC
> FROM (
> SELECT T1ID, Col1 AS C FROM T1
> UNION ALL
> SELECT T1ID, Col2 FROM T1
> UNION ALL
> SELECT T1ID, Col3 FROM T1
> ) T
> GROUP BY T1ID
> ) T
> or
> SELECT T1ID, MIN(C) FROM (
> SELECT T1ID, Col1 AS C FROM T1
> UNION ALL
> SELECT T1ID, Col2 FROM T1
> UNION ALL
> SELECT T1ID, Col3 FROM T1
> ) T1
> WHERE C IN (
> SELECT TOP 2 C FROM (
> SELECT T1ID, Col1 AS C FROM T1
> UNION ALL
> SELECT T1ID, Col2 FROM T1
> UNION ALL
> SELECT T1ID, Col3 FROM T1
> ) AS T1x
> WHERE T1x.T1ID = T1.T1ID
> AND C IS NOT NULL
> ORDER BY C DESC
> )
> GROUP BY T1ID
> If your table were in the form I used for the derived table above (one
> value column instead of three), you could write
> SELECT T1ID, MIN(C) FROM T1
> WHERE C IN (
> SELECT TOP 2 C FROM T1 AS T1x
> WHERE T1x.T1ID = T1.T1ID
> AND C IS NOT NULL
> ORDER BY C DESC
> )
> GROUP BY T1ID
> Steve Kass
> Drew University
>
> Ysandre wrote:
>

Query Help

Im trying to write a query that will return the next daily sub number and
Volume number for our submissions. the volume number is easy as it is
incremented each time, but the Daily sub number needs to be reset each day.
Each time a submission is made the numbers are incememted
ive come up with this so far but am struggling with the daily Sub numbers
select
Right('000000' + cast((VolumeNumber + 1) as varchar(6)), 6) As Volume,
Right('000' + cast((DailySerial + 1) as varchar(6)), 6) as DailySub
from dbo.BureauSubRecord
this result would be
Volume DailySub
-- --
000001 0001
Then the table would be updater to read
Volume DailySub
-- --
000002 0002 etc
However the first sub of the following day should read
Volume DailySub
-- --
000003 0001
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[BureauSubRecord]') and OBJECTPROPERTY(id, N'IsUserTable')
= 1)
drop table [dbo].[BureauSubRecord]
GO
CREATE TABLE [dbo].[BureauSubRecord] (
[BureauId] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[VolumeNumber] [varchar] (6) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[DailySerial] [varchar] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[LastSub] [datetime] NOT NULL
) ON [PRIMARY]
GO
SET NOCOUNT ON
INSERT INTO [BureauSubRecord]
([BureauId],[VolumeNumber],[DailySerial]
,[LastSub])VALUES('12345','000000','000'
,'Jan 1 2005 12:00:00:000AM')
SET NOCOUNT OFFHi
I am not sure about your DDL as it does not seem to match your description
The following is untested but you may want something like:
SELECT B.[BureauId], B.[VolumeNumber],
(SELECT COUNT(*) FROM dbo.BureauSubRecord S WHERE
S.[BureauId] = B.[BureauId]
AND S.[VolumeNumber] = B.[VolumeNumber]
AND S.[LastSub] < B.[LastSub]
AND CONVERT(CHAR(8), S.[LastSub], 112 ) = CONVERT(CHAR(8), B.[LastSub], 112
) ) + 1 AS [DailySerial],
B.[LastSub]
FROM dbo.BureauSubRecord B
John
"Peter Newman" wrote:

> Im trying to write a query that will return the next daily sub number and
> Volume number for our submissions. the volume number is easy as it is
> incremented each time, but the Daily sub number needs to be reset each da
y.
> Each time a submission is made the numbers are incememted
> ive come up with this so far but am struggling with the daily Sub numbers
> select
> Right('000000' + cast((VolumeNumber + 1) as varchar(6)), 6) As Volume,
> Right('000' + cast((DailySerial + 1) as varchar(6)), 6) as DailySub
> from dbo.BureauSubRecord
> this result would be
> Volume DailySub
> -- --
> 000001 0001
>
> Then the table would be updater to read
> Volume DailySub
> -- --
> 000002 0002 etc
> However the first sub of the following day should read
> Volume DailySub
> -- --
> 000003 0001
>
>
>
> if exists (select * from dbo.sysobjects where id =
> object_id(N'[dbo].[BureauSubRecord]') and OBJECTPROPERTY(id, N'IsUserTable')
> = 1)
> drop table [dbo].[BureauSubRecord]
> GO
> CREATE TABLE [dbo].[BureauSubRecord] (
> [BureauId] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [VolumeNumber] [varchar] (6) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [DailySerial] [varchar] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [LastSub] [datetime] NOT NULL
> ) ON [PRIMARY]
> GO
>
> SET NOCOUNT ON
> INSERT INTO [BureauSubRecord]
> ([BureauId],[VolumeNumber],[DailySerial]
,[LastSub])VALUES('12345','000000','000'
,'Jan 1 2005 12:00:00:000AM')
> SET NOCOUNT OFF
>

Wednesday, March 28, 2012

Query Help

Help, please!
I have 3 columns with numeric values. i want to write a query that returns
the following:
If all 3 columns are populated, then the middle value (not the average);
If one colunn has a null, then the lesser value,
If 2 are null, then the remaining value.
For example: if ColumnA = 5 and ColumnB = 10 and ColumnC = 12, then I want
the result to be "10". If ColumnB were null, then the result would be "5".
Making sense?
Is there a way to do this, without having to create a bazillion Case When
statements? (case when columnA > ColumnB and ColumnB < ColumnC then ColumnB
else case when columA < ColumnB and ColumnB > ColumnC then ColumnB else . .
.. and so on).
Just wondering.
Thank you!
Kyra
Financial Systems Analyst
CCNA, MCSE, MCSA, MCDBAKyra,
I think the bazillion CASEs is the only way to go. Here's one attempt:
CREATE TABLE T1 (T1ID INT NOT NULL IDENTITY, Col1 int, Col2 int, Col3 int)
GO
INSERT T1 VALUES (NULL, NUll, 1)
INSERT T1 VALUES (NULL, 2, NULL)
INSERT T1 VALUES (3, NUll, NULL)
INSERT T1 VALUES (NULL, 4, 5)
INSERT T1 VALUES (6, NULL, 7)
INSERT T1 VALUES (8,9, NULL)
INSERT T1 VALUES (10,11,12)
GO
SELECT
CASE
WHEN Col1 IS NULL AND Col2 IS NULL THEN Col3
WHEN Col2 IS NULL AND Col2 IS NULL THEN Col1
WHEN Col1 IS NULL AND Col3 IS NULL THEN Col2
WHEN Col1 IS NULL AND Col2 IS NOT NULL AND Col3 IS NOT NULL THEN
CASE WHEN Col2 < Col3 THEN Col2 Else Col3 END
WHEN Col2 IS NULL AND Col1 IS NOT NULL AND Col3 IS NOT NULL THEN
CASE WHEN Col1 < Col3 THEN Col1 Else Col3 END
WHEN Col3 IS NULL AND Col1 IS NOT NULL AND Col2 IS NOT NULL THEN
CASE WHEN Col1 < Col2 THEN Col1 Else Col2 END
WHEN Col1 <= Col2 AND Col2 <= Col3 THEN Col2
WHEN Col2 <= Col3 AND Col3 <= Col1 THEN Col3
WHEN Col3 <= Col1 AND Col1 <= Col2 THEN Col1
END
FROM T1
One thing your conditions left out: what if they're all NULL?
Hope this helps,
Ron
--
Ron Talmage
SQL Server MVP
"Ysandre" <Ysandre@.discussions.microsoft.com> wrote in message
news:3147EC21-B5A3-4725-B156-6A40834E5561@.microsoft.com...
> Help, please!
> I have 3 columns with numeric values. i want to write a query that returns
> the following:
> If all 3 columns are populated, then the middle value (not the average);
> If one colunn has a null, then the lesser value,
> If 2 are null, then the remaining value.
> For example: if ColumnA = 5 and ColumnB = 10 and ColumnC = 12, then I want
> the result to be "10". If ColumnB were null, then the result would be "5".
> Making sense?
> Is there a way to do this, without having to create a bazillion Case When
> statements? (case when columnA > ColumnB and ColumnB < ColumnC then
> ColumnB
> else case when columA < ColumnB and ColumnB > ColumnC then ColumnB else .
> .
> .. and so on).
> Just wondering.
> Thank you!
> Kyra
> --
> Financial Systems Analyst
> CCNA, MCSE, MCSA, MCDBA|||Ysandre,
Here is an alternative to Ron's solution. In general, you
would have an easier time if all the values were in one
column..
SELECT
T1ID,
CASE cntC
WHEN 1 THEN maxC
WHEN 2 THEN minC
WHEN 3 THEN sumC - maxC - minC
END AS C
FROM (
SELECT
T1ID,
SUM(C) as sumC,
MIN(C) as minC,
MAX(C) as maxC,
COUNT(C) as cntC
FROM (
SELECT T1ID, Col1 AS C FROM T1
UNION ALL
SELECT T1ID, Col2 FROM T1
UNION ALL
SELECT T1ID, Col3 FROM T1
) T
GROUP BY T1ID
) T
or
SELECT T1ID, MIN(C) FROM (
SELECT T1ID, Col1 AS C FROM T1
UNION ALL
SELECT T1ID, Col2 FROM T1
UNION ALL
SELECT T1ID, Col3 FROM T1
) T1
WHERE C IN (
SELECT TOP 2 C FROM (
SELECT T1ID, Col1 AS C FROM T1
UNION ALL
SELECT T1ID, Col2 FROM T1
UNION ALL
SELECT T1ID, Col3 FROM T1
) AS T1x
WHERE T1x.T1ID = T1.T1ID
AND C IS NOT NULL
ORDER BY C DESC
)
GROUP BY T1ID
If your table were in the form I used for the derived table above (one
value column instead of three), you could write
SELECT T1ID, MIN(C) FROM T1
WHERE C IN (
SELECT TOP 2 C FROM T1 AS T1x
WHERE T1x.T1ID = T1.T1ID
AND C IS NOT NULL
ORDER BY C DESC
)
GROUP BY T1ID
Steve Kass
Drew University
Ysandre wrote:

>Help, please!
>I have 3 columns with numeric values. i want to write a query that returns
>the following:
>If all 3 columns are populated, then the middle value (not the average);
>If one colunn has a null, then the lesser value,
>If 2 are null, then the remaining value.
>For example: if ColumnA = 5 and ColumnB = 10 and ColumnC = 12, then I want
>the result to be "10". If ColumnB were null, then the result would be "5".
>Making sense?
>Is there a way to do this, without having to create a bazillion Case When
>statements? (case when columnA > ColumnB and ColumnB < ColumnC then ColumnB
>else case when columA < ColumnB and ColumnB > ColumnC then ColumnB else . .
>.. and so on).
>Just wondering.
>Thank you!
>Kyra
>
>|||Thank you Ron and Steve, that was very helpful!!!!
Thanks,
ysandre
"Steve Kass" wrote:

> Ysandre,
> Here is an alternative to Ron's solution. In general, you
> would have an easier time if all the values were in one
> column..
> SELECT
> T1ID,
> CASE cntC
> WHEN 1 THEN maxC
> WHEN 2 THEN minC
> WHEN 3 THEN sumC - maxC - minC
> END AS C
> FROM (
> SELECT
> T1ID,
> SUM(C) as sumC,
> MIN(C) as minC,
> MAX(C) as maxC,
> COUNT(C) as cntC
> FROM (
> SELECT T1ID, Col1 AS C FROM T1
> UNION ALL
> SELECT T1ID, Col2 FROM T1
> UNION ALL
> SELECT T1ID, Col3 FROM T1
> ) T
> GROUP BY T1ID
> ) T
> or
> SELECT T1ID, MIN(C) FROM (
> SELECT T1ID, Col1 AS C FROM T1
> UNION ALL
> SELECT T1ID, Col2 FROM T1
> UNION ALL
> SELECT T1ID, Col3 FROM T1
> ) T1
> WHERE C IN (
> SELECT TOP 2 C FROM (
> SELECT T1ID, Col1 AS C FROM T1
> UNION ALL
> SELECT T1ID, Col2 FROM T1
> UNION ALL
> SELECT T1ID, Col3 FROM T1
> ) AS T1x
> WHERE T1x.T1ID = T1.T1ID
> AND C IS NOT NULL
> ORDER BY C DESC
> )
> GROUP BY T1ID
> If your table were in the form I used for the derived table above (one
> value column instead of three), you could write
> SELECT T1ID, MIN(C) FROM T1
> WHERE C IN (
> SELECT TOP 2 C FROM T1 AS T1x
> WHERE T1x.T1ID = T1.T1ID
> AND C IS NOT NULL
> ORDER BY C DESC
> )
> GROUP BY T1ID
> Steve Kass
> Drew University
>
> Ysandre wrote:
>
>sql

Query Help

Help, please!
I have 3 columns with numeric values. i want to write a query that returns
the following:
If all 3 columns are populated, then the middle value (not the average);
If one colunn has a null, then the lesser value,
If 2 are null, then the remaining value.
For example: if ColumnA = 5 and ColumnB = 10 and ColumnC = 12, then I want
the result to be "10". If ColumnB were null, then the result would be "5".
Making sense?
Is there a way to do this, without having to create a bazillion Case When
statements? (case when columnA > ColumnB and ColumnB < ColumnC then ColumnB
else case when columA < ColumnB and ColumnB > ColumnC then ColumnB else . .
.. and so on).
Just wondering.
Thank you!
Kyra
--
Financial Systems Analyst
CCNA, MCSE, MCSA, MCDBAKyra,
I think the bazillion CASEs is the only way to go. Here's one attempt:
CREATE TABLE T1 (T1ID INT NOT NULL IDENTITY, Col1 int, Col2 int, Col3 int)
GO
INSERT T1 VALUES (NULL, NUll, 1)
INSERT T1 VALUES (NULL, 2, NULL)
INSERT T1 VALUES (3, NUll, NULL)
INSERT T1 VALUES (NULL, 4, 5)
INSERT T1 VALUES (6, NULL, 7)
INSERT T1 VALUES (8,9, NULL)
INSERT T1 VALUES (10,11,12)
GO
SELECT
CASE
WHEN Col1 IS NULL AND Col2 IS NULL THEN Col3
WHEN Col2 IS NULL AND Col2 IS NULL THEN Col1
WHEN Col1 IS NULL AND Col3 IS NULL THEN Col2
WHEN Col1 IS NULL AND Col2 IS NOT NULL AND Col3 IS NOT NULL THEN
CASE WHEN Col2 < Col3 THEN Col2 Else Col3 END
WHEN Col2 IS NULL AND Col1 IS NOT NULL AND Col3 IS NOT NULL THEN
CASE WHEN Col1 < Col3 THEN Col1 Else Col3 END
WHEN Col3 IS NULL AND Col1 IS NOT NULL AND Col2 IS NOT NULL THEN
CASE WHEN Col1 < Col2 THEN Col1 Else Col2 END
WHEN Col1 <= Col2 AND Col2 <= Col3 THEN Col2
WHEN Col2 <= Col3 AND Col3 <= Col1 THEN Col3
WHEN Col3 <= Col1 AND Col1 <= Col2 THEN Col1
END
FROM T1
One thing your conditions left out: what if they're all NULL?
Hope this helps,
Ron
--
Ron Talmage
SQL Server MVP
"Ysandre" <Ysandre@.discussions.microsoft.com> wrote in message
news:3147EC21-B5A3-4725-B156-6A40834E5561@.microsoft.com...
> Help, please!
> I have 3 columns with numeric values. i want to write a query that returns
> the following:
> If all 3 columns are populated, then the middle value (not the average);
> If one colunn has a null, then the lesser value,
> If 2 are null, then the remaining value.
> For example: if ColumnA = 5 and ColumnB = 10 and ColumnC = 12, then I want
> the result to be "10". If ColumnB were null, then the result would be "5".
> Making sense?
> Is there a way to do this, without having to create a bazillion Case When
> statements? (case when columnA > ColumnB and ColumnB < ColumnC then
> ColumnB
> else case when columA < ColumnB and ColumnB > ColumnC then ColumnB else .
> .
> .. and so on).
> Just wondering.
> Thank you!
> Kyra
> --
> Financial Systems Analyst
> CCNA, MCSE, MCSA, MCDBA|||Ysandre,
Here is an alternative to Ron's solution. In general, you
would have an easier time if all the values were in one
column..
SELECT
T1ID,
CASE cntC
WHEN 1 THEN maxC
WHEN 2 THEN minC
WHEN 3 THEN sumC - maxC - minC
END AS C
FROM (
SELECT
T1ID,
SUM(C) as sumC,
MIN(C) as minC,
MAX(C) as maxC,
COUNT(C) as cntC
FROM (
SELECT T1ID, Col1 AS C FROM T1
UNION ALL
SELECT T1ID, Col2 FROM T1
UNION ALL
SELECT T1ID, Col3 FROM T1
) T
GROUP BY T1ID
) T
or
SELECT T1ID, MIN(C) FROM (
SELECT T1ID, Col1 AS C FROM T1
UNION ALL
SELECT T1ID, Col2 FROM T1
UNION ALL
SELECT T1ID, Col3 FROM T1
) T1
WHERE C IN (
SELECT TOP 2 C FROM (
SELECT T1ID, Col1 AS C FROM T1
UNION ALL
SELECT T1ID, Col2 FROM T1
UNION ALL
SELECT T1ID, Col3 FROM T1
) AS T1x
WHERE T1x.T1ID = T1.T1ID
AND C IS NOT NULL
ORDER BY C DESC
)
GROUP BY T1ID
If your table were in the form I used for the derived table above (one
value column instead of three), you could write
SELECT T1ID, MIN(C) FROM T1
WHERE C IN (
SELECT TOP 2 C FROM T1 AS T1x
WHERE T1x.T1ID = T1.T1ID
AND C IS NOT NULL
ORDER BY C DESC
)
GROUP BY T1ID
Steve Kass
Drew University
Ysandre wrote:
>Help, please!
>I have 3 columns with numeric values. i want to write a query that returns
>the following:
>If all 3 columns are populated, then the middle value (not the average);
>If one colunn has a null, then the lesser value,
>If 2 are null, then the remaining value.
>For example: if ColumnA = 5 and ColumnB = 10 and ColumnC = 12, then I want
>the result to be "10". If ColumnB were null, then the result would be "5".
>Making sense?
>Is there a way to do this, without having to create a bazillion Case When
>statements? (case when columnA > ColumnB and ColumnB < ColumnC then ColumnB
>else case when columA < ColumnB and ColumnB > ColumnC then ColumnB else . .
>.. and so on).
>Just wondering.
>Thank you!
>Kyra
>
>|||Thank you Ron and Steve, that was very helpful!!!!
Thanks,
ysandre
"Steve Kass" wrote:
> Ysandre,
> Here is an alternative to Ron's solution. In general, you
> would have an easier time if all the values were in one
> column..
> SELECT
> T1ID,
> CASE cntC
> WHEN 1 THEN maxC
> WHEN 2 THEN minC
> WHEN 3 THEN sumC - maxC - minC
> END AS C
> FROM (
> SELECT
> T1ID,
> SUM(C) as sumC,
> MIN(C) as minC,
> MAX(C) as maxC,
> COUNT(C) as cntC
> FROM (
> SELECT T1ID, Col1 AS C FROM T1
> UNION ALL
> SELECT T1ID, Col2 FROM T1
> UNION ALL
> SELECT T1ID, Col3 FROM T1
> ) T
> GROUP BY T1ID
> ) T
> or
> SELECT T1ID, MIN(C) FROM (
> SELECT T1ID, Col1 AS C FROM T1
> UNION ALL
> SELECT T1ID, Col2 FROM T1
> UNION ALL
> SELECT T1ID, Col3 FROM T1
> ) T1
> WHERE C IN (
> SELECT TOP 2 C FROM (
> SELECT T1ID, Col1 AS C FROM T1
> UNION ALL
> SELECT T1ID, Col2 FROM T1
> UNION ALL
> SELECT T1ID, Col3 FROM T1
> ) AS T1x
> WHERE T1x.T1ID = T1.T1ID
> AND C IS NOT NULL
> ORDER BY C DESC
> )
> GROUP BY T1ID
> If your table were in the form I used for the derived table above (one
> value column instead of three), you could write
> SELECT T1ID, MIN(C) FROM T1
> WHERE C IN (
> SELECT TOP 2 C FROM T1 AS T1x
> WHERE T1x.T1ID = T1.T1ID
> AND C IS NOT NULL
> ORDER BY C DESC
> )
> GROUP BY T1ID
> Steve Kass
> Drew University
>
> Ysandre wrote:
> >Help, please!
> >
> >I have 3 columns with numeric values. i want to write a query that returns
> >the following:
> >If all 3 columns are populated, then the middle value (not the average);
> >If one colunn has a null, then the lesser value,
> >If 2 are null, then the remaining value.
> >
> >For example: if ColumnA = 5 and ColumnB = 10 and ColumnC = 12, then I want
> >the result to be "10". If ColumnB were null, then the result would be "5".
> >Making sense?
> >
> >Is there a way to do this, without having to create a bazillion Case When
> >statements? (case when columnA > ColumnB and ColumnB < ColumnC then ColumnB
> >else case when columA < ColumnB and ColumnB > ColumnC then ColumnB else . .
> >.. and so on).
> >
> >Just wondering.
> >Thank you!
> >Kyra
> >
> >
> >
>

Monday, March 26, 2012

Query hangs during a seperate write

When writing 100's of records to 1 or more tables within a transaction, the
server seems to hang any query trying to read from those tables until either
a Timeout or the write process completes. I'm not doing a lock.
The writes and reads are separate applications.
What would cause this?How are you doing the write?
AMB
"Joe" wrote:

> When writing 100's of records to 1 or more tables within a transaction, th
e
> server seems to hang any query trying to read from those tables until eith
er
> a Timeout or the write process completes. I'm not doing a lock.
> The writes and reads are separate applications.
> What would cause this?
>
>|||using ADO.NET. The DataAdaptor is doing Inserts into the tables.
"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in message
news:9960EB96-B274-4A6C-85C0-3BB485E1D411@.microsoft.com...
> How are you doing the write?
>
> AMB
>
> "Joe" wrote:
>
the
either|||Do you know the isolation level being used in the transaction?
You can use Profiler to trace the locks, Lock:Acquired / Lock:Escalation and
Lock:Released.
AMB
"Joe" wrote:

> using ADO.NET. The DataAdaptor is doing Inserts into the tables.
> "Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in messag
e
> news:9960EB96-B274-4A6C-85C0-3BB485E1D411@.microsoft.com...
> the
> either
>
>|||Use with (nolock) on your select statements, for ex.
SELECT au_lname FROM authors WITH (NOLOCK)
This will ensure that select stmt. does the dirty rather than acquiring
an shared lock while the exclusive lock is on.|||You may have a deadlock, and insert is relying on an ID inserted in another
table which itself is waiting for the hanging insert to finish.
If it happens all the time - simply check your logic does the flow work? -
you not done something silly in a transaction.
Otherwise if its intermitant find what process blocks it and syncronise the
order which both processes use DB tables. Try and minimise the time spent in
transactions.
Use profiler to / or current actvity pane in Enterprise man to explore the
processes
Just an approach I've found useful.

Friday, March 23, 2012

Query for server user rights, databases and roles.

I'm trying to write a query that will list the server login name, the
databases they have access too, and what roles they have. I can do this
through Enterprise Manager no problem. I simply go to the server, security,
logins, right click on a user and it gives me the databases they have access
to plus their roles. All I want to do is write this in a query in query
analyzer. Thanks in advance for the help.
Message posted via http://www.droptable.com
Hi,
Execute the below system stored proc from query analyzer:-
sp_helplogins <Login_name>
Thanks
Hari
SQL Server MVP
"Frank Nadal via droptable.com" <forum@.nospam.droptable.com> wrote in
message news:4FCC7EC4933C0@.droptable.com...
> I'm trying to write a query that will list the server login name, the
> databases they have access too, and what roles they have. I can do this
> through Enterprise Manager no problem. I simply go to the server,
> security,
> logins, right click on a user and it gives me the databases they have
> access
> to plus their roles. All I want to do is write this in a query in query
> analyzer. Thanks in advance for the help.
> --
> Message posted via http://www.droptable.com

Query for server user rights, databases and roles.

I'm trying to write a query that will list the server login name, the
databases they have access too, and what roles they have. I can do this
through Enterprise Manager no problem. I simply go to the server, security,
logins, right click on a user and it gives me the databases they have access
to plus their roles. All I want to do is write this in a query in query
analyzer. Thanks in advance for the help.
Message posted via http://www.droptable.comHi,
Execute the below system stored proc from query analyzer:-
sp_helplogins <Login_name>
Thanks
Hari
SQL Server MVP
"Frank Nadal via droptable.com" <forum@.nospam.droptable.com> wrote in
message news:4FCC7EC4933C0@.droptable.com...
> I'm trying to write a query that will list the server login name, the
> databases they have access too, and what roles they have. I can do this
> through Enterprise Manager no problem. I simply go to the server,
> security,
> logins, right click on a user and it gives me the databases they have
> access
> to plus their roles. All I want to do is write this in a query in query
> analyzer. Thanks in advance for the help.
> --
> Message posted via http://www.droptable.com

Query for server user rights, databases and roles.

I'm trying to write a query that will list the server login name, the
databases they have access too, and what roles they have. I can do this
through Enterprise Manager no problem. I simply go to the server, security,
logins, right click on a user and it gives me the databases they have access
to plus their roles. All I want to do is write this in a query in query
analyzer. Thanks in advance for the help.
--
Message posted via http://www.sqlmonster.comHi,
Execute the below system stored proc from query analyzer:-
sp_helplogins <Login_name>
Thanks
Hari
SQL Server MVP
"Frank Nadal via SQLMonster.com" <forum@.nospam.SQLMonster.com> wrote in
message news:4FCC7EC4933C0@.SQLMonster.com...
> I'm trying to write a query that will list the server login name, the
> databases they have access too, and what roles they have. I can do this
> through Enterprise Manager no problem. I simply go to the server,
> security,
> logins, right click on a user and it gives me the databases they have
> access
> to plus their roles. All I want to do is write this in a query in query
> analyzer. Thanks in advance for the help.
> --
> Message posted via http://www.sqlmonster.com

Wednesday, March 21, 2012

Query for Backup Model

I'm trying to write some T-SQL code for adding some fills and thrills
to backups but ran into a snag. Is there a way to query what backup
model a database is using? I can see it if I right click a database,
go to properties, select the options tab, and then can see the current
model (full, bulk-logged, and simple) but I would need to hard code
each database. In the spirit of automation, I'd rather query for the
type and then work around that instead. But I've been all over the
master database and can't seem to find any field that will indicate
the backup type.

Any guru's out there that can point me in the right direction?

Thanks,
UtahHi Utah,

Try DATABASEPROPERTYEX in Books Online: http://msdn2.microsoft.com/en-us/library/ms186823.aspx
You'll want the Recovery property.

Thanks,

KenJ

On Mar 15, 5:45 pm, Utahd...@.hotmail.com wrote:

Quote:

Originally Posted by

I'm trying to write some T-SQL code for adding some fills and thrills
to backups but ran into a snag. Is there a way to query what backup
model a database is using? I can see it if I right click a database,
go to properties, select the options tab, and then can see the current
model (full, bulk-logged, and simple) but I would need to hard code
each database. In the spirit of automation, I'd rather query for the
type and then work around that instead. But I've been all over the
master database and can't seem to find any field that will indicate
the backup type.
>
Any guru's out there that can point me in the right direction?
>
Thanks,
Utah

|||On Mar 15, 7:43 pm, "KenJ" <kenjohn...@.hotmail.comwrote:

Quote:

Originally Posted by

Hi Utah,
>
Try DATABASEPROPERTYEX in Books Online:http://msdn2.microsoft.com/en-us/library/ms186823.aspx
>
You'll want the Recovery property.
>
Thanks,
>
KenJ
>
On Mar 15, 5:45 pm, Utahd...@.hotmail.com wrote:
>
>
>

Quote:

Originally Posted by

I'm trying to write some T-SQL code for adding some fills and thrills
to backups but ran into a snag. Is there a way to query what backup
model a database is using? I can see it if I right click a database,
go to properties, select the options tab, and then can see the current
model (full, bulk-logged, and simple) but I would need to hard code
each database. In the spirit of automation, I'd rather query for the
type and then work around that instead. But I've been all over the
master database and can't seem to find any field that will indicate
the backup type.


>

Quote:

Originally Posted by

Any guru's out there that can point me in the right direction?


>

Quote:

Originally Posted by

Thanks,
Utah- Hide quoted text -


>
- Show quoted text -


That could not have been more perfect. THANK YOU!

Friday, March 9, 2012

Query Delimna

I am Trying to Write a Query that calculates and
summarizes Pay from the following Tables:
Employee
EmpID
EmpName
RegularRate
OverTimeRate
Hours
Date
EmpID
Etype (1=Regular, 2=Overtime)
Hours
The Query I have been using unsuccessfully goes something
like this:
Select Employee.EmpID, Employee.EmpName,
Sum(Hours.Hours) as Hours,
sum(Hours.Hours * (Case When Hours.Etype=1 Then
Employee.RegularRate Else Employee.OverTimeRate End))
From Hours
Left Join Employee on Employee.EmpID = Hours.EmpID
Group by Employee.EmpID, Employee.EmpName
This Query will not run unless I also include the Etype,
RegularRate and OvertimeRate in my Group by statement.
When I do this, it throws off my totals.
Any Suggestions?
Thanks
Keith
Please post your DDL + INSERT statements of sample data + expected results.
Tom
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com
"Keith" <anonymous@.discussions.microsoft.com> wrote in message
news:09d201c4b6a5$9a8282c0$a601280a@.phx.gbl...
I am Trying to Write a Query that calculates and
summarizes Pay from the following Tables:
Employee
EmpID
EmpName
RegularRate
OverTimeRate
Hours
Date
EmpID
Etype (1=Regular, 2=Overtime)
Hours
The Query I have been using unsuccessfully goes something
like this:
Select Employee.EmpID, Employee.EmpName,
Sum(Hours.Hours) as Hours,
sum(Hours.Hours * (Case When Hours.Etype=1 Then
Employee.RegularRate Else Employee.OverTimeRate End))
From Hours
Left Join Employee on Employee.EmpID = Hours.EmpID
Group by Employee.EmpID, Employee.EmpName
This Query will not run unless I also include the Etype,
RegularRate and OvertimeRate in my Group by statement.
When I do this, it throws off my totals.
Any Suggestions?
Thanks
Keith

Query Delimna

I am Trying to Write a Query that calculates and
summarizes Pay from the following Tables:
Employee
EmpID
EmpName
RegularRate
OverTimeRate
Hours
Date
EmpID
Etype (1=Regular, 2=Overtime)
Hours
The Query I have been using unsuccessfully goes something
like this:
Select Employee.EmpID, Employee.EmpName,
Sum(Hours.Hours) as Hours,
sum(Hours.Hours * (Case When Hours.Etype=1 Then
Employee.RegularRate Else Employee.OverTimeRate End))
From Hours
Left Join Employee on Employee.EmpID = Hours.EmpID
Group by Employee.EmpID, Employee.EmpName
This Query will not run unless I also include the Etype,
RegularRate and OvertimeRate in my Group by statement.
When I do this, it throws off my totals.
Any Suggestions?
Thanks
KeithPlease post your DDL + INSERT statements of sample data + expected results.
Tom
---
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com
"Keith" <anonymous@.discussions.microsoft.com> wrote in message
news:09d201c4b6a5$9a8282c0$a601280a@.phx.gbl...
I am Trying to Write a Query that calculates and
summarizes Pay from the following Tables:
Employee
EmpID
EmpName
RegularRate
OverTimeRate
Hours
Date
EmpID
Etype (1=Regular, 2=Overtime)
Hours
The Query I have been using unsuccessfully goes something
like this:
Select Employee.EmpID, Employee.EmpName,
Sum(Hours.Hours) as Hours,
sum(Hours.Hours * (Case When Hours.Etype=1 Then
Employee.RegularRate Else Employee.OverTimeRate End))
From Hours
Left Join Employee on Employee.EmpID = Hours.EmpID
Group by Employee.EmpID, Employee.EmpName
This Query will not run unless I also include the Etype,
RegularRate and OvertimeRate in my Group by statement.
When I do this, it throws off my totals.
Any Suggestions?
Thanks
Keith

Query Delimna

I am Trying to Write a Query that calculates and
summarizes Pay from the following Tables:
Employee
EmpID
EmpName
RegularRate
OverTimeRate
Hours
Date
EmpID
Etype (1=Regular, 2=Overtime)
Hours
The Query I have been using unsuccessfully goes something
like this:
Select Employee.EmpID, Employee.EmpName,
Sum(Hours.Hours) as Hours,
sum(Hours.Hours * (Case When Hours.Etype=1 Then
Employee.RegularRate Else Employee.OverTimeRate End))
From Hours
Left Join Employee on Employee.EmpID = Hours.EmpID
Group by Employee.EmpID, Employee.EmpName
This Query will not run unless I also include the Etype,
RegularRate and OvertimeRate in my Group by statement.
When I do this, it throws off my totals.
Any Suggestions?
Thanks
KeithPlease post your DDL + INSERT statements of sample data + expected results.
--
Tom
---
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com
"Keith" <anonymous@.discussions.microsoft.com> wrote in message
news:09d201c4b6a5$9a8282c0$a601280a@.phx.gbl...
I am Trying to Write a Query that calculates and
summarizes Pay from the following Tables:
Employee
EmpID
EmpName
RegularRate
OverTimeRate
Hours
Date
EmpID
Etype (1=Regular, 2=Overtime)
Hours
The Query I have been using unsuccessfully goes something
like this:
Select Employee.EmpID, Employee.EmpName,
Sum(Hours.Hours) as Hours,
sum(Hours.Hours * (Case When Hours.Etype=1 Then
Employee.RegularRate Else Employee.OverTimeRate End))
From Hours
Left Join Employee on Employee.EmpID = Hours.EmpID
Group by Employee.EmpID, Employee.EmpName
This Query will not run unless I also include the Etype,
RegularRate and OvertimeRate in my Group by statement.
When I do this, it throws off my totals.
Any Suggestions?
Thanks
Keith

Saturday, February 25, 2012

query builder

hi friends
i've one question is it possible to use SMO to replicate "Query Designer" programmatically?
i want to write a query builder for end users and am wondering which objects in SMO i need to use to achieve what i want.
any ideas on this one much appreciated.
Thank you very much for your help.You can use SMO for part of the problem, but not all. You can use SMO to obtain a listing of tables, columns, and views and various properties (e.g. keys, datatypes, and etc). But this is no way in SMO to provide it some tables and conditional statements to generate a query.

Peter|||Yes Peter ,i was able to gather table,columns info from SMO but did not see any class inside this namespace to generate query .
do you have any other ideas/advise in writing a sql engine?
Thanks for your help.|||Sorry, I don't have any other suggestions.

Peter|||SMO will be helpful for you in obtaining metadata of the objects (tables, colums, foreign keys etc.) which will help you building the queries. However the query building portion is something you need to build yourself. SMO will not help you in any way.|||

Thanks for the reply Michiel.

finally we ended up using Report builder that ships with sql reporting services.

query builder

hi friends
i've one question is it possible to use SMO to replicate "Query Designer" programmatically?
i want to write a query builder for end users and am wondering which objects in SMO i need to use to achieve what i want.
any ideas on this one much appreciated.
Thank you very much for your help.You can use SMO for part of the problem, but not all. You can use SMO to obtain a listing of tables, columns, and views and various properties (e.g. keys, datatypes, and etc). But this is no way in SMO to provide it some tables and conditional statements to generate a query.

Peter|||Yes Peter ,i was able to gather table,columns info from SMO but did not see any class inside this namespace to generate query .
do you have any other ideas/advise in writing a sql engine?
Thanks for your help.|||Sorry, I don't have any other suggestions.

Peter|||SMO will be helpful for you in obtaining metadata of the objects (tables, colums, foreign keys etc.) which will help you building the queries. However the query building portion is something you need to build yourself. SMO will not help you in any way.|||

Thanks for the reply Michiel.

finally we ended up using Report builder that ships with sql reporting services.

query based on the top MTD sales

hello,
I need to write a query based on the top MTD sales in the series of each fabrics within series of Sales Group and Prod Group

Order by: Sales Group (alphabetical ord) , Prod Group (alphabetical ord) , sort Fabric Group based on the TOP MTD sales

Sales Gr: Active
Prod gr: Adult, Girls, Plus, LG
Fabric Gr: 1,2,3,4,5,6,7,8,...

Sales Gr: Dance
Prod gr: Adult, Girls, Plus, LG
Fabric Gr: 1,2,3,4,5,6,7,8,...

Sales Gr: Yoga
Prod gr: Adult, Girls, Plus
Fabric Gr: 1,2,3,4,5,6,7,8,...

Thank youhello,
I need to write a query based on the top MTD sales in the series of each fabrics within series of Sales Group and Prod Group

Order by: Sales Group (alphabetical ord) , Prod Group (alphabetical ord) , sort Fabric Group based on the TOP MTD sales
Each Fabric group has to be orderd by the Top MTD.

Sales Gr: Active
Prod gr: Adult, Girls, Plus, LG
Fabric Gr: a,b,c,d,e,f,g...
StyleNum: 1,2,3,4,5,6...

Sales Gr: Dance
Prod gr: Adult, Girls, Plus, LG
Fabric Gr: a,b,c,d,e,f,g...
StyleNum: 1,2,3,4,5,6...

Sales Gr: Yoga
Prod gr: Adult, Girls, Plus
Fabric Gr: a,b,c,d,e,f,g...
StyleNum: 1,2,3,4,5,6...|||you need to post some transaction data and the expected output of the query for us to understand where exactly the problem is.|||Hi

I'm afraid your description is a little breathless and difficult to follow. I suspect this is why you have not yet had a response. Please could you write in natural English what purpose the query is to serve?

Ta|||Hello,
Attached is a data file in which I need a sort as I said below.

Thank you|||Query has to show data in alphabetical order for each group (Sales Group, Product Group). Then the Fabric Group has to be at the highest order first based on the MDT Sales and then the Fabric Group has to be grouped by description itself.
Then style number and a color code.

Thank you

Sales Group Product Group FabricGroup STYLE NUM Retail Price WTD_Unit WTD_Sale WTD Avg Sales MTD_Unit MTD_Sale MTD_Avg_Sales YTD_Units YTD_Sales YTD_Avg_Sales
ADULT ACTIVE SUPPLEX 1561 $30.00 25 $716.40 $28.66 101 $2,975.40 $29.46 626 $17,896.50 $28.59
ADULT ACTIVE TRIGROUP 1677 $75.00 6 $344.34 $57.39 39 $2,282.02 $58.51 24 $1,287.60 $53.65
ADULT ACTIVE TRIGROUP 1677 $75.00 6 $344.34 $57.39 39 $2,282.02 $58.51 100 $5,755.44 $57.55
ADULT ACTIVE TRIGROUP 1677 $75.00 2 $117.58 $58.79 34 $2,009.67 $59.11 38 $2,125.20 $55.93
ADULT ACTIVE TRIGROUP 1677 $75.00 2 $117.58 $58.79 34 $2,009.67 $59.11 126 $7,229.39 $57.38
ADULT ACTIVE SUPPLEX 1562 $31.50 11 $343.35 $31.21 65 $1,997.73 $30.73 585 $17,091.59 $29.22
ADULT ACTIVE SUPPLEX 1561 $30.00 8 $223.20 $27.90 35 $1,024.80 $29.28 154 $4,335.60 $28.15
ADULT ACTIVE COTN LYCRA 8150 $21.00 8 $168.00 $21.00 49 $1,019.34 $20.80 404 $8,098.44 $20.05
ADULT ACTIVE YOGA CVC 5720 $33.00 11 $327.69 $29.79 31 $935.55 $30.18 220 $6,721.77 $30.55
ADULT ACTIVE SUPPLEX 5145 $39.00 6 $221.91 $36.99 23 $873.99 $38.00 212 $7,781.67 $36.71
ADULT ACTIVE SUPPLEX 7360 $23.00 14 $318.78 $22.77 39 $868.48 $22.27 178 $3,822.37 $21.47
ADULT ACTIVE SUPPLEX 1561 $30.00 6 $179.40 $29.90 29 $859.20 $29.63 35 $1,037.40 $29.64
ADULT ACTIVE TRIGROUP 1847 $48.00 2 $95.04 $47.52 17 $797.76 $46.93 25 $1,168.32 $46.73
ADULT ACTIVE YOGA CVC 8298 $42.00 3 $115.50 $38.50 19 $761.46 $40.08 177 $6,994.68 $39.52
ADULT ACTIVE SUPPLEX 7111 $36.00 7 $232.92 $33.27 22 $752.76 $34.22 98 $3,303.36 $33.71
ADULT ACTIVE TRIGROUP 1672 $48.00 0 $0.00 $0.00 18 $715.82 $39.77 74 $2,806.50 $37.93
ADULT ACTIVE TRIGROUP 1671 $44.00 2 $69.98 $34.99 19 $660.61 $34.77 82 $2,680.23 $32.69
ADULT ACTIVE STRETCH COTN 8202 $17.00 13 $209.95 $16.15 39 $635.97 $16.31 103 $1,633.70 $15.86
ADULT ACTIVE COTN LYCRA 8379 $36.00 2 $71.28 $35.64 19 $635.76 $33.46 175 $5,721.48 $32.69
ADULT ACTIVE YOGA CVC 5369 $36.00 5 $177.12 $35.42 18 $626.40 $34.80 310 $10,296.72 $33.22
ADULT ACTIVE COTN LYCRA 8166 $30.00 0 $0.00 $0.00 22 $625.20 $28.42 99 $2,781.60 $28.10
ADULT ACTIVE TRIGROUP 1845 $75.00 0 $0.00 $0.00 8 $597.00 $74.63 19 $1,401.00 $73.74
ADULT ACTIVE SUPPLEX 1561 $30.00 3 $90.00 $30.00 20 $596.40 $29.82 28 $817.20 $29.19
ADULT DANCE SUPPLEX 9963 $20.00 0 $0.00 $0.00 3 $35.49 $11.83 3 $35.49 $11.83
ADULT DANCE SUPPLEX 9963 $20.00 0 $0.00 $0.00 3 $35.49 $11.83 10 $193.60 $19.36
ADULT DANCE SUPPLEX 9963 $20.00 0 $0.00 $0.00 3 $35.49 $11.83 21 $293.65 $13.98
ADULT DANCE NYLON 9090 $15.50 1 $8.81 $8.81 4 $35.42 $8.86 4 $35.42 $8.86
ADULT DANCE NYLON 9090 $15.50 1 $8.81 $8.81 4 $35.42 $8.86 13 $188.79 $14.52
ADULT DANCE SATIN TOUCH 2809 $36.00 0 $0.00 $0.00 1 $35.28 $35.28 6 $207.36 $34.56
ADULT DANCE SATIN TOUCH 2812 $36.00 0 $0.00 $0.00 1 $35.28 $35.28 14 $483.48 $34.53
ADULT DANCE NYLON 9090 $15.50 0 $0.00 $0.00 4 $35.24 $8.81 17 $244.90 $14.41
ADULT DANCE NYLON 9090 $15.50 0 $0.00 $0.00 4 $35.24 $8.81 18 $152.02 $8.45
ADULT DANCE BODY SCULPT 2813 $35.00 0 $0.00 $0.00 1 $35.00 $35.00 9 $300.30 $33.37
ADULT DANCE BODY SCULPT COLLECTION (2006) 2813 $35.00 0 $0.00 $0.00 1 $35.00 $35.00 11 $359.45 $32.68|||Dupe post
http://www.dbforums.com/showthread.php?t=1217893

I still don't get it. There is a sticky at the top of this forum. It asks you to:
Post table DDL
Sample data in DML format
Expected results

It also explains how to do any of these if you don't know how. Please could you do this?|||FYI (not the OP) - this is also live in SQL Server:
http://www.dbforums.com/showthread.php?t=1217908

:D|||SalesGroup Text 255
ProductGroup Text 255
FabricGroup Text 255
TYPEYR Text 5
STYLE_NUM Text 50
STYLE_DESC Text 255
COLORCODE Text 3
COLORD Text 255
Retail Price Currency 8
WTD_Units_05_25_2006 Double 8
WTD_Sales_05_25_2006 Currency 8
WTD_Avg_Sales_05_25_2006 Currency 8
MTD_Units_May 2006 Double 8
MTD_Sales_May 2006 Currency 8
MTD_Avg_Sales_May 2006 Currency 8
YTD_Units_2006 Double 8
YTD_Sales_2006 Currency 8
YTD_Avg_Sales_2006 Currency 8

SalesGroup (In Alphabetical Order - Adult, Girls, Plus, Mens, Irrs),
for each Sales gr ther are the
ProductGroup (Alphabetical Order - Active, Dace, Legwr, Prvt),
For each Sales Gr and a Prod Group there are the
FabricGroups (has to apear as the highest total first for each FabricGroup based on the MTD Sales (Suplex, Cotton, Cotton LCRY)
and then it has to group the FabricGroup together. (If I have 10 entries for the Supplex and it is at the highest total, then the highest total has to be first and the rest apper after)
StyleID (1,2,3,4,5,6,7..),
Color (01,02,03,04,05),
RetailAmount,
WeeklySales,
MonthlySales
YTDSales

Thank you so much.|||the thing to do when there's a cross post is not to put pointers from one to the other, but to merge the threads, as i have done here

that the merged threads may be more confusing than separate threads is not our problem but the original poster's|||the thing to do when there's a cross post is not to put pointers from one to the other, but to merge the threads, as i have done hereLol - ya :D The pointers were more to flag up the posts to the Orange users than to create some complex web of a thread.

Although I do like to alternate my responses from one thread to another when I do get involved. That's just mischief though.

Monday, February 20, 2012

Query Analyzer..

Hi,
I am trying to write some queries in SQL Server 2005.
Is there any option like query analyzer?
Please guide me out..
Reg
ArijitUse SQL Server Management Studio (SSMS).
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada tom@.cips.ca
www.pinpub.com
"Arijit" <arijit77@.gmail.com> wrote in message
news:1133700925.182636.231720@.g44g2000cwa.googlegroups.com...
> Hi,
> I am trying to write some queries in SQL Server 2005.
> Is there any option like query analyzer?
> Please guide me out..
> Reg
> Arijit
>|||Hi
In SQL Server Management Studio you have the New Query button or the
File/New/Database Engine Query options to give you a query window. Under
Tools/Options you have a range of settings for Query Execution.
You may want to look at the virtual lab "SQL Server 2005 Introduction to SQL
Server Management Studio" at
http://msdn.microsoft.com/vstudio/t...ql/default.aspx for an
introduction on how to use it.
John
"Arijit" <arijit77@.gmail.com> wrote in message
news:1133700925.182636.231720@.g44g2000cwa.googlegroups.com...
> Hi,
> I am trying to write some queries in SQL Server 2005.
> Is there any option like query analyzer?
> Please guide me out..
> Reg
> Arijit
>|||Arijit wrote:

> Hi,
> I am trying to write some queries in SQL Server 2005.
> Is there any option like query analyzer?
> Please guide me out..
> Reg
> Arijit
Management Studio. You can also still use SQL Server 2000 Query
Analyzer for many things. Some of the GUI features won't work but for
straight TSQL development it's just fine.
David Portas
SQL Server MVP
--|||VS 2005 also has it and it stays up instead of closing and poking the select
text in your window as SSMS does (unless I missed something in SSMS which is
likely). See the server explorer or new "Database project" in VS2005. An
interesting note is that a "SQL Server Project" will not give you this query
designer. I can only assume because that is geared for clr projects. But
it would have been nice to be able to use QA within that project and also be
able to just "Run" (instead of debug) queries to see the grid results pane.
William Stacey [MVP]
"Arijit" <arijit77@.gmail.com> wrote in message
news:1133700925.182636.231720@.g44g2000cwa.googlegroups.com...
> Hi,
> I am trying to write some queries in SQL Server 2005.
> Is there any option like query analyzer?
> Please guide me out..
> Reg
> Arijit
>|||William Stacey [MVP] (william.stacey@.gmail.com) writes:
> VS 2005 also has it and it stays up instead of closing and poking the
> select text in your window as SSMS does (unless I missed something in
> SSMS which is likely). See the server explorer or new "Database
> project" in VS2005. An interesting note is that a "SQL Server Project"
> will not give you this query designer. I can only assume because that
> is geared for clr projects. But it would have been nice to be able to
> use QA within that project and also be able to just "Run" (instead of
> debug) queries to see the grid results pane.
He asked about Query Analyzer, not Query Designer. The Query Designer
certainly deserves its name: it designs the queries according to its own
idea, and queries often come out with a different meaning than they
entered. Certainly nothing to recommend.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx