My Sql Server database has three tables (Amount, wthdrwl, spent), each table has got id, and amount column.
I want to retrieve the amount column from these three different tables.
For both MySQL, SQL-Server, use UNION (implicit distinct) or UNION ALL:
SELECT Amount FROM Amount
UNION ALL
SELECT Amount FROM wthdrwl
UNION ALL
SELECT Amount FROM spent
I suppose there is some relation between three tables
SELECT A.Amount AS Amount ,
W.Amount AS Withdrawal ,
S.Amount AS Spent
FROM Amount A
LEFT OUTER JOIN wthdrwl W ON A.ID = W.ID
LEFT JOIN spent S ON A.ID = S.ID
Check here- SqlFiddle
Given your comment above, use a fixed string to indicate which table the values came from:
SELECT 'Amount' AS source, Amount FROM Amount
UNION ALL
SELECT 'whtdrwl' AS source, Amount FROM whtdrwl
UNION ALL
SELECT 'spent' AS source, Amount FROM spent
then you can easily identify the rows. You could write a query that does this as a single row with 3 columns, but it's far easier doing it this way.
If you want to put them all on one line then you have to have a way to link the columns together via a 2nd column, for example a txnId column that occurs in each of the 3 tables. Then you could do something like:
SELECT Amount.Amount Amt, whtdrwl.Amount Wdrl, spent.Amount Spd
FROM Amount, whthdrwl, spent
WHERE Amount.txnId=wthdrwl.txnId
Amount.txnId=spent.txnId
Use the following query-
select amount.amount, wthdrwl.amount, spent.amount from amount, wthdrwl, spent where amount.amount=wthdrwl.amount and wthdrwl.amount=spent.amount;
Here you have to use the table name along with the attribute to distinctly identify the amount attribute from 3 different table. You need to check for the equality of the id attribute in all 3 tables to correspond to a particular entity in a row of the resultset.
Related
I have a table with
id,column1, column2, column3,column4,column5
MySQL SELECT DISTINCT returns me exactly what I need and this query must remains exactly AS IS
SELECT DISTINCT column1,column3 from tablename
But as an output I have only 2 columns as requested in DISTINCT query above
column1,column3
Question - how to join to this result other corresponding columns from the same table ?
For example
column1,column3,id,column5
Spent a couple of hours in Google, the two 'nearest' idea is something like
SELECT DISTINCT column1,column3
from tablename AS tbl1
INNER JOIN (SELECT id,column5 from tablename) AS tbl2
But such a query anyway returns me ONLY
column1,column3
My Expected result should be
column1,column3 (exactly as a result of SELECT DISTINCT) + two other columns id,column3 - so that I could save this result to a new table with 4 columns.
But I have only two columns as I told above.
EXAMPLE added
http://sqlfiddle.com/#!9/1e4472/1
Please, pay attention - the query result does not show ID 4 because this ID has duplicates in BOTH fields - company and city. So this is exactly what I need.
So, HOW to show in results not only COMPANY and CITY, but also, for example, ID and MANAGER ?
In case of duplication, if you want to display the first(original) record with the same values of company & city (i.e. like here you want to show id=1 not id=4) then following query should work:
SELECT min(id) as id,manager,company,city
FROM DistinctResult
group by company,city;
Click here for Demo
Hope it helps!
Simply :
SELECT DISTINCT Column1,Column3,id,column5
Or alternatively , if there is more than 1 value for these columns:
SELECT Column1,Column3,MAX(id),MAX(column5)
GROUP BY Column1,Column3
But it really depends on the requirement and you weren't very clear.
I only have one table to count, I am not using any join. Is this possible?
Select engagement_type as name, COUNT(engagement_type) as y
From events
group By engagement_type
order By engagement_type
But only result is 1 row with count per engagement_type. I want to show all count of accounts without any engagement_type. Like these:
Will appreciate your answers! Thanks!
If there is a lookup-table, say EngagementTypes, where all possible values of engagement types are stored, then you can query this table to get the full list of all types and do a LEFT JOIN to events table in order to get the corresponding count:
Select t1.engagement_type as name, COUNT(t2.engagement_type) as y
From EngagementTypes AS t1
left join events as t2 on t1.engagement_type = t2.engagement_type
group By t1.engagement_type
order By t1.engagement_type
I have two tables... cloads and jpurch I need to pull transactionamount and transaction date column from both tables using one query. The transactions I need to pull must be between a date range of 1-1-2016 and 3-1-2016. Obviously the query I posted below wont work since it doesn't even address the jpurch in the where statement. The transaction dates do not need to match. I need all data for those columns.
select c.transactiondate, c.transactionamount, d.transactiondate, d.transactionamount
from cloads c, jpurch d
where c.transactiondate between '2016-05-01' and '2016-06-06'
#sanky send a good article on joins and is a good place to start. #SujeetSinha also brings up a point about how you will relate one table to the other.
I suspect the answer is you want only records between the tables that match (INNER JOIN) and that you want to match on transactiondate from one table to the other. In that case you could write a query like below. Because both tables have to match on an inner join your where statement for cloads would also filter jpurch.
select
c.transactiondate
,c.transactionamount
,d.transactiondate
,d.transactionamount
from
cloads c
INNER JOIN jpurch d
ON c.transactiondate = d.transactiondate
where
c.transactiondate between '2016-05-01' and '2016-06-06'
Thanks for More Info seems we as a community are still awaiting some help on what you want the data to look like in order to best guide you in your query. If you can take 3-4 rows from each of your tables show us the beginning data and a mock up of how you want it to look from those rows we will better be able to assist you.
If no relation and into 1 table are up meaning you want to APPEND one table to the other? If so use a UNION or UNION ALL depending on if you want all of the data returned or only DISTINCT values of it. ALL means give you everything with UNION alone means don't repeat rows that are already in the table on top of the union statement.
select
c.transactiondate
,c.transactionamount
from
cloads c
where
c.transactiondate between '2016-05-01' and '2016-06-06'
UNION ALL
select
d.transactiondate
,d.transactionamount
from
jpurch d
where
d.transactiondate between '2016-05-01' and '2016-06-06'
I need help makeing this selection.
Those are the 2 tables.
First table:
-there are Unique/Primary buyID-s( we can say that here buyID is and Order)
-distID-s can be many.
Second table:
-This table does not have an unique indentifier
-same buyID here ca more than one(basically buyID here offers more details on order, offers the a list of products for that order(buyID) from first table.
What I am trying to do, but I can't figure how to write the query:
-Select all buyID-s Where distID=119 From table 1(buyID-s will be 1724,1833 and 1890)
-From Table 2, sum the quantity, where buyID(from table2)=buyID(from table1).
As a result I must have:
distID=119
buyID=1724--------quantity=25 (as an example)
buyID=1833--------quantity=60
buyID=1890--------quantity=23(if there will also be the product_number list, will be awesome)
I hope this make sense.
I am not too much experienced,I am trying for # an hour, I am sure is not too hard, but this piss me of...
I am waiting for abit of help.
Thanks
Use LEFTJOIN and GROUP BY
select t1.distID, t2.buyID,sum(t2.quantity) as quantity
from table1 t1
left join table2 t2 on t2.buyID = t1.buyID
where t1.distID = 119
group by t1.distID, t2.buyID
If you also want to include product_number in resultset, then you have to select them using aggregate function like MAX(product_number) as product_number since the query use GROUP BY clause.
select sum(quantity) from Table2 where buyID in (select buyID from Table1 where distID=119)
This is my first time asking a question on here. It has been very helpful with learning.
I am trying to select a table and getting only rows that have a maximum value for its particular group in another table. One of the best answers that is very close but not quite there is this one (SQL Select only rows with Max Value on a Column) but it only relates to a single table. I have found some others with multiple table but not sure how exactly to use it.
I have a table with (simplified)
prodID, quantity, mach, etc
I then have a table with
prodStatusID, prodID, userID, subStatusID
a last table with sub status names
subStatusID, subStatusName
I am trying to get a table with all of the first table and the second table but only with the row that has the maximum status number and include the right status name.
My other concern which may not matter now but in a year or two when this thing starts to really fill up is performance. I dont know bad it is to have select inside a select but if I am trying to return all productions then it will be doing a query for every production.
Just to be clearer. in the second table prodStatus there might be 2 rows with prodID of 4 but the subStatusID for the first one would be 1 and the second one would be 2. The userID will be different. All I want to get back is the second row because it has the highest status number and I need the userID and statusName associated with that row.
I have been googling for 2 days to get this answer and I saw 1 about auctions but I just dont fully understand it even after researching it.
You need to create a subquery which get the maximum value of subStatusID for each prodID.
SELECT a.*, -- select only columns that you want to show
c.*, -- asterisks means all columns
d.*
FROM table1 a
INNER JOIN
(
SELECT prodID, max(subStatusID) maxID
FROM table2
GROUP BY prodID
) b ON a.prodID = b.prodID
INNER JOIN table2 c
ON b.prodID = c.prodID AND
b.maxID = c.subStatusID
INNER JOIN table3 d
ON c.subStatusID = d.subStatusID