Sql equivalent of this dax - mysql

I have a table in power bi which uses the below dax:
need inputs on SQL Query.
powerbi table name: demo,
employee is another table and demo table uses it and KEY IS ALREADY CREATED IN EMPLOYEE TABLE BY CONCATINATING employee'[EMP_CODE]&'employee'[dob]
key=SUMMARIZE( FILTER('employee','employee'[emp_STS]="working"),'employee'[EMP_CODE],'employee'[dob],"Key",'employee'[EMP_CODE]&'employee'[dob])
WORKEDHOURS = CALCULATE(MAX('Employee'[workhrs]), 'Empployee'[NAME]<>BLANK())
employeecode=SUMMARIZE( FILTER('employee','employee'[emp_STS]="working"),'employee'[EMP_CODE],'employee'[dob],"Key",'employee'[EMP_CODE]&'employee'[dob])
employeedob=SUMMARIZE( FILTER('employee','employee'[emp_STS]="working"),'employee'[EMP_CODE],'employee'[dob],"Key",'employee'[EMP_CODE]&'employee'[dob])
corresponding SQL Query I have tried :
select KEY,
CASE
WHEN NAME IS NOT NULL THEN MAX(HOURS)
END AS WORKEDHOURS,
EMP_CODE,
EMP_DOB
from employee where EMP_STS='WORKING' GROUP BY KEY,QC_NAME,DOB,EMP_CODE

Please, check this part and come with your feedback.
WITH
e1 AS (
SELECT
CONCAT([EMP_CODE],[dob]) AS [Key]
,EMP_CODE
,EMP_DOB
FROM employee
WHERE [emp_STS]='working'
GROUP BY [EMP_CODE],[dob]
)
,e2 AS(
SELECT
CONCAT([EMP_CODE],[dob]) AS [Key]
,MAX([workhrs]) AS [WorkedHours]
FROM employee
WHERE [NAME] IS NOT NULL -- ,[emp_STS]='working' ?
GROUP BY [EMP_CODE] ,[dob]
)
SELECT
e1.[Key]
,e1.[EMP_CODE]
,e1.[EMP_DOB]
,e2.[WorkedHours]
FROM e1
LEFT JOIN e2
ON e1.[Key]=e2.[Key]

Related

SQL - Column in field list is ambiguous

I have two tables BOOKINGS and WORKER. Basically there is table for a worker and a table to keep track of what the worker has to do in a time frame aka booking. I’m trying to check if there is an available worker for a job, so I query the booking to check if requested time has available workers between the start end date. However, I get stuck on the next part. Which is returning the list of workers that do have that time available. I read that I could join the table passed on a shared column, so I tried doing an inner join with the WORKER_NAME column, but when I try to do this I get a ambiguous error. This leads me to believe I misunderstood the concept. Does anyone understand what I;m trying to do and knows how to do it, or knows why I have the error below. Thanks guys !!!!
CREATE TABLE WORKER (
ID INT NOT NULL AUTO_INCREMENT,
WORKER_NAME varchar(80) NOT NULL,
WORKER_CODE INT,
WORKER_WAGE INT,
PRIMARY KEY (ID)
)
CREATE TABLE BOOKING (
ID INT NOT NULL AUTO_INCREMENT,
WORKER_NAME varchar(80) NOT NULL,
START DATE NOT NULL,
END DATE NOT NULL,
PRIMARY KEY (ID)
)
query
SELECT *
FROM WORKERS
INNER JOIN BOOKING
ON WORKER_NAME = WORKER_NAME
WHERE (START NOT BETWEEN '2010-10-01' AND '2010-10-10')
ORDER BY ID
#1052 - Column 'WORKER_NAME' in on clause is ambiguous
In your query, the column "worker_name" exists in two tables; in this case, you must reference the tablename as part of the column identifer.
SELECT *
FROM WORKERS
INNER JOIN BOOKING
ON workers.WORKER_NAME = booking.WORKER_NAME
WHERE (START NOT BETWEEN '2010-10-01' AND '2010-10-10')
ORDER BY ID
In your query, the column WORKER_NAME and ID columns exists in both tables, where WORKER_NAME retains the same meaning and ID is re-purposed; in this case, you must either specify you are using WORKER_NAME as the join search condition or 'project away' (rename or omit) the duplicate ID problem.
Because the ID columns are AUTO_INCREMENT, I assume (hope!) they have no business meaning. Therefore, they could both be omitted, allowing a natural join that will cause duplicate columns to be 'projected away'. This is one of those situations where one wishes SQL had a WORKER ( ALL BUT ( ID ) ) type syntax; instead, one is required to do it longhand. It might be easier in the long run to to opt for a consistent naming convention and rename the columns to WORKER_ID and BOOKING_ID respectively.
You would also need to identify a business key to order on e.g. ( START, WORKER_NAME ):
SELECT *
FROM
( SELECT WORKER_NAME, WORKER_CODE, WORKER_WAGE FROM WORKER ) AS W
NATURAL JOIN
( SELECT WORKER_NAME, START, END FROM BOOKING ) AS B
WHERE ( START NOT BETWEEN '2010-10-01' AND '2010-10-10' )
ORDER BY START, WORKER_NAME;
This is good, but its returning the start and end times as well. I'm just wanting the WOKER ROWS. I cant take the start and end out, because then sql doesn’t recognize the where clause.
Two approaches spring to mind: push the where clause to the subquery:
SELECT *
FROM
( SELECT WORKER_NAME, WORKER_CODE, WORKER_WAGE FROM WORKER ) AS W
NATURAL JOIN
( SELECT WORKER_NAME, START, END
FROM BOOKING
WHERE START NOT BETWEEN '2010-10-01' AND '2010-10-10' ) AS B
ORDER BY START, WORKER_NAME;
Alternatively, replace SELECT * with a list of columns you want to SELECT:
SELECT WORKER_NAME, WORKER_CODE, WORKER_WAGE
FROM
( SELECT WORKER_NAME, WORKER_CODE, WORKER_WAGE FROM WORKER ) AS W
NATURAL JOIN
( SELECT WORKER_NAME, START, END FROM BOOKING ) AS B
WHERE START NOT BETWEEN '2010-10-01' AND '2010-10-10'
ORDER BY START, WORKER_NAME;
This error comes after you attempt to call a field which exists in both tables, therefore you should make a reference. For instance in example below I first say cod.coordinator so that DBMS know which coordinator I want
SELECT project__number, surname, firstname,cod.coordinator FROMcoordinatorsAS co JOIN hub_applicants AS ap ON co.project__number = ap.project_id JOIN coordinator_duties AS cod ON co.coordinator = cod.email

NOT IN query not match values in my sql

My Query is:
select * from customer_purchase_order where purchase_id NOT IN (SELECT cus_po_no FROM cus_comercial_invoice)
In this purchase id is single values but cus_po_no are multiple in cus_comercial_invoice table.
For example:
Purchase_id 1: POV162031.
Purchase_id 2: POV162125.
cus_po_no : 'POV162031','POV162125'.
Not In Query Not working
If you want to join Rows on foreign , primary key , use JOIN
select * from customer_purchase_order cpo JOIN
cus_comercial_invoice cci
ON cpo.purchase_id = cci.cus_po_no
You are not correct, see this SQL fiddle - it works as expected.

SQL Query for correlated tables

I have 4 tables named General , Apartment , RoomsAndAmenities and BasisInfo.
In General table columns are GeneralId , Name , ApartmentId
In Apartment table columns are ApartmentId
In RoomsAndAmenities table columns are RoomsAndAmenitiesId , ApartmentId
In BasicInfo table columns are BasicInfoId , bedrooms , RoomsAndAmenitiesId.
I wants to get the Name column from General where bedrooms=1
plz help out me.
Thank You
Alter procedure FilterApartmentByBedroom
#bedroom varchar(255)= null
As
Begin
select distinct
general.name
from general
inner join rooms_and_amenities on rooms_and_amenities.apartmentID = general.apartmentID
inner join basic_information on basic_information.roomsAndAmenitiesID = rooms_and_amenities.roomsAndAmenitiesID
where bedrooms=#bedroom
End

How to refer to a SELECT statement result table in MySQL?

I have two very simple questions:
what is name of a select statement result table in MySQL (a name that I can use in another select statement, for example in next line)?
how to naming that above table (in question 1)?
1- what is name of a select statement result table of in mysql?(a name that I can use it in another select statement(for example in next line))
Resultsets will not have any name unless you define an alias name while executing.
select * from department dept
In the above example dept is an alias name for department table.
2- how to naming that above table(in question 1)?
There could be cases where you select information from a single or multiple tables and join together. For that whole set you can define an alias.
select
emp_id, salary, commission,
( salary + commission ) as total_sal,
dept.dept_name as department_name
from employee emp, department dept
where emp.dept_no = dept.dept_no
For the above query you can give alias when used like:
select * from (
select
emp_id, salary, commission,
( salary + commission ) as total_sal,
dept.dept_name as department_name
from employee emp, department dept
where emp.dept_no = dept.dept_no
) as filtered_results
where
department_name in ( 'sales', 'marketing' )
Refer to:
MySQL: Schema Object Names
If I am correct, you wish to create a from a select command in mysql? This should work.
CREATE TABLE fishTypes (type VARCHAR(100)) SELECT type FROM allFish;
Is this the solution you are looking for? Here allFish might have the schema
(number INT, type VARCHAR(100))
it was found. I must use views.

Getting multiple values from a query

I write a query to get values from 3 tables but this is returning multiple values so can any one tell where i went wrong
select c.CompanyName,cd.FedTaxID,cd.EmailAddress,cd.PhoneNumber
from tblcustomerdetail cd,tblcustomer c
where c.FedTaxID in (
select FedTaxID
from tblcustomer
where CustomerID in (
select LOginID
from tbluserlogindetail
where UserName like "pa%" and RoleTypeID='20'
)
)
and cd.FedTaxID in (
select FedTaxID
from tblcustomer
where CustomerID in (
select LOginID
from tbluserlogindetail
where UserName like "pa%" and RoleTypeID='20'
)
);
My relation is here
My 3 tables are `tbluserlogindetails, tblcustomerdetails and tblCustomer'
1) Initially i will get `Login ID` from `tblUserLoginDetail ` based on the `user name`.
2) Next based on `LoginID` i will get `FedTaxID` from tblcustomerDetail`
3) Next based on 'FedTaxID' i will get the the required details from `tblcustomer'
SELECT
tblcustomer.CompanyName,
tblcustomerdetail.FedTaxID,
tblcustomerdetail.EmailAddress,
tblcustomerdetail.PhoneNumber
FROM tbluserlogindetail, tblcustomer, tblcustomerdetail
WHERE
tbluserlogindetail.LOginID = tblcustomer.CustomerID
AND tblcustomer.FedTaxID = tblcustomerdetail.FedTaxID
AND tbluserlogindetail.UserName LIKE 'pa%'
AND tbluserlogindetail.RoleTypeID = '20'
Try something like this.
Subqueries have a slow perfomance.
MySQL - SELECT WHERE field IN (subquery) - Extremely slow why?