How to format SQL query using Pivot in SQL? - sql-server-2008

Find the images 1 and 2, I want result as image 2 using pivot SQL.
Old table
Desired Format

SELECT PermID,
ISNULL([New_User],0)as [New_User],ISNULL([Edit_User],0)as [Edit_User] ,ISNULL([Permission],0) as [Permission] ,ISNULL([Store-Credit],0)as [Store-Credit],ISNULL([Refund],0) as [Refund] ,ISNULL([Gift_Certificate],0)as [Gift_Certificate]
FROM
(SELECT PermID, Fname,'0' AS Active
FROM F3_BC_Users_Permissions_Master) AS SourceTable
PIVOT
(
max(Active)
FOR fname IN ([New_User],[Edit_User],[Permission],[Store-Credit],[Refund],[Gift_Certificate])
) AS PivotTable;

Related

mysql multiple similar table query[seems not duplicate]

I have a sales software that use MYSQL database and i want to make a web extension using PHP. the software creating a new table everyday for every days transaction with same column but name like sales_data_Yearmonthday.
e.g
sales_data_20190122
sales_data_20190123
sales_data_20190124
sales_data_20190125
sales_data_20190126
sales_data_20190127
sales_data_20190128
So my question is what will be the best way to query these table if i want to get sales report for last 7 days?
UNION ALL is one option to join all table but are there any other option to do that for getting best performance as 356 table will be created every year and each table may have contain over 5000 records.
its may not the best database design but i cannot change it.
Given the specified constraints (as unfortunate as that situation is)...
using UNION ALL is the most appropriate solution to satisfying the specification.
If we are wanting the "past seven days", then we (our code) needs to figure out which tables will be required (vs the tables that would be required e.g. "so far this month") and dynamically construct SQL text.
We first write the query against one table, get that tested.
SELECT t.fee
, t.fi
, t.fo
, t.fum
FROM sales_data_20190128 t
WHERE t.foo = ?
Then we just repeat that query for each table that might included rows we are interested in, excluding tables we know for sure will not contain rows we want, and combine the queries with UNION ALL set operator.
If we need the whole set ordered, then wrap each SELECT in parens, and finish with an ORDER BY clause. e.g.
( SELECT t.fee, t.fi, t.fo, t.fum
FROM sales_data_20190124 t
WHERE t.foo = ?
)
UNION ALL
( SELECT t.fee, t.fi, t.fo, t.fum
FROM sales_data_20190125
WHERE t.foo = ?
)
UNION ALL
( SELECT t.fee, t.fi, t.fo, t.fum
FROM sales_data_20190126 t
WHERE t.foo = ?
)
UNION ALL
( SELECT t.fee, t.fi, t.fo, t.fum
FROM sales_data_20190127 t
WHERE t.foo = ?
)
UNION ALL
( SELECT t.fee, t.fi, t.fo, t.fum
FROM sales_data_20190128 t
WHERE t.foo = ?
)
ORDER BY 1,2
Do NOT try to simplify the code by creating view that concatenates all of the tables together, and query against that. Don't do that.

SQL statement - compare two tables columns

I'm new to PHP and SQL.
I want to compare two table-columns.
This is what we have
Table 1(Review)
Table 2(Customer)
We want the Review_Customer_Id to be = to the Customer_Id in the Customer Table
How would you write this?
In my head it would be something like this:
SELECT * FROM Customer
WHERE Customer_Id = Review_Customer_Id IN (SELECT Review_Customer_Id FROM Review);
Check this Answer
SELECT (fistnameField + "" + LastnameField) AS Name FROM Customer WHERE Customer_Id IN (SELECT Customer_Review_Id FROM Review);

Select data from another table with where clause

What's wrong with this query, im selecting data from 3 different tables here. First title of exam from "class_exams" table , second selecting sum of total marks from "results" table. Query works fine without where clause.
SELECT id, exam_date , (
SELECT title
FROM class_exams
WHERE result_heads.exam_id = class_exams.id
) AS exam_title, (
SELECT sum( marks )
FROM results
WHERE result_heads.id = results.head_id
) AS obt_marks
FROM `result_heads` WHERE exam_title = 'test';
Error comes
Unknown column 'exam_title' in 'where clause'
Consider using Join
If I understand the table schema , it should be like this :
SELECT result_heads.id, result_heads.exam_date , sum( results.marks )AS obt_marks
FROM results JOIN result_heads
ON results.exam_id = result_heads.id
GROUP BY result_heads.id, result_heads.exam_date
I think you need to add the name of table in where clouse
WHERE `tbl_name`.`exam_title = 'test';
I know this is an old post, but I like to fill the answers where old posts end to prevent dead end posting. It looks like the table name is not being called out in the From clause
See this link http://www.techonthenet.com/mysql/where.php and look at the example - Joining Tables.

Get a list of ids not present in a table

I have a list of ids, and I want to query a mysql table for ids not present in the table.
e.g.
list_of_ids = [1,2,4]
mysql table
id
1
3
5
6
..
Query should return [2,4] because those are the ids not in the table
since we cant view ur code i can only work on asumption
Try this anyway
SELECT id FROM list_of_ids
WHERE id NOT IN (SELECT id
FROM table)
I hope this helps
There is a horrible text-based hack:
SELECT
substr(result,2,length(result)-2) AS notmatched
FROM (
SELECT
#set:=replace(#set,concat(',',id,','),',') AS result
FROM (
select #set:=concat(',',
'1,2,4' -- your list here
,',')
) AS setinit,
tablename --Your tablename here
) AS innerview
ORDER BY LENGTH(result)
LIMIT 1;
If you represent your ids as a derived table, then you can do this directly in SQL:
select list.val
from (select 1 as val union all
select 2 union all
select 4
) list left outer join
t
on t.id = list.val
where t.id is null;
SQL doesn't really have a "list" type, so your question is ambiguous. If you mean a comma separated string, then a text hack might work. If you mean a table, then something like this might work. If you are constructing the SQL statement, I would advise you to go down this route, because it should be more efficient.

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?