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 |
|
s_anr
Yak Posting Veteran
81 Posts |
Posted - 2009-10-18 : 18:28:55
|
| How do i get multiple counts in a query? Please your helpI run the following simple query to pull the records given below :select case_id, dateadd (hh,-7,dateAdd(ss, date, '19700101'))[Date],type, symbol from recordsraw data case_id Date type symbol123 10/18/2009 A X456 10/18/2009 A Y789 10/20/2009 A Z987 10/20/2009 B X654 10/20/2009 A X321 10/21/2009 B X Expected Results Date symbol type A type B Count Count 10/18/2009 X 1 010/18/2009 Y 1 010/18/2009 Z 0 010/20/2009 X 1 110/20/2009 Y 0 010/20/2009 Z 1 010/21/2009 X 0 110/21/2009 Y 0 010/21/2009 Z 0 0Cheerz!! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-10-18 : 19:20:39
|
what do you want to count ?Also the expected result and raw data does not seems to match. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
s_anr
Yak Posting Veteran
81 Posts |
Posted - 2009-10-19 : 06:22:27
|
| HiI hope the example below makes mores senseInventory # Date Vehicle_Type Fuel_type1 18/10/2009 Car Petrol3 18/10/2009 Bus Diesel5 18/10/2009 Bus Petrol6 18/10/2009 Bus Hybrid9 18/10/2009 Bus Petrol10 18/10/2009 Car DieselExpected Result : Date Vehicle_Type Count_Cars Count_Bus18/10/2009 Petrol 1 218/10/2009 Diesel 1 118/10/2009 Hybrid 0 1This example has data for just one day. But the actual data will have data for more than one day. So it should show the counts date after date. |
 |
|
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2009-10-19 : 06:44:51
|
| SELECT * FROM( SELECT date, fuel_type, vehicle_type FROM dbo.testTable --YOURTABEL NAME) AS abcPIVOT( COUNT(vehicle_type) FOR vehicle_type IN([CAR],[BUS])) AS Pvt |
 |
|
|
s_anr
Yak Posting Veteran
81 Posts |
Posted - 2009-10-19 : 07:10:32
|
| Hey thanks for the reply. i use the following conversion for datedateadd (hh,-7,dateAdd(ss, date, '19700101'))[Date]which make the output to show like the one given below :2009-10-05 14:40:52.000Since, the time part is also included in this conversion, the pivot table created takes each timestamp as a different entry. How do I take only the date part so that i get the counts for the day |
 |
|
|
s_anr
Yak Posting Veteran
81 Posts |
Posted - 2009-10-19 : 15:49:30
|
i figured it out.. thanks..:) |
 |
|
|
|
|
|
|
|