Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
zhshqzyc
Posting Yak Master
240 Posts |
Posted - 2009-01-08 : 15:33:57
|
Hello,I have a table that isDescr ID AMT TIME EMP RGS TOTALCash 000000052037 5.00 2009-01-08 09:25:26.337 3111 1 400.00Cash 000000052037 95.00 2009-01-08 09:25:33.377 3111 1 400.00Cash 000000052038 8.88 2009-01-08 09:37:53.070 3111 1 400.00Cash 000000052038 91.12 2009-01-08 09:37:55.823 3111 1 400.00Cash 000000052039 7.77 2009-01-08 10:51:41.467 3111 1 400.00Cash 000000052039 92.23 2009-01-08 10:51:44.250 3111 1 400.00Cash 000000052040 5.00 2009-01-08 14:51:15.537 3111 1 400.00Check 000000052040 4.00 2009-01-08 14:51:15.557 3111 1 400.00Cash 000000052040 95.00 2009-01-08 14:51:18.390 3111 1 400.00Check 000000052040 -4.00 2009-01-08 14:51:18.420 3111 1 400.00 What I want isCash 000000052037 5.00 2009-01-08 09:25:26.337 3111 1 400.00Cash 000000052038 8.88 2009-01-08 09:37:53.070 3111 1 400.00Cash 000000052039 7.77 2009-01-08 10:51:41.467 3111 1 400.00Cash 000000052040 5.00 2009-01-08 14:51:15.537 3111 1 400.00Check 000000052040 4.00 2009-01-08 14:51:15.557 3111 1 400.00Check 000000052040 -4.00 2009-01-08 14:51:18.420 3111 1 400.00 What is the proper query?Thanks |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-01-08 : 15:39:16
|
| on what basis are these in your output5.008.887.775.004.00-4.00and these are not95.0091.1292.2395.00 |
 |
|
|
zhshqzyc
Posting Yak Master
240 Posts |
Posted - 2009-01-08 : 15:45:49
|
| For the Cash, I want to get the first row from those have same ID.For Non cash, I may want all. |
 |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-01-08 : 15:51:54
|
something like this might work for youSELECT DESCR, ID_TOTAL, AMT, TIME, EMP, RGSFROM (SELECT DESCR, ID_TOTAL, AMT, TIME, EMP, RGS, ROW_NUMBER() OVER(PARTITION BY ID_TOTAL ORDER BY TIME ASC) AS SEQ FROM MY_TABLE) AWHERE A.SEQ = 1 OR A.DESCR = 'Check' |
 |
|
|
zhshqzyc
Posting Yak Master
240 Posts |
Posted - 2009-01-08 : 15:54:28
|
| Suppose this table is called table1.What is the query like? |
 |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-01-08 : 15:55:46
|
quote: Originally posted by rohitkumar something like this might work for youSELECT DESCR, ID_TOTAL, AMT, TIME, EMP, RGSFROM (SELECT DESCR, ID_TOTAL, AMT, TIME, EMP, RGS, ROW_NUMBER() OVER(PARTITION BY ID_TOTAL ORDER BY TIME ASC) AS SEQ FROM MY_TABLE) AWHERE A.SEQ = 1 OR A.DESCR = 'Check'above is wrong, try thisSELECT DESCR, ID_TOTAL, AMT, TIME, EMP, RGSFROM (SELECT DESCR, ID_TOTAL, AMT, TIME, EMP, RGS, ROW_NUMBER() OVER(PARTITION BY ID_TOTAL ORDER BY TIME ASC) AS SEQ FROM MY_TABLE WHERE DESCR = 'Cash') AWHERE A.SEQ = 1UNION ALLSELECT DESCR, ID_TOTAL, AMT, TIME, EMP, RGSFROM MY_TABLEWHERE DESCR = 'Check'
|
 |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-01-08 : 15:57:03
|
| replace MY_TABLE (2 places) in the query with your table name |
 |
|
|
zhshqzyc
Posting Yak Master
240 Posts |
Posted - 2009-01-08 : 16:08:06
|
| Can we use having(count(*) %2) something like? |
 |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-01-08 : 16:10:20
|
quote: Originally posted by zhshqzyc Can we use having(count(*) %2) something like?
why do you want to use HAVING? |
 |
|
|
zhshqzyc
Posting Yak Master
240 Posts |
Posted - 2009-01-08 : 16:11:52
|
| to get the first row. |
 |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-01-08 : 16:18:20
|
| cant think of a way of using having(count(*)%2) to get the desired result.I was asking the reason why do you want to use HAVING to get the first row? your teacher/boss asked you? |
 |
|
|
DeveloperIQ
Yak Posting Veteran
71 Posts |
Posted - 2009-01-08 : 20:01:17
|
| Rohit suggested the correct way. If you HAVE to use count, try thisSelect Descr, IDTotal, max(Amt), Max(Time), Max(Emp), Max(RGS) from #x Group by IDTotal, Descr having count(IDTOtal) > 1UNIONSelect Descr, IDTotal, max(Amt), Max(Time), Max(Emp), Max(RGS) from #x Group by IDTotal, Descr |
 |
|
|
DeveloperIQ
Yak Posting Veteran
71 Posts |
Posted - 2009-01-08 : 20:02:31
|
| Here is a cleaner version with your table nameSelect Descr, IDTotal, max(Amt), Max(Time), Max(Emp), Max(RGS)from Table1Group by IDTotal, Descrhaving count(IDTOtal) > 1UNIONSelect Descr, IDTotal, max(Amt), Max(Time), Max(Emp), Max(RGS)from Table1Group by IDTotal, Descr |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-09 : 01:00:17
|
| May Be Like This,,,,with ashishashish as(select *,row_number() over(partition by descr order by descr)as RN from (select idtotal,descr,max(amt) as AMT,max(time) as TIME,max(emp) as EMP,max(rgs) as RGS from test16 group by idtotal,descr) A)select * from ashishashish where descr='cash' union ALLselect B.IDTOTAL,B.DESCR,B.AMT,B.TIME,B.EMP,B.RGS,row_number() over(order by descr)AS rn from test16 B WHERE B.DESCR='check' Thanks,,, |
 |
|
|
|
|
|
|
|