sql-server-2000

Group By but include “missing” values

眉间皱痕 提交于 2019-12-02 17:21:37
问题 Suppose I have the following. select case when fcompany = 'Acme' then 'Red' when fcompany = 'Acme Rockets' then 'Blue' else 'Green' end Color ,sum(fann_sales) FROM slcdpm group by case when fcompany = 'Acme' then 'Red' when fcompany = 'Acme Rockets' then 'Blue' else 'Green' end Let's say it often returns with only two colors. What's the best way to pull all three colors and include 0 for the missing value? Union All? 回答1: Yes, Union All may be your best bet. SELECT 'red' AS Color, sum(fann

Concatenating Row Values

↘锁芯ラ 提交于 2019-12-02 17:21:22
问题 I was using Microsoft SQL server 2005 and was able to concatenate row values based on the following query: SELECT e1.EMP_ID, ( SELECT cast(Sector_ID as varchar(10)) + ';' FROM Employee_Sector_relationship e2 WHERE e2.Emp_ID = e1.Emp_ID ORDER BY Sector_ID FOR XML PATH('') ) AS Sectors FROM Employee_Sector_Relationship e1 GROUP BY Emp_ID But it doesn't work in Microsoft Server 2000. It gives me an error near the for keyword. Can anyone help me to concatenate the row values in Microsoft Server

Is there a way to list open transactions on SQL Server 2000 database?

坚强是说给别人听的谎言 提交于 2019-12-02 16:27:22
Does anyone know of any way to list open transactions on SQL Server 2000 database? I am aware that I can query the view sys.dm_tran_session_transactions on SQL 2005 (and later) database versions, however this is not available on SQL 2000. gbn For all databases query sys.sysprocesses SELECT * FROM sys.sysprocesses WHERE open_tran = 1 For the current database use: DBCC OPENTRAN codingbadger DBCC OPENTRAN helps to identify active transactions that may be preventing log truncation. DBCC OPENTRAN displays information about the oldest active transaction and the oldest distributed and nondistributed

Query to Return Top Items for Each Distinct Column Value

≡放荡痞女 提交于 2019-12-02 13:39:52
问题 If I have a table with the following fields ID, SomeFK, SomeTime How would I write a query return the latest/top 3 items (based on SomeTime ) for each SomeFK . So, the result might look like SomeFK Sometime 0 2012-07-05 0 2012-07-04 0 2012-07-03 1 2012-07-03 1 2012-07-02 1 2012-07-01 2 2012-07-03 2 2012-07-02 2 2012-07-01 ....etc.... Returning the latest items for a particular SomeFK is easy, but i just can't think how to do it for the above. I also feel it should be dead simple! EDIT:

SubSonic3: Method “FirstOrDefault” throws exception with SQL Server 2000

旧巷老猫 提交于 2019-12-02 11:58:14
I am using SubSonic3 with SQL Server 2000. I have problem with the method "FirstOrDefault" - it always throws an exception = "Line 1: Incorrect syntax near '('." from the SubSonic.Linq dll EDIT (Added code from comment): InventoryDAL = DAL project name (dll) Inventort= Subsonic3 Gnerated classes Name space WHWarehouses = gnerated object Dim WareH = (From Wh In InventoryDAL.Inventort.WHWarehouses.All _ Where Wh.WarehouseID = 1 ).FirstOrDefault I don't know SubSonic, but Hibernate has different dialects of SQL you can tell it to use, might want to see if there is any way to tell it to use a

Multiple sub queries

梦想与她 提交于 2019-12-02 11:35:29
Is it possible to get the result as below from the same table date-wise records: Enrolled Enrolled as Email Enrolled as Text Deals Redeemed <First Date> 7 5 2 6 <Next Date> 9 3 6 14 Table structure look something like this: Customer_id, field1, field2, responsecode, created_date My current query is something like this: Select Created, Enroll = (Select COUNT(*) from tblCustomer where field1 <> '' group by created), Email = (Select COUNT(field1) from tblCustomer where field1 = 'E-mail' and field1 <> '' group by created), Cell = (Select COUNT(*) from tblCustomer where field1 = 'Cell Phone' and

How can I count distinct multiple fields without repeating the query?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 10:22:38
I have a query with several groupings that returns a count per month. Something like this: SELECT field1, field2, year(someDate), month(someDate), count(*) as myCount FROM myTable WHERE field5 = 'test' GROUP BY field1, field2, year(someDate), month(someDate) The problem is that I want the count to be distinct per day, based on an id field + the date field (without the time). As in, I want to get the distinct count of ids each day, per month. So I want something like this: SELECT field1, field2, year(someDate), month(someDate), count(distinct someID, someDate) as myCount FROM myTable WHERE

SQL Server 2005: Importing data from SQL Server 2000

时光毁灭记忆、已成空白 提交于 2019-12-02 10:16:26
In SQL Server 2000, you have the "All Tasks... - Export Data" option. Where is this option the SQL Server 2005 Management Studio? Or, is there a SQL Server 2005 way of doing this? EDIT: I am using the Express edition. EDIT: Joel's response answers my question but Mike's answer gives a great alternative to those of us using the Express edition (vote him up!!). If you're using the express edition of management studio the Import and Export features aren't available. You could use the excellent SQL Server Database Publishing Wizard. http://www.microsoft.com/downloads/details.aspx?familyid=56E5B1C5

Concatenating Row Values

試著忘記壹切 提交于 2019-12-02 09:49:44
I was using Microsoft SQL server 2005 and was able to concatenate row values based on the following query: SELECT e1.EMP_ID, ( SELECT cast(Sector_ID as varchar(10)) + ';' FROM Employee_Sector_relationship e2 WHERE e2.Emp_ID = e1.Emp_ID ORDER BY Sector_ID FOR XML PATH('') ) AS Sectors FROM Employee_Sector_Relationship e1 GROUP BY Emp_ID But it doesn't work in Microsoft Server 2000. It gives me an error near the for keyword. Can anyone help me to concatenate the row values in Microsoft Server 2000? This is a technique that should work for you. You can execute this in one batch statement if you

How can I determine the last time any record changed in a specific Sql Server 2000 database?

大兔子大兔子 提交于 2019-12-02 08:59:09
I have a SQL Server 2000 database instance that is rarely updated. I also have a database table which has no columns holding each row's created date or modified date. Is there any way that I can determine the last time an update or insert was performed on the database as a whole, so that I can at least put a bound on when the specific records in the table may have changed? Note: I am looking for information about transactions that have already occurred. Triggers may help we should I require this again in the future, but does not address the problem I'm trying to describe. If it can be done,