I'm trying to calculate the SUM of MEMDISC but I keep getting this error on a stored procedure
Column 'dbo.ZZ.CNUM is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
This is my code
select
CNUM,
DESCRIPT,
ZCONTDESC,
substring(str(OCCDATE),9,2) + '/'+ substring(str(OCCDATE),7,2) + '/' + substring(str(OCCDATE),3,4),
COWNNUM,
CAST((CAST(substring(str(ANVDATE),3,4) AS INT) -1) AS CHAR(4))+'-' +substring(str(ANVDATE),3,4),
sum(MEMDISC)
from
dbo.ZZ
What it should look like?
As suggested in comments by #Lucky and #LukStorms, when you use aggregation functions you need to GROUP BY all fields which are not included in an aggregation function. In your case you need something like this:
select CNUM, DESCRIPT, ZCONTDESC,
substring(str(OCCDATE),9,2) + '/'+ substring(str(OCCDATE),7,2) + '/' + substring(str(OCCDATE),3,4),
COWNNUM,
CAST((CAST(substring(str(ANVDATE),3,4) AS INT) -1) AS CHAR(4))+'-' +substring(str(ANVDATE),3,4),
sum(MEMDISC)
from dbo.ZZ
group by CNUM, DESCRIPT, ZCONTDESC,
substring(str(OCCDATE),9,2) + '/'+ substring(str(OCCDATE),7,2) + '/' + substring(str(OCCDATE),3,4),
COWNNUM,
CAST((CAST(substring(str(ANVDATE),3,4) AS INT) -1) AS CHAR(4))+'-' +substring(str(ANVDATE),3,4),
Related
I have a basic query that looks like this.
SELECT Database_Name,
FilingDate,
SUM(ISNULL([column1], 0) + ISNULL(column2], 0) +
ISNULL([column3], 0) + ISNULL([column4], 0)) AS Total
FROM SomeTable(NOLOCK)
GROUP BY Database_Name,
FilingDate
ORDER BY Database_Name,
FilingDate DESC
This query outputs results that look like this.
I would like to take the dates returned in the FilingDate column and use them as new column headers with the totals for each database and date being used as the row content. The end result should look like this:
My research suggests that a pivot is the best option but I'm struggling to find the right way to execute it as my dates change each day Any assistance would be appreciated.
If this is MS SQL, you can use a dynamic pivot table. Here is a solution using your query (should work, but I don't have the base data to test it).
SELECT Database_Name,
FilingDate,
SUM( ISNULL(column1 ,0) +
ISNULL(column2],0) +
ISNULL([column3],0) +
ISNULL([column4],0)
) AS Total
INTO #T1
FROM SomeTable(NOLOCK)
GROUP BY Database_Name,
FilingDate
DECLARE #PivotColumnHeaders varchar(MAX)
SELECT #PivotColumnHeaders =
COALESCE(
#PivotColumnHeaders + ',[' + CAST(UC.FilingDate AS NVARCHAR(10)) + ']',
'[' + CAST(UC.FilingDate AS NVARCHAR(10)) + ']'
)
FROM (SELECT FilingDate FROM #T1 GROUP BY FilingDate) UC
DECLARE #PQuery varchar(MAX) = '
SELECT * FROM (SELECT Database_Name, FilingDate, Total FROM #T1 T0) T1
PIVOT (SUM([Total]) FOR FilingDate IN (' + #PivotColumnHeaders + ') ) AS P'
EXECUTE (#PQuery)
DROP TABLE #T1
I am working on a SQL View over a table with different values in it. I am trying to combine two columns in a table to be displayed in a single column of a view separated by a '/'.
For Example I have two columns in a table named In and inVolume with values for 'In' being
1,2,NULL
and for 'inVolume ' being
NULL, 100, 200
and the results I was expecting are 1/(NULL or Empty), 2/100, (NULL or Empty)/200 but when I created the view and ran it the results were (NULL or Empty), 2/100, (NULL or Empty). The issue being it is making the column in view as NULL if any of the columns In or inVolume in the table are NULL.
I created the following view
SQL View
CREATE VIEW [dbo].[Result]
AS
with CTE as(
SELECT distinct
CC.Product,
CC.Term,
CC.TermID,
iCC.In,
iCC.Out,
iCC.inVolume,
iCC.outVolume
FROM Cust CC
CROSS APPLY (SELECT TOP 3
In,
Out,
inVolume,
outVolume
FROM Cust iCC
WHERE CC.Term = iCC.Term and CC.Product = iCC.Product
ORDER BY iCC.In DESC, iCC.Out ASC) iCC)
select Product, Term, cast(inVolume as varchar(99)) + '/' + cast(In as varchar(99)) as value, inlabel as label from CTE
union
select Product, Term, cast(Out as varchar(99)) + '/' + cast(outVolume as varchar(99)), outlabel from CTE
GO
Any better way to deal with this?
Replace this:
cast(outVolume as varchar(99))
Whit this:
cast((CASE WHEN outVolume IS NULL THEN 0 ELSE outVolume END) as varchar(99))
On every field. Hope this help.
Use IsNull to enable nullable-column concatentation
select Product, Term,
IsNull(inVolume, '(NULL OR EMPTY)', cast(inVolume as varchar(99))) + '/' +
IsNull(In, '(NULL OR EMPTY)', cast(In as varchar(99))) as value,
inlabel as label from CTE
UNION
select Product, Term,
IsNull(Out, '(NULL OR EMPTY)', cast(Out as varchar(99))) + '/' +
IsNull(outVolume, '(NULL OR EMPTY)', cast(outVolume as varchar(99))),
outlabel from CTE
Replace
cast(inVolume as varchar(99)) + '/' + cast(In as varchar(99))`
with
cast(inVolume as varchar(99)) + '/'
+ ISNULL(cast(In as varchar(99)), '(NULL OR EMPTY)')`
and
cast(Out as varchar(99)) + '/' + cast(outVolume as varchar(99))`
with
cast(Out as varchar(99)) + '/'
+ ISNULL(cast(outVolume as varchar(99)), '(NULL OR EMPTY)')
I'm having trouble figuring out how to replace a value on a stored procedure result based on values from another table. I have a table [LOG] that is formatted as such:
TIME STAMP, TAG, DESCRIPTION, EVENTCODE, SUBEVENTCODE
30-Aug-2013 10:14:10, TAG X, HI TEMP FAULT, 3, 16
30-Aug-2013 10:12:10, TAG Y, HI PRESS FAULT, 3, 16
...
And another table [EVENTS] which basically describes what the EVENTCODE is:
EVENT, DESCRIPTION
1, FAULT
2, LOGIC
3, ALARM
I would like to have the stored procedure retrieve 2000 entries (rows) of the 1st table and, instead of showing EVENTCODE as a number, display the description contained in the 2nd table on the result.
e.g:
TIME STAMP, TAG, DESCRIPTION, EVENTCODE, SUBEVENTCODE
30-Aug-2013 10:14:10, TAG X, HI TEMP FAULT, ALARM, 16
Reason is I have another software that interacts with the result of the stored procedure, and wouldn't like to create another table to hold these results within the database.
Here is what the stored procedure looks like so far:
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Get2kEvents]
AS
BEGIN
SELECT TOP 2000 CAST(datepart(day,TIME_STAMP) as char(2)) + '-' +
CAST(datename(month,TIME_STAMP) as char(3)) + '-' +
CAST(datepart(year,TIME_STAMP) as char(4))+ ' ' + CONVERT(varchar,TIME_STAMP,108)
as 'TIME STAMP',
[TAG],
[DESCRIPTION],
[EVENTCODE],
[SUBEVENTCODE]
FROM [Arc_DB].[dbo].[LOG]
ORDER BY TIME_STAMP DESC
END
GO
I appreciate your assistance. Sorry if this is too basic, but I wasn't able to figure out a solution for this while browsing this and other websites.
Cheers.
TM
You want to join the two tables. ie:
SELECT TOP 2000 CAST(datepart(day,TIME_STAMP) as char(2)) + '-' +
CAST(datename(month,TIME_STAMP) as char(3)) + '-' +
CAST(datepart(year,TIME_STAMP) as char(4))+ ' ' + CONVERT(varchar,TIME_STAMP,108)
as 'TIME STAMP',
[TAG],
[DESCRIPTION],
Events.Description,
[SUBEVENTCODE]
FROM [Arc_DB].[dbo].[LOG]
inner join events on log.eventcode = events.event
ORDER BY TIME_STAMP DESC
Use a JOIN:
SELECT TOP 2000 CAST(datepart(day,TIME_STAMP) as char(2)) + '-' +
CAST(datename(month,TIME_STAMP) as char(3)) + '-' +
CAST(datepart(year,TIME_STAMP) as char(4))+ ' ' + CONVERT(varchar,TIME_STAMP,108)
as 'TIME STAMP',
[TAG],
L.[DESCRIPTION],
E.[DESCRIPTION],
[SUBEVENTCODE]
FROM [Arc_DB].[dbo].[LOG] L
INNER JOIN [Arc_DB].[dbo].[EVENTS] E ON E.EVENT = L.EVENTCODE
ORDER BY TIME_STAMP DESC
I would use a left join and check for missing codes:
SELECT TOP 2000 CAST(datepart(day,TIME_STAMP) as char(2)) + '-' +
CAST(datename(month,TIME_STAMP) as char(3)) + '-' +
CAST(datepart(year,TIME_STAMP) as char(4))+ ' ' + CONVERT(varchar,TIME_STAMP,108)
as 'TIME STAMP',
[TAG],
[DESCRIPTION],
CASE WHEN EVENTS.DESCRIPTION IS NULL
THEN 'UNKNOWN CODE '+CAST(LOG.EVENTCODE AS VARCHAR(20))
ELSE EVENTS.DESCRIPTION
END AS [Event Type],
[EVENTCODE],
[SUBEVENTCODE]
FROM LOG
LEFT JOIN EVENTS ON EVENTS.EVENT= LOG.EVENTCODE
ORDER BY TIME_STAMP DESC
declare #lkaklf as varchar(Max)
Select ss.Data from SplitString('14,72', ',') as ss
Select #lkaklf = CONVERT(varchar, COALESCE( + #lkaklf + ',', '') + '''' + Data + '''') From
(
Select Data from SplitString('14,72', ',')
)de
select #lkaklf
print #lkaklf
Select * from LPO Where CONVERT(varchar, LPO.LocalPurchaseOrderId) in (#lkaklf)
#lkalf value is Printing in message but not coming into select query... Why ?
You cannot do what you want. I would recommend that you skip over the splitting part of the query and just do:
where ','+#lkaklf+',' like ','+cast(LPO.LocalPurchaseOrderId as varchar(255))+',%'
That is, just use string comparisons.
If you really want to use SplitString(), then put the results in a temporary table:
insert into #t
select data from splitstring('14,72', ',')
And then use a subquery:
where cast(#lkalklf as varchar(255)) in (select data from #t)
I'm trying to update a column with the concatenated and converted results of two other columns in order to create a column with a date field. The SELECT statement returns the values I want to Update with, but I'm missing something (probably simple) on the update. It won't execute because
"the subquery returns more than one value".
However, I don't want to update with the same value for each row, but rather the concatenated result for each row.
What am I missing?
UPDATE myTable
SET myDate =
(
SELECT
CONVERT (Date,(CONVERT (NVarchar, CreatedYear) + '-' + CONVERT (NVarchar, CreatedMonth) + '-' + '01') ,0)
FROM myTable
)
I believe you have an extra SELECT that is not required. Please try:
UPDATE myTable
SET myDate =
CONVERT (Date,(CONVERT (NVarchar, CreatedYear) + '-' + CONVERT (NVarchar, CreatedMonth) + '-' + '01') ,0)
FROM myTable
This is a bit more readable in my opinion:
UPDATE dbo.tblControlMaster SET AuditAddDate = CAST(CreatedYear AS NVARCHAR(4)) + '-' + CAST(CreatedMonth AS NVARCHAR(2)) + '-' + '01'