Showing posts with label newbie. Show all posts
Showing posts with label newbie. Show all posts

Thursday, March 29, 2012

Create an array in a result field

I am between the "newbie" and "intermediate" stages of writing SQL code and I am wondering if there is a way to capture multiple results into one field so I can basically create a "set" for a unique identifier. Here is few result samples I receive from this code I am using now.

ReqNo ProcID

7102005 1409

7102005 1796

7139003 1411

7139003 6097

7261030 1409

7261030 1796

7268303 3998

7268303 4000

I would like to create a single row for each "ReqNo" and have a field that will an array of the "ProcID" results I receive. In other words, for the first "ReqNo" 7102005, can I create a field that will combine the 1409, 1796 into one field? I am trying to capture an array of integers used for that "ReqNo" so I can use that as a unique identifier in a join for another table.

So, ideally my result would be:

ReqNo ProcSet

7102005 1409, 1796

7139003 1411, 6097

7261030 1409, 1796

7268303 3998, 4000

Is this possible?

declare

@.startdate smalldatetime,

@.enddate smalldatetime ,

@.month int,

@.year int

select

@.startdate = dateadd (dd, -7, getdate())

SELECT

@.month = datepart (month, @.startdate),

@.year = datepart (year, @.startdate)

SELECT

@.startdate = convert (smalldatetime, convert(varchar(2), @.month) + "/1/" + convert (varchar(4), @.year))

select

@.enddate = dateadd (dd, 1 , @.startdate)

select distinct

pp_req_no as ReqNo,

pp_cproc_id_r as ProcID

from

risdb_rch08_stag..performed_procedure

(index pp_serv_time_r_ndx)

where

pp_service_time_r between @.Startdate and @.Enddate

and pp_status_v = 'CP'

and pp_rep_id > 0

order by

pp_req_no, pp_cproc_id_r

You could create a function that would concatenate the fields together and return them as a string. This is not really a recommended practice, but can be occassionally useful for presentation. One trick is to use a local variable to accumulate the results. Of course, your function would be specific to this table.

I am not up on 2005 features, but it seems like there is a new unpivot operator that might work.

This demonstrates how to do the accumulation into a local variable.

use Northwind

go

Declare @.result varchar(1000)

Select @.result = Case When @.result Is Not Null Then @.result + ', ' Else '' End + Convert(varchar(10),OrderID)

From Orders

Where CustomerID = 'ALFKI'

Select @.result|||

Confused?

So are you saying that this syntax @.result + ', ' will build the results in the field? This looks like it will concatenate one result followed by the OrderID in your example (123456, 99999).

How will that build multiple results in one field? I understand how to declare and select, but this does not seem to make sense to me. I will be the first to admit if I am missing something here...which could very well be the case. Bear with me.

|||

Hi,

SELECT t3.ReqNo, MAX(case t3.seq when 1 then t3.ProcID end)

+ MAX(case t3.seq when 2 then ', ' + t3.ProcID else '' end)

+ MAX(case t3.seq when 3 then ', ' + t3.ProcID else '' end) AS ProcID

FROM ( SELECT ReqNo, ProcID, (SELECT COUNT(*) FROM yourTable AS t2 WHERE t2.ReqNo = t1.ReqNo and t2.ProcID <= t1.ProcID) AS seq

FROM yourTable AS t1

) as t3

GROUP BY t3.ReqNo

Or you can use this for SQL Server 2005:

SELECT t3.ReqNo, t3.[1] + coalesce(', ' + t3.[2], '') + coalesce(', ' + t3.[3], '') AS ProcID

FROM (SELECT ProcID, ReqNo, ROW_NUMBER() OVER(PARTITION BY ReqNo ORDER BY ProcID) AS seq FROM yourTable) as t1

PIVOT (MAX(ProcID) for seq in ([1], [2], [3])) AS t3

Or using CTE in SQL Server 2005:

With MyCTE(ReqNo, ProcID, ProcIDs, myNum) AS

(SELECT a.ReqNo, CONVERT(varchar(50), MIN(a.ProcID)) as col1, CONVERT(varchar(50),(a.ProcID)) as ProcIDs, 1 as myNum

FROM yourTable a GROUP BY a.ReqNo, CONVERT(varchar(50),(a.ProcID))

UNION ALL

SELECT b.ReqNo, CONVERT(varchar(50), b.ProcID), CONVERT(varchar(50), (c.ProcIDs + ',' + b.ProcID)), c.myNum+1 as myNum

FROM yourTable b INNER JOIN MyCTE c ON b.ReqNo=c.ReqNo

WHERE b.ProcID>c.ProcID

)

SELECT a.ReqNo, ProcIDs FROM MyCTE a INNER JOIN (SELECT Max(a1.myNum) as myNumMax, a1.ReqNo FROM MyCTE a1

group by a1.ReqNo) b on a.ReqNo=b.ReqNo AND a.myNum= b.myNumMax ORDER BY a.ReqNo

|||

Sorry that I wasn't very clear.

You would build a User Defined Function that would be specific to the table you are working with and would accept a parameter that would identify the records -- like the customerID in the Northwind case. The function would concatenate the values into the local variable and return that variable. You could then call this function as part of your Select clause.

Performance would likely be a challenge.

Here is a NorthWind example

Drop Function OrderList

go

Create Function OrderList( @.cust varchar(10) )

Returns varchar(1000)

As

Begin

Declare @.result varchar(1000)

Select@.result =

Case When @.result Is Not Null

Then @.result + ', '

Else ''

End + Convert(varchar(10), OrderID )

FromOrders

WhereCustomerId = @.cust

return @.result

End

go

SelectCustomerID,

CompanyName,

dbo.OrderList( CustomerID )

FromCustomers

Monday, March 19, 2012

CR8.5 COM documentation

Hi

I'm a VB and CR newbie.

I have been looking for some documentation about which methods I can use on a crystal report object. I have found at lot of examples using google but almost none of them shows more than just creating the object and open the report.

I would like to send a SQL string to the report and execute it.

I'm using VB5

Where can I find the documentation about this?

cheers
Henryjust do an online search for documentation. i do have it, bt its 5.x MB, so i dont think it'll upload here...i cant remember the exact url i found it...
http://www.businessobjects.com
try that...
Also, try the samples in crystal. it should be in ur Crystal installed folder-->
E:\Crystal\Samples\En\Code\Visual Basic
tons of support!
(I can show u how slove ur prob, bt i only know in vc++)

Sunday, March 11, 2012

CR10 and vb6

Hi to all,
Im still a newbie in cr10, Im making a report from my database using vb6 as it's front end. I have a question regarding filters in CR10 since in data environment as we know we can do this dte1.rsCmd.Filter or
dte1.rsCmd.Requery

How do i do this in CR10? I have a customized filters ( a series of cbo boxes to filter search in database) in my form..

Any help is welcome guys. I really need help

Thanks in advance!Hi again,
I hope i stated my question clear. Im looking for a way to filter my database by using the vb6 cbo boxes and printing it using crystal report 10.. Im using MySQL as my database. Any ebooks or tuts, suggestions are welcome.|||You can use selection formula
CR.selection formula=Column='Some value'"

or

CR.SQLQuery="YourQuery"

Friday, February 24, 2012

Couple of newbie questions

My company is moving to SQL Server soon, and I'm certainly excited about it. Currently we are using a FoxPro based solution which is slow and unreliable.

I'm working on the configuration and have a few questions. We are planning on going with a SQL Server 2000 Standard Edition running on Windows 2000 Server. Some of what Microsoft has said on the web site is very vague concerning the licensing and other maximums.

What is the max memory that you can use with SQL Standard on a Windows 2000 Server (not Advanced)?
If you have more memory in the server than SQL Standard will support, is that OK? For example, can you say, if SQL Standard maxes out at 2 GB, put in 3 GB, and let the OS have the rest? Our database will probably be around 2 GB to start.
Do you need seperate CALs on Windows 2000, or does it work like other apps like Exchange, where you only need the CAL's for the application (or in this case, the Processor licences).
For a 3 processor server, does anyone think that for about 60-70 users that I should go with a CAL or Processor license model?
Should I spring for the 2MB cache on the processor? Planning on a 3 way 700 Mhz Xeon.

Thanks in advance.According to Books Online:

1. What is the max memory that you can use with SQL Standard on a Windows 2000 Server (not Advanced)?

2GB is the maximum that SQL Std can use.

2. If you have more memory in the server than SQL Standard will support, is that OK? For example, can you say, if SQL Standard maxes out at 2 GB, put in 3 GB, and let the OS have the rest? Our database will probably be around 2 GB to start.

If the OS can see the additional RAM, this should be ok. I cannot think how much memory that Win2k std can use.

3. Do you need seperate CALs on Windows 2000, or does it work like other apps like Exchange, where you only need the CAL's for the application (or in this case, the Processor licences).

If you go with per processor licensing of SQL Server, that is all you need.

4. For a 3 processor server, does anyone think that for about 60-70 users that I should go with a CAL or Processor license model?

I found SQL2k Std per processor licensing at ~4500 multiplied by 3 you would be at ~$13,500

I found SQL2k Std 10 client for $2000. If you purchased 7 of these (70 user), you would be at ~$14,000

Right now, the price is about the same. You need to weigh the options, and look toward the future.

What happens if the 3 processor server needs 5 more processors? What happens if you add 40 more users? How will this impact your licenses (and cost)?

5. Should I spring for the 2MB cache on the processor? Planning on a 3 way 700 Mhz Xeon.

I don't know. What is the price difference between 2MB, 1MB, and 'normal' Xeons? We have an 8 way 700 with 2MB cache and things perform quite well. I am sure that we paid a premium for the 2MB chips...I do not know what performance would be like if we had lesser Xeons. Maybe someone else can help out here with some suggestions.

________________
Keith|||2. If you have more memory in the server than SQL Standard will support, is that OK? For example, can you say, if SQL Standard maxes out at 2GB, put in 3 GB, and let the OS have the rest? Our database will probably be around 2 GB to start.

Win2k and NT Standard see 4GIG max which is the memory space directly addressable by a a 32bit operating system.

The 2 gig limit for SQL Server comes from the fact that the NT architecture has a 2gig per process memory limit.

5. Should I spring for the 2MB cache on the processor? Planning on a 3 way 700 Mhz Xeon.

It's hard to say. All of our SQL Servers now have 2MB processor caches. We had one Dell 4-way server with 512K (standard) caches that severely under-performed and was replaced by a Compaq server. But the Compaq server was much more carefully specified so direct comparisons are tough. I know this. I've read of people with performance problems when CPU utilization is at 100% on all 4 processors. On our Compaq servers, when all four processors are at 100%, people can still work. It's a little slower, but almost goes unnoticed except by those that cause the performance spike (period end processes, some custom reports). On the Dell, if all 4 where at 100% no one moved. It would stop everyone in their tracks. Compaq's processor multi-processor architecture probably has something to do with it, but I bet the 2MB cache in each processor has something to do with it also.

If the cost is not unreasonable go for the 2mb cache. We choose slower processors with 2MB cache over a faster processor with 512k or 1mb and haven't looked back.|||Thanks for the repsonse. It will be a few thousand more to upgrade from the 1 MB to 2 MB cache, so it may be difficult to get that passed through our budget.

Out of curiosity, how many users are you guys dealing with? I'll be around 50 concurrent to start, while not expecting that number to go up to more than 75 any time soon (a few years off).|||We have about 60 users hitting the system full time.

Keep in mind, it is not so much the user count, but the way the application and database were created that really affects performance.

________________
Keith|||I don't even know exactly right now. Probably right around 35-60 depending on the time of day. But really it's not the number of users, but the work you expect the server to perform for those users.

Our primary server runs an accounting and distribution package that is heavily optimized for SQL Server and WAN environments relying on Triggers, and stored procedures, and temp tables for reports. (And not measly select * from thistable stored procedures, these are real ones that do real work.) This application works the server. 1MB may be just fine. At the time we specified the hardware we had the choice of 1mb cache and 650Mhz processors or 2mb Cache and 450mhz processors to fit in our price range. We went with the 450mhz processors and 2mb cache. 20

Make sure your application isn't just a fancy data store. We way equipped some hardware we have right now for a mission critical clustered solution. Quad processor Active/Active Compaq machines with 4gig each running SQL Enterprise and NT Enterprise, and we get the app installed on a test machine and I find it was ported from Oracle and AS/400 and is mostly flat files stored in the database. No stored procedures, no referential integrity, hardly even any indexes. The groupresponsible for getting the hardware obviously didn't do their researchfirst. Such a database will hardly be taxing the CPU's and is more likely to have blocking issues than performance issues. I can't wait until we go live...

Couple of Easy Questions, newbie here

I am using Merge Replication for my scenario (POS). I have made the publication (articles are a set of tables in DB ABC) and its subscription (in same DB and diff tables xyz1, xyz2). I have scheduled the push subscription as run continuously between Dates(e.g current date to 3 days ahead, just for testing). Now, Can I change this schedule? How can I edit/remove the artilce from the above publication?
ok i have found the solution for modifying the schedule here How to: Specify Synchronization Schedules (SQL Server Management Studio)
now i am searching for how can i edit publication(article(s))

Tuesday, February 14, 2012

counting DONE and NOT DONE totals (was "Newbie SQL problem")

Hello all,

i'm a newbie in sql, and currently i am facing some problems in sql. My questions is as shown:

I have a table with about 30,000 records, the sample columns of this table is as following:

--------------------
CPN | MPN | Status | Requester | Project_Name | ReceivedDate
--------------------
CN8 CNA1 DONE John SUN 2006/03/01
CN8 CNA2 NOT_DONE John SUN 2006/03/01
BF7 GHE1 DONE Alex MICRO 2006/04/01
BF8 GHE2 DONE Alex MICRO 2006/04/01
BF9 GHE3 NOT_DONE Alex MICRO 2006/04/01
BF0 GHE4 NOT_DONE Alex MICRO 2006/04/01

How can i make it to become:
--------------------
Project_Name | Requester | ReceivedDate | Total_DONE(%) |Total_CPN(%)| DONE | NOT_DONE | Total |

SUN | John | 2006/03/01 | 50% | 100% | 1 | 1 | 2
MICRO| Alex | 2006/04/01 | 50% | 50% | 2 | 2 | 4

Total_DONE(%) is base on how many % of MPN done
Total_CPN(%) is base on how many % of CPN done

For example, in John case, 'CNA1' is done while 'CNA2' is not done, so Total_DONE(%) = 50%, but since both MPN is under same CPN (CN8), so once one of the MPN under this CPN is done, it considered done for Total_CPN(%), so Total_CPN(%) = 100%

Any unclear and doubt please ask me in the thread.
Your advices and help is much appreciated, thanks!Hi edx. What have you tried so far? regards, Fazza|||So far i still cant figure out how to make it done, do u have any idea?|||Here's a hint. Try using a CASE statement to convert your DONE and NOT_DONE to zeros and ones. Then SUM and divide the results to get your percentages.|||Thanks for the reply urquel, ya, it's work by using the 'case' method for Total_DONE(%), but how bout the Total_CPN(%)?Since the Total_CPN have nested group method involved, whereby after group by 'project_name', i need to group again by the CPN, means if one of the MPN under same CPN is DONE, it's considered DONE..

Here is my progress so far:)

----------------------
SELECT project_name , requester, ReceiveDate,
COUNT(CASE WHEN Status = 'DONE' THEN 1 END) * 100 / COUNT(*) as [Total_DONE(%)],
COUNT(CASE WHEN Status = 'DONE' THEN 1 END) as DONE,
COUNT(CASE WHEN Status = 'NOT_DONE' THEN 1 END) as X_DONE,
COUNT(*) as TOTAL,
GROUP BY Project_Name, Requester, ReceivedDate

----------------------

Still having trouble for the CPN(%)...anyone able to help?

Counting all the characters in a column

I'm a SQL newbie...'nuf said.

How do I use a SELECT statement to count all the characters in a given column? I would like to calcuate the total number of characters, the average length, etc.

I can use the LEN function to get a single row like this:

SELECTLEN(NAME1)

FROM INDEXINFO

WHERE NDX = 101

I played around with the COUNT function but that seems to be for counting rows. In VB I would use a FOR EACH clause and keep a running total for the result of the LEN function. Is this a problem better handled using a T-SQL script or can it be done with SELECT statement?

Thanks!

DeBug

Umm...I changed my key words in my Google search and figured out the following:

SELECTSUM(LEN(NAME1))FROM INDEXINFO

WHERE NDX < 100

Ok, this got me the total and I can of course change the WHERE clause but can I also get the AVE at the same time?

|||SELECT SUM(...), AVG(...) FROM INDEXINFO
WHERE NDX < 100|||

There are problems with using LEN(). See this thread for a recent discussion.

You could do the following:

Code Snippet


SELECT
ColumnLength = max( datalength( Name1 )),
ColumnAvg = avg( datalength( Name1 ))
FROM IndexInfo
WHERE NDX = 101

|||Thank you for your reply!|||

Hi Arnie,

You have to take in mind if the column is a unicode one, because for those, every character is 2 bytes.

AMB

|||

Arnie,

Thank you and Phil for helping me with this. Here is the final version:

SELECT

Name1 =sum(datalength( Name1 )),

Name2 =sum(datalength( name2 )),

Name3 =sum(datalength( name3 )),

Name4 =sum(datalength( name4 )),

Date =sum(datalength( date ))

FROM IndexInfo

SELECT

Name1 =avg(datalength( Name1 )),

Name2 =avg(datalength( name2 )),

Name3 =avg(datalength( name3 )),

Name4 =avg(datalength( name4 )),

Date =avg(datalength( date ))

FROM IndexInfo

btw, how do you do that code snippet?|||

I must have trailing spaces somewhere....I used both the len and datalength for the results to text:

Name1 Name2 Name3 Name4 Date

-- -- -- -- --

644653 1047334 59933 69820 888709

(1 row(s) affected)

Name1 Name2 Name3 Name4 Date

-- -- -- -- --

644597 1047332 59933 69820 888709

|||Look for the rows where the LEN() and DATALENGTH() don't match.|||

Great Reminder Alejandro,

DATALENGTH() reports the number of bytes used to store the field value, so when using DATALENGTH() with unicode (nchar(), nvarchar()) datatypes, it is necessary to divide by two if you are handling single byte characters.

|||

I wrapped the blanks in a delimiter to make it show a little better. I now need to review the data entry code to see why it let someone key all those blank spaces Sad

Thanks again to all that helped!

DeBug

SELECT NDX, MY_BLANKS = ('-->' + NAME1 + '<--') FROM INDEXINFO

WHERE DATALENGTH(NAME1) > LEN(NAME1);

NDX MY_BLANKS
-- --
263 --> <--
427 --> <--

(2 row(s) affected)