Getting the max count for each group
Hi, I am a newbie and working on a project for school. I have been working this problem for a couple of days and made a bit of progress.
I have two tables, student and enroll for a college database. I need to show the students who have the most enrollments in each major. So far, I have:
WITH COUNT_Q AS
SELECT B.SSN, B.MAJOR, COUNT (A.CLASS_NO) AS COUNTCLASS
FROM ENROLL A, STUDENT B
WHERE A.SSN = B.SSN
GROUP BY B.SSN, B.MAJOR
SELECT MAX(COUNTCLASS), MAJOR
FROM COUNT_Q
GROUP BY MAJOR
I am getting the correct results but need to show the SSN and Major of the studens. When I add in the SSN and Major, like below, I get back too many records.
WITH COUNT_Q AS
SELECT B.NAME, B.SSN, B.MAJOR, COUNT(A.SSN) AS COUNTCLASS
FROM ENROLL A, STUDENT B
WHERE A.SSN = B.SSN
GROUP BY B.NAME, B.SSN, B.MAJOR
SELECT *
FROM COUNT_Q
WHERE (COUNTCLASS) IN (SELECT MAX(COUNTCLASS)
FROM COUNT_Q
GROUP BY MAJOR
Can someone help point me in the right direction?
SELECT B.NAME,
B.SSN,
B.MAJOR
FROM (
SELECT B.NAME,
B.SSN,
B.MAJOR,
COUNT(A.CLASS_NO) SSN_MAJOR_COUNTCLASS
DENSE_RANK() OVER(PARTITION BY MAJOR ORDER BY COUNT(A.CLASS_NO) DESC) RNK
FROM ENROLL A,
STUDENT B
WHERE A.SSN = B.SSN
GROUP BY B.NAME,
B.SSN,
B.MAJOR
WHERE RNK = 1
/SY.
Edited by: Solomon Yakobson on Mar 27, 2011 6:53 PM
Similar Messages
-
Getting the first row for each group
Hi Everyone,
I have a query which returns a number of rows, all of which are valid. What I need to do is to get the first row for each group and work with those records.
For example ...
client flight startairport destairport stops
A fl123 LGW BKK 2
A fl124 LHR BKK 5
B fl432 LGW XYZ 7
B fl432 MAN ABC 8
.... etc.
I would need to return one row for Client A and one row for Client B (etc.) but find that I can't use the MIN function because it would return the MIN value for each column (i.e. mix up the rows). I also can use the rownum=1 because this would only return one row rather than one row per group (i.e. per client).
I have been investigating and most postings seem to say that it needs a second query to look up the first row for each grouping. This is a solution which would not really be practical because my query is already quite complex and incorporating duplicate subqueries would just make the whole thing much to cumbersome.
So what I really new is a "MIN by group" or a "TOP by group" or a "ROWNUM=1 by group" function.
Can anyone help me with this? I'm sure that there must be a command to handle this.
Regards and any thanks,
Alan Searle
Cologne, GermanySomething like this:
select *
from (
select table1.*
row_number() over (partition by col1, col2 order by col3, col4) rn
from table1
where rn = 1In the "partition by" clause you place what you normally would "group by".
In the "order by" clause you define which will have row_number = 1.
Edit:
PS. The [url http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/functions004.htm#i81407]docs have more examples on using analytical functions ;-)
Edited by: Kim Berg Hansen on Sep 16, 2011 10:46 AM -
There are tables(and columns) like:
'Clients'(clientID)
'Houses' (houseID)
'Visits' (clientID, houseID, visit_date)
'Contracts'(contractID, houseID, clientID, rentDate_from, rentDate_end)
I have problem with writing MS SQL query of this kind:
how many visits to houses did each client, before renting one of them?
Its easy to count total number of Visits for each client, listing all visits + group by clientID and selecting count(*) for each group.
Lets say this is select_1, and select_2 is listing all Contracts for all clients.
Select_1 is not answer, because count must be performed only on groups, which:
-have at least 1 row "like" row in select_2 (it means that at least one of visited houses was rented, because it can happen that client visited few houses, but rented other, not visited house). my idea for this is comparing select_1 and select_2 with:
"where s1.clientID=s2.clientID and s1.houseID=s2.houseID"
-each group must have all rows(visits) with date of same day or earlier than contract date
maybe: "datediff(day, s1.visit_date, s2.rentDate_from) >= 0"In future, please provide proper DML, DDL and example data, like I have for you below.
DECLARE @clients TABLE (clientID INT, name VARCHAR(20))
INSERT INTO @clients (clientID, name)
VALUES (1, 'Jonathan'),(2, 'Christopher'),(3, 'James'),(4, 'Jean-Luc'),(5, 'William')
DECLARE @houses TABLE (houseID INT, address VARCHAR(20))
INSERT INTO @houses (houseID, address)
VALUES (1, 'NX01'),(2, 'NCC 1701'),(3, 'NCC 1071A'),(4, 'NCC 1701D'),(5, 'NCC 1701E')
DECLARE @visits TABLE (clientID INT, houseID INT, visitDateTime DATETIME)
INSERT INTO @visits (clientID, houseID, visitDateTime)
VALUES (1,1,'2001-01-01 12:13:14'),
(2,2,'2001-01-02 12:13:14'),
(3,2,'2001-01-01 12:13:14'),(3,3,'2001-01-01 12:13:14'),
(4,4,'2001-01-01 12:13:14'),(4,5,'2001-01-01 12:13:14'),
(5,4,'2001-01-01 12:13:14'),(5,5,'2001-01-01 12:13:14')
DECLARE @contracts TABLE (contractID INT IDENTITY, houseID INT, clientID INT, rentStartDate date, rentEndDate date)
INSERT INTO @contracts (houseID, clientID, rentStartDate, rentEndDate)
VALUES (1,1,'2001-01-02',NULL),(2,2,'2001-01-02',NULL),(3,3,'2001-01-02',NULL),(4,4,'2001-01-02',NULL),(5,5,'2001-01-02',NULL)
SELECT contractID, c.houseID, c.clientID, rentStartDate, rentEndDate, cl.clientID, name, h.houseID, address, COUNT(v.clientID) AS visits
FROM @contracts c
LEFT OUTER JOIN @clients cl
ON c.clientID = cl.clientID
LEFT OUTER JOIN @houses h
ON c.houseID = h.houseID
LEFT OUTER JOIN @visits v
ON c.clientID = v.clientID
AND c.rentStartDate >= v.visitDateTime
GROUP BY contractID, c.houseID, c.clientID, rentStartDate, rentEndDate, cl.clientID, name, h.houseID, address -
How can I get the shutter count for my Canon 7D Mark II using Windows XP?
How can I get the shutter count for my Canon 7D Mark II using Windows XP? I've been looking lots of places and doing some file uploads, but I can't seem to find anything. I'm wary of downloading software I know nothing about. Any help is greatly appreciated.
Solved!
Go to Solution.Nevermind, just, sort of answered my own question. Doesn't work on XP, but using a Win 7 'puter I was able to use Shutter Count, which now works w/ the 7D Mark II.
-
Firefox will not show links to flv files. I get the error message for each flv file: "File not found. Firefox can't find the file at http:// (path) .flv." Any mov and swf files in this same path will show. I can see the videos in Safari so the paths are correct.
Is this a webpage that contains a link to a flv file? Please post a link to the page and tell us which link(s) are the problem flv files or else post a link to the .flv file itself.
Alternately, click on one of the sample FLV File links on this page and tell us exactly what happens:
http://www.mediacollege.com/adobe/flash/video/tutorial/example-flv.html
It might also help if you post the exact error message, including the path to the flv file.
'''Note:'''
Depending on how you have Firefox set up, clicking on a FLV File link will either save the FLV file to your computer or Firefox may open it automatically in an external application right after downloading (Firefox may ask you first). Firefox itself can't play FLV files so you need a "helper" application (or a plugin for flv files, if ther is one. You can see if Firefox is already set up to download or open FLV files by going to Firefox Preferences and looking in the Applications list. Find the FLV file type in the list and, if the action is "Open with", it should show the application that can play FLV files (e.g., VLC Media Player or Perian). See [[Managing filetypes]] for more information. -
How to get the time interveral for each status of worklow (wating,Inprocess
Hi Friends,
I have requreiemnt to get the time interval for each status of workflow.
ex.
status
workflow id | startd date | enddate | waiting | Inprocessing|Error|......
1026 10.10.2008 25.10.2008 1hr 1hr 30 min 2 hr
Please do give me idea how to get the time stam of each status of workflow.
Thanks,
D.prabhuHi,
I think the following tables would be helpful to you:-
SWW_CONT Container Contents for Work Item Data Container
SWW_CONTOB "Container Cont. for Work Item Data Container (Only Objects)"
SWWLOGHIST History of a work item
SWWORGTASK Assignment of WIs to Org.Units and Tasks
SWWUSERWI Current Work Items Assigned to a User
SWWWIHEAD Header Table for all Work Item Types
Hope it Helps!
Regards,
Kanika -
Getting the latest area for each case
dear all;
I have a table with the following info below
create table tmp_a
idv number(30),
personid number(30),
Area varchar2(200),
dateadded date,
is_close number(2)
insert into tmp_a
(idv, personid, area, dateadded, is_close)
values
(1, 10001, 'ZONE_A', sysdate, 1);
insert into tmp_a
(idv, personid, area, dateadded, is_close)
values
(2, 10002, 'ZONE_B', sysdate, 0);
insert into tmp_a
(idv, personid, area, dateadded, is_close)
values
(3, 10003, 'ZONE_A', sysdate, 0);
insert into tmp_a
(idv, personid, area, dateadded, is_close)
values
(4, 10003, 'ZONE_A', sysdate, 0);
Now I would like to get this below
idv Personid Area Dateadded Is_clode
4 10003 Zone_A 3/3/2011 1:00:37PM 0
2 10002 Zone_B 3/3/2011 12:17;14PM 0the output is based on the fact, if there are duplicates associated with the area, the query should pick the latest one...In this case, the latest one for zone_A is idv 4.
kindly note, idv is a primary key. How do I go about doing that? All help is appreciated. Thank you.Hi,
ROW_NUMBER returns a unique number for every row. If you have n rows, it will return the integers 1 through n, without skipping or duplicating any numbers, so you can be confident it will return the number 1 on exactly 1 row.
ROW_NUMBER (PARTITION BY area ...) means that a separate set of integers, each starting with 1, will be returned for each distinct value of area.
ROW_NUMBER (PARTITION BY area ORDER BY idv DESC) means that the numbers will be assigned in DESCending order by idv. The row with the highest idv for each area will be the row for which ROW_NUMBER returns 1.
The LAST function, as demonstrated by Solomon, is a good thing to have in your toolbox, but you have to code a somewhat complicated expression for each column (except the GROUP BY column, and the column that determines the order). When you need to pick an entire row, I find it easier to use ROW_NUMBER.
Also, ROW_NUMBER can easily be adapted if your needs change. Where I work, people are always saying things like "Remember that query you wrote for me, that returns the latest row fro each area? Can you make it return the latest 5 rows?" or "It truns out idv isn't unique. Can you make it return all the rows with the highest idv?" It's easy to change the ROW_NUMBER solution to do things like that. -
Help in finding the total count for each month
Hello everyone,
Sometime back I had posted about the date function. I have that problem solved now my problem is that after finding all records which against each record I need to find the total count for that month if no records than I have to set it to a 0.
Here is how my program has been written.
PROCEDURE extract_materials
IS
CURSOR xyz
BEGIN
FOR xyz1 IN xyz LOOP
-- once inside the loop for each of the record from
the cursor I need to find a total count for that record for everymonth in the year.
All these records will be instered in to a staging table
Can you please help me out with this. I really appreciate this.
Thanks in advance.
-VaniAndrew - you missed the tricky bit of the spec - include missing months with a zero.
You need to do an outer join to a select from any big table with a function involving rownum and add_months to generate all the possible months. However, its time to go home and I can't be bothered to work out the finer details. -
Find out and get the bit value for each pixel in image
i use 16bit image. the image is 200x200 pixels. i need to know what is
the bit value for each pixel. example(0111111011111101).
where i can see the bit value output. if somebody got the example surce code please send it to me.coz i need the bit value to find out how many RGB color that use in the image. thanks for ur help.How many logins do you have???
-
How to select a max row for each group in SQL ( with distinct code )
Dear friends,
My table is as show in below… for each ‘grpid’ I want get row with Max tiv value and code should be distinct. Ie in result, code & grpid will come only once.
grpid
code
Tiv
2
GB
9
2
IN
7
1
GB
11
1
US
10
Result: ( we are selecting IN even though for grpid 2
‘GB’ has max value )
grpid
code
Tiv
2
IN
7
1
GB
11
-SajidThanks for reply..
please see my DML+ DL
CREATE TABLE [dbo].[tab](
[grpid] [int] NOT NULL,
[code] [varchar](2) NOT NULL,
[Tiv] [int] NOT NULL
) ON [PRIMARY]
delete from tab
insert into tab values (2 , 'GB' ,12)
insert into tab values (2 , 'IN' ,10)
insert into tab values (1 , 'GB' ,11)
insert into tab values (1 , 'US' ,10)
insert into tab values (3 , 'GB' ,10)
insert into tab values (3 , 'US' ,9)
insert into tab values (3 , 'UA' ,10)
insert into tab values (3 , 'IN' ,8)
Result:
grpid code
tiv
1
US 10
2
GB 12
3
UA 10
Thanks in Advance..
-Sajid
CREATE TABLE [dbo].[tab](
[grpid] [int] NOT NULL,
[code] [varchar](2) NOT NULL,
[Tiv] [int] NOT NULL
) ON [PRIMARY]
delete from tab
insert into tab values (2 , 'GB' ,12)
insert into tab values (2 , 'IN' ,10)
insert into tab values (1 , 'GB' ,11)
insert into tab values (1 , 'US' ,10)
insert into tab values (3 , 'GB' ,10)
insert into tab values (3 , 'US' ,9)
insert into tab values (3 , 'UA' ,10)
insert into tab values (3 , 'IN' ,8)
;with mycte as
(select grpid,code,tiv , row_number() over(Partition by code Order by Tiv DESC) rn1, row_number() over(Partition by grpid Order by Tiv DESC) rn2
from tab)
,mycte1 as (Select *
, row_number() over(Partition by grpid, rn1 Order by rn2 ASC) rn3
from mycte
WHERE rn1=1
Select grpid,code,tiv from mycte1
Where rn3=1
drop table tab -
Get the max count of the column list item
Hi,
I have a list with the 'TO' column of type people. My task is to get persons name who is featured the max number of time in the 'TO' column.
What is the best possible solution OOB. I dont want to go with custom solution.
Thanks,
AnkitHello Ankit,
I understand that you want to get total of "To" column. I would suggest you modify the current view of list and use "Group By" and Total section to get Max number of that column. Just try this and check whether it fulfils your need or
not.
Hemendra:Yesterday is just a memory,Tomorrow we may never see<br/> Please remember to mark the replies as answers if they help and unmark them if they provide no help -
Text labels started showing up next to my app buttons going across toolbar. Because the names are fairly long (I don't need them per se); I can now only see about 10 buttons. I made sure the the box in the toolbar preferences menu which says "show text labels for each app." was unchecked. I even tried checking it, then unchecking it.
I can't get the labels to go away. I used to be able to see and therefore USE EASILY about 30 -40 app buttons. I just downloaded the latest update to firefox 8.I am having something similar going on and can't sort out what it is doing:
ldiffs:
dn: automountMapName=auto_master,dc=example,dc=edu
objectClass: top
objectClass: automountMap
automountMapName: auto_master
dn: automountKey=/foo,automountMapName=auto_master,ou=Mounts,dc=soe,dc=ucsc,
dc=edu
objectClass: automount
automountKey: /foo
automountInformation: auto.foo,dc=example,dc=edu -rw,resvport,
hard,intr,nosuid,tcp
Second one:
dn: automountMapName=auto.foo,dc=example,dc=edu
objectClass: top
objectClass: automountMap
automountMapName: auto.foo
dn: automountKey=tstaff,automountMapName=auto.foo,dc=example,dc=edu
objectClass: top
objectClass: automount
automountInformation: fileserver:/export/foo/tstaff
automountKey: tstaff
9/25/09 11:45:25 AM com.apple.automountd[1101] t0xb0289000 name=tstaff[] map=auto.foo,dc=example,dc=edu opts=rw,resvport,hard,intr,nosuid,tcp path=/foo direct=0
9/25/09 11:45:25 AM com.apple.automountd[1101] t0xb0289000 getmapent_ds called
9/25/09 11:45:25 AM com.apple.automountd[1101] t0xb0289000 getmapent_ds: key=[ tstaff ]
9/25/09 11:45:25 AM com.apple.automountd[1101] t0xb0289000 ds_match called
9/25/09 11:45:25 AM com.apple.automountd[1101] t0xb0289000 ds_match: key =[ tstaff ]
9/25/09 11:45:25 AM com.apple.automountd[1101] t0xb0289000 ds_match: Searching for tstaff,automountMapName=auto.foo,dc=example,dc=edu
9/25/09 11:45:25 AM automountd[1101] ds_search failed
exiting ...
It seems like it can't find the trigger point tstaff. It is looking for:
ds_match: Searching for tstaff,automountMapName=auto.foo,dc=example,dc=edu
which isn't what the DN is in ldap:
Distinguished Name: automountKey=tstaff,automountMapName=auto.foo,dc=example,dc=edu
any thoughts?
regards,
Derek -
How to select a max row for each group in SQL
Dear Frindz,
I want select countries with maximum value of 'Value' for a 'grpid'. Also already selected 'Country' should not be considered for other 'grpid' while checking the maximum. ( ie Country or grpid should not be repeated in the result )
CREATE TABLE [dbo].[test1](
[Country] [varchar](4) NULL,
[grpid] [int] NOT NULL,
[Value] [float] NULL,
[Row_number] [bigint] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'US', 49707, 604456458, 1)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'US', 909, 604456458, 2)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'US', 231, 604456457, 3)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GB', 5086, 497654945, 4)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GB', 50147, 405759433, 5)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GB', 909, 405759433, 6)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GB', 231, 405759433, 7)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CA', 49707, 353500201, 8)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CA', 5086, 353500201, 9)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CA', 909, 353500201, 10)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CA', 231, 353500201, 11)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'JP', 49707, 198291290, 12)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'JP', 5086, 198291290, 13)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'JP', 909, 198291290, 14)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'JP', 231, 198291290, 15)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'MX', 49707, 181884714, 16)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'MX', 909, 181884714, 17)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'MX', 231, 181884714, 18)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'BR', 49707, 105147054, 19)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'BR', 5086, 105147054, 20)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'BR', 909, 105147054, 21)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'AR', 49707, 94774929, 22)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'AR', 5086, 94774929, 23)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'AR', 909, 94774929, 24)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'AR', 231, 94774929, 25)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'ZA', 49707, 84560514, 26)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'ZA', 909, 84560514, 27)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'ZA', 231, 84560514, 28)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CN', 49707, 68966682, 29)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CN', 909, 68966682, 30)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CN', 231, 68966682, 31)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CB', 49707, 65020665, 32)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CB', 5086, 65020665, 33)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CB', 909, 65020665, 34)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CB', 231, 65020665, 35)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GT', 49707, 42016510, 36)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GT', 5086, 42016510, 37)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GT', 909, 42016510, 38)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CR', 49707, 37116544, 39)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CR', 5086, 37116544, 40)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CR', 909, 37116544, 41)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CR', 231, 37116544, 42)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'SV', 49707, 31103322, 43)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'SV', 5086, 31103322, 44)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'SV', 909, 31103322, 45)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'NI', 49707, 17415843, 46)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'NI', 5086, 17415843, 47)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'NI', 909, 17415843, 48)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'HN', 49707, 12516273, 49)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'HN', 5086, 12516273, 50)
GO
INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'HN', 909, 12516273, 51)
GO
Result:
Country
grpid
Value
Row_number
US
49707
604456458
1
GB
5086
497654945
4
CA
909
353500201
10
JP
231
198291290
15
-SajidHi Pituach, thanks for your reply.
in result Country or grpid should not be repeated... in your result 'GB' and 'US' is repeating.
1) select countries with maximum value of 'Value' for a 'grpid'
2) 'grpid' & country should not be repeated.
3) we need to take next maximum
value if 'grpid' not there a for
highest value.
Result:
Country
grpid
Value
Row_number
US
49707
604456458
1
GB
5086
497654945
4
CA
909
353500201
10
JP
231
198291290
15
-Sajid
-Sajid -
My sample dataset (below) contains 3 groups -- 'a', 'b', 'c'. I need to retrieve the Min(startDate) and Max(EndDate) for each group so that the output looks something like this (date format not an issue):
fk minStart maxEnd
a 1/13/1985 12/31/2003
b 2/14/1986 12/31/2003
c 4/26/1987 12/31/2002
What is the Tsql to perform this type of operation? Note: the actual data resides in a sql server 2000 DB. If the Tsql is different between version 2000 and the later versions -- I would be grateful for both versions of the Tsql
--I noticed that multiple lines of Insert values doesn't work in Sql Server 2000 -- this sample is in Sql Server 2008
create table #tmp2(rowID int Identity(1,1), fk varchar(1), startDate datetime, endDate datetime)
insert into #tmp2
values
('a', '1/13/1985', '12/31/1999'),
('a', '3/17/1992', '12/31/1997'),
('a', '4/21/1987', '12/31/2003'),
('b', '2/14/1986', '12/31/2003'),
('b', '5/30/1993', '12/31/2001'),
('b', '6/15/1994', '12/31/2003'),
('b', '7/7/2001', '12/31/2003'),
('c', '4/26/1987', '12/31/1991'),
('c', '8/14/1992', '12/31/1998'),
('c', '9/10/1995', '12/31/2002'),
('c', '10/9/1996', '12/31/2000')
Thanks
Rich PRich
It is unclear what you are trying to achieve, you said that it is SQL Server 2000 but provide a sample data with SQL Server 2008 syntax
Is it possible to use UNION ALL for your queries to make its one
select * from
select * from #tmp2 t1 where exists
(select * from (select top 1 * from #tmp2 t2 where t2.fk = t1.fk order by t2.startdate) x where x.rowID = t1.rowID)
UNION ALL
select * from #tmp2 t1 where exists
(select * from (select top 1 * from #tmp2 t2 where t2.fk = t1.fk order by t2.Enddate desc) x where x.rowID = t1.rowID)
as der order by fk
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Need to get the exact time of each picture that I saved ( maybe as a excel sheet or word )
Hi guys,
I am actually a new labview user. I have successfullly been able to save pictures. But unfortunately I got two problems. The first one is that I need to use two cameras at the same time. The program I have so far is for one camera. The second problem is that I need to get the exact time for each picture saved in the profile.
I would appreciate if someone can help me with that. Anyway plz find attached the subvi.
Regards,
Abbas
Attachments:
Abbas progress.vi 54 KBHi Abbas,
First, I notice that you are using IMAQ for USB. Is there any particular reason you are not using IMAQdx? This is the newer, supported driver for use with USB cameras that supports acquiring from multiple cameras at the same time.
Check these two articles:
Can I Acquire from Multiple USB Cameras Simultaneously Using IMAQdx?
Can I Acquire from Two USB Cameras Simultaneously with NI-IMAQ for USB Cameras 1.0?
And also this example (if you camera does not support acquiring from multiple cameras at the same time):
Toggle Between Multiple USB Cameras
As for timestamps, some cameras output them with the frame timing. However, usually this is not a feature of USB cameras; if it is not, you can look into timing options within LabVIEW. Try playing with the property node for IMAQdx and the Acquisition Attributes.
Cheers,
Marti C
Applications Engineer
National Instruments
NI Medical
Maybe you are looking for
-
Error while Image loading from database
I'am using generic handler for loading Image from data bases. Its work fine when when I hardcode the ImageID in QureyString when call Handler. But it's giving errors of different type when I dynamically pass value to qurey string(from datatable). "Th
-
How do I add multiple photos to mission control for my desktop?
How do I add multiple photos to my desktop thru mission control. I have a 5 year old 23" iMac.
-
Need easy instructions on creating a 2 computer network
Hi! Recently I aquired a second Mac, a MacBook Pro. I'd like to set up a mini network between my 2 computers. The MBP is wireless connected to the Internet and the stationary through Ethernet cable to the Internet, directly to the broadband modem. It
-
Problems publishing shared review comments in Acrobat 8.0
I created a shared review in Acrobat 8.0 and posted it to a Sharepoint workspace. A reviewer published comments but I was not able to see them. He double-checked, and he was able to see his comments but I was still unable to see them. He checked agai
-
Unable to find localejbs/CallBicXIRaBean
Hell All, As a part of Seeburger installation we have deployed the bicXIRaSCA.sca to XI server. The SDM log confirms about the successful deployment of the same. 07/08/24 18:06:48 - *********************************************************** 07/08/2