|
For SQL-Server:
select employee.name, designation.name
from employee
left outer join designation
on designation.designation_id = employee.designation_id
where employee.code = IsNull(@EmployeeCode, employee.code)
and IsNull(designation.name, '') = IsNull(@DesignationName, designation.name)
order by employee.name The outer join links the two tables together - even if there is no designation.
The IsNull bit allows you to optionally pass in a NULL or a proper value for the employee code and the designation.
Hope that helps.
Andy
|
|
|
|
|
I have these (amongst others) tables in my database:
Tasks and Successors
Tasks hold details about task, and Successors has two taskIDs as forgien keys (jointly primary key) to hold data on the relationship between task.
Ie Task A has Tasks B and C as successors.
Task Table has three records for the task. The Successor table has TaskA/B and TaskA/C primary key feilds.
I'm using this on a C# Clone of MS Project. So I'm reading this data in and converting them into classes, in a tree structure so each Task Object has a List<task> of successors.
I can read this data in fine by just parsing the data in the tables. The problem comes when I want to put newly created data into the database, which is stored in this tree of instances of Task.
[b]If I put add a Task record into the database, how do I get the primary key of the one I've just put in so that I can create the records in the successor table to link them?[/b]
In my example I will probably have to added the tasks 1st so that when I add the successor records, the tasks actually exists. The algorithim for this will be quite simple if I was able to get the primary keys when I adding the tasks.
Anyone have any idea on how to do this?
Thanks for any help,
Ian.
|
|
|
|
|
If you are using SQL Server, use SELECT ... = SCOPE_IDENTITY() after your insert statement.
|
|
|
|
|
|
Here is the table
Project col1 col2 col3 col4
A 151,46 80 0,29 2
A 529,14 83 0,29 1
B 3391,3 80 0 2
B 3706,2 75 0,06 1
C 8685,3 78 2,41 1
C 9005,2 71 0 2
How do I get the max value for col1 for each project and its other values?
ie. project a's max value is 529,14. its others value is 80, 0.29 and 2
Project col1 col2 col3 col4
A 529,14 83 0,29 1
B 3706,2 75 0,06 1
C 9005,2 71 0 2
I have this query, but it results everything, not only the max value for each project
select project, max(col1), col2, col3, col4 from out_pumptable group by project, col2, col3, col4
|
|
|
|
|
Try
SELECT g.Maximum, * from Projects, (SELECT MAX(col1) as Maximum FROM projects GROUP BY project)g WHERE MAX(col1) = g.Maximum
|
|
|
|
|
it looks good, but the column project you have after group by isnt used anywhere. is it something missing before group by?
never mind about that.
I have tried the query and it results a little too much. it seems it loops for each max value and result for all row
|
|
|
|
|
You're right - it has to be like that:
SELECT g.Maximum, * from Projects, (SELECT MAX(col1) as Maximum, project FROM projects GROUP BY project)g WHERE MAX(col1) = g.Maximum
|
|
|
|
|
It still doesnt work.
It says an aggregate cant be in WHERE clause
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Tried with Having and it gives a lot other errors
|
|
|
|
|
Sorry my fault, i copied the wrong string:
This should be the right thing:
SELECT g.Maximum, * from Projects, (SELECT MAX(col1) as Maximum, project FROM projects GROUP BY project)g WHERE col1 = g.Maximum <br />
|
|
|
|
|
its working , just one more thing >.<<br mode="hold" />
some rows in col1 has same value, is it possible to just have one of the values, just take from one row?
|
|
|
|
|
Add a top 1 for example to the first select-statement.
|
|
|
|
|
top 1 seems not be working good for this
I get this result, (partially), from the query you gave me
3320,9 P110101
3320,9 P110101
3220 P110102
3220 P110102
529,14 P110301
3706,2 P111501
P110102 got two rows, how do i get one of it only?
|
|
|
|
|
Add a Group by -statement to the very end of the whole query.
|
|
|
|
|
it feels like im getting annoying now
group by statement might work if i have an aggregate statement somewhere, now there isnt i cant do the group by
i am trying distinct now, but i get incorrect syntax
SELECT g.Maximum, distinct posnumber from out_pumptable, (SELECT MAX(flow_lpm) as Maximum, posnumber FROM out_pumptable GROUP BY posnumber)g WHERE flow_lpm = g.Maximum
this is actual code, posnumber is project
|
|
|
|
|
This works for me:
SELECT g.Maximum, out_pumptable.posnumber from out_pumptable, (SELECT MAX(flow_lpm) as Maximum, posnumber FROM out_pumptable GROUP BY posnumber)g WHERE flow_lpm = g.Maximum group by g.Maximum, out_pumptable.posnumber
|
|
|
|
|
Thanks a lot and thanks for your time. I appreciate this
|
|
|
|
|
I believe you need to join the table to a subset of itself.
You create a subquery that gives you the values of project and the highest col1 and attribute that to a temptable. You then join you orignal table to your temp table on the values of col1. This should do the trick!
SELECT PT.* FROM out_pumptable PT join
(select project, max(col1) as MaxVal from out_pumptable group by project) TEMPTABLE1 on TEMPTABLE1.MaxVal = PT.col1
|
|
|
|
|
hi all.
i have a stored procedure that get 2 parameter.
i want to select a special filed from my store procedure.
can i call my procedure from a function and how i should do it.
(i need some code).
sepel
|
|
|
|
|
I got a "Database Timeout" error.
I m using an MFC application with MSDE database.
What can be the reason(s) for this??
|
|
|
|
|
An operation couldn't be completed in the database in the timescale expected. Simplistically you can think of there being two potential timeouts - one when you can't connect to the database in a reasonable amount of time (known as a connection timeout) and the other when you can't complete a database operation in a reasonable amount of time (known as a command timeout).
|
|
|
|
|
But why should a database transfer take so long when transferring on a local database????
I mean what are the possibilities????
|
|
|
|
|
There are all sorts of possibilities, from slow rebuilding of indexes (every time you do an insert/update/delete) indexes are reevaluated to single action updates that should be performed as set updates.
You need to use the SQL Profiler to see what is happening on this database.
|
|
|
|
|
Pete,
there is another problem that i face quite often.
i transfer like 20-30 rows to the database it happens fast.
But as the database size increases(1500 rows or so), insertion of 20 rows takes 20 seconds!!!!!
my program is like :
for(i=0;i<=row_count;i++)<br />
{<br />
m_pSet->AddNew();<br />
m_pSet->Update();<br />
m_pSet->Requery();<br />
}
|
|
|
|
|
yashveer wrote: m_pSet->Requery();
Is this statement absolutely necessary?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|