Transact SQL :: How To Add Subtotal / Grandtotal Using Case And Grouping Sets

Jun 15, 2015

How do you incorporate a case statement so that you can add "sub total" and grand total" to each grouping set section? Trying to see how to incorporate case.

[URL] ....

SELECT
CustomerID,
SalesPersonID,
YEAR(OrderDate) AS 'OrderYear',
SUM(TotalDue) AS 'TotalDue'
FROM Sales.SalesOrderHeader

[Code] .....

View 4 Replies


ADVERTISEMENT

How To Put Subtotal And Grandtotal In A Matrix

Jul 10, 2007

hello how can i put subtotals and grandtotals into a matrix table



ive tried adding a table footer but this option isnt available



thanks

View 1 Replies View Related

Transact SQL :: How To Use GROUPING SETS

Jul 12, 2015

I was working on what I was told was SQL 2012 and it turns out it is SQL 2005.  I wrote two procs that I need to convert to 2005.  Here is the code:

SELECT
era_provider_name AS Provider,
RIGHT([era_upi], 5) AS 'ERA Upi',
[era_fy] AS 'ERA FY',
ProcGrp,
COUNT(DISTINCT UCI) AS 'Client Count',

[Code]....

I'm not finding an efficient way to do this.  I cannot use GROUPING SETS with 2005.  Here is the code for the second proc:

SELECT
CASE
WHEN GROUPING(era_provider_name) = 1 THEN 'TOTALS'
ELSE era_provider_name
END AS era_provider_name,
CASE WHEN GROUPING(era_fy) = 1 THEN 'TOTALFY'

[Code] ...

The results as in SQL 2012 are exactly as I would like them.  I want to mimic those results in 2005.

View 6 Replies View Related

Transact SQL :: How To Get Sum Of Results From GROUPING SETS

Jul 8, 2015

I needed to add in the Fiscal Year (FY) to group my results by FY.  I changed my code to the following:

/*
EXEC ADAMHsp_BSS_GetNonMedicaidReportTotals
@pstrProviderName = 'FAM SER-WOOD'
*/

ALTER PROCEDURE [dbo].[ADAMHsp_BSS_GetNonMedicaidReportTotals]
@pstrProviderNameVARCHAR(100)
AS
BEGIN

[Code] ....

See my result set in the picture below.  The rows with NULL in the 'ProcGrp' column have the totals of the groupings by FY that I am looking for - that's great.  What I want to do now is have another row that contains the sums of the values from any row where 'ProcGrp' is null so that I have a totals row.

View 3 Replies View Related

Transact SQL :: Grouping With Case Statement

Aug 9, 2015

In the below query, I can get the individual/single group by columns as well as multiple but I cannot control the order in which I would like to group by the data.

So lets say I want to group the data by OS->browser->browser_version(just one example) then I cannot achieve that as the order of OS column comes later in the query.

I know one option would be to write a dynamic SQL but i dont want to do that because of performance reasons. Any other way this can be achieved?

select 
case when @include_browser =
1 then browser_name end as browser_name, 
case when @include_browser_version
= 1 then browser_version end
as browser_version,

[Code] ....

View 4 Replies View Related

How To SubTotal With Top N Grouping?

Nov 23, 2005

Hi all,

View 10 Replies View Related

Grouping Results Into Sets Of 5?

Jun 19, 2013

I have been tasked with creating a report that shows sales of our products grouped into buckets of 5 each, DESC. I have a table that has the itemNo and revenue. The final report would be something like:

Top 5 Spreads$695,066
Next 5 Spreads$467,845
Next 5 Spreads$416,946
Next 5 Spreads$361,946
Next 5 Spreads$305,607
Next 5 Spreads$270,567
Bottom Spreads$15,954

My initial thinking was to use row_number() and partition to label the rows in groups of 5 like:

item 130,0001
item 229,0001
item 328,0001
item 427,0001
item 526,0001
item 625,0002
item 724,0002
item 823,0002
item 922,0002
item 1021,0002
item 1120,0003
item 1219,0003
item 1318,0003

And then Sum the revenue, grouping by this row_number. But I haven't been able to get it working right.

View 13 Replies View Related

Problem With Simple Grouping Subtotal

May 13, 2008



In SRS2000, I have a simple grouping matrix report setup like the following:




Code Snippet
=Fields!Date.Value Total
=Fields!Section.Value =Fields!Count.Value
Total

It's essentially a table of departmental statistics that would report like this:




Code Snippet
5/1/08 5/2/08 5/3/08 5/4/08 Total
Dept1 0 99 33 4 XX
Dept2 1 0 2 1 XX
Total XX XX XX XX XX






The report has 2 groups - 1 for the dates and 1 for the departments.

The report data itself displays correctly. My problem comes in when I try to add the Subtotals.

I simply right-click on each of the two Groups and chose "Subtotal". This action inserts the Total column & row respectively and you would think everything is just fine.

Unfortunately when I actually run the report, I see that the Totals it's coming up with are incorrect. In fact, they are the totals of the FIRST row/column only.

Meaning - (using the previous example) - I'd see this:



Code Snippet
5/1/08 5/2/08 5/3/08 5/4/08 Total
Dept1 0 99 33 4 0
Dept2 1 0 2 1 1
Total 0 99 33 4 0


It's like it's not taking anything into account except the first instance.

And it's not like you can actually *see* the expression or formula being used for the Total function. The data cells are just grayed.. all you can really change is the "Total" text.

I must be doing something really stupid here, but for the life of me I don't see what it is.

Any ideas?

Thanks!

View 1 Replies View Related

Need Help On GROUPING Sets Of Rows And Comparison

Jan 24, 2007

Hi folks. Hope all the gurus including Brett,Pat Phalen, RdjBarov, r937 are fine. ;)
been so long to ask stupid question. Here's the question and i really need help on this.

i have data that tracks patterns of bus stops from one point to another.
like point a, to point b, point b to point c forms one pattern.
point a to point c , point c to point b should be a different pattern.


create table #journeypatterns (patternid int ,points varchar(100))
go
insert #journeypatterns
select 1 ,'a' union all select 1 ,'b' union all select 1,'c'
union all select 2,'a' union all select 2,'c' union all select 2,'b'
union all select 3 ,'a' union all select 3 ,'b' union all select 3,'c'


select * from #journeypatterns


patternid points
1 a
1 b
1 c
2 a
2 c
2 b
3 a
3 b
3 c


what i want is to get unique pattern value of sequence of points by grouping on patternid. if the sequence of points change, i need a unique value against that pattern.
like for patternid 1, sequence of points a,bc for example should be abc.
for patternid 2, sequence of points a,c,b for example should be acb.
again patternid 3, sequence of points a,bc for example should be abc.

i tried CHECKSUM_AGG which brutally failed in production because the checksum values for each single point when summed produce SAME result for different patterns.

select checksum_agg(binary_checksum(points)) ,patternid
from #journeypatterns
group by patternid

961
962
963

here patternid 2 should be different because sequence is acb. i know checksum is not the right approach for what i need.

I NEED A GENERIC FUNCTION, that marks the pattern differences, my ultimate goal was to create a procedure, whom a patternid should be passed, and it would result the NEXT patternid in the table which has the SAME ORDER OF point sequences.

now folks, i can do this holding all data into a temp table and write a cursor to traverse through each patternid and concatenate the sequence of points.
BUT, using this approach is the ugliest, as it has slow down the process badly and boss is not happy with the performance. the table holds a lot of data.
I NEED a query rather than a cursor on the fly to resolve this.
Here's the query i am using to get the current sequence of a pattern and then i have to search all sequences similarly against it.

declare @patternid int
set @patternid =1
declare @sequence [varchar] (100)
declare @id varchar(10)
declare cr_sequence cursor fast_forward for select points from #journeypatterns where patternid=@patternid
open cr_sequence
fetch next from cr_sequence into @id
while @@fetch_status = 0
begin
select @sequence = isnull(@sequence,'')+@id
fetch next from cr_sequence into @id
end
print @sequence -- next i have code to find the similar sequence for another patternid.... which is not mentioned here but is similar

View 5 Replies View Related

Grouping Sets Sample With Rollup

Sep 4, 2014

How to writing this query using rollup or grouping sets in two way?

data
shop time date sku discount% sales qty amount
A 13:00pm, 2014-feb-11 apple 30% 5 #20
A 13:00pm, 2014-feb-11 apple 30% 5 #20
A Shop Qty :10 Amt:40
B 23:00pm, 2014-feb-11 apple 30% 5 #20
B 23:00pm, 2014-feb-11 apple 30% 5 #20
B shop Qty :10 Amt $40
Grand total qty:20 , Amt $80

View 1 Replies View Related

Sort Matrix On Dummy Grouping Value/subtotal

Apr 23, 2008



I have matrix with 3 row groupings
Parent, Child, Baby

I have a value in the data cell and also a dummy column grouping to show some % values.

The % is basically what the row subtotal represents as a % of all the data in the dataset.

The Parent row group also has a subtotal, so the % here will show all values within the parent as a % of all data.

I have interactive sorting on Parent, Child and Baby.

but what I want to do is this:

Interactive sorting of the parent-subtotal-percent column values to cause a physical reordering of the parents.

i.e. show me the parents in order of their "% as a total of the entire dataset"

Possible?

View 5 Replies View Related

Grouping (Case)

Nov 27, 2006

I have a table of Stores and the profit generated per mile.

Store|Miles|Profit
Store1|1|20,000
Store1|2|22,000
Store1|3|11,000
Store1|4|20,000
Store1|5|22,000
Store1|6|15,000
Store1|7|20,000
Store1|8|32,000
Store1|9|15,000
Store2|1|20,000
Store2|2|22,000
Store2|3|11,000
Store2|4|20,000
Store2|5|22,000
Store2|6|15,000
Store2|7|20,000
Store2|8|32,000
Store2|9|15,000

I want to group the stores by miles and sum the profit

Store1|Miles 1-5| 95,000
Store1|Miles 6-9| 82,000
Store2|Miles 1-5| 95,000
Store2|Miles 6-9| 82,000

Im guessing its a case stmnt. But i can only figure out how to make the colomn headings labled 1-5 and 6-9.


Thanks,
Dynasty

View 2 Replies View Related

Transact SQL :: Upper Case To Lower Case Conversion

May 4, 2015

I have column with value of all upper case, for example, FIELD SERVICE, is there anyway, I can convert into Field Service?

View 7 Replies View Related

Grouping With Case Statement?

Apr 2, 2008

I am attempting to get one line with all the price info for three fields.
The end result looks like this
PT_UD_KeyPatternPrice CastingPrice MachiningPrice
FG-020006-000 0.00 1840.00 0.00
FG-020006-000 750.00 0.00 0.00
FG-020006-000 0.00 0.00 3945.00

I would like it to look like this

PT_UD_KeyPatternPrice CastingPrice MachiningPrice
FG-020006-000 750.00 1840.00 3945.00

What am I doing wrong?
Here is my query

SELECT PT_UD_KEY,
'PatternPrice' =
CASE
WHEN PT_UD_NUMBER = 1 THEN TOTAL
ELSE 0.00
END,
'CastingPrice' =
CASE
WHEN PT_UD_NUMBER = 15 THEN TOTAL
ELSE 0.00
END,
'MachiningPrice' =
CASE
WHEN PT_UD_NUMBER = 3 THEN TOTAL
ELSE 0.00
END
FROM CAL.dbo.EXT00103
WHERE PT_WINDOW_ID='ITEM PRICE' AND LEFT(PT_UD_KEY,2)='FG' AND PT_UD_NUMBER IN (1,3,15) AND TOTAL >0.00
ORDER BY PT_UD_KEY

View 3 Replies View Related

Transact SQL :: Get 2 Different Result Sets With GetDate Function

Aug 4, 2015

Periodically throughout the day a report is manually pulled from a SQL Server database.  Is their a way w/o me adding a field to the database to have the result set return the "new" results?  For example, lets say this is our DDL

Create Table OneTwoThree
(
id int
,date11 datetime
,firefly varchar(10)
)

Insert Into OneTwoThree Values (1, '08/03/2015 18:43:32.012', 'Hi'), (2, '08/03/2015 18:44:11.011', 'No'),
(3, '08/03/2015 19:36:33.011', 'Second'), (4, '08/03/2015 19:37:33.011', 'Alpha')

Prior I could use this syntax, but that was only with needing to generate 1 result set.  

Select id, convert(varchar(10), date11, 101) As [Date], firefly from onetwothree
where CONVERT(varchar(10), date11, 101) < CONVERT(varchar(10), GetDate(), 101)

Looking at my datetime values, let's say the 1st was generated at 18:45, obviously the 1st two records in the table would be returned. And let's say a 2nd time I need to generate I want to exclude the 1st two entries as they have already been verified. How can I do such w/o adding a field to the table?

View 11 Replies View Related

Joining On And Grouping By CASE Function Column Alias (URGENT)

Apr 14, 2004

I REALLY need to perform a JOIN and a GROUP BY on a CASE function column alias, but I'm receiving an "Invalid column name" error when attempting to run the query. Here's a snippet:

SELECT NewColumn=
CASE
WHEN Table1.Name LIKE '%FOO%' THEN 'FOO TOO'
END,
Table2.SelectCol2
FROM Table1
JOIN Table2 ON NewColumn = Table2.ColumnName
GROUP BY NewColumn, Table2.SelectCol2
ORDER BY Table2.SelectCol2

I really appreciate any help anyone can provide.

Thanks,
DC Ross

View 5 Replies View Related

Transact SQL :: Sum Amount By Grouping Date

Oct 3, 2015

I have a table that have customer name and their bill amount

a) tblBilling

which shows records like below (Billing Dates are shown in Year-Month-Day format)

Invoice   Customer             BillingDate       Amount            Tax
--------------------------------------------------------------------------
1             ABC                       20015-10-2           1000.00            500.00
2             DEF                        20015-10-2           2000.00        1000.00
3             GHI                        20015-10-2           1000.00            500.00
4             JKL                         20015-10-3           5000.00          2500.00
5             MNO                     20015-10-3           1500.00            750.00
6             PQR                       20015-10-4            500.00            250.00
7             STU                        20015-10-4           1000.00           500.00
8             VWX                      20015-10-4            2500.00         1250.00

I want to perform a query that should SUM Amount and Tax Colums by date basis, so we could get the following result

 BillingDate       Amount            Tax
--------------------------------------------------------------------------
20015-10-2           4000.00            2000.00
20015-10-3           6500.00            3250.00
20015-10-4           4000.00            2000.00

View 3 Replies View Related

Transact SQL :: Parent Child Grouping

Nov 4, 2015

I have a table as below

ItemID ParentID
6 NULL
7 NULL
8 7
11 7
12 8

Here ParentID null means it does not have the parent its the master. I want result in below format.

ItemID ParentID
6 NULL
--No parent no child
7 NULL
--No parent but has child
8 7
--7 is the parent of 8
12 8
--8 is the parent
11 7
--7 is the parent

Basically it should be in parent id then child id after that child's childID , In a recursive way.

View 5 Replies View Related

Transact SQL :: Grouping Data Within A DB Table?

Jul 9, 2015

I have a table with couple of hundred thousand records, i have added a new column to the table named MD_Group.

Now i need this column populated, so that every 10,000 row set in that table gets a numeric (incremental number) assigned starting from 1.

So the first 10,000 rows will have MD_Group = 1, next 10,000 rows will have MD_Group = 2 , next 10K MD_Group = 3 etc.

For testing purpose here is a results table with total of 11 rows where we want data grouped every 3 rows.

MD_Group
CustomerNumber
AmountBilled
1
12
15243
1
1231234
15243

[code]....

-- Create Sample Table

Declare @GroupRelation_Test TABLE (
MD_Group [varchar](20),
CustomerNumber [varchar](20) ,
AmountBilled [varchar](20) ,

[code]....

-- Test data

INSERT INTO @GroupRelation_Test
( CustomerNumber, AmountBilled,MinAmounttBilled)
SELECT'12','15243','' UNION ALL
SELECT'1231234','15243','' UNION ALL
SELECT'463','15243','' UNION ALL
SELECT'442','15243','' UNION ALL

[code]....

View 22 Replies View Related

Transact SQL :: Summing With A Function In Grouping

Jun 23, 2015

I have a table with duration values for different machine states. I 'm trying have a sum of the duration value of each state ( the duration sum , was an earlier question).

declare
@tblDurations
TABLE
(StateName
nvarchar(30),Duration
nvarchar(30))

[code]...

I want the output to have sum of the duration ( from my function ), grouped by the state.So output should be :

Breaks 00:02:03:40
Meetings 00:00:01:50
Running  15:21:07:16

I think this can be done with windows functions, but how I don't know.

View 10 Replies View Related

Transact SQL :: Sum Values By Grouping Name From Two Tables

Nov 6, 2015

I have two tbles that have ItemName and their bill amount

a) tblLunch

which shows records like below

Invoice   Item                          Amount  
--------------------------------------------
1             COFFEE                       1000.00 
2             TEA                          2000.00
3             ICE CREAM                1000.00

b) tblDinner

which shows records like below

Invoice   Item                      Amount  
------------------------------------------------------------
1             COFFEE                  1000.00 
2             TEA                        2000.00
3             PASTA         1000.00

I want to perform a query that should SUM Amount Columns by Grouping the Item from both the tables, so we could get the following result

Item                      Amount  
------------------------------
COFFEE                  2000.00 
TEA                        4000.00
ICE CREAM           1000.00
PASTA                    1000.00

View 3 Replies View Related

Transact SQL :: Grouping And Updating The Table

Dec 5, 2015

I have a table like this. 

AS-IS
 
Column1 Column2  count
a b 20
a b 25
c d 12
c d 22

And I need to update the same as below.

TO-BE
 
Column1 Column2  count
a b 45
c d 34

How to do it?

View 4 Replies View Related

Transact SQL :: Query To Generate XML And Grouping?

Jul 29, 2015

I am trying to generate XML path from a SQL Server Table. Table Structure and sample data as below 

CREATE TABLE #OfferTransaction
( [OfferLoanAmount1] INT
,[offferid1ProgramName] VARCHAR(100)
,[Offer1LenderName] VARCHAR(100)
,[offerid1LenderNMLSID] INT

[code]....

what changes do I need in my query so that the XML looks like the one above ( DESIRED XML). Is it possible via query changes?

View 3 Replies View Related

Transact SQL :: Grouping Based Of Column Value

Nov 5, 2015

Below is the data I have in table name

TeamStatus
T 1 Complete or Escalate
T 2 Pick Up
T 2 Resolve Case
T 1 Pick Up
T 1 Complete or Escalate
T 1 Pick Up
T 1 Complete or Escalate

I want to get he group based of Resolve Case value in Status Column. Anything before Resolve case will be considered as Group 1 and after Resolve Case status should be considered as Group 2. Below is desired new Group column,

Group TeamStatus
Group 1 T 1Complete or Escalate
T 2 Pick Up
T 2 Resolve Case

Group 2 T 1Pick Up
T 1Complete or Escalate
T 1 Pick Up
T 1 Complete or Escalate

View 7 Replies View Related

Grandtotal Row Diff Between Analytic And Regular Aggregate Functions?

Nov 22, 2006

Hi,
Assume there is no sale with over 2000 units, the first query returns no row while the latter returns 1 row with a null value. I am not questioning the result, I would like to understand conceptually what semantic difference between the Sql99 analytic function and regular aggregate function makes the result different in this case.

select sum(Quantity) over() from orderdetails where Quantity > 2000


select sum(Quantity) from orderdetails where Quantity > 2000

thanks in advance,
Jeopardy

View 2 Replies View Related

Show Child Subtotal In Parent's Subtotal Row

Apr 1, 2008



My report has two groups, company and error type for each company.
Company1

Functional Error
Data Error
Other Error
My goal is to show the subtotals for each error types when I show Company's subtotal/total; in the group 1 footer area.
Total for Functional Error:
Total for Data Error:
Total for Company1:

Please help me in figuring out how to do that.

Thank you in advance for your help.

View 3 Replies View Related

Transact SQL :: Grouping Similar Rows After Unpivot?

Sep 23, 2015

I've unpivoted some data and stored it in a temp table variable

idNumFreqDtFreqrn
16100120120101M2
16100120120101M3
16100120100101M4
16100120100101M5
16100120060101M6
16100120000929Q7
16100119990101A8
16100119970101M9

Using the above data, if two rows have the same FreqDt, I want to see the record with the lowest row number.

So it should look like the below

idNumFreqDtFreqrn
16100120120101M2
16100120100101M4
16100120060101M6
16100120000929Q7
16100119990101A8
16100119970101M9

I've used the below code to accomplish it

SELECT DISTINCT
CASE WHEN t2.idNum IS NULL THEN t1.idNum ELSE t2.idNum END,
CASE WHEN t2.FreqDt IS NULL THEN T1.FreqDt else t2.FreqDt END,
CASE WHEN t2.freq is null then t1.freq else t2.freq end
FROM @tmptbl as t1 LEFT JOIN @tmptbl as t2
ON t1.idNum = T2.idNum
AND t1.FreqDt = t2.FreqDt
AND t1.rn = (t2.rn-1)

After all this, I'm supposed to condense the result set to only include sequential frequency dates with unique frequencies.should look like below (this is where I'm stuck)

idNumFreqDtFreq
16100120060101M
16100120000929Q
16100119990101A
16100119970101M

answer is below:

SELECT T1.*
FROM @t as t1 LEFT JOIN @t as t2
ON t1.idnum = T2.idnum
AND t1.freq = t2.freq
AND t1.rn = (t2.rn-1)
WHERE t2.idnum IS NULL

View 5 Replies View Related

Transact SQL :: Grouping Records With A Date Range Into Islands

Nov 18, 2015

I tried to ask a similar question yesterday and got shot down, so I'll try again in a different way.  I have been looking online at the gaps and islands approach, and it seems to always be referencing a singular field, so i can't find anything which is clear to get my head around it.In the context of a hotel (people checking in and out) I would like to identify how long someone has been staying at the hotel (The Island?) regardless if they checked out and back in the following day.

Data example:
DECLARE @LengthOfStay TABLE
(
PersonVARCHAR(8) NOT NULL,
CheckInDATE NOT NULL,
CheckOutDATE NULL

[code]...

View 7 Replies View Related

Transact SQL -- How Do I Case &>?

Dec 8, 2005

Hello, I am having some transact SQL problems.

ALLOWED:

Column1,
CASE FOO
WHEN 1 THEN 'Hello'
ELSE 'Goodbye'
END as MyValue,
Column2


NOT ALLOWED:

Column1,
CASE FOO
WHEN > 1 THEN 'Hello'
ELSE 'Goodbye'
END as MyValue,
Column2


The problem seems to be the > symbol. Apparantly the case statement is only for equality checks, and nothing else.

So, what can I do to fix this? I have these kinds of comparisons all over.

~Le

View 2 Replies View Related

Transact SQL :: CASE Statement For A WHERE

Sep 25, 2015

I've been beating my head against this for a bit and haven't been able to figure this out I want to pull 1 set of values between a date time range if @Report = '1' but if @Report = 2 or 3 I want it to drop 3 of the parameters so it will pull all items. by dropping the where parameters for @BeginRangeDate and @LookOutDate and L.COLineStatus. My report works great if I go in and run Report 1 with the date parameters in the where statement and also works great for Reports 2, 3 if I REM out those 3 items in the Where statement. Here is what I currently have

CASE@Report
WHEN'1'THEN
WHEREL.RequestedShipDate>=@BeginRangeDate
ANDL.RequestedShipDate<=@LookoutDate
ANDDO.ForecastDate>=@ForecastDate

[Code] .....

View 7 Replies View Related

Transact SQL :: How To Use More Than 10 Case Statements

May 12, 2015

I need to breakdown some information with just initials (sometimes 2, sometimes 3 if the initials are already used) SQL Server throws the error of:

Msg 125, Level 15, State 4, Line 1

Case expressions may only be nested to level 10.

What should I alter, or how should I write this query so I can use all of the needed case statements? (their are actually about 24 when statements, but this is just to get a working example to display)

Select
case
when employee_name Like 'Jorge Jones' Then 'JJ'
when employee_name Like 'Mike Mikes' Then 'MM'
when employee_name Like 'Albert Alvarez' Then 'AA'
when employee_name Like 'Hernandez-Sotata' Then 'HS'

[code]....

View 5 Replies View Related

Transact-SQL Help - CASE Statement And Group By

Jul 20, 2005

I've always been mistified why you can't use a column alias in the group byclause (i.e. you have to re-iterate the entire expression in the group byclause after having already done it once in the select statement). I'mmostly a SQL hobbiest, so it's possible that I am not doing this in the mostefficient manner. Anyone care to comment on this with relation to thefollowing example (is there a way to acheive this without re-stating theentire CASE statement again in the Group By clause?):Select 'Age' =CaseWHEN(SubmittedOn >= DATEADD(dd, - 30, GETDATE())) AND (SubmittedOn <=DATEADD(dd, - 0, GETDATE()))THEN '0-30 Days Old'WHEN(SubmittedOn >= DATEADD(dd, - 60, GETDATE())) AND (SubmittedOn <=DATEADD(dd, - 31, GETDATE())) Then '31-60 Days Old'WHEN(SubmittedOn >= DATEADD(dd, - 90, GETDATE())) AND (SubmittedOn <=DATEADD(dd, - 61, GETDATE())) Then '61-90 Days Old'WHEN(SubmittedOn <= DATEADD(dd, - 91, GETDATE())) THEN '91+ Days Old'ELSE cast(SubmittedOn as varchar(22))end,max(SubmittedOn), COUNT(SCRID) AS NbrSCRsFrom SCRViewWHERE(StatusSort < 90) ANDCustomerID = 8 andUserID = 133group byCaseWHEN(SubmittedOn >= DATEADD(dd, - 30, GETDATE())) AND (SubmittedOn <=DATEADD(dd, - 0, GETDATE()))THEN '0-30 Days Old'WHEN(SubmittedOn >= DATEADD(dd, - 60, GETDATE())) AND (SubmittedOn <=DATEADD(dd, - 31, GETDATE())) Then '31-60 Days Old'WHEN(SubmittedOn >= DATEADD(dd, - 90, GETDATE())) AND (SubmittedOn <=DATEADD(dd, - 61, GETDATE())) Then '61-90 Days Old'WHEN(SubmittedOn <= DATEADD(dd, - 91, GETDATE())) THEN '91+ Days Old'ELSE cast(SubmittedOn as varchar(22))endOrder by max(submittedon) descThanks,Chad

View 4 Replies View Related

Transact SQL :: CASE Expression - NULL

Jun 19, 2015

DECLARE @I1 VARCHAR(5),
@I2 VARCHAR(5)
;
WITH cte
AS (SELECT 1 AS i,
'val1' AS j UNION ALL
SELECT 2,
'val2')

[code]....

Why @i1 is null but not @i2 ? I'm trying to assign values after grouping by the column.

View 17 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved