Showing posts with label guys. Show all posts
Showing posts with label guys. Show all posts

Tuesday, March 27, 2012

Creating a View with detailed informations

Hello,
I have a code for creating view in T-SQL. I want to ask you guys, i want to make this result set should grouped by DepoAdi column and StokKodu (this is an alias sure you can get it from code). Did i make it on group by line? My second problem is i want to add 2 columns to this query. This 2 column will calculate some values with SUM function and - operator. At CRM.Depolar.DepoBilgileri table i have a column named Miktar (this one stores int type datas) and i have a column named islemturu(this one stores 1 or 0). I want to calculate Miktar values which rows has islemturu column 0 and subtract them from which rows has islemturu column 1 value and this computing action must be based on Grouped columns.

DepoAdi | StokKodu | Miktar | IslemTuru
ABS SK101 5 0
ABS SK101 3 1
ABS SK102 4 0
ABS SK102 3 1

This is the table and i'm imaging view what i want now...

DepoAdi | StokKodu | Miktar
ABS SK101 2
ABS SK102 1

How can i add this resultset to my code..

Thanks for reading... Waiting your answers.. Happy coding...

CREATE VIEW [CRM.Depolar.DepoDurumlari]

AS

SELECT [CRM.Depolar.DepoBilgileri].DepoAdi,

[CRM.Objeler.TemelGruplar.TureyenGruplar].GrupKodu + [CRM.Objeler.ObjeKodlari].ObjeKodu AS StokKodu

FROM [CRM.Depolar.DepoBilgileri], [CRM.Objeler.TemelGruplar.TureyenGruplar], [CRM.Objeler.ObjeKodlari], [CRM.Depolar.DepoHareketleri]

WHERE [CRM.Depolar.DepoBilgileri].Id IN (SELECT DepoBilgileri

FROM [CRM.Depolar]

WHERE Id IN (SELECT Depo

FROM [CRM.Depolar.DepoHareketleri]))

AND [CRM.Objeler.TemelGruplar.TureyenGruplar].Id IN (SELECT ObjeGrubu

FROM [CRM.Objeler]

WHERE Id IN (SELECT Id

FROM [CRM.StokKartlar]

WHERE Id IN (SELECT StokKart

FROM [CRM.Depolar.DepoHareketleri])))

AND [CRM.Objeler.ObjeKodlari].Id IN (SELECT StokKodu

FROM [CRM.StokKartlar.KartBilgileri]

WHERE Id IN (SELECT KartBilgileri

FROM [CRM.StokKartlar]

WHERE Id IN (SELECT StokKart

FROM [CRM.Depolar.DepoHareketleri])))

GROUP BY [CRM.Depolar.DepoBilgileri].DepoAdi, [CRM.Objeler.TemelGruplar.TureyenGruplar].GrupKodu, [CRM.Objeler.ObjeKodlari].ObjeKodu

Select DepoAdi

, StokKodu

, (Giren - Cikan) As Miktar

From

(

Select Depo.DepoAdi

, Depo.StokKodu

, Sum(Depo.Miktar) As Giren

, 0 As Cikan

From DepoBilgileri As Depo

Where IslemTuru = 0

Group By Depo.DepoAdi, Depo.StokKodu

Union All

Select Depo.DepoAdi

, Depo.StokKodu

, 0 As Giren

, Sum(Depo.Miktar) As Cikan

From DepoBilgileri As Depo

Where IslemTuru = 1

Group By Depo.DepoAdi, Depo.StokKodu

) As Core

|||

Thanks for your reply.. I solved problem with making some changes in my code.

CREATE VIEW [CRM.Depolar.DepoDurumlari]

AS

SELECT [CRM.Depolar.DepoBilgileri].DepoAdi,

[CRM.Objeler.TemelGruplar.TureyenGruplar].GrupKodu + [CRM.Objeler.ObjeKodlari].ObjeKodu AS StokKodu,

(SELECT SUM(CASE [CRM.Depolar.DepoHareketleri].IslemTuru

WHEN 0

THEN [CRM.Depolar.DepoHareketleri].Miktar

ELSE

-1 * [CRM.Depolar.DepoHareketleri].Miktar

END)

FROM [CRM.Depolar.DepoHareketleri]

GROUP BY [CRM.Depolar.DepoHareketleri].Depo, [CRM.Depolar.DepoHareketleri].StokKart) AS Miktar,

(SELECT SUM(CASE [CRM.Depolar.DepoHareketleri].IslemTuru

WHEN 0

THEN [CRM.Depolar.DepoHareketleri].Tutar

ELSE

-1 * [CRM.Depolar.DepoHareketleri].Tutar

END)

FROM [CRM.Depolar.DepoHareketleri]

GROUP BY [CRM.Depolar.DepoHareketleri].Depo, [CRM.Depolar.DepoHareketleri].StokKart) AS Tutar

FROM [CRM.Depolar.DepoBilgileri], [CRM.Objeler.TemelGruplar.TureyenGruplar], [CRM.Objeler.ObjeKodlari], [CRM.Depolar.DepoHareketleri]

WHERE [CRM.Depolar.DepoBilgileri].Id IN (SELECT [CRM.Depolar].DepoBilgileri

FROM [CRM.Depolar]

WHERE [CRM.Depolar].Id IN (SELECT [CRM.Depolar.DepoHareketleri].Depo

FROM [CRM.Depolar.DepoHareketleri]))

AND [CRM.Objeler.TemelGruplar.TureyenGruplar].Id IN (SELECT [CRM.Objeler].ObjeGrubu

FROM [CRM.Objeler]

WHERE [CRM.Objeler].Id IN (SELECT [CRM.StokKartlar].Id

FROM [CRM.StokKartlar]

WHERE [CRM.StokKartar].Id IN (SELECT [CRM.Depolar.DepoHareketleri].StokKart

FROM [CRM.Depolar.DepoHareketleri]

GROUP BY [CRM.Depolar.DepoHareketleri].StokKart)))

AND [CRM.Objeler.ObjeKodlari].Id IN (SELECT [CRM.StokKartlar.KartBilgileri].StokKodu

FROM [CRM.StokKartlar.KartBilgileri]

WHERE [CRM.StokKartlar.KartBilgileri].Id IN (SELECT [CRM.StokKartlar].KartBilgileri

FROM [CRM.StokKartlar]

WHERE [CRM.StokKartlar].Id IN (SELECT [CRM.Depolar.DepoHareketleri].StokKart

FROM [CRM.Depolar.DepoHareketleri]

GROUP BY [CRM.Depolar.DepoHareketleri].StokKart)))

GROUP BY [CRM.Depolar.DepoBilgileri].DepoAdi, [CRM.Objeler.TemelGruplar.TureyenGruplar].GrupKodu, [CRM.Objeler.ObjeKodlari].ObjeKodu

Sunday, March 25, 2012

creating a template

Hi Guys
I have developed a new report in reporting services 2005. now i want to
use this report as a template. Where do i store this report in sql
server sub directory so that it is available in the template option
whenever i try to create a new report.
Any help would be great.
PassxSearch for "report.rdl" or report project in your HDD. Because I dont
remember the exact directory infact i have done it lot of times. or may be
just right click on the report and see the property, so that you can get
complete directory.
Amarnath
"Passx" wrote:
> Hi Guys
> I have developed a new report in reporting services 2005. now i want to
> use this report as a template. Where do i store this report in sql
> server sub directory so that it is available in the template option
> whenever i try to create a new report.
> Any help would be great.
>
> Passx
>|||Hi
I tried doing the same thing but was not able to find it. Found one in
vs8 folder. But when i placed my report in that folder was not able to
find that in the report template menu.
Any help would be great.
Passx
Amarnath wrote:
> Search for "report.rdl" or report project in your HDD. Because I dont
> remember the exact directory infact i have done it lot of times. or may be
> just right click on the report and see the property, so that you can get
> complete directory.
> Amarnath
> "Passx" wrote:
> > Hi Guys
> >
> > I have developed a new report in reporting services 2005. now i want to
> > use this report as a template. Where do i store this report in sql
> > server sub directory so that it is available in the template option
> > whenever i try to create a new report.
> >
> > Any help would be great.
> >
> >
> > Passx
> >
> >

Thursday, March 22, 2012

Creating a sustaining counter as in work orders - unique

Hi Guys,

Im still new to this SQL stuff and have a question about creating a counter that does not reset if I drop the temp table. Is this possible? I need to add new work order number (counter) to a daily orders table/view/procedure and I can make it work for one day but then when I drop the temp table, it resets the counter back. How can I keep the Max(orderno) going forward to the next day?

I am a little versed in stored procedures too. We are using SQL 2000 at the moment in the office.

Any ideas for this simple minded gal?

Thanks!
Jo

Quote:

Originally Posted by Joell

Hi Guys,

Im still new to this SQL stuff and have a question about creating a counter that does not reset if I drop the temp table. Is this possible? I need to add new work order number (counter) to a daily orders table/view/procedure and I can make it work for one day but then when I drop the temp table, it resets the counter back. How can I keep the Max(orderno) going forward to the next day?

I am a little versed in stored procedures too. We are using SQL 2000 at the moment in the office.

Any ideas for this simple minded gal?

Thanks!
Jo


try the IDENTITY column. it may not be sequential, but it's unique and it will not reset|||

Quote:

Originally Posted by ck9663

try the IDENTITY column. it may not be sequential, but it's unique and it will not reset


Thank you so much for responding! I did try the Identity column but I dont know how to make it not reset when pulling the stored procedure tomorrow. Should I send you my code? Maybe I shouldnt create table? How can you put an identity column in a SELECT stmt?|||

Quote:

Originally Posted by Joell

Thank you so much for responding! I did try the Identity column but I dont know how to make it not reset when pulling the stored procedure tomorrow. Should I send you my code? Maybe I shouldnt create table? How can you put an identity column in a SELECT stmt?


try sending your code... just those that will be needed|||Hello Again,

Here is my cutdown code. There is a lot more to the Select stmt but I cut it down just to show you around it. I would like to put the end result into a procedure so that I can push it to crystal reports and my end users can then export to Excel. Maybe even schedule the proc to run as a DTS package?

create procedure @.businessunit varchar(30), @.datepulled datetime

as

create table #UPSDaily(
Location_no varchar(20) not null,
OrderNo int Identity(100000,1) not null
)

Insert into #UPSDaily

Select c_id_alpha Location_No,
-- Dont I need a placeholder here somehow for orderno from the creation of the table above?
from cust inner join rxrf on cust.c_id = rxrf.c_id
where rxrf.next_date = getdate()

-- then I need to keep the last value of the OrderNo for the next day's pull of data.

declare @.intCounter int
select @.intCounter = coalesce(max(orderno), 1) from #UPSDaily
declare @.when datetime
set @.when = getDate()

update #UPSDaily
set @.intCounter = OrderNO = @.intCounter + 1

drop table #UPSDaily -- but this removes my orderno value for the beginning of the next day.
--I need the next sequential number to start off the next day's pull of data.

Thanks so much for your help again! - JOELL|||

Quote:

Originally Posted by Joell

Hello Again,

Here is my cutdown code. There is a lot more to the Select stmt but I cut it down just to show you around it. I would like to put the end result into a procedure so that I can push it to crystal reports and my end users can then export to Excel. Maybe even schedule the proc to run as a DTS package?

create procedure @.businessunit varchar(30), @.datepulled datetime

as

create table #UPSDaily(
Location_no varchar(20) not null,
OrderNo int Identity(100000,1) not null
)

Insert into #UPSDaily

Select c_id_alpha Location_No,
-- Dont I need a placeholder here somehow for orderno from the creation of the table above?
from cust inner join rxrf on cust.c_id = rxrf.c_id
where rxrf.next_date = getdate()

-- then I need to keep the last value of the OrderNo for the next day's pull of data.

declare @.intCounter int
select @.intCounter = coalesce(max(orderno), 1) from #UPSDaily
declare @.when datetime
set @.when = getDate()

update #UPSDaily
set @.intCounter = OrderNO = @.intCounter + 1

drop table #UPSDaily -- but this removes my orderno value for the beginning of the next day.
--I need the next sequential number to start off the next day's pull of data.

Thanks so much for your help again! - JOELL


by the looks of this, you're just getting the last OrderNo? coz you're dropping the table anyway...|||

Quote:

Originally Posted by ck9663

by the looks of this, you're just getting the last OrderNo? coz you're dropping the table anyway...


Is there a better way to structure (maybe some kind of while loop) to run my select while grabbing the next incremental value? If so, how do I code that? Its just not working as is. I need a sequential number to restart over each day that the query runs. If I use a table, then doesnt that make the database larger when it is not necessary? All I am trying to do is create a seq number for the work orders each day and not duplicate any number. The seq number needs to be in a column of the select statement that I am running. Does that make better sense than before?

HELP!|||Hi Jo,

do you have a field in the table with todays date in it ?

If you do, select the max date and compare with today, if the date part of today is bigger, reset your counter..

If not then post the table structure for the table and the temp table..

Regards Purple|||

Quote:

Originally Posted by Purple

Hi Jo,

do you have a field in the table with todays date in it ?

If you do, select the max date and compare with today, if the date part of today is bigger, reset your counter..

If not then post the table structure for the table and the temp table..

Regards Purple


I think I had a typo in my question. What I need to have is a counter that does NOT reset each day. So for my last order on 8/27 is 833230, then the first order on 8/28 should be 833231. Does that make better sense? sorry for all of my confusion.

I do not know how to write the code for it.

Help please.

Jo|||Hi Jo,

am I missing something, cant you just use an auto increment field ?

Purple|||

Quote:

Originally Posted by Purple

Hi Jo,

am I missing something, cant you just use an auto increment field ?

Purple


That sounds logical but I dont know how to do that. I have tried the Identity counter but then if you use a temp table, it resets the next time you run the query. I dont want it to reset. I need unique values every time I run the query and to never reset. I also do not want to add tables to my database. I just need to pull existing data and add an sequential number that will not reset each day I run the query.

Isnt there another way? Maybe could you tell me about the auto increment?|||Jo,

Why are you using a temp table ?

Purple|||

Quote:

Originally Posted by Purple

Jo,

Why are you using a temp table ?

Purple


I thought creating a temp table would be better than to have a table created for 11 separate business divisions of work orders that need to be pulled every day and imported into another system. Wouldnt it make the database very large in a small amount of time?

how else could I pull data from one system, attach a sequential number and then import it into another system? system = database|||Hi Jo,

It is often difficult to analyse the problem when somewhat distant from the basic requirements. From what you have described, I think I would look again at the database structure.

Maybe add an int column with a foreign key to a business unit table and have all of the 11 business units work orders in one table,

I have no idea of what you perceive large is, MSSQL will be fine with multiple million rows in a table if it is appropriately indexed and running on a server with enough grunt.

If you use an autoincrement field for the work order number you will automatically get unique incremental work order numbers.

Does this help or have I missed the point ?

Regards Purple|||

Quote:

Originally Posted by Purple

Hi Jo,

It is often difficult to analyse the problem when somewhat distant from the basic requirements. From what you have described, I think I would look again at the database structure.

Maybe add an int column with a foreign key to a business unit table and have all of the 11 business units work orders in one table,

I have no idea of what you perceive large is, MSSQL will be fine with multiple million rows in a table if it is appropriately indexed and running on a server with enough grunt.

If you use an autoincrement field for the work order number you will automatically get unique incremental work order numbers.

Does this help or have I missed the point ?

Regards Purple


You are awesome and I appreciate your help. I am not very versed in SQL lingo and so defining the problem is a bit of a challenge for me.

I am using proprietary software and trying to pull from it into another package without incrementing the values in the proprietary software.

If I create a new table and add a int column, then on day 2, how do I get the counter to NOT reset? My logic is not such that I can remove the previous day's work orders yet so I will be building and building this table adding new work orders every day. Maybe a better question would be if I create a new table on day 1, how do I add to it on day 2, day 3, keeping the counter int going?

Jo|||

Quote:

Originally Posted by Joell

You are awesome and I appreciate your help. I am not very versed in SQL lingo and so defining the problem is a bit of a challenge for me.

I am using proprietary software and trying to pull from it into another package without incrementing the values in the proprietary software.

If I create a new table and add a int column, then on day 2, how do I get the counter to NOT reset? My logic is not such that I can remove the previous day's work orders yet so I will be building and building this table adding new work orders every day. Maybe a better question would be if I create a new table on day 1, how do I add to it on day 2, day 3, keeping the counter int going?

Jo


Jo...

In order to keep this value, you have to store it somewhere. What do you with the daily tables? Do you have a one master table that contain it all? if you do, you can take the max(counter)+1 on that master table as the starting counter on your daily table. for this, you might need a trigger to handle the counter on your daily table. make sure that your counter is the PK on both table to ensure uniqueness.|||Hi Jo,

I suggest you have a master workorder table which holds all of the work orders created for all of the business areas and this a permanent table not a temp table..

When you create the table workOrder add a field as the primary key and set it as an auto increment field, this will be the work order id.

Now when you insert rows into the table the work order id field is automatically incremented by one for every new row. (dont try to set a value for this field on the insert, if you want the numbers to start from a specific value, ie other than 1 specify a seed value)

Also create a field to represent the business unit as an int and use a join to the business unit table where you may have columns like

buId buName buContact etc...

I would also reiterate my suggestion to take some time out of the coding work to reconsider the database structure - Mary (one of the site administrators) has written this article which you may find helpful..

Regards Purple

Monday, March 19, 2012

creating a partition table in developer edition (2005)

Hi Guys,
I have 2 environments i work on , one is SQL server 2005 enterprise
edition and the other is developer edition (both with Service Pack).
i have a scripts creating a partition table who runs well on the
Enterprise version but when i run it over the Development server it
does nothing (it doesn't fail and no warnings are thrown but the
resulting table is not partitioned).
anyone has a clue ?
according to Microsoft developer edition should support partition
tables .
below is the code relevant :
-- drop partition schema
-- drop partition function
-- create partition function
-- create partition schema
CREATE TABLE [dbo].[Fact_DaySales22](
[Day_Key] [int] NOT NULL,
[DW_Store_Key] [smallint] NOT NULL,
[DW_Item_Key] [int] NOT NULL,
[DW_Supplier_Key] [int] NOT NULL,
[MatrixMemberId] [smallint] NOT NULL,
[Sales] [decimal](13, 2) NULL,
[Discount] [decimal](13, 3) NULL,
[Qty] [int] NULL,
[WeightedQty] [decimal](9, 3) NULL,
[Tax] [decimal](9, 3) NULL,
[Cost] [decimal](13, 3) NULL
CONSTRAINT [PK_Fact_DaySales22] PRIMARY KEY NONCLUSTERED
(
[MatrixMemberId] ASC,
[Day_Key] ASC,
[DW_Store_Key] ASC,
[DW_Item_Key] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [MonthlyDataPScheme22]([Day_Key])
) ON [MonthlyDataPScheme22]([Day_Key])
Have the filegroups been created on the development server? Are the
appropriate partitioning function and the partitioning schema in place?
These things are not evident from your post.
ML
Matija Lah, SQL Server MVP
http://milambda.blogspot.com/

creating a partition table in developer edition (2005)

Hi Guys,
I have 2 environments i work on , one is SQL server 2005 enterprise
edition and the other is developer edition (both with Service Pack).
i have a scripts creating a partition table who runs well on the
Enterprise version but when i run it over the Development server it
does nothing (it doesn't fail and no warnings are thrown but the
resulting table is not partitioned).
anyone has a clue '
according to Microsoft developer edition should support partition
tables .
below is the code relevant :
-- drop partition schema
-- drop partition function
-- create partition function
-- create partition schema
CREATE TABLE [dbo].[Fact_DaySales22](
[Day_Key] [int] NOT NULL,
[DW_Store_Key] [smallint] NOT NULL,
[DW_Item_Key] [int] NOT NULL,
[DW_Supplier_Key] [int] NOT NULL,
[MatrixMemberId] [smallint] NOT NULL,
[Sales] [decimal](13, 2) NULL,
[Discount] [decimal](13, 3) NULL,
[Qty] [int] NULL,
[WeightedQty] [decimal](9, 3) NULL,
[Tax] [decimal](9, 3) NULL,
[Cost] [decimal](13, 3) NULL
CONSTRAINT [PK_Fact_DaySales22] PRIMARY KEY NONCLUSTERED
(
[MatrixMemberId] ASC,
[Day_Key] ASC,
[DW_Store_Key] ASC,
[DW_Item_Key] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [MonthlyDataPScheme22]([Day_Key])
) ON [MonthlyDataPScheme22]([Day_Key])Have the filegroups been created on the development server? Are the
appropriate partitioning function and the partitioning schema in place?
These things are not evident from your post.
ML
Matija Lah, SQL Server MVP
http://milambda.blogspot.com/