SELECT statement for 3 employee Id's - sql

Please can someone help me with the SQL code that list all the data for the 3 Employee ID's:
60-578-2269, 50-218-3739 and 80-772-4580 ? I was only able to pull the data for one Employee ID below:
SELECT *
FROM [dbo].Employee_Certification
WHERE Employee_ID = '60-578-2269'
Also, the sql statement that list every employee that was hired after 1st April 2021, in date format 04/01/2021. I ran this:
SELECT *
FROM [dbo].Employee_Certification
WHERE Date_Hired> = 01/04/2021
but the error message below came up:
Msg 245, Level 16, State 1, Line 23 Conversion failed when converting
the nvarchar value '4/1/21' to data type int.

For the first query use WHERE IN (...) with a tuple of employee IDs which you want to find:
SELECT *
FROM [dbo].Employee_Certification
WHERE Employee_ID IN ('60-578-2269', '50-218-3739', '80-772-4580');
For the second query, use a proper date literal which SQL Server will recognize:
SELECT *
FROM [dbo].Employee_Certification
WHERE Date_Hired >= '20210401';

Related

12 future numbers required in Oracle SQL

I have one requirement in SQL where
We have couple of market ids
Let say 20,30,40
And here for each market using one plsql function we are getting one campaign number let's say for market 20 I got 20210306,for mrkt 30 we got 20210307, and for 40 mrkt id 20210308.
Now what I want to achieve for each market we should have current campaign no as 20210306 as well as 12 future campaign should be produced by SQL query based on each current campaign of the market.
I am doing it using union all and campaign+1,2,3 and so on which is taking time . Do we have some brief logic for it.
Please suggest
Thanks,
Vijay
You can generate series of data (numbers or dates or even characters) using the LEVEL pseudo column with a CONNECT BY clause.
In your case, it sounds like you want generate the next 10 numbers starting from 20210306.
That could be done like this:
WITH current_campaign ( nr ) AS
( SELECT 20210306 FROM dual )
SELECT
nr + level
FROM
current_campaign
CONNECT BY
level < 13;
20210307
20210308
20210309
20210310
20210311
20210312
20210313
20210314
20210315
20210316
20210317
20210318
The 20210306 could also represent the date March, 6, 2021. If you want to generate those numbers date based numbers you could do that too. For example (starting from 0629 so the result shows the month change, only showing 5 rows)
WITH current_campaign ( cdate ) AS
( SELECT DATE'2021-06-29' FROM dual ) SELECT
TO_CHAR(cdate + level,'YYYYDDMM')
FROM
current_campaign
CONNECT BY
level < 6;
20213006
20210107
20210207
20210307
20210407

SQL: How to populate a census column with a count of active clients for each date based on admit date and discharge date

I need to find the number of active clients on any given date based on a table ('client_table') with three columns: 'client_id', 'admit_date', and 'discharge_date'.
To do this, I've created a second table ('census_table') with two columns: 'date' and 'census'. I need to populate the 'census' column with a count of the number of active clients for each date in the 'date' column (which I've populated with every date from 1/1/2000 to 12/31/2050.
The logic should be that IF 'admit_date' <= 'date' AND 'discharge_date' >= 'date', then the client should be counted in the census for that date, but I don't know how to code this in SQL.
SELECT COUNT(client_id) AS census
FROM client_table
WHERE admit_date <= census_table.date
AND discharge_date >= census_table.date
This returns error:
"Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "census_table.date" could not be bound.
Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "census_table.date" could not be bound."
Try this way:
SELECT COUNT(client_id) AS census,(select date from census_table) as mydate
FROM client_table
WHERE admit_date <= mydate
AND discharge_date >= mydate
Sql server told you roughly what the problem is: census_table.date is a multi-part identifier that can't be bound to anything. This is because the first part of the identifier, census_table, doesn't mean anything where you have used it because isn't listed as a table in the FROM part of your query.
It's not clear how you were planning to manage calculating the answer for each date. Maybe you want to go through the dates one by one and use that SQL to calculate the census for each date? In that case, you would replace census_table.date with a query parameter. That would look something like this:
SELECT COUNT(client_id) AS census
FROM client_table
WHERE admit_date <= #query_date
AND discharge_date >= #query_date
The way you specify the value for the query parameter would depend on what environment you are using to execute the SQL.
More likely you want to write some SQL to do the whole calculation in one go. If so, you would need to change your summary calculation to summarize by census_table.date rather than giving a single overall total. Take a look at https://www.w3schools.com/sql/sql_groupby.asp
Taking that approach, you could simply add census_table into the FROM part of the statement. Take a look at https://www.w3schools.com/sql/sql_join_inner.asp
This is the SQL query that gives me the values I need (and can then average over a defined period of time in Power BI).
SELECT [date], COUNT(DISTINCT
(CASE WHEN admit_date <= [date] AND discharge_date >= [date] THEN client_id END))
AS census
FROM client_table, census_table
GROUP BY [date]
ORDER BY [date]
Much thanks for the help guiding me in the right direction.

Sql select query confusion when passing getdate() & its value to where condition

I have a Employee table like below
ID Name Mobile Ondate Address
1 Ankit 1234567895 2016-11-08 10:10:04.540 abc
2 Amit 4521545258 2016-11-08 11:10:04.540 bcd
3 Amit2 7541258562 2016-11-08 12:10:04.540 gfd
Now i write select query like below then it gives all records of Employee table
select * from Employee where convert(date,ondate)='2016-11-08 12:10:04.540'
but when i pass getdate() direct in where condition then it gives empty
select * from Employee where convert(date,ondate)=getdate()
while select getdate() result is 2016-11-08 12:10:04.540
so please give proper reason about it.
This is data type precedence at work. In your first query, in the WHERE clause you have a date on one side of a comparison and a varchar on the other. date wins, your string is converted to a date, the time is ignored and every row matches.
In your second query, you have a date on one side of the comparison and a datetime on the other side. datetime wins, the date is converted (back) into a datetime, and the datetimes don't match on their time components.
If you want to select values for today, use something like:
select * from Employee
where ondate >= DATEADD(day,DATEDIFF(day,0,GETDATE()),0) and
ondate < DATEADD(day,DATEDIFF(day,0,GETDATE()),1)
Where the DATEADD/DATEDIFF expressions are effectively computing "midnight at the start of today" and "midnight at the start of tomorrow". Both expressions will be computed once, and any index on the ondate column can then be used, if one exists, and we avoid excessively transforming column data.
try this.
select * from Employee where convert(date,ondate)=Convert(date,getdate())
SELECT * from Employee where DATEDIFF(DAY,Ondate,'2016-11-08 10:10:04.540') = 0
OR
SELECT * from Employee where DATEDIFF(DAY,Ondate,GETDATE()) = 0

ORA-01847 error when not using dates in the query

I'm trying to run this query in an Oracle 11g database:
SELECT * FROM BALANCE B
WHERE B.EMPLOY_ID = '0016'
AND B.PROJETCT_ID = '5TM-1305002.01.01.01'
but the following error is displayed:
ORA 01847 : day of the month must be between 1 and last day of the month
The EMPLOY_ID field is varchar2(4) and the PROJECT_ID is a varchar2(20).
I do not understand why the Oracle database is trying to convert the parameter values to date values. What is going on?
What happens if you try this?
SELECT * FROM BALANCE B
WHERE TO_CHAR(B.EMPLOY_ID) = '0016'
AND TO_CHAR(B.PROJETCT_ID) = '5TM-1305002.01.01.01'
It could be due to invalid data in one of date columns in table.
You can check your query with rownum condition, just to make sure that the where clause is correct or not.
If it is working then there is no because of B.EMPLOY_ID and B.PROJETCT_ID columns.

Varchar invalid for Sum operator

I have a table called Cos and the datatype of Amt is Float and sample data looks like:
Acct Period F_year Amt
Detf 1 2011 Null
Detf 2 2011 Null
Detf 3 2011 1669.57
FTE 1 2011 3205.11
FTE 2 2011 0
FTE 3 2011 Null
I wrote a query like:
Select Acct,Period,F_year, Sum(AMT) as Amt
from dbo.Cos
Group By Acct,Period,F_year
Where Amt is not null
But i am getting this error:
Msg 8117, Level 16, State 1, Line 1
Operand data type varchar is invalid for sum operator.
Can anyone help me?
Try doing this:
Select Acct,Period,F_year, Sum(isnull(cast(AMT as float),0)) as Amt
from dbo.Cos
Group By Acct,Period,F_year
Apparently, the value "1669.57" is a string. So what does it mean to add this value to another?
The error message is correct: It's not valid to add text values together. If it was valid, I could not tell what the result should be.
You should either change your column type to a numeric type, or convert it somehow before trying to add it.
If Amt is intended to be used for mathematical operations, then it should be type Decimal and not varchar.