|
SELECT
b.RowNumber,
iSrno,
vUserCode,
dLoggedDate
FROM (
SELECT
iSrno,
vUserCode,
dLoggedDate,
ROW_NUMBER() OVER ( PARTITION BY vUserCode ORDER BY vUserCode DESC ) 'RowNumber'
FROM HOUserLog) b
WHERE RowNumber = 1
/*
Here HOUserLog is the table from where the Group By MAX needs to be pick.
ROw_Number() OVER ( PARTITION BY .... ) will generate the RowNumber according to the Group By
Column you provided in ORDER BY ..... DESC Clause.
Now pick the values for every RowNumber Column = 1 will get you the solution.
MARK AS ANSWER IF WORKS
- Happy Coding -
Vishal Vashishta
|
|
|
|
|
After running a report in SSRS 2005 I am trying to export it into excel and I am getting an error
"Index is out of range. Must be non-negative and less than the size of the collection. Parameter name:index"
When I remove the "Page break at start" or the "Page break at end" grouping property, it fixes the problem, but I need this property on because that is how the report gets seperated onto seperate Excel worksheets by the groups I have set up. Any suggestions???
|
|
|
|
|
Hi All,
I have same table structure in Mysql and sqlserver.
If there is change in Mysql table, then sqlserver table should get updated.
Please suggest proper way to do.
We dont have SSIS we only use ssms2005 express.
Ramkumar
("When you build bridges you can keep crossing them. ")
http://ramkumarishere.blogspot.com
|
|
|
|
|
Ramkumar_S wrote: Please suggest proper way to do.
Sql Express doesn't offer this functionality out of the box. You'd have several options;
- Add the MySql server as a linked server and sync using triggers
- Write an app to do the synchronization, using the Sync Framework
- Upgrade Sql Server
Bastard Programmer from Hell
|
|
|
|
|
I suspect the solution would require that you first analyze what happens if there is an error.
For example you update the MySQL table and at that moment the SQL Server database is down so of course the corresponding table cannot be updated then - so what do you expect to happen then?
Additionally how soon does the data need to be in the SQL Server database?
|
|
|
|
|
I have a requirement as in below
DECLARE @TEST TABLE (X VARCHAR(10),Y VARCHAR(10))
insert into @TEST values ('A','B'),('C','D'),('E','F'),('B','A'),('D','C'),('F','E')
SELECT A.* FROM @TEST A, @TEST B WHERE A.X = B.y
gives
X Y
A B
C D
E F
B A
D C
F E
But I have to get only the combination (AB or BA) ,(CD or DC),(EF or FE) in my select statement. In my sql we have some thing called DistinctRow.. What is it in SQL ..Pelase help ..urgent..
|
|
|
|
|
Try using the Distinct Keyword
SELECT Distinct(A.x), A.Y FROM @TEST A, @TEST B WHERE A.X = B.y
|
|
|
|
|
here it is
DECLARE @TEST TABLE (X VARCHAR(10),Y VARCHAR(10))
insert into @TEST values ('A','B')
insert into @TEST values ('C','D')
insert into @TEST values ('E','F')
insert into @TEST values ('B','A')
insert into @TEST values ('D','C')
insert into @TEST values ('F','E')
SELECT A.X
,(
select top 1 b.y from @test as b where b.y > a.x and b.x<a.y
) as subY
FROM @TEST A
where
(
select top 1 b.y from @test as b where b.y > a.x and b.x<a.y
) is not null
order by a.x
I Love T-SQL
"VB.NET is developed with C#.NET"
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
Excellent .. thanks for the reply..
|
|
|
|
|
I was looking for some key word like distinctrow in MYSQL.. dont we have any thing like that in SQL.
|
|
|
|
|
Finally SELECT X,Y FROM @TEST WHERE X
|
|
|
|
|
There exists DISTINCT option to retrieve non-repeated values, but your case was different from using DISTINCT.
Glad to help you.
I Love T-SQL
"VB.NET is developed with C#.NET"
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
I have a table called tbloffence where we can see list of offence which workers have done in worksite.so now i want to display the sumtotal of their offence in .aspx page based on workes userid,but the condition is like this:
a) offence since last login:
b)offence in last 1 month.
i want to write a sql query based on this condition...how can i track"offence since last login"in sql server 2005
|
|
|
|
|
This is not an specific question. To get better answer, I would suggest you to generalize the question and provide more information.
Anyway
This is a very simple problem if I did understand properly.
All you need is to record log in time. that is everytime user log in the user.
And you would have to work in two different Query or may be in Procedeure, where you would send the user_id and procedure will return the result.
|
|
|
|
|
Actually i want to workin 3 tables
1)tbloffence : where we will get the sum(offence) here i track maincontractorID and sum(offence) query will be like this :
"SELECT sum(o.OffenceID) as OffenceNumber,u.UserID
from tblOffence o
inner join tblSubContractor ts on
o.MainContractorID=ts.SubContractorID"
from here i will chk the tblUSER with these subcontractorID :
"inner join tblUser u on
u.SubContractorID=ts.SubContractorID" .
and my doubt is that upto here i will get the total sum of offence and appropriate userid.,for eg
UserID : 520 TotalOffence:6000
but there is another table called tblAuditTrailSession where we will get logondate and userid which is tracking here..so i want to display offence based on userlogin(userid) ..since last login. i want a help in query format..Hope this is a generalized format question.
modified 19-Apr-12 4:26am.
|
|
|
|
|
I’ve got a big problem. Could anyone give me a hand?
My first aim(not the final goal) is:fliter the duplicate data with query statment( select not delete), that is, the various entries of EndTime and Qos for a same ServiceID due to the repeated feedback. The rule is to only keep the records with the lowest Qos for a same ServiceID. If there are several records having the lowest Qos, then only keep whatever one of these records. The client accepts and prefers to this scheme. In this example, for the ID=6,7, and 8, just keep 6 or 7. And I have got answer of this aim here (thanks for TenmanS14 ):
SELECT MIN(Qos), serviceid, id, TCID, EndTime from service
GROUP BY serviceid
aim2: Qos=1 represents the Satisfied Service. The monthly total Satisfied Services achieved by each technical support engineer is referred to as “Personal Total Satisfied Services this Month”. Those technical support engineers whose “Personal Total Satisfied Service this Month” ranking among the top 2 in that month are referred to as the “Top 2 since this Month”. If they are outstanding enough to be entitled to the “Top 2 since this Month” every month, then they can be referred to as “Outstanding of Class 1”. All in all, this step is to compute the “Outstanding of Class 1”. In this example, “Top 2 since this Month” for January is Andrew and Jacob, and that for February are Andrew, Dlyan, and Jacob. Therefore, the honor title of “Outstanding of Class 1” is awarded to Andrew and Jacob.
please help me with aim2 (finally I should reach the aim 4, the final goal), result of aim 2 should be
TCID
Andrew
Jacob
explain:There are some technical service data (ServiceID, TCID, EndTime, and QoS) in a whole year, and the field ID is a unique primary key because there are some duplicates.
some data:
ServiceID ID TCID EndTime Qos
2000 2 Jacob 2011/1/1 2
2000 3 Jacob 2011/1/1 2
2001 4 Jacob 2011/1/1 2
2002 5 Jacob 2011/2/3 1
2003 6 Tyler 2011/1/4 1
Data Structure:
ID: Unique primary key of record
ServiceID: ID of a certain service
TCID: ID of a technical support engineer
EndTime: Ending Time of aservice
Qos:Quality of service (1 Satisfied; 2 Average; 3Unsatisfactory/Dissatisfied).
DDL and insert SQL(mysql5):
CREATE TABLE `service` (
`ServiceID` INTEGER(11) NOT NULL,
`ID` INTEGER(11) NOT NULL ,
`TCID` VARCHAR(40) NOT NULL,
`EndTime` DATE NOT NULL,
`Qos` CHAR(1) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `ID` (`ID`)
);
COMMIT;
INSERT INTO `service` (`ServiceID`, `ID`, `TCID`, `EndTime`, `Qos`) VALUES
(2004, 9, 'Jacob', '2011-02-04', '1'),
(2000, 2, 'Jacob', '2011-01-01', '2'),
(2000, 3, 'Jacob', '2011-01-01', '2'),
(2001, 4, 'Jacob', '2011-01-01', '2'),
(2002, 5, 'Jacob', '2011-02-03', '1'),
(2003, 6, 'Tyler', '2011-01-04', '1'),
(2003, 7, 'Tyler', '2011-01-04', '1'),
(2003, 8, 'Tyler', '2011-01-03', '2'),
(2005, 10, 'Jacob', '2011-02-05', '1'),
(2006, 11, 'Jacob', '2011-02-04', '2'),
(2007, 12, 'Jacob', '2011-01-08', '1'),
(2008, 13, 'Tyler', '2011-02-06', '1'),
(2009, 14, 'Dylan', '2011-02-08', '1'),
(2010, 15, 'Dylan', '2011-02-09', '1'),
(2014, 16, 'Andrew', '2011-01-01', '1'),
(2013, 17, 'Andrew', '2011-01-01', '1'),
(2012, 18, 'Andrew', '2011-02-19', '1'),
(2011, 19, 'Andrew', '2011-02-02', '1'),
(2015, 20, 'Andrew', '2011-02-01', '1'),
(2016, 21, 'Andrew', '2011-01-19', '1'),
(2017, 22, 'Jacob', '2011-01-01', '1'),
(2018, 23, 'Dylan', '2011-02-03', '1'),
(2019, 24, 'Dylan', '2011-01-09', '1'),
(2020, 25, 'Dylan', '2011-01-01', '1'),
(2021, 26, 'Andrew', '2011-01-03', '1'),
(2021, 27, 'Dylan', '2011-01-11', '1'),
(2022, 28, 'Jacob', '2011-01-09', '1'),
(2023, 29, 'Tyler', '2011-01-19', '1'),
(2024, 30, 'Andrew', '2011-02-01', '1'),
(2025, 31, 'Dylan', '2011-02-03', '1'),
(2026, 32, 'Jacob', '2011-02-04', '1'),
(2027, 33, 'Tyler', '2011-02-09', '1'),
(2028, 34, 'Daniel', '2011-01-06', '1'),
(2029, 35, 'Daniel', '2011-02-01', '1');
COMMIT;
Almost forget to say that, just a reminder, the client only accept the SQL instead of stored procedure in database to implement it. And I only need query statement because customer did not allow us to write database.
this is my first 2 aims, there are 2 more, I am intended to complete all these steps one after another. Could anyone help achieve the first goal? I know it is quite complicated and many thanks to you in advance.
aim3:Then, to compute the “Outstanding of Class 2” (the engineers of “Top 2 since this Month” are not included) equals to compute those ranking the third and the forth places. In this example, the “Outstanding of Class 2” is the Tyler.
aim4:The final goal is to combine the “Outstanding of Class 1” with “Outstanding of Class 2”. The result will be ultimately transferred to report for rendering. My dataSet is just like:
TCID level
Andrew top2
Jacob top2
Tyler top4
modified 18-Apr-12 5:05am.
|
|
|
|
|
not sure about looping in MySQL, believe its tricky, so I'd take care of that in by building a recordset in your program code, first your gonna need each distinct ServiceID...
SELECT DISTINCT serviceid from service
put the results of that into an array..
then your gonna have to loop through each one with something like...
SELECT *
FROM `service`
WHERE ID
IN (
SELECT id
FROM service
WHERE serviceid = frompreviousstatement
)
ORDER BY Qos ASC
LIMIT 0 , 1
and put each result from that into another array...
|
|
|
|
|
thank you for your reply, but I need a resultset object, It's hard to write it step by step like your solution.
I mean I need a single query statement
my result of goal 1 should be:
2000 2 Jacob 2011/1/1 2
2001 4 Jacob 2011/1/1 2
2002 5 Jacob 2011/2/3 1
2003 6 Tyler 2011/1/4 1
2004 9 Jacob 2011/2/4 1
2005 10 Jacob 2011/2/5 1
2006 11 Jacob 2011/2/4 2
2007 12 Jacob 2011/1/8 1
2008 13 Tyler 2011/2/6 1
2009 14 Dyla 2011/2/8 1
2010 15 Dyla 2011/2/9 1
2011 19 Andrew 2011/2/2 1
2012 18 Andrew 2011/2/19 1
2013 17 Andrew 2011/1/1 1
2014 16 Andrew 2011/1/1 1
2015 20 Andrew 2011/2/1 1
2016 21 Andrew 2011/1/19 1
2017 22 Jacob 2011/1/1 1
2018 23 Dyla 2011/2/3 1
2019 24 Dyla 2011/1/9 1
2020 25 Dyla 2011/1/1 1
2021 26 Andrew 2011/1/3 1
2022 28 Jacob 2011/1/9 1
2023 29 Tyler 2011/1/19 1
2024 30 Andrew 2011/2/1 1
2025 31 Dyla 2011/2/3 1
2026 32 Jacob 2011/2/4 1
2027 33 Tyler 2011/2/9 1
2028 34 Daniel 2011/1/6 1
2029 35 Daniel 2011/2/1 1
|
|
|
|
|
SELECT MIN(Qos), serviceid, id, TCID, EndTime from service
GROUP BY serviceid
maybe this then...
|
|
|
|
|
Thank you. It worked just like what I want. I cant believe it because your answer is more simple than another answher that I have got few hours ago:
SELECT DISTINCT serviceid,tcid,endtime,qos FROM (SELECT * FROM service ORDER BY serviceid, qos, id) AS base GROUP BY serviceid
thank you again genius, and plz help me with aim2-4.
|
|
|
|
|
Have a look at this query:
WITH id AS (
SELECT serviceid,tcid,Min(EndTime) endtime,Min(Qos) qos
FROM service
GROUP BY serviceid,tcid
)
SELECT s.ServiceID,s.ID,s.TCID,s.EndTime,s.Qos
FROM service s,id
WHERE s.serviceid = id.serviceid
AND s.tcid = id.tcid
AND s.endtime = id.endtime
AND s.qos = id.qos
|
|
|
|
|
hi,Jörgen Andersson
thank you for your help. the statement you posted seemed like a sqlsvr statement, I ran it on mysql and got a error:You have an error in your SQL syntax near "with.....".
I should run it on mysql.
in any case, I have got the correct answer of aim 1( I have write it in the top/first post), and plz help me with aim 2-4( It's still in trouble).
|
|
|
|
|
SELECT * FROM (SELECT COUNT(TCID) as C, TCID FROM service WHERE Qos = 1
and MONTH(EndTime) = 1
GROUP BY TCID) AS T
ORDER BY C DESC
This'll rank em for a month, I'll leave you to learn how to increment that month value to loop through for every one...
SELECT * FROM (SELECT COUNT(TCID) as count, TCID, Month FROM (SELECT month(endtime) as Month, tcid, serviceid FROM service
WHERE qos = 1
group by month(endTime),tcid, serviceid) AS T
GROUP BY month, tcid) AS S
order by month, count DESC
alternatly, this'll give you all of the months in the statement, I'll leave you to actually learn some SQL to refine it to get exactly what you want...
|
|
|
|
|
Thanks a lot!
I add filter statement( aim 1, without it the result will not be correct),like:
SELECT * FROM (SELECT COUNT(TCID) as count, TCID, Month FROM (
SELECT month(endtime) as Month, tcid, serviceid
FROM (SELECT MIN(Qos) qos, serviceid, id, TCID, EndTime from service
GROUP BY serviceid
)T1
WHERE qos = 1
group by month(endTime),tcid, serviceid
)As T
GROUP BY month, tcid) AS S
order by month, count DESC
and It got correct data:
count TCID Month
4 Andrew 1
3 Jacob 1
2 Tyler 1
2 Dylan 1
1 Daniel 1
4 Dylan 2
4 Jacob 2
4 Andrew 2
2 Tyler 2
1 Daniel 2
I think It is half of aim 2, thank you. please go on ,I think Top-2-in-January(rank) is Andrew/Jacob, Top-2-in-February(rank) is Andrew/Dylan/Jacob . So the result of aim2 should be 2 reocrders: Andrew/Jacob
modified 19-Apr-12 4:11am.
|
|
|
|
|
|