Forum Replies Created

Viewing 15 posts - 1,051 through 1,065 (of 1,491 total)

  • RE: Sum two columns from two tables

    Use an OUTER JOIN and use ISNULL or COALESCE on the outer quantity. eg

    SELECT T1.location, T1.number, T1.quantity - ISNULL(T2.quantity, 0) AS quantity

    FROM

    (

    &nbsp&nbsp&nbsp&nbspSELECT T11.location, T11.number, SUM(T11.quantity) AS quantity

    &nbsp&nbsp&nbsp&nbspFROM Table1 T11

    &nbsp&nbsp&nbsp&nbspGROUP BY...

  • RE: Store result in Variable of dynamic query

    Use sp_executesql with an OUTPUT parameter.

    DECLARE @word sysname

    EXEC sp_executesql

    &nbsp&nbsp&nbsp&nbspN'SELECT TOP 1 @DynamicWord = [name] FROM sysobjects'

    &nbsp&nbsp&nbsp&nbsp,N'@DynamicWord sysname OUTPUT'

    &nbsp&nbsp&nbsp&nbsp,@word OUTPUT

    SELECT @word

  • RE: Sum two columns from two tables

    SELECT T1.location, T1.number, T1.quantity - T2.quantity AS quantity

    FROM

    (

    &nbsp&nbsp&nbsp&nbspSELECT T11.location, T11.number, SUM(T11.quantity) AS quantity

    &nbsp&nbsp&nbsp&nbspFROM Table1 T11

    &nbsp&nbsp&nbsp&nbspGROUP BY T11.location, T11.number

    ) T1

    &nbsp&nbsp&nbsp&nbspJOIN

    &nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT T21.location, T21.number, SUM(T21.quantity) AS quantity

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFROM Table2 T21

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHERE T21.param1 > 0

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND T21.param2...

  • RE: Using the IN keyword with Parameters

    You will have to do a COUNT. Something like:

    SELECT DISTINCT

    &nbsp&nbsp&nbsp&nbspC.CategoryID

    &nbsp&nbsp&nbsp&nbsp,P.*

    &nbsp&nbsp&nbsp&nbsp,F.FeatureValueID

    FROM tblProduct P

    &nbsp&nbsp&nbsp&nbspJOIN tblProductFeatureValue F

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON P.ProductID = F.ProductID

    &nbsp&nbsp&nbsp&nbspJOIN tblProductCategory C

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON P.ProductID = C.ProductID

    &nbsp&nbsp&nbsp&nbspJOIN

    &nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT F1.ProductID

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFROM tblProductFeatureValue F1

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHERE F1.FeatureValueID IN

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT X1.item.value('@value','int')

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFROM @XMLDoc.nodes('/root/item') X1(Item)

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp)

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspGROUP...

  • RE: self Join issue

    SELECT empid

    &nbsp&nbsp&nbsp&nbsp,ISNULL(SUM(CASE WHEN activitytype = 'Billable' THEN noOfHrs END), 0) AS Billable

    &nbsp&nbsp&nbsp&nbsp,ISNULL(SUM(CASE WHEN activitytype = 'NonBillable' THEN noOfHrs END), 0) AS NonBillable

    &nbsp&nbsp&nbsp&nbsp,SUM(noOfHrs) AS Total

    FROM testTiemsheet

    GROUP BY empid

  • RE: SELECT statement with variable numbers of columns

    I think you would be better off doing the pivot in the front end.

    If you really want to do it in the db I suspect you will have to resort...

  • RE: Help with GROUP BY

    Sure, just use the same idea. Something like:

    SELECT O.[ID]

    &nbsp&nbsp&nbsp&nbsp,O.Location

    &nbsp&nbsp&nbsp&nbsp,SUM(CASE WHEN O.[Date] >= M.ThisMonth THEN O.Quanity END) AS TotalQuantityThisMonth

    &nbsp&nbsp&nbsp&nbsp,SUM(CASE WHEN O.[Date] >= M.ThisMonth THEN O.Cost END) AS TotalCostThisMonth

    &nbsp&nbsp&nbsp&nbsp,SUM(CASE WHEN O.[Date] >=...

  • RE: Help with GROUP BY

    A derived table is sometimes known as an inline view.

    The derived table below has an alias of D:

    SELECT O1.[ID]

    &nbsp&nbsp&nbsp&nbsp,O1.Location

    &nbsp&nbsp&nbsp&nbsp,SUM(O1.Quanity) AS TotalQuantity

    &nbsp&nbsp&nbsp&nbsp,SUM(O1.Cost) AS TotalCost

    &nbsp&nbsp&nbsp&nbsp,D.[Date] AS LatestSalesDate

    FROM OrderData O1

    &nbsp&nbsp&nbsp&nbspLEFT JOIN

    &nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT O2.[ID], O2.Location,...

  • RE: Adding a column

    You may be better off forgetting about the trigger and using a computed column.

    Something like:

    ALTER TABLE humanresources.employee

    ADD AgeWhenHired

    AS YEAR(hiredate) - YEAR(birthdate)

    &nbsp&nbsp&nbsp&nbsp- CASE

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHEN MONTH(hiredate) > MONTH(birthdate)

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspTHEN 0

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHEN MONTH(hiredate) = MONTH(birthdate)

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND DAY(hiredate)...

  • RE: Query help urgent

    As you only want to count non-null values, use DATALENGTH to convert image to int.

    eg WHEN 1 THEN Image1 becomes WHEN 1 THEN DATALENGTH(Image1) etc

    [Edit]

    or you could alter saby's CASE...

  • RE: Select *

    Kenneth Wilhelmsson (10/2/2008)


    This is really another subject, but I feel I have to disagree anyway.

    What you're essentially proposing here, is that the model doesn't count.

    Whenever you're using 'select *' instead...

  • RE: Updating table based on coalesce reults in where clause?

    I do not have time to look at your code, but based on your latest explaination you could try something like:

    DECLARE @TermYear char(4)

    &nbsp&nbsp&nbsp&nbsp,@Term varchar(6)

    SELECT @TermYear = '2008'

    &nbsp&nbsp&nbsp&nbsp,@Term = 'Fall'

    UPDATE academic

    SET...

  • RE: Updating table based on coalesce reults in where clause?

    What you are attempting is far from clear. Maybe something like this:

    DECLARE @YearStart char(4)

    &nbsp&nbsp&nbsp&nbsp,@TermStart varchar(6)

    &nbsp&nbsp&nbsp&nbsp,@YearEnd char(4)

    &nbsp&nbsp&nbsp&nbsp,@TermEnd varchar(6)

    SELECT @YearStart = '2006'

    &nbsp&nbsp&nbsp&nbsp,@TermStart = 'Fall'

    &nbsp&nbsp&nbsp&nbsp,@YearEnd = '2008'

    &nbsp&nbsp&nbsp&nbsp,@TermEnd = 'Fall'

    UPDATE academic

    SET status = 'N'

    WHERE...

  • RE: Query help urgent

    This may help you.

    -- *** Test Data **

    DECLARE @t TABLE

    (

    &nbsp&nbsp&nbsp&nbspCol1 int NOT NULL

    &nbsp&nbsp&nbsp&nbsp,Image1 varchar(10) NULL

    &nbsp&nbsp&nbsp&nbsp,Image2 varchar(10) NULL

    &nbsp&nbsp&nbsp&nbsp,Image3 varchar(10) NULL

    &nbsp&nbsp&nbsp&nbsp,Image4 varchar(10) NULL

    &nbsp&nbsp&nbsp&nbsp,Image5 varchar(10) NULL

    &nbsp&nbsp&nbsp&nbsp,Image6 varchar(10) NULL

    &nbsp&nbsp&nbsp&nbsp,Image7 varchar(10) NULL

    &nbsp&nbsp&nbsp&nbsp,Image8 varchar(10) NULL

    &nbsp&nbsp&nbsp&nbsp,Image9 varchar(10)...

  • RE: Creating a pseudo column with hard coded values in the result set of SELECT statement

    I am not sure what you are trying to do, but the following may help.

    SELECT C.Customer_ID, C.[Name], C.Country, R.Region

    FROM Countries C

    &nbsp&nbsp&nbsp&nbspJOIN

    &nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT 1, 'East' UNION ALL

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT 2, 'West' UNION ALL

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT 3,...

Viewing 15 posts - 1,051 through 1,065 (of 1,491 total)