Showing posts with label car. Show all posts
Showing posts with label car. Show all posts

Thursday, March 22, 2012

Calculated Column based on the last record

Hi everyone,

I am thinking of this is possible:

ID Type TypeID

1 car 1

2 car 2

3 house 1

4 car 3

5 house 2

6 house 3

7 car 4

8 car 5

9 car 6

10 house 4

I have identity column (ID), Varchar (Type), Int (TypeID)

I need the TypeID to be an identity for each Type column, incremented the same way as the ID column but values depends on the Type column value.

How can I calculate this?

Thanks!

Use 2 tables.

Table 1:

Set concatenated primary key as Type + TypeID identity

Table 2:

Primary Key Identity ID int

Type varchar()

TypeID int

Adamus

|||

Try:

Code Snippet

create table dbo.t1 (

ID int not null identity,

[Type] varchar(25) not null

)

go

insert into dbo.t1([Type]) values('car')

insert into dbo.t1([Type]) values('car')

insert into dbo.t1([Type]) values('house')

insert into dbo.t1([Type]) values('car')

insert into dbo.t1([Type]) values('house')

insert into dbo.t1([Type]) values('house')

insert into dbo.t1([Type]) values('car')

insert into dbo.t1([Type]) values('car')

insert into dbo.t1([Type]) values('car')

insert into dbo.t1([Type]) values('house')

go

select

ID,

[Type],

(

select count(*)

from dbo.t1 as b

where b.[Type] = a.[Type] and b.ID <= a.ID

) as TypeID

from

dbo.t1 as a

order by

[Type],

[TypeID]

go

-- SS 2005

select

ID,

[Type],

row_number() over(partition by [Type] order by ID) as TypeID

from

dbo.t1

order by

[Type],

[TypeID]

go

drop table dbo.t1

go

AMB

|||

Since this is a derivable value, rather than using a column in the base table consider using an expression to extract the ranking value. In general cases folks use a view or a computed column for such requirements. In t-SQL, here are a few options:

-- #1

SELECT "id", "type",
RANK() OVER ( PARTITION BY "type" ORDER BY "id" )
FROM tbl
ORDER BY "id" ;

--#2

SELECT "id", "type",
( SELECT COUNT(*) FROM tbl t2
WHERE t2.type = t1.type
AND t2."id" <= t1."id" )
FROM tbl t1 ;

--#2

SELECT t1."id", t1."type", COUNT(*)
FROM tbl t1
JOIN tbl t2
ON t2.type = t1.type
AND t2."id" <= t1."id"
GROUP BY t1."id", t1."type"
ORDER BY t1."id";

|||

I'd like to comment that although both posted approaches will work, it may be the beginning of a maintenance nightmare. If, in fact, the values will be stored and not derived, 2 tables would support good design and optimize the cognition when alterations on the query are required.

Also, if they are calculated, why not calculate on insert?

Adamus

|||

thanks!

i created a stored procedure to insert calculated column using the row_number() over(partition by [Type] order by ID) as TypeID

Monday, March 19, 2012

calc totals for months question

I have the following records:
Unit Create_date Type
A 01/02/2004 Clock
A 01/15/2004 Car
A 01/20/2004 Truck
A 01/23/2004 Clock
A 01/24/2004 Clock
A 01/25/2004 Car
A 01/27/2004 Truck
A 02/01/2004 Clock
A 02/02/2004 Car
A 02/15/2004 Truck
A 02/20/2004 Car
A 02/27/2004 Car
A 06/03/2004 Truck
A 06/04/2004 Truck
A 06/15/2004 Clock
A 06/29/2004 Car
Report Output example
Unit Created Type
A Jan 04 Clock
Type Count = 3
A Jan 04 Car
Type Count = 2
A Jan 04 Truck
Type Count = 2
Total for January = 7
A Feb 04 Clock
Type Count = 1
A Feb 04 Car
Type Count = 3
A Feb 04 Truck
Type Count = 1
Total for February = 5
A June 04 Clock
Type Count = 1
A June 04 Car
Type Count = 1
A June 04 Truck
Type Count = 2
Total for June = 4
I am trying to determine how to count the totals for the months.
Any ideas?
Thanks.Just use a table, and add a table grouping "MonthGrouping" like e.g.:
=Year(Fields!TimeStamp.Value)*100 + Month(Fields!TimeStamp.Value)
Then add a second (inner) table grouping "TypeGrouping" which groups on the
type, e.g.: =Fields!Type.Value
These two groupings should give you the basic structure of the desired
output. Finally, to determine the counts for the months, just add an
expression like this to the "MonthGrouping" groop footer:
=CountRows("MonthGrouping")
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Mike" <Mike@.discussions.microsoft.com> wrote in message
news:83250B36-B1BC-4601-9002-87EDE106838E@.microsoft.com...
>I have the following records:
> Unit Create_date Type
> A 01/02/2004 Clock
> A 01/15/2004 Car
> A 01/20/2004 Truck
> A 01/23/2004 Clock
> A 01/24/2004 Clock
> A 01/25/2004 Car
> A 01/27/2004 Truck
> A 02/01/2004 Clock
> A 02/02/2004 Car
> A 02/15/2004 Truck
> A 02/20/2004 Car
> A 02/27/2004 Car
> A 06/03/2004 Truck
> A 06/04/2004 Truck
> A 06/15/2004 Clock
> A 06/29/2004 Car
> Report Output example
> Unit Created Type
> A Jan 04 Clock
> Type Count = 3
> A Jan 04 Car
> Type Count = 2
> A Jan 04 Truck
> Type Count = 2
> Total for January = 7
> A Feb 04 Clock
> Type Count = 1
> A Feb 04 Car
> Type Count = 3
> A Feb 04 Truck
> Type Count = 1
> Total for February = 5
> A June 04 Clock
> Type Count = 1
> A June 04 Car
> Type Count = 1
> A June 04 Truck
> Type Count = 2
> Total for June = 4
> I am trying to determine how to count the totals for the months.
> Any ideas?
>
> Thanks.
>

Thursday, February 16, 2012

Buying a car without a driver's seat

Express :
Isn't that like getting the SQL server without the mangement program?
Without the buying of courseNo, it's like buying a Windows PC without a mouse :-)
WBR, Evergray
--
Words mean nothing...
"Tammy B" <querygirl@.plumbbob32domain.com> wrote in message
news:rKydnagtv6wUUpLZnZ2dnUVZ_sCdnZ2d@.co
mcast.com...
> Express :
> Isn't that like getting the SQL server without the mangement program?
> Without the buying of course
>|||Tammy B wrote:
> Express :
> Isn't that like getting the SQL server without the mangement program?
> Without the buying of course
Express is fine if you just want to plug in an application and run it.
Otherwise take a look at the other editions:
http://www.microsoft.com/sql/prodin...e-features.mspx
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||The Management Studio application for SSEE can be downloaded from Microsoft
seperately. Some distributed applications that use SSEE only need or want
the runtime installed.
"Tammy B" <querygirl@.plumbbob32domain.com> wrote in message
news:rKydnagtv6wUUpLZnZ2dnUVZ_sCdnZ2d@.co
mcast.com...
> Express :
> Isn't that like getting the SQL server without the mangement program?
> Without the buying of course
>|||On Wed, 8 Mar 2006 23:47:38 -0700, Tammy B wrote:

>Express :
>Isn't that like getting the SQL server without the mangement program?
Hi Tammy,
I'd rather compare it to buying (or: getting) a very basic car, with no
automatic gear, no servo-brake, no wheel ratification, no cruise
control, and no remote controlled doorlock.
You can still do anything you have to do, but it takes some more effort.
As David said, Express is not primarily positioned for development work,
but rather to serve as a backend DB for an application. If you want a
version of SQL Server for development work, consider buying the
developer edition:
http://www.microsoft.com/sql/editio...er/default.mspx
Hugo Kornelis, SQL Server MVP