I want to separate the morning and evening patient checked-in count gender-wise and also if gender age less then show the Female child and M child on the basis of gender.
SELECT
COUNT(ch.EnteredOn) AS counter,
CONVERT(date, ch.EnteredOn) AS sessionDay,
SUM(CASE WHEN CAST(CONVERT(CHAR(2), ch.EnteredOn, 108) AS INT) < 12 THEN 1 ELSE 0 END) AS 'Morning',
SUM(CASE WHEN CAST(CONVERT(CHAR(2), ch.EnteredOn, 108) AS INT) >= 12 THEN 1 ELSE 0 END) AS 'Evening',
SUM(CASE WHEN p.Gender = 1 THEN 1 ELSE 0 END) AS Male,
SUM(CASE WHEN p.Gender = 2 THEN 1 ELSE 0 END) AS Fmale,
SUM(CASE WHEN DATEDIFF(hour, P.DOB, GETDATE()) / 8766 <= 18
AND P.Gender = 1 THEN 1 ELSE 0 END) AS MChild,
SUM(CASE WHEN DATEDIFF(hour, P.DOB, GETDATE()) / 8766 <= 18
AND P.Gender = 2 THEN 1 ELSE 0 END) AS FChild
FROM
Patient.CheckIn ch
INNER JOIN
Patient.Patients P ON ch.PatientId = p.PatientId
GROUP BY
Ch.EnteredOn
I have only three columns like Gender, Time and DOB. Gender id 1 shows the male and Gender id 2 is using for females.
enter image description here
SELECT convert(date, ch.EnteredOn) AS sessionDay, CAST( CONVERT(CHAR(2), ch.EnteredOn, 108) AS INT) <12 as morning, count(ch.EnteredOn) AS counter
,sum(case when p.Gender=1 Then 1 ELSE 0 end) as Male
,sum(case when p.Gender=2 Then 1 ELSE 0 end) as Fmale
,Sum( case when DATEDIFF(hour,P.DOB,GETDATE())/8766<=18 AND P.Gender=1 Then 1 ELSE 0 end ) as MChiled
,Sum( case when DATEDIFF(hour,P.DOB,GETDATE())/8766<=18 AND P.Gender=2 Then 1 ELSE 0 end ) as FChiled
FROM Patient.CheckIn ch
inner join Patient.Patients P on ch.PatientId=p.PatientId
Group by Ch.EnteredOn
Group BY can be
Group by convert(date, ch.EnteredOn) AS sessionDay, CAST( CONVERT(CHAR(2), ch.EnteredOn, 108) AS INT) <12
I have a part of a query below where I have created new columns based on conditions:
select
sum(case when Overall_Time_Spent < 0 then 1 else 0 end) as Errors,
sum(case when Overall_Time_Spent between 0 and 3 then 1 else 0 end) as _0_3_days,
sum(case when Overall_Time_Spent = 4 then 1 else 0 end) as _4_days,
sum(case when Overall_Time_Spent = 5 then 1 else 0 end) as _5_days,
sum(case when Overall_Time_Spent between 6 and 8 then 1 else 0 end) as _6_8_days,
sum(case when Overall_Time_Spent >= 9 then 1 else 0 end) as more_than_9_days,
avg(case when Overall_Time_Spent < 0 then 100.0 else 0 end) as Errors_percent,
avg(case when Overall_Time_Spent between 0 and 3 then 100.0 else 0 end) as _0_3_percent,
avg(case when Overall_Time_Spent = 4 then 100.0 else 0 end) as _4_percent,
avg(case when Overall_Time_Spent = 5 then 100.0 else 0 end) as _5_percent,
avg(case when Overall_Time_Spent between 6 and 8 then 100.0 else 0 end) as _6_8_percent,
avg(case when Overall_Time_Spent >= 9 then 100.0 else 0 end) as more_than_9_days_percent,
How can I add a query within this one where I can add TWO MORE columns which give me sum of all the sums and sum of all of avg
Thanks in advance
The CASE expressions you have covers <0, between 0 and 8 and >=9, which is every possibility, provided that your data is an int. Thus, all you need to add is
COUNT(Overall_Time_Spent) AS DaysTotal
If it isn't an int, then your CASE expressions will miss values between 3 and 4, 4 and 5, 5 and 6 and 8 and 9, however, the COUNT would include them.
Try to use subquery:
SELECT a.*, (Errors + _0_3_days...) as Total FROM (
select
sum(case when Overall_Time_Spent < 0 then 1 else 0 end) as Errors,
sum(case when Overall_Time_Spent between 0 and 3 then 1 else 0 end) as _0_3_days,
sum(case when Overall_Time_Spent = 4 then 1 else 0 end) as _4_days,
sum(case when Overall_Time_Spent = 5 then 1 else 0 end) as _5_days,
sum(case when Overall_Time_Spent between 6 and 8 then 1 else 0 end) as _6_8_days,
sum(case when Overall_Time_Spent >= 9 then 1 else 0 end) as more_than_9_days
) as a
I have a table whose output is this
i have written my query like this
SELECT
c.DivisionNameEn,
a.LF_CompanyDivisionID,
0 as code,
SUM(a.totInvoiceAmnt) AS Sales,
'Sales' Salesperson,
sum(a.totInvoiceAmnt) as Sum ,
sum(a.totInvoiceAmnt) /sum(a.totInvoiceAmnt) *100 as MonthPercent ,
YEAR(a.TransactionDate) [Year],
MONTH(a.TransactionDate) [Month],
CASE
WHEN datepart(month,a.TransactionDate) = 1 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 1 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 2 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 2 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 3 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 4 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 5 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 6 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 7 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 8 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 9 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 10 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 10 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 11 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 12 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
ELSE 0
END as MonthTotal
from POS_salesHeader a
INNER JOIN dbo.STR_CompanyDivision AS c ON c.id = a.LF_CompanyDivisionID
--WHERE YEAR(a.TransactionDate) = YEAR(GETDATE()) AND MONTH(a.TransactionDate) = MONTH(GETDATE()) /*To filter by month*/
GROUP BY YEAR(a.TransactionDate), MONTH(a.TransactionDate) , c.DivisionNameEn ,LF_CompanyDivisionID
it is giving me out put like this
I want a row which give me month Total totInvoiceAmnt and till now Year Total totInvoiceAmnt for example if today its 15th April it gives me total totInvoiceAmnt of only April with the column name MonthsTotal and in another column it give me till now yearly totInvoiceAmnt total from month of jan + Feb + March +April
I have the following query;
SELECT STOCK_CODE,
dbo.manu_STOCK.DESCRIPTION,
QTY_IN_STOCK,
Quantity,
ForecastDate
FROM [FS25-w2k8\SQLEXPRESS].sagel50_46772.dbo.SalesForecastLines AS SalesForecastLines1
INNER JOIN dbo.manu_STOCK
ON SalesForecastLines1.ProductCode = dbo.manu_STOCK.STOCK_CODE
This brings up the following information;
STOCK_CODE DESCRIPTION QTY_INSTOCK Quantity ForecastDate
523 gel 12 10 01/08/2014
523 gel 12 10 08/08/2014
I want to be able to modify the query so that it displays the following formation
Stock Code Description WK1 WK2
523 gel 22 22
So it will sum qty in stock and quantity on the first date and the column will be called wk1, second week - wk2 etc.
Can you advise on this please?
IF you want to pivot the results for 52 weeks, you can use the following query. This will pivot data for 52 weeks. This is just concept, I could not test this query.
SELECT * FROM
( SELECT
dbo.manu_STOCK.STOCK_CODE AS [Stock Code],
dbo.manu_STOCK.DESCRIPTION,
DATEPART(WEEK,[Date]) Wk,
QTY_IN_STOCK + Quantity AS Stock
FROM [FS25-w2k8\SQLEXPRESS].sagel50_46772.dbo.SalesForecastLines AS SalesForecastLines1
INNER JOIN dbo.manu_STOCK
ON SalesForecastLines1.ProductCode = dbo.manu_STOCK.STOCK_CODE
) AS Source
PIVOT
(
SUM(Stock)
FOR WK IN
([0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]
,[11],[12],[13],[14],[15],[16],[17],[18],[19],[20]
,[21],[22],[23],[24],[25],[26],[27],[28],[29],[30]
,[31],[32],[33],[34],[35],[36],[37],[38],[39],[40]
,[41],[42],[43],[44],[45],[46],[47],[48],[49],[50]
,[51],[52])
) AS PVT ;
DECLARE #Stock TABLE
(
STOCK_CODE INT,
DESCRIPTION VARCHAR(50),
QTY_INSTOCK INT,
QUANTITY INT,
FORECASTDATE DATETIME
)
INSERT INTO #Stock
( STOCK_CODE, DESCRIPTION, QTY_INSTOCK, QUANTITY, FORECASTDATE )
VALUES
( 523, 'gel', 12, 10, '01/08/2014' ),
( 523, 'gel', 12, 10, '08/08/2014' )
SELECT
s.STOCK_CODE,
s.DESCRIPTION,
s.STOCKYEAR,
SUM(CASE WHEN s.STOCKWEEK = 1 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk1,
SUM(CASE WHEN s.STOCKWEEK = 2 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk2,
SUM(CASE WHEN s.STOCKWEEK = 3 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk3,
SUM(CASE WHEN s.STOCKWEEK = 4 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk4,
SUM(CASE WHEN s.STOCKWEEK = 5 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk5,
SUM(CASE WHEN s.STOCKWEEK = 6 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk6,
SUM(CASE WHEN s.STOCKWEEK = 7 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk7,
SUM(CASE WHEN s.STOCKWEEK = 8 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk8,
SUM(CASE WHEN s.STOCKWEEK = 9 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk9,
SUM(CASE WHEN s.STOCKWEEK = 10 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk10,
SUM(CASE WHEN s.STOCKWEEK = 11 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk11,
SUM(CASE WHEN s.STOCKWEEK = 12 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk12,
SUM(CASE WHEN s.STOCKWEEK = 13 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk13,
SUM(CASE WHEN s.STOCKWEEK = 14 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk14,
SUM(CASE WHEN s.STOCKWEEK = 15 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk15,
SUM(CASE WHEN s.STOCKWEEK = 16 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk16,
SUM(CASE WHEN s.STOCKWEEK = 17 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk17,
SUM(CASE WHEN s.STOCKWEEK = 18 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk18,
SUM(CASE WHEN s.STOCKWEEK = 19 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk19,
SUM(CASE WHEN s.STOCKWEEK = 20 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk20,
SUM(CASE WHEN s.STOCKWEEK = 21 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk21,
SUM(CASE WHEN s.STOCKWEEK = 22 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk22,
SUM(CASE WHEN s.STOCKWEEK = 23 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk23,
SUM(CASE WHEN s.STOCKWEEK = 24 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk24,
SUM(CASE WHEN s.STOCKWEEK = 25 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk25,
SUM(CASE WHEN s.STOCKWEEK = 26 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk26,
SUM(CASE WHEN s.STOCKWEEK = 27 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk27,
SUM(CASE WHEN s.STOCKWEEK = 28 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk28,
SUM(CASE WHEN s.STOCKWEEK = 29 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk29,
SUM(CASE WHEN s.STOCKWEEK = 30 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk30,
SUM(CASE WHEN s.STOCKWEEK = 31 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk31,
SUM(CASE WHEN s.STOCKWEEK = 32 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk32,
SUM(CASE WHEN s.STOCKWEEK = 33 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk33,
SUM(CASE WHEN s.STOCKWEEK = 34 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk34,
SUM(CASE WHEN s.STOCKWEEK = 35 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk35,
SUM(CASE WHEN s.STOCKWEEK = 36 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk36,
SUM(CASE WHEN s.STOCKWEEK = 37 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk37,
SUM(CASE WHEN s.STOCKWEEK = 38 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk38,
SUM(CASE WHEN s.STOCKWEEK = 39 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk39,
SUM(CASE WHEN s.STOCKWEEK = 40 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk40,
SUM(CASE WHEN s.STOCKWEEK = 41 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk41,
SUM(CASE WHEN s.STOCKWEEK = 42 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk42,
SUM(CASE WHEN s.STOCKWEEK = 43 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk43,
SUM(CASE WHEN s.STOCKWEEK = 44 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk44,
SUM(CASE WHEN s.STOCKWEEK = 45 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk45,
SUM(CASE WHEN s.STOCKWEEK = 46 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk46,
SUM(CASE WHEN s.STOCKWEEK = 47 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk47,
SUM(CASE WHEN s.STOCKWEEK = 48 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk48,
SUM(CASE WHEN s.STOCKWEEK = 49 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk49,
SUM(CASE WHEN s.STOCKWEEK = 50 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk50,
SUM(CASE WHEN s.STOCKWEEK = 51 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk51,
SUM(CASE WHEN s.STOCKWEEK = 52 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk52,
SUM(CASE WHEN s.STOCKWEEK = 53 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk53
FROM
(
SELECT s.STOCK_CODE, s.DESCRIPTION, DATEPART(YEAR, s.FORECASTDATE) STOCKYEAR, DATEPART(WEEK, s.FORECASTDATE) STOCKWEEK, SUM(s.QTY_INSTOCK) QTY_INSTOCK, SUM(s.QUANTITY) QTY
FROM #Stock s
GROUP BY s.STOCK_CODE, s.DESCRIPTION, DATEPART(YEAR, s.FORECASTDATE), DATEPART(WEEK, s.FORECASTDATE)
) s
GROUP BY s.STOCK_CODE, s.DESCRIPTION, s.STOCKYEAR
ORDER BY s.STOCK_CODE, s.DESCRIPTION, s.STOCKYEAR
Group by the code, desc, year and week and then sum by week for each week of the year (adding extra week depending on day beginning / end of the year falls.
I'm breaking my head over a SQL statement comparing 2 columns and when they are not similar the statement should add 1 to the count.
Here is my code.
SELECT
COUNT(*) CASE WHEN Column1 = Column2 THEN '0' ELSE '1' END AS result
FROM [Sheet1$]
GROUP BY Column1
What am I doing wrong? I get the error message of a missing operator in the query expression.
COUNT(*) is going to count the rows regardless of the value in result.
SUM(result) might be what you are looking for
this should do it:
SELECT
SUM(CASE WHEN Column1 = Column2 THEN 0 ELSE 1 END) AS NumberOfDiffs,
SUM(CASE WHEN Column1 = Column2 THEN 1 ELSE 0 END) AS NumberOfEquals
FROM [Sheet1$]
SELECT count(*)
WHERE NOT (Column1 = Column2)
FROM [Sheet1$]
will count how many rows don't have equal columns.
If you want the count the number of absent days from a attendance table use the following query, D1 is the first day, D2 is the second day...and so on
SELECT TOP (200) PayrollYear, PayrollMonth, EmployeeId, EmployeeName, JobNo, SUM(CASE WHEN D1 = 'A' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN D2 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D3 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D4 = 'A' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN D5 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D6 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D7 = 'A' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN D8 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D9 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D10 = 'A' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN D11 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D12 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D13 = 'A' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN D14 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D15 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D16 = 'A' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN D17 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D18 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D19 = 'A' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN D20 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D21 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D22 = 'A' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN D23 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D24 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D25 = 'A' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN D26 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D27 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D28 = 'A' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN D29 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D30 = 'A' THEN 1 ELSE 0 END) + SUM(CASE WHEN D31 = 'A' THEN 1 ELSE 0 END)
AS TotAbsent
FROM T_EmpAttendance
GROUP BY PayrollYear, PayrollMonth, EmployeeId, EmployeeName, JobNo
SELECT count(*),CASE WHEN col1=col2 THEN 'same' ELSE 'different' END AS x
FROM theTable
group by CASE WHEN col1=col2 THEN 'same' ELSE 'different' END
I am using Oracle, it do not allow me to group by x (alias name) so I am grouping by CASE statement and it works fine.