473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

help query sql server 2005

8
HI!

I have 2 tables. table1(id_tab1, desc) and table2(id_tab2, id_tab1, data, type)


Example:

table1 (id_tab1, desc) (int, varchar(50))
-------------------------------

( 1, xpta)
( 2, xpte)
( 3, xpti)


table2 (id_tab2, id_tab1, date, type) (int , int, datetime, int)
-----------------
(1, 1, 10-10-2007, 1)
(2, 1, 12-10-2007, 2)
(1, 2, 10-10-2007, 1)
(2, 2, 12-10-2007, 2)
(2, 1, 12-10-2007, 3)
(1, 3, 10-10-2007, 1)

I need de result like: (for all id table1 i want the max date for type 1 or 2)

(table1.id_tab1, table2.data, tipo)
---------------------------------
( 1, 12-10-2007, 2)
( 2, 12-10-2007, 2)
( 3, 10-10-2007, 1)


My query (don't work) :(

SELECT table1.id_tab1, MAX( table2.data), tab2. tipo
WHERE table2. type = '1' OR table2. type = '2'
FROM table1 INNER JOIN
table2 ON table2.id_tab1 = table1.id_tab1
GROUP BY table1.id_tab1, type
ORDER BY table1.id_tab1


thanks.
May 21 '07 #1
1 923
almaz
168 Expert 100+
My query (don't work) :(

SELECT table1.id_tab1, MAX(table2.data), tab2. tipo
WHERE table2. type = '1' OR table2. type = '2'
FROM table1 INNER JOIN
table2 ON table2.id_tab1 = table1.id_tab1
GROUP BY table1.id_tab1, type
ORDER BY table1.id_tab1
Expand|Select|Wrap|Line Numbers
  1. SELECT table1.id_tab1, MAX(table2.data), table2.type
  2. FROM table1 INNER JOIN
  3.      table2 ON table2.id_tab1 = table1.id_tab1
  4. WHERE table2.type = 1 OR table2.type = 2
  5. GROUP BY table1.id_tab1, table2.type
  6. ORDER BY table1.id_tab1
May 22 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: - | last post by:
Hello to all I need help on some sql statements and wondering is anyone out there is able to help. I've extracted some data from the database into a new table consisting of 4 fields CustomerID...
7
by: angelasg | last post by:
Here is sample data I'm working with: ID ShiftDate SegTime 99 5/2/2005 5/2/2005 1:00:00 PM 99 5/2/2005 5/2/2005 1:04:00 PM 99 5/2/2005 5/2/2005 1:43:00 PM 99 5/2/2005...
3
by: Rodney Garland | last post by:
Hi All, I am a relative beginner to Python and am looking for help with sending and XML message and getting back a return file. The server is: https://node.deq.state.or.us/node/node.asmx I...
3
by: loosecannon_1 | last post by:
I get a 90-120 second blocking when send 15 or so simultaneous queries to SQL Server 2000 that query a view made up of two joined tables. After each query is blocking for the same amount of time...
8
by: Xu, Wei | last post by:
Hi, I have wrote the following sql sentence.Do you have comments to improve the performance.I have created all the indexed. But it's still very slow.Thanks The primary key is proj_ID and...
1
by: Learner | last post by:
Hi there, I have installed Sql server 2005 developer on my machine which already has a Sql server 2000 installed on. Now i am trying to query the Sqlserver 2005 data(Ex: from Person.Address...
2
by: schoultzy | last post by:
Hello Everyone, This is probably a simple fix so be kind when/if you reply. The query below retrieves information for individuals based on a column named ATTRIB_DEF, and assorted other columns;...
2
by: click37 | last post by:
Is there a query that will let me see what SQL users has what access to which DBs and what lvl access they have?
1
by: alvinstraight38 | last post by:
I have a client who has been receiving hundreds of SQL timeout error messages in their error logs. Specifically, the message looks like this: MESSAGE :...
2
by: Dinesh | last post by:
Hi experts, I am working on SQL Server 2005. Now i have to write a query which will extract some information from a table. My main table is having few columns supose 3 columns. EmpID ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.