i want to use pivot query using two different table.
i have two table one is GPadvance and second one is GPexpenditure .
i have given advance to gp and get the advance report form the gpadvance. second is whatever the expenduture done by gp it saved in gpexpenditure table. now i want to use pivot table accordingly to GP wise like
you can see the first one data is coming from gpadvance and second column is amount spent data is coming is gpexpenduture.
i want to use single query to use both table data if gp has spent money or not,
iam using the following pivot table for the advance release
select * from
(
select Unit_Name,gpname,isnull(amount,0)as amount,Mcomponent
from AdvGP_report
where unit_id='7' and year='2014-15' and quarter='4'
) as s
pivot
(
sum(amount) for mcomponent in("1","2","3")
) as a
and second pivot query for amount spent is
(
select isnull(financialtarget,0)as FT,MinorComponent_Id
from GPWDPMPR
where unitid='7' and mpryear='2014-15'
) as i
pivot
(
sum(FT) for MinorComponent_Id in("19","4","8")
) as b
its giving the result but repeated many times the amount.
Related
I have a database of stock tickers. Each ticker has a "structure" that signifies the type of deal we participated in in order to receive the shares. Examples of structures are Primary Offerings ("PO"), Secondary Offerings ("SEC"), "CMPO", "PIPE", etc. It is possible that the database includes a ticker multiple times with different structure. For example, the ticker "AAPL" could exist 2 times - one time with a structure of "PO" and another time with a structure of "SEC". I am trying to write a query that gathers all of the instances of a ticket if at least one of the structures is "PO". I want to show all of the structures that we have participated in in that ticker.
select * from #mytable where structure = 'PO' will only show me the deals with structure PO. If a ticker has a structure equal to 'PO' then I want all deals with that ticker regardless of the structure.
I have also tried:
select * from (select distinct ticker from #mytable where structure = 'PO') PO
join #mytable m on PO.ticker = m.ticker
This works, but joining a table to the same table seems redundant and it seems like there should be a better way to do this.
I am writing this query in SQL -- could someone please help me write a query for this situation? Thank you!!
I don't think there is a way around selecting from the table twice. however if you don't like the self join you could do a correlated subquery.
select * from mytable where ticker in
(select ticker from mytable where structure = 'PO')
I have multiple tables in my project and I want to count out something from a table which is video table , first let me attach the table Images...
above is table_std
above is tablesubject
above is table_chapter
above is table video
now I want to fund total sum of videotime which is in the tbvid and want it against stdid like below
so here first it all starts with standard table , next table subtable is dependent of std table then chap table is depends over sub table and vid table depends over chap table.
for me the sql query is going to be very difficult if anyone can help me with this , it will be so appreciated !
Refer from
SUM(subquery) in MYSQL
SELECT `std`.*,
(SELECT SUM(`video`.`vidtime`) FROM `video` WHERE `vidid` = `std`.`stdid`) AS `sum_vidtime`
FROM `table_std` AS `std`
If you want to use only these columns: stdid, vidtime (sum_vidtime in my case) then it is no need to join multiple tables. Use sub query.
And as everyone said, please don't use image for an example.
I am having a table "all_data" which consists 2 set of records
a) Independent records which should get fetched all times
b) All records from table "all_product"
Now I am also having few more tables like
'MS_product', 'apple_product' ,'Linux_product' and all these tables are subset of table 'all_product'.
I already have used left or right join but looks like this will not be usefful.
I want to fetch all the Independent records from table 'all_data' and only matched records from other tables i.e. 'ms_product' so final output should have
all independent records + matched record from 'MS_product' or 'apple_product' or 'Linux_product' table.
You can use the MINUS operator for the first query :
SELECT *
FROM all_records
MINUS
(
SELECT *
FROM MS_product
UNION
SELECT *
FROM Apple_product
)
It will gives you all the tuples who are not in the MS_product and Apple_product tables.
However I don't recommend to design your database like you are doing. Prefer a single table with a dedicated column for the brand product.
I have two tables that have different data that I need to merge. They do have similarities such as: Order number, Name, type or product. But they have separate data as well like: Order date, and Engravings.
Would I do two separate Append queries in Access into a merged table? Or one Append queries? Or just keep the data separate?
I am new to Access and trying to find the best way to approach this.
Merging the two tables into one completely defeats the purpose of using a database and you're better off using excel at that point. You want to split the data as much as possible along logical lines so that you can find, say... all the orders that Mr X has ever made for a specific product. And in that case you're going to want to have separate tables for customers, orders, engravings and the like.
The best practice from a design standpoint is to place fields that each table has in common into a third "master" table, then create relationships from that table to the existing tables and delete the data that has been transferred to the main table (except for the primary keys, which have to be common with your master table).
To create the master table, use a Make Table query to generate the master table based on one of your tables, then an append query to add any products in the master table that might not be common to both, based on the other table. Finally, delete queries for each table would rid you of redundant data in both original tables.
However, I strongly suggest you use Microsoft's tutorials and download the NorthWind sample database so you can get an idea of what a properly structured database looks like. The beginner's learning curve for access is very steep and having well built example databases is almost a requisite.
Make a backup of your database(s) and play with it until it turns out right. Do not make the mistake of playing with live data until you know what you're doing.
As you have similar fields on either table, take the Order number field from both tables using a union query. Something like:
SELECT tbl_Delivery_Details.OrderNo
FROM tbl_Delivery_Details
GROUP BY tbl_Delivery_Details.OrderNo
UNION
SELECT tbl_Delivery_Header.[Order number]
FROM tbl_Delivery_Header
GROUP BY tbl_Delivery_Header.[Order number];
This would take the order numbers from the delivery details table and from the delivery header table and merge them into one list with only one instance of each order number. Save the query.
You could then use this query in a new query. Bring in your 2 tables to this query and insert the fields from either table that you require.
As users add records to the tables they will be added to the union selet query when it is next run.
PB
It depends on what you want to do. Let's assume you have tables A (with 50 records) and B (with 75) records, and both tables have a similar column called OrderID.
Appending Rows:
If you want to create a table with 125 total records by combining records (rows) from A and records (rows) from B, run the following two queries:
Query 1:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1 as DATA INTO C
FROM A;
Query 2:
INSERT INTO C ( ORDER_NUMBER, DATA )
SELECT B.ORDER_NUMBER, B.TEXT_FIELD2
FROM B;
Appending Columns: If you want to create a table with 75 total records where you are appending columns from A to the columns in B, then run the following query:
SELECT B.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A RIGHT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
... in a similar way, you can append columns in B to columns in A in a new table C with a total of 50 records by running the following query:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A LEFT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
I wanted to know if it was possible to make a select on a table that contains multiple field and join them in 1 result :
Example :
Table :
id
dayOne_City
dayTwo_City
dayThree_City
Result : one column that contains the rows of all the cities (Distinct).
2) Am i better to do a view if i have a lot of query to that specific list ?
3) should i do 3 select with union ?
Thank you
You should be fine with:
select dayOne_City from YourTable
UNION
select dayTwo_City from YourTable
UNION
select dayThree_City from YourTable
However, you should review your design to allow multiple cities per whatever-is-that-your-table stores. That is, create an actual many-to-many relationship by creating an intermediate table between YourTable and Cities.
select concat_ws(',', id, dayOne_city, dayTwo_city, dayThree_city, etc...) as allInOne
Details on the function used here. However, I should ask why you're doing this. By joining the fields together, you're destroy any chance of reliably extracting/separating the data again later. Only do this kind of "bulking" if you never plan on using separate portions of the data elsewhere based on the results of this query.