Getting multiple values from a query - mysql

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?

Related

SQL: Joining 3 tables to generate report dashboard

I am trying to join 3 different tables that holds my test execution results as "PASS", "FAIL" and "SKIP". There are 2 common properties in these 3 tables on the basis of which I need to club my result i.e. "BUILD_NUMBER" and "COMPONENT".
Tried several approach but does not get the desired result.
Best result reached so far.
Sample query:
select test_execution.COMPONENT, test_execution.BUILD_NUMBER,
count(test_execution.TEST_STATUS) as PASS from (test_execution
INNER JOIN test_execution_fail ON
test_execution.BUILD_NUMBER = test_execution_fail.BUILD_NUMBER) group by
COMPONENT,BUILD_NUMBER;
My tables look like below:
CREATE TABLE test_execution_skip (
BUILD_NUMBER int,
TEST_NAME varchar(255),
TEST_CLASS varchar(255),
COMPONENT varchar(255),
TEST_STATUS varchar(255)
);
Other two tables are exactly same with test_execution and test_execution_fail as their names.
test_execution table holds 3 records(all pass values), test_execution_fail table holds 2 records (all fail values) and test_execution_skip table holds 1 record(skip value).
I want to populate data that will show me BUILD_NUMBER, COMPONENT, TOTAL, PASS, FAIL, SKIP as records where TOTAL, PASS, FAIL and SKIP will show the respectives counts.
Any help is appreciated here.
Not sure if this answers your question but you could try something like this
WITH cte AS (
SELECT * FROM test_execution
union
SELECT * FROM test_execution_fail
UNION
SELECT * FROM test_execution_skip
)
SELECT t.*, (SKIP + FAIL + PASS) AS TOTAL FROM (
select
COMPONENT,
BUILD_NUMBER,
SUM(IF(TEST_STATUS = 'skip', 1, 0 )) as SKIP,
SUM(IF(TEST_STATUS = 'fail', 1, 0 )) as FAIL,
SUM(IF(TEST_STATUS = 'pass', 1, 0 )) as PASS
FROM cte
group by COMPONENT,BUILD_NUMBER
)t
db fiddle

How to remove the results from else in a "case when " expression while building the table

Im trying to build a graph in Chartio using SQL code, i want 3
categories in my results : RunningCosts, StorageCosts and OtherCosts.
The problem is that i have an other category i dont want that appears in the graph.
SELECT {DATE_BUCKET.BUCKET('TIMESTAMP_ADD(CAST(`usage_start_time` AS TIMESTAMP), INTERVAL {UTC_OFFSET.RAW} HOUR)', UTC_OFFSET)} AS `Date_BucketOfCreated_At`,
CASE when((sku.description LIKE "%Licensing Fee%"
OR sku.description LIKE "%running%")
AND
(SELECT value
FROM UNNEST(labels)
WHERE KEY = "env")="prod"
AND
(SELECT value
FROM UNNEST(labels)
WHERE KEY="family")="gaming")THEN "RunningCosts"
WHEN ((sku.description LIKE "%storage%"
OR sku.description LIKE "%SSD%")
AND
(SELECT value
FROM UNNEST(labels)
WHERE KEY = "env")="prod"
AND
(SELECT value
FROM UNNEST(labels)
WHERE KEY="family")="gaming")THEN "StorageCosts"
WHEN (sku.description NOT LIKE "%storage%"
AND sku.description NOT LIKE "%SSD%"
AND sku.description NOT LIKE "%Licensing Fee%"
AND sku.description NOT LIKE "%running%"
AND
(SELECT value
FROM UNNEST(labels)
WHERE KEY = "env")="prod"
AND
(SELECT value
FROM UNNEST(labels)
WHERE KEY="family")="gaming") THEN "OtherCosts"
ELSE "autre"
END AS `Origin`,
ROUND(SUM(cost), 2) AS charges
FROM gcp.gcp_billing_export_v1_00A342_468C35_E1E69C
GROUP BY Date_BucketOfCreated_At,
Origin
ORDER BY Date_BucketOfCreated_At
Since it is a new field, you can't just add to your query a where clause. But you can select from the result of that query using where.
SELECT * FROM (
-- ORIGINAL QUERY
) WHERE Origin <> 'autre'

MySQL View's SELECT contains a subquery in the FROM clause... help to rewrite?

I'm building a web app that needs to reference data from a single view.
I know that I can't have a from clause in a sub-query in create or replace view but I was only able to come up with one version of this query that returns the result I need.
After hours of trawling through posts on here I couldn't come up with any solutions for it.
Is there someone with more smarts than I have that can re-fudge this query in such a way that all the data can be returned in a single view?
Here is the query ...
CREATE
OR REPLACE VIEW in_stock_levels AS
SELECT
*,
`in_stock` - `maximum_stock` AS levels
FROM(
SELECT
stock_levels.id AS id,
stock_levels.part_number AS part_number,
stock_levels.minimum_stock AS minimum_stock,
stock_levels.maximum_stock AS maximum_stock,
(
SELECT
COUNT(*)
FROM
automatic_transmission_jobs
WHERE
part_number = stock_levels.part_number
AND test_result != "Not Tested"
AND status != "pwa"
) AS in_stock
FROM
stock_levels
) AS check_stock
Any help is much appreciated.
Without having more information on your schema, but based on what you posted, the following query should work for your CREATE OR REPLACE VIEW...
SELECT
stock_levels.id,
stock_levels.part_number,
stock_levels.minimum_stock,
stock_levels.maximum_stock,
COUNT(automatic_transmission_jobs.id) AS in_stock,
in_stock - maximum_stock AS levels
FROM stock_levels
LEFT OUTER JOIN automatic_transmission_jobs USING (part_number)
WHERE
automatic_trasmission_jobs.test_result != 'Not Tested'
AND automatic_trasmission_jobs.status !='pwa'
GROUP BY stock_levels.id;
Do you use phpMyAdmin or MySQLWorkbench to design and test your queries?
Doing it this way works, I know it's not the most efficient,
CREATE
OR REPLACE VIEW in_stock_levels AS
SELECT
stock_levels.id AS id,
stock_levels.part_number AS part_number,
stock_levels.minimum_stock AS minimum_stock,
stock_levels.maximum_stock AS maximum_stock,
(
SELECT
COUNT(*)
FROM
automatic_transmission_jobs
WHERE
part_number = stock_levels.part_number
AND test_result != "Not Tested"
AND status != "pwa"
) AS in_stock,
(
SELECT
COUNT(*)
FROM
automatic_transmission_jobs
WHERE
part_number = stock_levels.part_number
AND test_result != "Not Tested"
AND status != "pwa"
) - stock_levels.maximum_stock AS stock_level
FROM
stock_levels
Thanks for all your help Ingo :)

Returning all records from derived table name that also have IN statements

I'm trying to retrieve a single record from my database, BUT I don't know which table name to query until I query another record.
If I knew the table name and ID the end query would look something like this.
SELECT * FROM `materials_sheet_stock` WHERE `id` = 2
But since I do NOT know the table name or the ID in that table I'm trying to break it up a bit.
This query will successfully retrieve the table name for the above query
SELECT tb1.*
FROM (SELECT `tag_table`
FROM `materials_group_tags_mapping`
WHERE `tag_id` IN
(SELECT `materials_group_tags`.`id`
FROM `materials_group_tags`
WHERE `materials_group_tags`.`name` = "frameless_base_side_material_unexposed")) AS tb1
which in this case is materials_sheet_stock
This query will successfully retrieve the ID that I need for the above query
(SELECT `materials_group_tags_mapping`.`tag_value`
FROM `materials_group_tags_mapping`
WHERE `materials_group_tags_mapping`.`tag_id` IN
(SELECT `materials_group_tags`.`id`
FROM `materials_group_tags`
WHERE `materials_group_tags`.`name` = "frameless_base_side_material_unexposed"))
But now when I put them all together in 1 query using IN it keeps throwing errors about not finding columns, or that all tables need an alias. I've tried editing the following code for like an hour with no luck. Hopefully you guys can spot the error. Here's the final code that I am using.
SELECT tb2.*
FROM (SELECT `tag_table`
FROM `materials_group_tags_mapping`
WHERE `tag_id` IN
(SELECT `materials_group_tags`.`id`
FROM `materials_group_tags`
WHERE `materials_group_tags`.`name` = "frameless_base_side_material_unexposed") ) as tb2
WHERE tb2.`id` IN
(SELECT `materials_group_tags_mapping`.`tag_value`
FROM `materials_group_tags_mapping`
WHERE `materials_group_tags_mapping`.`tag_id` IN
(SELECT `materials_group_tags`.`id`
FROM `materials_group_tags`
WHERE `materials_group_tags`.`name` = "frameless_base_side_material_unexposed"))

mySQL - INSERT query that matches the same records as this SELECT query?

I've got a select query I'm using to pick out contacts in my DB that haven't been spoken to in a while. I'd like to run an INSERT query to enter in a duplicate note for all the records that are returned with this select query... problem is I'm not exactly sure how to do it.
The SELECT query itself is likely a bit of a convoluted mess. I basically want to have the most recent note from each partner selected, then select ONLY partners that haven't got a note from a certain date and back... the SELECT query goes:
SELECT * FROM
(
SELECT * FROM
(
SELECT
partners.partners_id,
partners.CompanyName,
notes.Note,
notes.DateCreated
FROM
notes
JOIN
partners ON notes.partners_id = partners.partners_id
ORDER BY notes.DateCreated DESC
) AS Part1
GROUP BY partners_id
ORDER BY DateCreated ASC
) AS Part2
WHERE
DateCreated <= '2013-01-15'
How would a run an INSERT query that would only go into the same records as this SELECT?
The insert would enter records such as:
INSERT INTO notes
(
notes_id,
partners_id,
Note,
CreatedBy,
DateCreated
)
SELECT
UUID(),
partners.partners_id,
'Duplicated message!',
'User',
'2013-02-14'
FROM
partners
If you want to do this all in SQL, you could use an UPDATE statement.
UPDATE tablename SET note='duplicate' where id in ( your statement here);
Note that in order for this to work 'id' needs to be a column from 'tablename'. Then, your statement has to return a single column, not *. The column returned needs to be the id that will let your update statement know which rows to update in 'tablename'.