Showing posts with label queryselect. Show all posts
Showing posts with label queryselect. Show all posts

Monday, March 19, 2012

Help needed with DATETIME Arithmetic

Hi,

I have the following query:

SELECT dbo._Fault.id, dbo._Fault.date_created, dbo._Fault.reference,
dbo._Fault.class, dbo._Fault.owner, dbo._Fault.Contact_Name, dbo._Fault.
Product,
dbo._Fault.Problem_Description,
dbo._Fault.Action_Taken, dbo._Fault.Problem_Solution,
dbo._Fault.Duration_start_date, dbo._Fault.Duration_duration,
dbo._Fault.Duration_date_summary,
dbo._Fault.Fault_Status, dbo._Fault.Fault_Source, dbo._Fault.Part_Number,
dbo._Fault.Serial_No,
dbo._Product.id AS PRODUCTID, dbo._Product.description
FROM dbo._Fault LEFT OUTER JOIN
dbo._Product ON dbo._Fault. Product = dbo._Product.id

I need an additional field appended to the end of this (called
'DATECOMPLETED') that will take the 'Duration_start_date' and add the
'Duration_duration' to it - effectively giving me a new datetime field that
is newer than the start date.

I have tried unseccessfully to amend the code but I am afraid my knowledge
of SQL is limited. I would greatly appreciate if someone could show me how
to do this.

Thanks,

DarrenMintyman wrote:

Quote:

Originally Posted by

Hi,
>
I have the following query:
>
SELECT dbo._Fault.id, dbo._Fault.date_created, dbo._Fault.reference,
dbo._Fault.class, dbo._Fault.owner, dbo._Fault.Contact_Name, dbo._Fault.
Product,
dbo._Fault.Problem_Description,
dbo._Fault.Action_Taken, dbo._Fault.Problem_Solution,
dbo._Fault.Duration_start_date, dbo._Fault.Duration_duration,
dbo._Fault.Duration_date_summary,
dbo._Fault.Fault_Status, dbo._Fault.Fault_Source, dbo._Fault.Part_Number,
dbo._Fault.Serial_No,
dbo._Product.id AS PRODUCTID, dbo._Product.description
FROM dbo._Fault LEFT OUTER JOIN
dbo._Product ON dbo._Fault. Product = dbo._Product.id
>
I need an additional field appended to the end of this (called
'DATECOMPLETED') that will take the 'Duration_start_date' and add the
'Duration_duration' to it - effectively giving me a new datetime field that
is newer than the start date.
>
I have tried unseccessfully to amend the code but I am afraid my knowledge
of SQL is limited. I would greatly appreciate if someone could show me how
to do this.
>
Thanks,
>
Darren


If Duration_duration is a number and you want it to be the number of
days added to Duration_start_date you would add something like this to
the select list:

dateadd(dd, Duration_duration, Duration_start__date)|||Thanks for the reply. That seems to have done the trick :o)

"ZeldorBlat" <zeldorblat@.gmail.comwrote in message
news:1158695486.220267.171700@.m73g2000cwd.googlegr oups.com...

Quote:

Originally Posted by

>
Mintyman wrote:

Quote:

Originally Posted by

>Hi,
>>
>I have the following query:
>>
>SELECT dbo._Fault.id, dbo._Fault.date_created, dbo._Fault.reference,
>dbo._Fault.class, dbo._Fault.owner, dbo._Fault.Contact_Name, dbo._Fault.
>Product,
> dbo._Fault.Problem_Description,
>dbo._Fault.Action_Taken, dbo._Fault.Problem_Solution,
>dbo._Fault.Duration_start_date, dbo._Fault.Duration_duration,
> dbo._Fault.Duration_date_summary,
>dbo._Fault.Fault_Status, dbo._Fault.Fault_Source, dbo._Fault.Part_Number,
>dbo._Fault.Serial_No,
> dbo._Product.id AS PRODUCTID,
>dbo._Product.description
>FROM dbo._Fault LEFT OUTER JOIN
> dbo._Product ON dbo._Fault. Product =
>dbo._Product.id
>>
>I need an additional field appended to the end of this (called
>'DATECOMPLETED') that will take the 'Duration_start_date' and add the
>'Duration_duration' to it - effectively giving me a new datetime field
>that
>is newer than the start date.
>>
>I have tried unseccessfully to amend the code but I am afraid my
>knowledge
>of SQL is limited. I would greatly appreciate if someone could show me
>how
>to do this.
>>
>Thanks,
>>
>Darren


>
If Duration_duration is a number and you want it to be the number of
days added to Duration_start_date you would add something like this to
the select list:
>
dateadd(dd, Duration_duration, Duration_start__date)
>

Monday, March 12, 2012

Help needed on simple SUM query

I tried this query:
SELECT `QuoteId`, `QuoteItemId`, SUM(QtyQuoted*Price)
FROM `QuoteItem`
GROUP BY `QuoteId`
HAVING (`QuoteId` LIKE "250033")
to get the values of each QuoteItemId.
There are 7 different items ion this quote, but this query is summing
them ALL together.
I want results like:
250033, 1, 50
250033, 2, 38
250033, 3, 75
250033, 4, 100
etc...
Please advise where I'm getting it wrong.
I googled the topic and I must just have some killer blinders on.
I did try a few other things, but no go.
I know it's going to be simple, but I'm just not seeing it.
Thanks-a-bunch,
Mo
Use square brackets as name delimiters - not single quotes.
Tom
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau
"Mo" <Mehile.Orloff@.gmail.com> wrote in message
news:35683345-c1af-4eac-b8fb-58e7bd73205d@.e10g2000prf.googlegroups.com...
I tried this query:
SELECT `QuoteId`, `QuoteItemId`, SUM(QtyQuoted*Price)
FROM `QuoteItem`
GROUP BY `QuoteId`
HAVING (`QuoteId` LIKE "250033")
to get the values of each QuoteItemId.
There are 7 different items ion this quote, but this query is summing
them ALL together.
I want results like:
250033, 1, 50
250033, 2, 38
250033, 3, 75
250033, 4, 100
etc...
Please advise where I'm getting it wrong.
I googled the topic and I must just have some killer blinders on.
I did try a few other things, but no go.
I know it's going to be simple, but I'm just not seeing it.
Thanks-a-bunch,
Mo
|||As Tom noted you need to remove the single quotes. I see your grouping is
incorrect too. Try this:
SELECT QuoteId, QuoteItemId, SUM(QtyQuoted * Price)
FROM QuoteItem
GROUP BY QuoteId, QuoteItemId
HAVING QuoteId = '250033'
HTH,
Plamen Ratchev
http://www.SQLStudio.com
|||On Apr 2, 10:33Xam, "Tom Moreau" <t...@.dont.spam.me.cips.ca> wrote:
> Use square brackets as name delimiters - not single quotes.
> --
> X XTom
> ----
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON X Canadahttps://mvp.support.microsoft.com/profile/Tom.Moreau
> "Mo" <Mehile.Orl...@.gmail.com> wrote in message
> news:35683345-c1af-4eac-b8fb-58e7bd73205d@.e10g2000prf.googlegroups.com...
> I tried this query:
> SELECT `QuoteId`, `QuoteItemId`, SUM(QtyQuoted*Price)
> FROM `QuoteItem`
> GROUP BY `QuoteId`
> HAVING (`QuoteId` LIKE "250033")
> to get the values of each QuoteItemId.
> There are 7 different items ion this quote, but this query is summing
> them ALL together.
> I want results like:
> 250033, 1, 50
> 250033, 2, 38
> 250033, 3, 75
> 250033, 4, 100
> etc...
> Please advise where I'm getting it wrong.
> I googled the topic and I must just have some killer blinders on.
> I did try a few other things, but no go.
> I know it's going to be simple, but I'm just not seeing it.
> Thanks-a-bunch,
> Mo
Thanks.
I've figured out how to do what I need by replacing the HAVING
statement with a WHERE statement between the FROM and GROUP BY.
Thanks.
-Mo
|||On Apr 2, 10:55Xam, "Plamen Ratchev" <Pla...@.SQLStudio.com> wrote:
> As Tom noted you need to remove the single quotes. I see your grouping is
> incorrect too. Try this:
> SELECT QuoteId, QuoteItemId, SUM(QtyQuoted * Price)
> FROM QuoteItem
> GROUP BY QuoteId, QuoteItemId
> HAVING QuoteId = '250033'
> HTH,
> Plamen Ratchevhttp://www.SQLStudio.com
Thanks, I'll put your advise into operation.
Regards.
|||>>
I've figured out how to do what I need by replacing the HAVING
statement with a WHERE statement between the FROM and GROUP BY.[vbcol=seagreen]
I don't think that was the (only) problem.
:-)