Forum Replies Created

Viewing 15 posts - 856 through 870 (of 1,491 total)

  • RE: Query

    You can do it all with dynamic SQL:

    DECLARE @sql varchar(8000)

    ,@CRLF char(2)

    ,@tab char(1)

    ,@TABLENAME sysname

    SELECT @CRLF = CHAR(13) + CHAR(10)

    ,@tab = CHAR(9)

    ,@TABLENAME = 'Commissioner'

    ,@SQL =

    'SELECT ' + @TABLENAME + ' AS TableName'...

  • RE: problems with a more complex query than I'm used to :)

    Or maybe:

    ;WITH cte

    AS

    (

    SELECT *

    ,ROW_NUMBER() OVER (PARTITION BY custID, module ORDER BY visit_date DESC) AS RowNum

    FROM @t

    )

    SELECT T.*

    FROM @t T

    JOIN

    (

    SELECT C.*

    FROM cte C

    JOIN

    (

    SELECT custID, module

    ,MIN(found_count) AS found_count

    ,MIN(quality) AS quality

    ,MIN(pressure) AS pressure

    FROM...

  • RE: problems with a more complex query than I'm used to :)

    Try something like:

    ;WITH cte

    AS

    (

    SELECT *

    ,ROW_NUMBER() OVER (PARTITION BY custID, module ORDER BY visit_date DESC) AS RowNum

    FROM @t

    )

    SELECT C.*

    FROM cte C

    JOIN

    (

    SELECT custID, module

    ,MIN(found_count) AS found_count

    ,MIN(quality) AS quality

    ,MIN(pressure) AS pressure

    FROM cte

    WHERE RowNum...

  • RE: CTE and Self Join with NULL Values

    To drive by withdrawals with the same idea, try the following:

    ;WITH Reenrollments

    AS

    (

    SELECT *

    ...

  • RE: CTE and Self Join with NULL Values

    Thus all withdrawals will have enrollments following it.

    In my query they do.

    The idea is that RowNum % 2 = 1 for enrollments and RowNum % 2 = 0 for withdrawals.

  • RE: CTE and Self Join with NULL Values

    Assuming an enrollment is never followed by an enrollment, I would be inclined to ignore the EndDate.

    Try something like:

    ;WITH Reenrollments

    AS

    (

    SELECT *

    ,ROW_NUMBER() OVER (PARTITION BY StudentID ORDER BY StartDate) As RowNum

    FROM...

  • RE: Basic question about the order of query execution

    The main clauses in SQL are logically executed in the following order:

    FROM (including JOINS)

    WHERE

    GROUP BY

    HAVING

    SELECT

    DISTINCT

    ORDER BY

    TOP

  • RE: Displaying varchar as time

    -- *** Test Data ***

    DECLARE @t TABLE

    (

    minutes varchar(3) NOT NULL

    )

    INSERT INTO @t

    SELECT '10'

    UNION ALL SELECT '20'

    UNION ALL SELECT '60'

    -- *** End Test Data ***

    SELECT REPLACE(STR(minutes/60, 2), ' ', '0')

    + ':'

    +...

  • RE: SQL Query Question on Union All

    INSERT INTO DATABASE..STAG_TEMP_TABLE

    SELECT ABC,SUM(ZXC)ZXC,SUM(XYZ)XYZ

    FROM

    (

    -- Filter data here as the result will be passed up.

    -- These two selects may also run in parallel.

    SELECT *

    FROM DATABASENAME..WWW

    WHERE A1 BETWEEN '20091009' AND '20100408'

    AND...

  • RE: Big difference in count when modified

    Try putting the 'AND c.AcctName is NULL' into the ON clause of the join to table3.

    Also:

    a.FllwUpDate < DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0)

    will be more efficient than:

    a.FllwUpDate < Convert(DateTime,convert(varchar(12),getdate(),101))

  • RE: Renaming Tables

    The problem with either renaming the tables or using ALTER VIEW is likely to be obtaining the schema lock.

    To avoid this, you should update the main table from the staging...

  • RE: ANSI and Non Ansi issue.

    As this query is quite straightforward to translate, prove that these two statements produce different results.

    select Count(*)

    from dbo.it_1_Revenue__Costs rev,dbo.it_2_elist e,dbo.it_2_LOB l,

    dbo.it_2_RegionCurrency rg,dbo.it_4_Months_QTRS mnt,dbo.et_Summary_Income_Statement inc

    where rev.itemiid*=inc.dimension_1_revenu

    and l.itemiid*=inc.dimension_2_lob

    and e.itemiid*=inc.elist

    and rg.itemiid*=inc.dimension_2_region

    and mnt.itemiid*=inc.dimension_4_months

    SELECT COUNT(*)

    FROM...

  • RE: ANSI and Non Ansi issue.

    This should work, the trick is to follow the WHERE clause:

    SELECT COUNT(*)

    FROM dbo.et_Summary_Income_Statement inc

    RIGHT JOIN dbo.it_1_Revenue__Costs rev

    ON rev.itemiid = inc.dimension_1_revenu

    RIGHT JOIN dbo.it_2_LOB l

    ON l.itemiid = inc.dimension_2_lob

    RIGHT JOIN dbo.it_2_elist e

    ON e.itemiid...

  • RE: Trigger runs with manual insert, but not when apps does insert on table

    Try something like:

    SET ANSI_NULLS, QUOTED_IDENTIFIER ON

    GO

    ALTER TRIGGER NewAgentRegistration

    ON dbo.Client_Agent

    AFTER INSERT

    AS

    SET NOCOUNT ON;

    BEGIN TRY

    INSERT INTO dbo.Asset(_RGuid, AsNumber, ASName, ASRDate, ASEDate)

    SELECT _RGuid

    ,''

    ,''

    ,'' -- Should this be a date???

    ,'' -- Should this be...

  • RE: Trigger runs with manual insert, but not when apps does insert on table

    You have two problems:

    1. Your trigger will not cope if multiple rows are inserted.

    2. You have a multi-statement trigger with no error handling.

    Also, ASNumber is just a formated copy of...

Viewing 15 posts - 856 through 870 (of 1,491 total)