Tuesday, March 20, 2012
a TSQL question
with the minimum ID.
is there a way to do that in one single Select statement?
Following statement should return the record that has the minimum ID too.
SELECT TOP 200 ID , CREATEDATE
FROM TBL_RECORDS
ORDER BY CREATEDATE DESC
ID column has IDENTITY property and There could be records with negative IDs
which come through synchronization.
Thanks in advance..Well, what do you mean by single select? Union would probably be the best
solution here, why dont you use that?
MC
"prefect" <uykusuz@.uykusuz.com> wrote in message
news:uJjpdS$KGHA.2300@.TK2MSFTNGP15.phx.gbl...
>i want to write a query that will return last created 200 records + record
>with the minimum ID.
> is there a way to do that in one single Select statement?
> Following statement should return the record that has the minimum ID too.
> SELECT TOP 200 ID , CREATEDATE
> FROM TBL_RECORDS
> ORDER BY CREATEDATE DESC
> ID column has IDENTITY property and There could be records with negative
> IDs which come through synchronization.
> Thanks in advance..
>|||Thanks for the answers ,
> Well, what do you mean by single select? Union would probably be the best
> solution here, why dont you use that?
that is what i mean with single select! i plan to use this query in a RDA
application. I am
not sure that RDA's Pull method with "tracking on" option supports the
UNION operator.
Anyway , i will give a try with UNION method,i hope it works :)
Sunday, March 11, 2012
A single group spanning pages...
I basically want to put a
"product XXX continued on next page..." at the bottom of the page if the
group is going to be split. Thanks in advance.
MichaelOn May 18, 10:10 am, Michael C <Micha...@.discussions.microsoft.com>
wrote:
> Is there any way to identify if a group is going to be split between pages?
> I basically want to put a
> "product XXX continued on next page..." at the bottom of the page if the
> group is going to be split. Thanks in advance.
> Michael
As far as I know, there is not; however, you could try using an
expression like follows to group by:
=Ceiling(RowNumber(Nothing)/NumberOfRowsPerPage)
And then set the table/matrix control to start each group on a new
page. Then you could check to determine how many rows are in the
dataset and divide those by NumberOfRowsPerPage to determine
(possibly) the number of pages to put the message on. Hope this helps.
Regards,
Enrique Martinez
Sr. Software Consultant|||http://blogs.msdn.com/chrishays/archive/2006/09/27/ContinuedHeader.aspx
"Michael C" wrote:
> Is there any way to identify if a group is going to be split between pages?
> I basically want to put a
> "product XXX continued on next page..." at the bottom of the page if the
> group is going to be split. Thanks in advance.
> Michael
>
A single computer cannot connect to MSDE
using it no problems. 1 computer cannot connect or even see msde when
enumerating sql servers using sqldmo. This computer can ping the server msde
is on no problem. My thought was to next look at the netlibrary on the
server and on the client and verify both TCP and named pipes setup. Any
further suggestions on how to get this fixed would be appreciated. There is
not a firewall issue.
Thanks
Have you made sure the MDAC on that client is updated?
http://www.microsoft.com/downloads/d...displaylang=en
"Ferde" <Ferde@.discussions.microsoft.com> wrote in message
news:218DE741-04BA-4C0E-B4E3-9CF71CBED37C@.microsoft.com...
> I have an instance of MSDE running that have 3 computers connected to it
and
> using it no problems. 1 computer cannot connect or even see msde when
> enumerating sql servers using sqldmo. This computer can ping the server
msde
> is on no problem. My thought was to next look at the netlibrary on the
> server and on the client and verify both TCP and named pipes setup. Any
> further suggestions on how to get this fixed would be appreciated. There
is
> not a firewall issue.
> Thanks
A single computer cannot connect to MSDE
using it no problems. 1 computer cannot connect or even see msde when
enumerating sql servers using sqldmo. This computer can ping the server msd
e
is on no problem. My thought was to next look at the netlibrary on the
server and on the client and verify both TCP and named pipes setup. Any
further suggestions on how to get this fixed would be appreciated. There is
not a firewall issue.
ThanksHave you made sure the MDAC on that client is updated?
http://www.microsoft.com/downloads/...&displaylang=en
"Ferde" <Ferde@.discussions.microsoft.com> wrote in message
news:218DE741-04BA-4C0E-B4E3-9CF71CBED37C@.microsoft.com...
> I have an instance of MSDE running that have 3 computers connected to it
and
> using it no problems. 1 computer cannot connect or even see msde when
> enumerating sql servers using sqldmo. This computer can ping the server
msde
> is on no problem. My thought was to next look at the netlibrary on the
> server and on the client and verify both TCP and named pipes setup. Any
> further suggestions on how to get this fixed would be appreciated. There
is
> not a firewall issue.
> Thanks
Thursday, March 8, 2012
A simple group by query seems Impossible
FOR EACH fac_id, get the INSPECTION_ID of the earliest start_date:
INSPECTION_ID FAC_ID START_DATE
300720040628004 566 2004-07-07
300720030618002 566 2003-06-25
200820021219011 1436 2002-12-19
300720030206002 1458 2003-02-03
300720030206003 1458 2003-02-05
300720030121002 1480 2003-03-25
300720030121003 1480 2005-02-02
200820031230001 1436 2003-12-30
300720040616006 1566 2004-08-26
300720040616001 1566 2001-08-26
300720040616005 1566 2002-08-26
300720040616002 1566 2003-08-26
300720040616003 1566 2004-08-26
300720040616004 1566 2006-08-26
300720040504004 1569 2004-09-10
DESIRED OUTPUT
INSPECTION_ID FAC_ID START_DATE
300720030618002 566 2003-06-25
200820021219011 1436 2002-12-19
300720030206002 1458 2003-02-03
300720030121002 1480 2003-03-25
200820031230001 1436 2003-12-30
300720040616001 1566 2001-08-26
300720040504004 1569 2004-09-10you have FAC_ID 1436 listed twice with two start dates ... this appears to voilate the ground rules, since there are only 6 distinct FAC_ID values.
Given that, this should work:
-- create the table
create table #temp (
INSPECTION_ID bigint,
FAC_ID int,
START_DATE datetime)
-- populate the table
insert into #temp
select 300720040628004, 566 , '2004-07-07'
UNION
select 300720030618002, 566 , '2003-06-25'
UNION
select 200820021219011, 1436, '2002-12-19'
UNION
select 300720030206002, 1458, '2003-02-03'
UNION
select 300720030206003, 1458, '2003-02-05'
UNION
select 300720030121002, 1480, '2003-03-25'
UNION
select 300720030121003, 1480, '2005-02-02'
UNION
select 200820031230001, 1436, '2003-12-30'
UNION
select 300720040616006, 1566, '2004-08-26'
UNION
select 300720040616001, 1566, '2001-08-26'
UNION
select 300720040616005, 1566, '2002-08-26'
UNION
select 300720040616002, 1566, '2003-08-26'
UNION
select 300720040616003, 1566, '2004-08-26'
UNION
select 300720040616004, 1566, '2006-08-26'
UNION
select 300720040504004, 1569, '2004-09-10'
-- code to select FAC_ID with earliest start date
select t.INSPECTION_ID, t.FAC_ID, t.START_DATE
from #temp t
inner join
(select t2.fac_id, min(t2.start_date) start_date
from #temp t2
group by t2.FAC_ID) t2 on t2.start_date = t.start_date
order by t.fac_id
-- only 6 distinct FAC_ID
select distinct (fac_id)
from #temp
drop table #temp|||I'm simply going to assume that listing TWO 1436 rows in the output was a mistake, if not you're going to have to give a better explaination of what you want - if you want the earliest START_DATE for each FAC_ID, then shouldn't be getting two rows for a single FAC_ID, should you?
Seems to me the problem is how to reterive the INSPECTION_ID from the minimum row without messing up the grouping. I've long wished SQL had the ability to do this...
SELECT ... FROM ... WHERE MIN_OF(column)
but alas, it does not exist natively. Here's a way to do it with a nested query.
SELECT i.*
FROM inspection_table AS i
JOIN (SELECT f=fac_id, s=MIN(start_date)
FROM inspection_table
GROUP BY fac_id) AS q ON i.fac_id = q.f AND i.start_date = q.s
Sunday, February 19, 2012
a query
im trying to build a query...
the target of this query is to show the sales man, and the balance of the salesman of credits of a single plan.
the data is stored in the table as following,
RowID,SalesManID,PlanID,Credit,TransactionType
the balanace of of a salesman of a single plan is
the (sum(credit) where Transactiontype = 0 ) - (sum(credit) where transaction = 1 )
ofcourse for every single plan and sales man...
please if u need any question to have more detials just ask
and thank u for ur careTry this:
|||thank you
SELECT PlanID, SalesManID,
SUM(CASE WHEN Transactiontype=0 THEN credit WHEN Transactiontype=1 THEN -credit ELSE 0.0 END) as Balance
From YourTable
Group By PlanID,SalesManID
its a great way
but mmm
it seems that it is not working
as if all of them has transaction type 0, but in the database there is 0 and 1
so is there anything i can do|||Did you get the minus sign in from of the second condition?
SELECT PlanID, SalesManID,
SUM(CASE WHEN Transactiontype=0 THEN credit WHEN Transactiontype=1 THEN-credit ELSE 0.0 END) as Balance
From YourTable
Group By PlanID,SalesManID
If it still doesn't work then try this as a diagnostic. Just run it in query analyzer to see what is going on. You should just see the unaggregated values with some have positive values corresponding to transactiontype=0 and negative for transactiontype=1.
SELECT PlanID, SalesManID,Transactiontype,
CASE WHEN Transactiontype=0 THEN credit WHEN Transactiontype=1 THEN -credit ELSE 0.0 END as Balance
From YourTable
</code>
a q about counting
New to SQL. Got some questions about it.
Suppose I have two tables. Each of them has a single column, named as
c1. For table T1, I have:
1
1
1
3
3
5
7
9
For table T2, I have:
1
2
3
4
5
1
3
The exercise I want to do is to select the number of occurence in T1
for those elements in T2. For above tables, I want to show:
1 3 ( i.e. "1" is in T2 and shows 3 times in T1)
2 0 (i.e. "2" is in T2 but doesn't show in T1)
3 2 (i.e. "3" is in T2 and show 2 times in T1)
It seems I can't figure out a good way to do this. Any help will be
appreciated.
ThanksDE (ooff@.hotmail.com) writes:
Quote:
Originally Posted by
Suppose I have two tables. Each of them has a single column, named as
c1. For table T1, I have:
1
1
1
3
3
5
7
9
For table T2, I have:
1
2
3
4
5
1
3
The exercise I want to do is to select the number of occurence in T1
for those elements in T2. For above tables, I want to show:
1 3 ( i.e. "1" is in T2 and shows 3 times in T1)
2 0 (i.e. "2" is in T2 but doesn't show in T1)
3 2 (i.e. "3" is in T2 and show 2 times in T1)
SELECT T2.c1, coalesce(COUNT(T1.c1), 0)
FROM T2
LEFT JOIN T1 ON T2.c1 = T1.c1
GROUP BY T2.c1
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||I will give you a different solution for your homework, so you will
still have to think for yourself (which should I take? and why?):
SELECT c1, COUNT(*)
FROM T1
GROUP BY c1
UNION ALL
SELECT DISTINCT c1, 0
FROM T2
WHERE NOT EXISTS (
SELECT *
FROM T1
WHERE T1.c1 = T2.c1
)
ORDER BY c1
HTH,
Gert-Jan
DE wrote:
Quote:
Originally Posted by
>
Hi,
>
New to SQL. Got some questions about it.
>
Suppose I have two tables. Each of them has a single column, named as
c1. For table T1, I have:
1
1
1
3
3
5
7
9
For table T2, I have:
1
2
3
4
5
1
3
The exercise I want to do is to select the number of occurence in T1
for those elements in T2. For above tables, I want to show:
1 3 ( i.e. "1" is in T2 and shows 3 times in T1)
2 0 (i.e. "2" is in T2 but doesn't show in T1)
3 2 (i.e. "3" is in T2 and show 2 times in T1)
>
It seems I can't figure out a good way to do this. Any help will be
appreciated.
>
Thanks
a problem with transactions
i created an explicite transaction which contains 2 update statements
both of these two update statements act on a single table with the name of
"TABLE1".
these table has two column "name varchar(20)" & "account (int)".
this table also has a check constraint on the "account" column which resric
ts
the value of this column to be greater than 100.
here is the table before execting the transaction:
<<
NAME ACCOUNT
' ali ' 140
' nima ' 200
here is the code of my transaction:
<<
begin transaction
update table1 set account=account-50 where name='ali'
update table1 set account=account+50 where name='esi'
commit transaction
and this is table1 after executing the transaction:
<<
NAME ACCOUNT
' ali ' 140
' nima ' 250
if the value of the "account" column be for example " 140" before executing
the
transaction , with executing the transaction a check constraint violation
happens
and the first update statement does not affect on the table but the second
update statement affect permanently on the table.
alltransactions are "ATOMIC" .Either all the satements inside them should
perform or none of them. with executing this transaction the "CONSISTANCY"
feature off transactions is ignored too.(140+200 before execution) &
(140+250 after execution).
wwwwhhhhhhhhhhhhhhyyyyyyyyyyyyyyyy'''The problem is that you don't have any error handling. A constraint
violation does not terminate a batch or a transaction, it only terminates
the offending statement and sets @.@.ERROR. You must always, Always, ALWAYS
check @.@.ERROR after every DML statement (INSERT, UPDATE, DELETE) and every
stored procedure call. It is also possible for a transaction to be rolled
back when an error occurs, but for the batch to continue executing. That
means that statements following an error are no longer protected by a
transaction! Failing to include error handling defeats the purpose of
transactions.
There is a statement, SET XACT_ABORT, which is supposed to cause every
transaction to be rolled back and terminated whenever an error occurs, but I
don't rely on it. I use @.@.ERROR and @.@.ROWCOUNT because that gives me more
control over the error recovery process.
"pooyan_pdm" <pooyan_pdm@.discussions.microsoft.com> wrote in message
news:3C02A013-830C-42C7-AB75-C53348528FDA@.microsoft.com...
> hi
> i created an explicite transaction which contains 2 update statements
> both of these two update statements act on a single table with the name of
> "TABLE1".
> these table has two column "name varchar(20)" & "account (int)".
> this table also has a check constraint on the "account" column which
resricts
> the value of this column to be greater than 100.
> here is the table before execting the transaction:
> <<
> NAME ACCOUNT
>
> ' ali ' 140
> ' nima ' 200
>
>
>
> here is the code of my transaction:
> <<
> begin transaction
> update table1 set account=account-50 where name='ali'
> update table1 set account=account+50 where name='esi'
> commit transaction
>
>
> and this is table1 after executing the transaction:
> <<
> NAME ACCOUNT
>
> ' ali ' 140
> ' nima ' 250
>
>
>
> if the value of the "account" column be for example " 140" before
executing
> the
> transaction , with executing the transaction a check constraint
violation
> happens
> and the first update statement does not affect on the table but the
second
> update statement affect permanently on the table.
> alltransactions are "ATOMIC" .Either all the satements inside them should
> perform or none of them. with executing this transaction the
"CONSISTANCY"
> feature off transactions is ignored too.(140+200 before execution) &
> (140+250 after execution).
>
> wwwwhhhhhhhhhhhhhhyyyyyyyyyyyyyyyy'''
>|||Brian
But there are some kind of error that terminate batches and @.@.ERROR does not
catch them
"Brian Selzer" <brian@.selzer-software.com> wrote in message
news:%23wF7tWdkFHA.2916@.TK2MSFTNGP14.phx.gbl...
> The problem is that you don't have any error handling. A constraint
> violation does not terminate a batch or a transaction, it only terminates
> the offending statement and sets @.@.ERROR. You must always, Always, ALWAYS
> check @.@.ERROR after every DML statement (INSERT, UPDATE, DELETE) and every
> stored procedure call. It is also possible for a transaction to be rolled
> back when an error occurs, but for the batch to continue executing. That
> means that statements following an error are no longer protected by a
> transaction! Failing to include error handling defeats the purpose of
> transactions.
> There is a statement, SET XACT_ABORT, which is supposed to cause every
> transaction to be rolled back and terminated whenever an error occurs, but
> I
> don't rely on it. I use @.@.ERROR and @.@.ROWCOUNT because that gives me more
> control over the error recovery process.
> "pooyan_pdm" <pooyan_pdm@.discussions.microsoft.com> wrote in message
> news:3C02A013-830C-42C7-AB75-C53348528FDA@.microsoft.com...
> resricts
> executing
> violation
> second
> "CONSISTANCY"
>|||You're absolutely right. It's important to keep that in mind. It's also
important to understand that the nature of error and transaction management
change within triggers. However, neither of these supplant the requirement
to manage errors that do not terminate a batch.
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:uylJrjekFHA.3936@.TK2MSFTNGP10.phx.gbl...
> Brian
> But there are some kind of error that terminate batches and @.@.ERROR does
not
> catch them
>
>
> "Brian Selzer" <brian@.selzer-software.com> wrote in message
> news:%23wF7tWdkFHA.2916@.TK2MSFTNGP14.phx.gbl...
terminates
ALWAYS
every
rolled
That
but
more
should
>
Thursday, February 16, 2012
a Null check for a single record
I have a table that may contains Null filled columns.
I have a SP that do Update and insert.
How can i do a null check to avoid trying to update null fields?
let say Type_1 column is null, I need a condition that will do a quick check (single record is sufficient) to see that it has null value and to skip the update command:
CREATE PROCEDURE Lan_Insert_Data_Type
as
DECLARE @.Single_Rec int
Begin
-- ??? HERE comes the condition --
UPDATE Data_type
SET Product_Num = LanTable.ProductNum,
Data =LanTable.Type_1
FROM Data_Type p
JOIN LanTable
ON Lantable.ProductNum = p.Product_num where p.Data_Type =1
Thanks
Yossi--Try This
CASE When Type_1 IS NULL
Then
--Do nothing
SET @.Single_Rec = @.Single_Rec
ELSE
UPDATE Data_type
SET Product_Num = LanTable.ProductNum,
Data =LanTable.Type_1
FROM Data_Type p
JOIN LanTable
ON Lantable.ProductNum = p.Product_num where p.Data_Type =1
END|||Originally posted by eschapir
--Try This
CASE When Type_1 IS NULL
Then
--Do nothing
SET @.Single_Rec = @.Single_Rec
ELSE
UPDATE Data_type
SET Product_Num = LanTable.ProductNum,
Data =LanTable.Type_1
FROM Data_Type p
JOIN LanTable
ON Lantable.ProductNum = p.Product_num where p.Data_Type =1
END
Thaks for the reply
but its not working,
maybe i missed some thing|||/* something like this ? */
if not exists(
select *
FROM Data_Type p
JOIN LanTable
ON Lantable.ProductNum = p.Product_num
where p.Data_Type=1 and LanTable.Type_1 is null
)
UPDATE Data_type
SET Product_Num = LanTable.ProductNum,
Data =LanTable.Type_1
FROM Data_Type p
JOIN LanTable
ON Lantable.ProductNum = p.Product_num where p.Data_Type =1|||Originally posted by ispaleny
/* something like this ? */
if not exists(
select *
FROM Data_Type p
JOIN LanTable
ON Lantable.ProductNum = p.Product_num
where p.Data_Type=1 and LanTable.Type_1 is null
)
UPDATE Data_type
SET Product_Num = LanTable.ProductNum,
Data =LanTable.Type_1
FROM Data_Type p
JOIN LanTable
ON Lantable.ProductNum = p.Product_num where p.Data_Type =1
Works great...
Thanks mate