How to convert Int to Varchar - c#

I tried to convert Int to Varchar but I'm unable to get output. Can anyone suggest any other way to do so?
I have below query and need to cast StoreNo (int) to a varchar:
ALTER PROCEDURE [dbo].[getrevenue]
#date1 DATE,
#date2 DATE,
#StoreNo NVARCHAR(max)
AS
BEGIN
DECLARE #sql_test NVARCHAR(max)
--SET #StoreNo='68,78,104'
SET #sql_test = 'SELECT t1.transtoreno AS StoreNO
,t3.NAME AS NAME
,t1.Dealdate AS DATE
,t1.UKEI AS UKEI
,t2.SubTotal AS SubTotal
,ISNULL(t2.SubTotalDiscount, 0) AS SubToatlDiscount
,ISNULL(t1.twoeyesSubtotalDiscount, 0) AS TwoeyeSubTotalDiscount
,t2.ValueInquiries AS TotalDiscount
,t2.NetSale AS Netsale
,t2.TotalSale AS ToatlSale
,t2.Cash AS Cash
,ISNULL(t2.GiftVochuer, 0) AS GiftVochuer
,ISNULL(t2.Card, 0) AS Card
,ISNULl(t2.Suica, 0) AS Suica
,t2.WONPOINT AS WAONPOINT
,t1.TaxExemption AS TAXExemption
,t2.TaxTotal AS TaxTotal
,t2.Returngoods AS Returngoods
,t2.Regiminus AS RegiMinus
,t2.PrintRecipt AS printrecipt
,ISNULL(t1.Visitorcount, 0) AS VisitorCount
FROM (
SELECT CAST(StoreNo AS NVARCHAR) AS transtoreno
,(DealDate) AS Dealdate
,SUM(SalePrice) AS UKEI
,SUM(TansuNebikiPrice) AS twoeyesdicount
,SUM(SubTotalNebiki2Price) AS twoeyesSubtotalDiscount
,SUM(TotalSalePrice - Si1Tax - RegiMinusNo) AS Netsale
,SUM(SpecialConsumptionTaxPrice) AS TaxExemption
,Sum(RegiMinusNo) AS Receiptissue
,SUM(VisitorCount) AS Visitorcount
FROM POS_TtlTran
GROUP BY StoreNo
,DealDate
) t1
LEFT OUTER JOIN (
SELECT DATE AS D
,cast(StoreNo AS NVARCHAR) AS s
,SUM(CASE
WHEN SerialNo LIKE 23
THEN DayTotalAmt
ELSE 0
END) AS Cash
,SUM(CASE
WHEN SerialNo LIKE 31
THEN DayTotalAmt
ELSE 0
END) AS Card
,SUM(CASE
WHEN SerialNo LIKE 30
THEN DayTotalAmt
ELSE 0
END) AS GiftVochuer
,SUM(CASE
WHEN SerialNo LIKE 138
THEN DayTotalAmt
ELSE 0
END) AS Returngoods
,SUM(CASE
WHEN SerialNo LIKE 160
THEN DayTotalAmt
ELSE 0
END) AS PrintRecipt
,SUM(CASE
WHEN SerialNo LIKE 304
THEN DayTotalAmt
ELSE 0
END) AS Suica
,SUM(CASE
WHEN SerialNo LIKE 26
THEN DayTotalAmt
ELSE 0
END) AS WONPOINT
,SUM(CASE
WHEN SerialNo LIKE 139
THEN DayTotalAmt
ELSE 0
END) AS Regiminus
,SUM(CASE
WHEN SerialNo LIKE 4
THEN DayTotalAmt
ELSE 0
END) AS SubToTal
,SUM(CASE
WHEN SerialNo LIKE 7
THEN DayTotalAmt
ELSE 0
END) AS SubTotalDiscount
,SUM(CASE
WHEN SerialNo LIKE 8
THEN DayTotalAmt
ELSE 0
END) AS TwoeyesubTotalDiscount
,SUM(CASE
WHEN SerialNo LIKE 18
THEN DayTotalAmt
ELSE 0
END) AS ValueInquiries
,SUM(CASE
WHEN SerialNo LIKE 22
THEN DayTotalAmt
ELSE 0
END) AS TotalSale
,SUM(CASE
WHEN SerialNo LIKE 114
THEN DayTotalAmt
ELSE 0
END) AS TaxTotal
,SUM(CASE
WHEN SerialNo LIKE 2
THEN DayTotalAmt
ELSE 0
END) AS NetSale
FROM POS_FinTtl
GROUP BY StoreNo
,DATE
) t2 ON t1.transtoreno = t2.s
AND t1.Dealdate = t2.D
LEFT OUTER JOIN (
SELECT StoreNo AS No
,StoreName AS NAME
FROM Store
) t3 ON t2.s = t3.No
WHERE (
t1.transtoreno IN ('''
+ CAST(#StoreNo AS NVARCHAR(max)) + ''')
AND (t1.Dealdate between ''' + CAST(#date1 AS VARCHAR(30)) + '''
AND ''' + CAST(#date2 AS VARCHAR(30)) + '''))'
END

Hope this one will work
ALTER PROCEDURE [dbo].[getrevenue] #date1 DATE
,#date2 DATE
,#StoreNo NVARCHAR(max) `
AS
BEGIN
DECLARE #sql_test NVARCHAR(max)
--SET #StoreNo='68,78,104'
SET #sql_test ='SELECT t1.transtoreno AS StoreNO
,t3.NAME AS NAME
,t1.Dealdate AS DATE
,t1.UKEI AS UKEI
,t2.SubTotal AS SubTotal
,ISNULL(t2.SubTotalDiscount, 0) AS SubToatlDiscount
,ISNULL(t1.twoeyesSubtotalDiscount, 0) AS TwoeyeSubTotalDiscount
,t2.ValueInquiries AS TotalDiscount
,t2.NetSale AS Netsale
,t2.TotalSale AS ToatlSale
,t2.Cash AS Cash
,ISNULL(t2.GiftVochuer, 0) AS GiftVochuer
,ISNULL(t2.Card, 0) AS Card
,ISNULl(t2.Suica, 0) AS Suica
,t2.WONPOINT AS WAONPOINT
,t1.TaxExemption AS TAXExemption
,t2.TaxTotal AS TaxTotal
,t2.Returngoods AS Returngoods
,t2.Regiminus AS RegiMinus
,t2.PrintRecipt AS printrecipt
,ISNULL(t1.Visitorcount, 0) AS VisitorCount
FROM (
SELECT CAST(StoreNo AS NVARCHAR) AS transtoreno
,(DealDate) AS Dealdate
,SUM(SalePrice) AS UKEI
,SUM(TansuNebikiPrice) AS twoeyesdicount
,SUM(SubTotalNebiki2Price) AS twoeyesSubtotalDiscount
,SUM(TotalSalePrice - Si1Tax - RegiMinusNo) AS Netsale
,SUM(SpecialConsumptionTaxPrice) AS TaxExemption
,Sum(RegiMinusNo) AS Receiptissue
,SUM(VisitorCount) AS Visitorcount
FROM POS_TtlTran
GROUP BY StoreNo
,DealDate
) t1
LEFT OUTER JOIN (
SELECT DATE AS D
,cast(StoreNo AS NVARCHAR) AS s
,SUM(CASE
WHEN SerialNo LIKE 23
THEN DayTotalAmt
ELSE 0
END) AS Cash
,SUM(CASE
WHEN SerialNo LIKE 31
THEN DayTotalAmt
ELSE 0
END) AS Card
,SUM(CASE
WHEN SerialNo LIKE 30
THEN DayTotalAmt
ELSE 0
END) AS GiftVochuer
,SUM(CASE
WHEN SerialNo LIKE 138
THEN DayTotalAmt
ELSE 0
END) AS Returngoods
,SUM(CASE
WHEN SerialNo LIKE 160
THEN DayTotalAmt
ELSE 0
END) AS PrintRecipt
,SUM(CASE
WHEN SerialNo LIKE 304
THEN DayTotalAmt
ELSE 0
END) AS Suica
,SUM(CASE
WHEN SerialNo LIKE 26
THEN DayTotalAmt
ELSE 0
END) AS WONPOINT
,SUM(CASE
WHEN SerialNo LIKE 139
THEN DayTotalAmt
ELSE 0
END) AS Regiminus
,SUM(CASE
WHEN SerialNo LIKE 4
THEN DayTotalAmt
ELSE 0
END) AS SubToTal
,SUM(CASE
WHEN SerialNo LIKE 7
THEN DayTotalAmt
ELSE 0
END) AS SubTotalDiscount
,SUM(CASE
WHEN SerialNo LIKE 8
THEN DayTotalAmt
ELSE 0
END) AS TwoeyesubTotalDiscount
,SUM(CASE
WHEN SerialNo LIKE 18
THEN DayTotalAmt
ELSE 0
END) AS ValueInquiries
,SUM(CASE
WHEN SerialNo LIKE 22
THEN DayTotalAmt
ELSE 0
END) AS TotalSale
,SUM(CASE
WHEN SerialNo LIKE 114
THEN DayTotalAmt
ELSE 0
END) AS TaxTotal
,SUM(CASE
WHEN SerialNo LIKE 2
THEN DayTotalAmt
ELSE 0
END) AS NetSale
FROM POS_FinTtl
GROUP BY StoreNo
,DATE
) t2 ON t1.transtoreno = t2.s
AND t1.Dealdate = t2.D
LEFT OUTER JOIN (
SELECT StoreNo AS No
,StoreName AS NAME
FROM Store
) t3 ON t2.s = t3.No
WHERE (
t1.transtoreno IN ('''
+ CONVERT(varchar(MAX), #StoreNo) + ''')
AND (t1.Dealdate between ''' + CAST(#date1 AS VARCHAR(30)) + '''
AND ''' + CAST(#date2 AS VARCHAR(30)) + '''))'
END

where (t1.transtoreno IN ('''
+ CAST(#StoreNo AS nvarchar(max)) + ''')
Should be
where (t1.transtoreno IN ('''
+ replace(CAST(#StoreNo AS nvarchar(max)),',',''',''') + ''')

Related

SQL Server - An expression of non-boolean type specified in a context where a condition is expected [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
This post was edited and submitted for review 21 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
This way actually work in C#. But it does not work in SQL Server when executed:
Select
t3.MNAME + cast(t3.N as nvarchar) as 'کد دسته بندي',
t3.NAME as 'شرح دسته',
sum(case when t1.DT = 1 then t1.N1 - t1.N2 else 0 end) as 'خريد',
sum(case when t1.DT = 8 then t1.N1 - t1.N2 else 0 end) as 'برگشت خريد',
'' as 'حواله شعب',
'' as 'برگشت شعب',
'' as 'فروش',
sum(case when t1.DT = 9 then t1.N1 - t1.N2 else 0 end) as 'برگشت فروش',
sum(case when t1.DT = 10 then t1.N1 - t1.N2 else 0 end) as 'ت کاردکس',
sum(N1 - N2) as 'موجودي',
sum(case when t1.DT = 4 then t1.N1 - t1.N2 else 0 end) as 'ح شعب',
sum(case when t1.DT = 5 then t1.N1 - t1.N2 else 0 end) as 'ح ت شعب',
sum(case when t1.DT = 6 then t1.N1 - t1.N2 else 0 end) as 'ب شعب',
sum(case when t1.DT = 7 then t1.N1 - t1.N2 else 0 end) as 'ب ت شعب',
sum(case when t1.DT = 2 then t1.N1 - t1.N2 else 0 end) as 'ح ت فروش',
sum(case when t1.DT = 3 then t1.N1 - t1.N2 else 0 end) as 'فروش جزئي',
sum(case when t1.DT = 12 then t1.N1 - t1.N2 else 0 end) as 'فروش عادي',
sum(case when t1.DT = 11 then t1.N1 - t1.N2 else 0 end) as 'برگشت فروش تعدادي'
From
BC as t1, _TBS_TYPE as t3
Where
substring(t1.BARCODE, 1, 1) = t3.MNAME
and substring(t1.BARCODE, 2, 1) = cast(t3.N as nvarchar)
and t3.MNAME + cast(t3.N as nvarchar) and t3.NAME
group by
t3.MNAME, t3.N, t3.NAME
order by
2, 3
Error :
An expression of non-boolean type specified in a context where a condition is expected, near 'and'.
Try the below - without assigning some value you define a condition like - and t3.MNAME + cast(t3.N as nvarchar) and t3.NAME
Also, it is best to use explicit JOIN
Select t3.MNAME+cast(t3.N as nvarchar) as 'کد دسته بندي',
t3.NAME as 'شرح دسته',
sum(case when t1.DT = 1 then t1.N1-t1.N2 else 0 end) as 'خريد',
sum(case when t1.DT = 8 then t1.N1-t1.N2 else 0 end) as 'برگشت خريد',
'' as 'حواله شعب',
'' as 'برگشت شعب',
'' as 'فروش',
sum(case when t1.DT = 9 then t1.N1-t1.N2 else 0 end) as 'برگشت فروش',
sum(case when t1.DT = 10 then t1.N1-t1.N2 else 0 end) as 'ت کاردکس',
sum(N1-N2) as 'موجودي' ,
sum(case when t1.DT = 4 then t1.N1-t1.N2 else 0 end) as 'ح شعب',
sum(case when t1.DT = 5 then t1.N1-t1.N2 else 0 end) as 'ح ت شعب',
sum(case when t1.DT = 6 then t1.N1-t1.N2 else 0 end) as 'ب شعب',
sum(case when t1.DT = 7 then t1.N1-t1.N2 else 0 end) as 'ب ت شعب',
sum(case when t1.DT = 2 then t1.N1-t1.N2 else 0 end) as 'ح ت فروش',
sum(case when t1.DT = 3 then t1.N1-t1.N2 else 0 end) as 'فروش جزئي',
sum(case when t1.DT = 12 then t1.N1-t1.N2 else 0 end) as 'فروش عادي',
sum(case when t1.DT = 11 then t1.N1-t1.N2 else 0 end) as 'برگشت فروش تعدادي'
From BC as t1 inner join _TBS_TYPE as t3
on substring(t1.BARCODE,1,1)=t3.MNAME and substring(t1.BARCODE,2,1)=cast(t3.N as nvarchar)
group by t3.MNAME,t3.N,t3.NAME
order by 2,3
In Where condition filter right side of the value is missing in your code.
Where
substring(t1.BARCODE, 1, 1) = t3.MNAME
and substring(t1.BARCODE, 2, 1) = cast(t3.N as nvarchar)
and t3.MNAME + cast(t3.N as nvarchar) and t3.NAME -------->Missing code here

SQL Server : searching for occurrences in different months

I am trying to do a project which uses Visifire to display graphed data. It takes in a single DataTable. I need to create an SQL query that searches a date column for a particular month (format is: Friday, 03 November 2017) and then stores how many times that month is repeated. It has to do this for all 12 months. This is done in C# in visual studio. Currently, it displays no data on the line graph.
public DataTable ReadDataMonthlyBooked()
{
// Declare references (for table, reader and command)
DataTable monthlyBookedTable = new DataTable();
SqlDataReader reader;
SqlCommand command;
string selectString = "SELECT COUNT (BookingNum) AS Bookings "
+ "FROM Booking "
+ "WHERE Booking.EndDate LIKE 'January' "
+ "AND Booking.EndDate LIKE 'February' "
+ "AND Booking.EndDate LIKE 'March' "
+ "AND Booking.EndDate LIKE 'April' "
+ "AND Booking.EndDate LIKE 'May' "
+ "AND Booking.EndDate LIKE 'June' "
+ "AND Booking.EndDate LIKE 'July' "
+ "AND Booking.EndDate LIKE 'August' "
+ "AND Booking.EndDate LIKE 'September' "
+ "AND Booking.EndDate LIKE 'October' "
+ "AND Booking.EndDate LIKE 'November' "
+ "AND Booking.EndDate LIKE 'December'";
try
{
// Create a new command
command = new SqlCommand(selectString, cnMain);
// open the connection
cnMain.Open();
command.CommandType = CommandType.Text;
reader = command.ExecuteReader();
// read data from readerObject and load in table
monthlyBookedTable.Load(reader);
reader.Close(); //close the reader
cnMain.Close(); //close the connection
return monthlyBookedTable;
}
catch (Exception ex)
{
return (null);
}
}
This is the current SQL query I have and I hope its hopelessly wrong with logic errors but I do not know SQL well enough.
select 'Friday, 03 November 2017'
select cast('03 November 2017' as date)
select SUBSTRING('Friday, 03 November 2017', CHARINDEX(',', 'Friday, 03 November 2017') + 2, 100)
select cast(SUBSTRING('Friday, 03 November 2017', CHARINDEX(',', 'Friday, 03 November 2017') + 2, 100) as date)
select datepart(month, cast(SUBSTRING('Friday, 03 November 2017', CHARINDEX(',', 'Friday, 03 November 2017') + 2, 100) as date))
select datepart(month, cast(Booking.EndDate, CHARINDEX(',', Booking.EndDate) + 2, 100) as date))
, COUNT (BookingNum) AS Bookings "
from booking
group by datepart(month, cast(Booking.EndDate, CHARINDEX(',', Booking.EndDate) + 2, 100) as date))
You'll need a group by, like this
SELECT COUNT(BookingNum) as Bookings, MONTH(EndDate) as Month
FROM Booking
GROUP BY MONTH(EndDate)
But as your EndDate is a varchar, you'll have to extract the month
SELECT COUNT(BookingNum) as Bookings
, SUBSTRING(EndDate,
CHARINDEX(',',EndDate) + 4,
CHARINDEX(' ',EndDate, CHARINDEX(',',EndDate) + 5) - CHARINDEX(',',EndDate) + 4)
FROM Booking
GROUP BY SUBSTRING(EndDate,
CHARINDEX(',',EndDate) + 4,
CHARINDEX(' ',EndDate, CHARINDEX(',',EndDate) + 5) - CHARINDEX(',',EndDate) + 4)
There may be a better way to do this (cursors maybe?) but basically you want to get the location of the first ',' and add 4 to get the month (assuming the date is always 2 digits like in your example) and then use that as a start index for a search of the first blank (that'll be the end of the month name) and substract the first index we found to get the length fo the month.
So, basically, just change the date to a DATE field
or cast it
SELECT COUNT(BookingNum) as Bookings, MONTH(CONVERT(DATE, EndDate)) as Month
FROM Booking
GROUP BY MONTH(CONVERT(DATE, EndDate))
But I don't know if the format you are using will be interpreted correctly by the engine.
The error is in your SQL statement.
SELECT
COUNT(BookingNum) AS Bookings
FROM
Booking
WHERE
Booking.EndDate LIKE '%January%',
AND Booking.EndDate LIKE '%February%',
AND Booking.EndDate LIKE '%March%',
AND Booking.EndDate LIKE '%April%',
AND Booking.EndDate LIKE '%May%',
AND Booking.EndDate LIKE '%June%',
AND Booking.EndDate LIKE '%July%',
AND Booking.EndDate LIKE '%August%',
AND Booking.EndDate LIKE '%September%',
AND Booking.EndDate LIKE '%October%',
AND Booking.EndDate LIKE '%November%',
AND Booking.EndDate LIKE '%December%',
You try to get a record where the month is every month in the year at the same time.
If I'm right, you try to get the number of bookings per month.
So it should look something much more like
SELECT
SUM(CASE WHEN Booking.EndDate LIKE '%January%' THEN 1 ELSE 0 END) AS JanuaryBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%February%' THEN 1 ELSE 0 END) AS FebruaryBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%March%' THEN 1 ELSE 0 END) AS MarchBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%April%' THEN 1 ELSE 0 END) AS AprilBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%May%' THEN 1 ELSE 0 END) AS MayBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%June%' THEN 1 ELSE 0 END) AS JuneBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%July%' THEN 1 ELSE 0 END) AS JulyBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%August%' THEN 1 ELSE 0 END) AS AugustBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%September%' THEN 1 ELSE 0 END) AS SeptemberBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%October%' THEN 1 ELSE 0 END) AS OctoberBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%November%' THEN 1 ELSE 0 END) AS NovemberBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%December%' THEN 1 ELSE 0 END) AS DecemberBookings
FROM
Booking

Calculate Monday as first day of the week with a stored procedure

I have this C# code to pull up the number of postings created within the last week, which is working fine. But it's pulling up count from "last 7 days"; what I want to do is to set the first day of the week to "Monday" so that when we pull up the count of posting for "week", it should pull up the number from "monday to sunday" instead of default "last 7 days".
I created a class to set Monday as the 1st day of the week, but what I want to know is how can I (if I can) encapsulate that method in this code to simply pull count of posting etc
Here is my code:
if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
{
lblJobPostings.Text = ds.Tables[0].Rows[0]["new_job_posting_this_week"].ToString();
}
if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
{
lblNewEmployers.Text = ds.Tables[1].Rows[0]["new_employer_this_week"].ToString();
}
if (ds.Tables[2] != null && ds.Tables[2].Rows.Count > 0)
{
lblNewInstitutes.Text = ds.Tables[2].Rows[0]["new_institutes_this_week"].ToString();
}
public static DateTime CallFirstDayOfWeek(DateTime input)
{
int Delta = (7 - (DayOfWeek.Monday - input.DayOfWeek)) % 7;
return input.AddDays(-Delta);
}
Stored procedure:
select COUNT(od.id) [new_job_posting_this_week]
from rs_job_posting od
where od.date_created = GETDATE()-7
Try this in your Stored Procedure:
DECLARE #CurrentWeekday AS INT
DECLARE #LastSunday AS DATETIME
DECLARE #LastMonday AS DATETIME
SET #CurrentWeekday = DATEPART(WEEKDAY, GETDATE())
// Count backwards from today to get to the most recent Sunday.
// (#CurrentWeekday % 7) - 1 will give the number of days since Sunday;
// -1 negates for subtraction.
SET #LastSunday = DATEADD(DAY, -1 * (( #CurrentWeekday % 7) - 1), GETDATE())
// Monday is obviously one day after last Sunday.
SET #LastMonday = DATEADD(DAY, 1, #LastSunday)
SELECT COUNT(od.id) [new_job_posting_this_week]
FROM rs_job_posting od
WHERE od.date_created >= #LastMonday
select COUNT(od.id) [new_job_posting_this_week]
from rs_job_posting od
where od.date_created Between Convert(DateTime, DATEADD(D,-7,GETDATE())) And Convert(DateTime, GetDate())
To select 7 days starting on say last Monday, iterate through the last 7 days in C# until you hit Monday, then pass that date into the SQL Proc.
for (int i = 0; i < 7; i++)
{
DateTime mydate = DateTime.Now.AddDays(-1);
if (mydate.DayOfWeek == DayOfWeek.Monday)
{
//call sp here
}
}

sum of columns that are already sum in sql

SELECT DISTINCT emp_no, sum(CASE WHEN isnull(PresAbs,0)='WO' THEN 1 ELSE 0 END) WO,
sum(CASE WHEN isnull(PresAbs,0)='WP' THEN 1 ELSE 0 END) WP,
sum(CASE WHEN isnull(PresAbs,0)='HL' THEN 1 ELSE 0 END) HL,
sum(CASE WHEN isnull(PresAbs,0)='A' THEN 1 ELSE 0 END) A,**sum(WO+WP+HL+A)**
from TRN_ATTN072013 WHERE CONVERT(varchar,Tdate,112)>'20130712' and CONVERT(varchar,Tdate,112)<'20130717'
group by emp_no
ORDER BY emp_no
i want to sum up this columns WO,WP,HL,A how do i sum these columns
If this is SQL server then you can use CTE
with tablesum as
(
SELECT DISTINCT emp_no, sum(CASE WHEN isnull(PresAbs,0)='WO' THEN 1 ELSE 0 END) WO,
sum(CASE WHEN isnull(PresAbs,0)='WP' THEN 1 ELSE 0 END) WP,
sum(CASE WHEN isnull(PresAbs,0)='HL' THEN 1 ELSE 0 END) HL,
sum(CASE WHEN isnull(PresAbs,0)='A' THEN 1 ELSE 0 END) A
from TRN_ATTN072013
WHERE CONVERT(varchar,Tdate,112) > '20130712' and CONVERT(varchar,Tdate,112) < '20130717'
group by emp_no
)
select t.*, t.WO + t.WP + t.HL + t.A
from tablesum t
order by t.emp_no
in mysql ;)
SELECT emp_no, wo+wp+hl+A FROM
(SELECT DISTINCT emp_no, sum(CASE WHEN isnull(PresAbs,0)='WO' THEN 1 ELSE 0 END) WO,
sum(CASE WHEN isnull(PresAbs,0)='WP' THEN 1 ELSE 0 END) WP,
sum(CASE WHEN isnull(PresAbs,0)='HL' THEN 1 ELSE 0 END) HL,
sum(CASE WHEN isnull(PresAbs,0)='A' THEN 1 ELSE 0 END) A,**sum(WO+WP+HL+A)**
from TRN_ATTN072013 WHERE CONVERT(varchar,Tdate,112)>'20130712' and CONVERT(varchar,Tdate,112)<'20130717'
group by emp_no
ORDER BY emp_no) AS SUB_Q;

How to Nested Case in Select SQL?

I have this select Case SQL statement which compute the totalvolume of a given quantity.
SELECT
DropshipPackinglist.CaseNumber as 'CASE NO.',
DropshipPackinglist.ItemNumber as 'BOM NO.',
DropshipPackinglist.Quantity as 'QTY',
CASE
WHEN DropshipPackinglist.Quantity >=31 and DropshipPackinglist.Quantity <= 36 then '1090x730x1460'
WHEN DropshipPackinglist.Quantity >=25 and DropshipPackinglist.Quantity <= 30 then '1090x730x1230'
WHEN DropshipPackinglist.Quantity >=19 and DropshipPackinglist.Quantity <= 24 then '1090x730x1000'
WHEN DropshipPackinglist.Quantity >=13 and DropshipPackinglist.Quantity <= 18 then '1090x720x790'
WHEN DropshipPackinglist.Quantity >=7 and DropshipPackinglist.Quantity <= 17 then '1090x720x570'
WHEN DropshipPackinglist.Quantity >=1 and DropshipPackinglist.Quantity <= 6 then '1090x720x350'
ELSE 'Unkown'
end
as 'TOTAL VOLUME (MM3)'
FROM DropshipPackinglist INNER JOIN
HuaweiDescription ON DropshipPackinglist.ItemNumber = HuaweiDescription.ItemNumber
WHERE (DropshipPackinglist.BatchCode LIKE '%0005041007100AHWA11HG')
-------------------------------------------------------------------------------------------
Result:
CaseNumber ItemNumber Quantity TotalVolume
1 52411573 5 1090x720x350
1 52411576 20 1090x730x1000
2 52411576 36 1090x730x1460
-------------------------------------------------------------------------------------------
Now is, i want to group casenumber and result with only one totalvolume.
And the result will be this one.
CaseNumber ItemNumber Quantity TotalVolume
1 52411573 5 1090x730x1230 -- sum(casenumber 1)=25
1 52411576 20 1090x730x1230 --
2 52411576 36 1090x730x1460
How to solve this one..thanks in regards.
;with SuperSelect as
(
SELECT dpl.CaseNumber as 'CASE NO.'
,dpl.ItemNumber as 'BOM NO.'
,dpl.Quantity as 'QTY'
,CASE WHEN dpl.Quantity >= 31 and dpl.Quantity <= 36 then '1090x730x1460'
WHEN dpl.Quantity >= 25 and dpl.Quantity <= 30 then '1090x730x1230'
WHEN dpl.Quantity >= 19 and dpl.Quantity <= 24 then '1090x730x1000'
WHEN dpl.Quantity >= 13 and dpl.Quantity <= 18 then '1090x720x790'
WHEN dpl.Quantity >= 7 and dpl.Quantity <= 17 then '1090x720x570'
WHEN dpl.Quantity >= 1 and dpl.Quantity <= 6 then '1090x720x350'
ELSE 'Unkown'
end as 'TOTAL VOLUME (MM3)'
FROM DropshipPackinglist dpl
INNER JOIN HuaweiDescription hd ON dpl.ItemNumber = hd.ItemNumber
WHERE (dpl.BatchCode LIKE '%0005041007100AHWA11HG')
)
select *, sum([QTY]) over (partition by ss.[CASE NO.]) [TotalVolume]
from SuperSelect ss
If you need just one row per caseNumber then use
SELECT CaseNumber, Quantity, SUM(ItemNumber) TotalVolume
FROM (...YourOriginalQuery...)
GROUP BY CaseNumber, Quantity
If you need all rows but want also report Total per case number then use the following query:
SELECT CaseNumber, ItemNumber, Quantity,
SUM(ItemNumber) OVER(PARTITION BY CaseNumber) TotalVolume
FROM (SELECT DropshipPackinglist.CaseNumber, DropshipPackinglist.ItemNumber,
DropshipPackinglist.Quantity,
CASE
WHEN DropshipPackinglist.Quantity >= 31
AND DropshipPackinglist.Quantity <= 36 THEN
'1090x730x1460'
WHEN DropshipPackinglist.Quantity >= 25
AND DropshipPackinglist.Quantity <= 30 THEN
'1090x730x1230'
WHEN DropshipPackinglist.Quantity >= 19
AND DropshipPackinglist.Quantity <= 24 THEN
'1090x730x1000'
WHEN DropshipPackinglist.Quantity >= 13
AND DropshipPackinglist.Quantity <= 18 THEN
'1090x720x790'
WHEN DropshipPackinglist.Quantity >= 7
AND DropshipPackinglist.Quantity <= 17 THEN
'1090x720x570'
WHEN DropshipPackinglist.Quantity >= 1
AND DropshipPackinglist.Quantity <= 6 THEN
'1090x720x350'
ELSE
'Unkown'
END AS 'TOTAL VOLUME (MM3)'
FROM DropshipPackinglist
INNER JOIN HuaweiDescription
ON DropshipPackinglist.ItemNumber = HuaweiDescription.ItemNumber
WHERE (DropshipPackinglist.BatchCode LIKE '%0005041007100AHWA11HG'))

Categories