getting different column names from 2 tables in sql server - sql

I have Item and Sales tables with different columns like,
Item
ItemName
Itemcost
ItemQty
Sales
SalesName
SalesDate
SalesQty
In the above tables there is not common column,but i need to get output as
ItemName,ItemCost,SalesName,SalesDate.
I tried with union,
select ItemName,ItemCost from Item
union
select SalesName,SalesDate from Sales
but am getting only 1 table columns like ItemName,ItemCost
Please help me on this issue
Thanks In Advance
Venkat.

Use below sql query :
select i.ItemName, i.ItemCost, s.SalesName, s.SalesDate
from Item as i, Sales as s;

Use the below query :
This will join every row in table1 with table2 (the Cartesian product) returning all columns.
Select im.ItemName, im.ItemCost, sl.SalesName,sl.SalesDate from Item as im, Sales as sl;

Related

Get max date (last date sale) column against each item no?

I have following SQL Query which return result
of itemno and no of total quantity sale.I want itemdescripton column against each item no as well.ITEMDESC# column in table invitems.
SQL QUERY :
select INITEMS.ITEMNO,(COUNT(INITEMS.ITEMNO)*COUNT(INITEMS.QTY)) 'Item
Sale',INITEMS.ITEMDESC#1 from InvItems INITEMS
INNER JOIN InvHdr HDR ON INITEMS.INVNO=HDR.INVNO
WHERE INITEMS.TYPE='3'
GROUP BY INITEMS.ITEMNO,INITEMS.ITEMDESC#1
I want max date (last date item sale) column in result.Date column in InvHdr table against each InvNo
As explain in the earlier comments, your query does not works because you have different ITEMDESC for the same ITEMNO
This you "gives" you the result that you want
select INITEMS.ITEMNO,
MAX(INITEMS.ITEMDESC#1) AS 'ITEMDESC',
(COUNT(INITEMS.ITEMNO)*COUNT(INITEMS.QTY)) 'Item Sale'
FROM InvItems INITEMS
INNER JOIN InvHdr HDR ON INITEMS.INVNO = HDR.INVNO
WHERE INITEMS.TYPE = '3'
GROUP BY INITEMS.ITEMNO
But you must check why there are such as in your data. You can list out those ITEMNO that is of such case
SELECT ITEMNO
FROM InvItems
GROUP BY ITEMNO
HAVING MAX(ITEMDESC#1) <> MIN(ITEMDESC#1)
Assuming you are working with an SQL Server version higher than 2005 (which is a pretty safe assumption these days), you can use the over clause with aggregating functions, thus potentially eliminating the need for the group by clause (that is potentially since group by will return distinct results for each value (or set of values) that exists in the group by clause - so you might also need to use distinct:
SELECT initems.itemno,
COUNT(initems.itemno) OVER (PARTITION BY initems.itemno)
* SUM(initems.qty) OVER (PARTITION BY initems.itemno)
FROM InvItems AS initems
WHERE initems.type = '3'

Get Sum of quantities from multiple tables?

I have at least 8 tables from where I need to match the customer name and fetch the quantities and get the sum of all the quantities fetched from these 8 tables. I am trying to write a code which will ignore the customer whose sum of quantities is zero.
For an example lets take two tables purchase_sugar and sales_sugar I have tried a lot of queries but only this one is returning some result which is wrong.
SELECT sum(purchase_sugar.qty + sales_sugar.qty) AS Total_Amount from purchase_sugar inner join sales_sugar on purchase_sugar.supplier = sales_sugar.customer WHERE purchase_sugar.supplier = "+str(x.id)+"
The Table structures are like:
purchase_sugar have two columns supplier and qty.
And sales_sugar have structure like customer and qty.
How can I get the SUM of QUANTITIES of these tables if I provide one name and search it through these tables and get the quantities. The other thing is that I dont want the customer to be found in all the tables. If it is found in one table we should just get the quantity from that one table and for that reason I don't think that JOIN is useful or may be i am wrong.
To take care of the situation where a supplier/customer is not in all the tables, you can use union all and group by:
select name, sum(p_qty) as sum_p, sum(s_qty) as sum_s,
sum(p_qty) + sum(s_qty)
from ((select ps.supplier as name, ps.qty as p_qty, 0 as s_qty
from purchase_sugar ps
) union all
(select ss.customer as name, 0, ss.qty
from sales_sugar ss
)
) s
group by name;
Notes:
This query gets results for all names. You can use a where clause to restrict the results to one name.
You don't have to split the quantities into two (or eight) different columns, if you just want the overall sum.
You can aggregate before the union all, but that is not necessary.
you should JOIN the sum and not sum the join
select t1.purchase_sum + sales_sum as Total_Amount
from (
select purchase_sugar.supplier, sum(purchase_sugar.qty) as purchase_sum
from purchase_sugar
group by purchase_sugar.supplier
) t1
inner join (
select sales_sugar.customer, sum(sales_sugar.qty) as sales_sum
from sales_sugar
group by sales_sugar.customer
) t2 on t1.supplier = t2.customer and t1.supplier = "+str(x.id)+"

Get a column from each Table

I'm working with SQL Server 2008
I have 2 tables and I want to get 2 columns from the 1st table and 1 column from the 2nd table
when I make JOIN it gives me another things...
here is my code:
Select SUM(Sales.CDnum) as CDnum, SUM(Sales.Total) as TotalMoney,
SUM(Expenses.Costs) as Expenses, SUM(Sales.Total - Expenses.Costs) as Winnings
from Sales
JOIN Expenses
ON Sales.ID = Expenses.ID
but when I execute the code it gives me only one raw from the 1st table and 1 raw from the second ... because I have 1 raw in the 2nd table and 1st I have many raws...
someone can help me....
as I see ID columns are not related to each other in both tables, so you need to union them, for example like in this query:
select sum(q.sales), sum(q.expense), sum(q.sales)-sum(q.expense) as winnings
from (
select sum(Sales.Total) as sales, 0 as expense from Sales
union all
select 0 as sales, sum(Expenses.Costs) as expense from Expenses
) as q

subquery the same table in select statement

I have a resturant db and I need to total up the total value of all the items sold individually. So if I sold a hamburger that has a base price of $10.00 with bacon which costs $1.00 and a hambuger(again $10.00) with avacado that costs $0.50 I need to get $21.50 returned. My invoice table looks like this:
invoice_num item_num price item_id parent_item_id
111 hmbg 10.00 guid_1 ''
111 bacn 1.00 guid_2 guid_2
112 hmbg 10.00 guid_3 ''
112 avcd 0.50 guid_4 guid_3
I can get the sum of all the parent items like this:
SELECT item_num, SUM(price) FROM invoices WHERE parent_item_id = ''
it is the adding of the toppings that is confusing me. I feel like I need to add a subquery in the SUM but I'm not sure how to go about doing it and referencing the original query to use the item_id.
SELECT item_num, sum(i.price) + sum(nvl(x.ingred_price,0))
FROM invoices i
LEFT OUTER JOIN
(SELECT parent_item_id
, sum(price) ingred_price
FROM invoices
WHERE parent_item_id IS NOT NULL
GROUP BY parent_item_id) x
ON x.parent_item_id = i.item_id
WHERE i.parent_item_id IS NULL
GROUP BY item_num
Here's a SQL Fiddle that proves the above code works. I used Oracle, but you should be able to adapt it to whatever DB you are using.
Assumption: You don't have more than one level in a parent child relationship. E.g. A can have a child B, but B won't have any other children.
Not clear based on your question (see my comment) but as I understand it a simple group by will give you what you want. If not please explain (in the original question) why does this query does not work --- what is it missing from your requirements?
SELECT item_num, SUM(price)
FROM invoices
GROUP BY item_num
Hard to say, but looks like you need recursive cte.
Here's example for PostgreSQL:
with recursive cte as (
select
t.invoice_num, t.price, t.item_id, t.item_num
from Table1 as t
where t.parent_item_id is null
union all
select
t.invoice_num, t.price, t.item_id, c.item_num
from Table1 as t
inner join cte as c on c.item_id = t.parent_item_id
)
select invoice_num, item_num, sum(price)
from cte
group by invoice_num, item_num
sql fiddle demo
I've used null for empty parent_item_id (it's better solution than using empty strings), but you can change this to ''.

Need to sum two columns from two tables, separately, based on products in table 3

I have three tables.
Table 1: Product mapping table,
Table 2: FY12 Sales Numbers,
Table 3: FY13 Sales Numbers.
Basically, I want the end result to be three columns: Product, FY12 Qty, FY13 Qty.
Right now I join the Product Mapping table to the Two Sales tables. If I do this one sales table at a time I get the right numbers. As soon as I try to get the sum from the other table the numbers are all off.
Here is the code that works for getting quantity from one table, but I can't figure out how to get another column in the result that sums the same product groupings from another table.
select PRODUCT_MAPPING.PRODUCT_FAMILY as PRODUCT_FAMILY,
PRODUCT_MAPPING.PRODUCT_CONFIG as PRODUCT_CONFIG,
sum(CURRENTWEEK_PIPE.FY13_QUANTITY) as FY13_QUANTITY
from PRODUCT_MAPPING PRODUCT_MAPPING,
CURRENTWEEK_PIPE CURRENTWEEK_PIPE
where CURRENTWEEK_PIPE.PRODUCT_ITEM=PRODUCT_MAPPING.PRODUCT_ITEM
and PRODUCT_MAPPING.PRODUCT_CONFIG is not null
group by PRODUCT_MAPPING.PRODUCT_FAMILY, PRODUCT_MAPPING.PRODUCT_CONFIG
order by PRODUCT_MAPPING.PRODUCT_FAMILY ASC, PRODUCT_MAPPING.PRODUCT_CONFIG ASC
do you try to use subqueries?
SELECT sum(CURRENTWEEK_PIPE.FY13_QUANTITY), sum(CURRENTWEEK_PIPE.FY12_QUANTITY) From .....
select PRODUCT_MAPPING.PRODUCT_FAMILY as PRODUCT_FAMILY,
PRODUCT_MAPPING.PRODUCT_CONFIG as PRODUCT_CONFIG,
(SELECT sum(CURRENTWEEK_PIPE.FY13_QUANTITY), sum(CURRENTWEEK_PIPE.FY12_QUANTITY) From ....... ) as FY13_QUANTITY from PRODUCT_MAPPING PRODUCT_MAPPING,
CURRENTWEEK_PIPE CURRENTWEEK_PIPE where CURRENTWEEK_PIPE.PRODUCT_ITEM=PRODUCT_MAPPING.PRODUCT_ITEM
and PRODUCT_MAPPING.PRODUCT_CONFIG is not null group by PRODUCT_MAPPING.PRODUCT_FAMILY, PRODUCT_MAPPING.PRODUCT_CONFIG order
by PRODUCT_MAPPING.PRODUCT_FAMILY ASC, PRODUCT_MAPPING.PRODUCT_CONFIG
ASC