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

Wednesday, March 28, 2012

Max function

Hi,

The question is about the way max function works in SQL. I have to implement a database design where in i have to write a stored procedure which will perform an insert in the table/tables. This table/tables will in the long run have a large number of records, i have been instructed to create another table (lets call it tableA) which will have the primary keys of all the other tables and their respective maximum values being used. While performing an insert into one of these tables, i will have to probe the tableA and find out the currently used id for that table ( in which i have to insert a new row) and use the value for insertion.

According to me, we can use a max clause in the stored procedure and achieve the following or use an Indentity column to do the same.

I will need insights into how max function works when it tells me the number of records (does it traverse thru the entire table or has the number of rows stored somewhere? I just need the number of rows in the table and nothing else but would like to know the max function working)
Also how will identity column perform under these situations.

Any information required, i will be glad to give.

VihangJust use the identity field, it's what it's designed for. If you need a table which reflects the maximum id field in each for some reason (but hopefully you don't any more), then use a view which queries the max(id) field from each table, and a 'union all' to show the lot. Something like this:

create view maxids as
select 'table1' as tablename, max(id) as maxid
from table1
union all
select 'table2' as tablename, max(id) as maxid

from table2
union all
...etc

I hope this helps...

Rob

--
Rob Farley
President - Adelaide SQL Server User Group
rob_farley@.hotmail.com (Email &

Msgr)
http://msmvps.com/blogs/robfarley|||Hi Rob,

Thank you for your prompt input. I appreciate it, I have understood what you have suggested.

I need to understand how does the max function work? does it go thru all the rows to calculate the maximum of a column?.

Regards,
Vihang|||Not when there's a useful index it can use. As these 'id' fields will be your primary keys (if not, they should be!), then the system can very easily find out what the maximum value is. Being identity columns, the system is designed to be able to very quickly report what the largest value is.|||Hi Rob,

Thanks for your insights, I now know how it really works.

Regards,
Vihang

Monday, March 26, 2012

Maually update gridview

I want to write the results of a checkbox to my database, but I am stuck on the syntax?

Here's what I have so far, could somebody pleae help., my aim is to not show a line when checkbox ticked, not to actually delete the record from the database

Hi, The problem that I have now is that yes I want to delete from gridview, but I still want the old record to be in database ,not deleted. Does anybody know how I can evaluate my checkbox, and update to the database the results of the checkbox,I am not sure of the update syntax?

string strsql ="update * from jobs order by ID asc";

SqlConnection conn =newSqlConnection(ConnectionString);

SqlDataAdapter ad =newSqlDataAdapter(strsql, conn);

DataSet ds =newDataSet();ad.Fill(ds,"TASKS");

GridView1.DataSource = ds;

GridView1.DataBind();

got stuck here, basically I cant work out how to evauate the results of a checkbox, and write back to the database, can anybody help?

The update syntax should be

Update JOBS Set Column_Name = Value where ID= ID_Value

COLUMN_NAME is the Name of Column you want to update

Value is the new value which you want to go in database

ID is the Column for which Id user Selected

ID_Value is the value of ID

Let me know if this helped you.

Monday, March 19, 2012

matrix or crosstab query

Hi friends,

I need to write a crosstab query in oracle like we do in Access. Here are my sample tables.

tblCat
---
sno (primary key)
cat_id

tblDistribution
------
area_id
dist_id (primary key)
dist_name

tblConsumer
-----
serviceno (primary key)
sno (references sno(tblCat))
area_id

I need the display as follows:

categories
----
dist_id & dist_name 1 2 3 4 5 ... (cat_ids...)
-------------------
id, name concatenated count(serviceno).....

ie., I want the No.of service for each category Id and for dist_id and dist_name.... The cat_id should not be duplicated.

Plz help me out of thios query as I need it badly. I've done it using crosstab query wizard in Access. here is the query

TRANSFORM Count(tb_consumer.Service_No) AS CountOfService_No
SELECT tb_distribution.Dist_Id + " - " +tb_distribution.Dist_Name AS Id_Name
FROM (tb_category INNER JOIN tb_consumer ON tb_category.SNO = tb_consumer.SNO) INNER JOIN tb_distribution ON tb_consumer.Area_Id = tb_distribution.Area_Id
GROUP BY tb_distribution.Dist_Id, tb_distribution.Dist_Name
PIVOT tb_category.Cat_Id;

If any one can, plz tell me how to write or convert the same to ORACLE or SQL Server.

Thanks in advance...!The process is very simple.

You need to create a pivot table :

field1 will appear as row header
field2 will appear as column header

Select field1, field2 into pivot from mytable where ....

This gives you a table with all the data you need to work with.

Now ...

Select field1, count(field2) as totalfield2,
sum(case field2 when 'xxxx' then 1 else 0) as colhdr1,
sum(case field2 when 'yyyy' then 1 else 0) as colhdr2,
sum(case field3 when 'zzzz' then 1 else 0) as colhdr3
from pivot
group by field1
order by field1

This should produce the results much like the Access CrossTab query. You can refine it to meet your requirements. I did not fully understand what you were looking for, but hopefully this will help.

you need to recreate the pivot table every time you run!

Friday, March 9, 2012

Matrix calculation for percent

How do I write expression to calculate a value to show percentage of the
subtotal value in a Matrix?
I have already tried =Fields!name.value/sum(Fields!name.value) and this
does not work
Example:
Matrix has Coulmn group Matrix_col and a Row group Matrix_Row and I would
like to show the percentage of the subtotal for Matrix_Row in a column next
to Matrix Col.
Matrix_Col Percent
Matrix_Row
value 1 %25
value 2 %50
value 1 %25
Total 4Create a column, and set your expression to Fields!Name.Value /
First(Fields!Amount.Value, "MatrixColumnGroupName") - assuming that your
first row is a total.
If it's not, you might be able to use SUM if you add the Matrix column group
name = Fields!Name.Value / SUM(Fields!Amount.Value, "MatrixColumnGroupName")
Kaisa M. Lindahl
"Curtis" <gilbertson.curtis@.leg.wa.gov> wrote in message
news:uB5u4igFGHA.1028@.TK2MSFTNGP11.phx.gbl...
> How do I write expression to calculate a value to show percentage of the
> subtotal value in a Matrix?
> I have already tried =Fields!name.value/sum(Fields!name.value) and this
> does not work
> Example:
> Matrix has Coulmn group Matrix_col and a Row group Matrix_Row and I would
> like to show the percentage of the subtotal for Matrix_Row in a column
> next
> to Matrix Col.
> Matrix_Col Percent
> Matrix_Row
> value 1 %25
> value 2 %50
> value 1 %25
>
> Total 4
>|||Thank you very much for your answer, I've been looking for a solution for
months.
"Kaisa M. Lindahl" wrote:
> Create a column, and set your expression to Fields!Name.Value /
> First(Fields!Amount.Value, "MatrixColumnGroupName") - assuming that your
> first row is a total.
> If it's not, you might be able to use SUM if you add the Matrix column group
> name = Fields!Name.Value / SUM(Fields!Amount.Value, "MatrixColumnGroupName")
> Kaisa M. Lindahl
> "Curtis" <gilbertson.curtis@.leg.wa.gov> wrote in message
> news:uB5u4igFGHA.1028@.TK2MSFTNGP11.phx.gbl...
> > How do I write expression to calculate a value to show percentage of the
> > subtotal value in a Matrix?
> >
> > I have already tried =Fields!name.value/sum(Fields!name.value) and this
> > does not work
> >
> > Example:
> > Matrix has Coulmn group Matrix_col and a Row group Matrix_Row and I would
> > like to show the percentage of the subtotal for Matrix_Row in a column
> > next
> > to Matrix Col.
> >
> > Matrix_Col Percent
> >
> > Matrix_Row
> >
> > value 1 %25
> >
> > value 2 %50
> >
> > value 1 %25
> >
> >
> >
> > Total 4
> >
> >
>
>

Wednesday, March 7, 2012

Matrix - Calculate percentage column

How do I write expression to calculate a value to show percentage of the
subtotal value in a Matrix? example:
Matrix has Coulmn group Matrix_col and a Row group Matrix_Row and I would
like to show the percentage of the subtotal for Matrix_Row in a column next
to Matrix Col.
Matrix_Col Percent
Matrix_Row
value 1 %25
value 2 %50
value 1 %25
Total 4I would try this in the third column
Field!Name.Value/ SUM(Field!Name.Value)
I'd also give this third column a percentage format
Med bouchenafa
"Curtis" <gilbertson.curtis@.leg.wa.gov> a écrit dans le message de news:
%23tJnBVXFGHA.2320@.TK2MSFTNGP11.phx.gbl...
> How do I write expression to calculate a value to show percentage of the
> subtotal value in a Matrix? example:
> Matrix has Coulmn group Matrix_col and a Row group Matrix_Row and I would
> like to show the percentage of the subtotal for Matrix_Row in a column
> next to Matrix Col.
>
> Matrix_Col Percent
> Matrix_Row
> value 1 %25
> value 2 %50
> value 1 %25
>
> Total 4
>|||Med, Thanks for your reply. I have tried this already and it does not work.
I tried it in the third column as well and it still does not work. Any
other ideas?
"Med Bouchenafa" <com.hotmail@.bouchenafa> wrote in message
news:uuhpEWfFGHA.516@.TK2MSFTNGP15.phx.gbl...
>I would try this in the third column
> Field!Name.Value/ SUM(Field!Name.Value)
> I'd also give this third column a percentage format
> Med bouchenafa
>
> "Curtis" <gilbertson.curtis@.leg.wa.gov> a écrit dans le message de news:
> %23tJnBVXFGHA.2320@.TK2MSFTNGP11.phx.gbl...
>> How do I write expression to calculate a value to show percentage of the
>> subtotal value in a Matrix? example:
>> Matrix has Coulmn group Matrix_col and a Row group Matrix_Row and I
>> would like to show the percentage of the subtotal for Matrix_Row in a
>> column next to Matrix Col.
>>
>> Matrix_Col Percent
>> Matrix_Row
>> value 1 %25
>> value 2 %50
>> value 1 %25
>>
>> Total 4
>>
>

Saturday, February 25, 2012

matching users profiles using sql

Hi folks

I have a section in my networking site where people can write things about themselves, such as likes dislikes, hobbies etc for their profile.

I am looking to provide a very basic profile matching service, and have done a bit of searching to find out what the best way to approach this feature is. I have looked at using the sql LIKE clause, but since the profile information can be any length, there seem to be too many variables to account for. I have also looked at identifying keywords in profile blurbs to do the matching too, but I am struggling to see how this could be done with relative ease.

My reason for posting this is simply to get some experienced knowledge about where to start the problem, of even some resources to look at that I may not have found yet.

If anyone could give me some advice on where to start i would be very gratefulTongue Tied

I am develping in vb, with an sql server 2005 back-end

Hello my friend,

You are right about the SQL LIKE clause not being so useful when dealing with large amounts of text. I have worked on something similar before. The best thing to do is to have as many closed questions as possible regarding the profiles. And when they want to be more specific, add sub categories, and sub sub categories and so on. Then the SQL is easy. Most of the big guys do this. If you ever try and create a new hotmail account for example, they throw loads of checkboxes at you and throw the appropriate adverts at you every so often.

Kind regards

Scotty

|||

Hi Scotty

Thanks for the advice. I'm going to begin the development of this feature very soon so i will document what i have come up with here.