TableA
Shop ID
Item
Price
Shop A
Item1
101
Shop A
Item2
102
Shop A
Item3
103
Shop A
Item4
104
Shop A
Item5
105
Shop A
Item6
106
Shop A
Item7
107
......
.....
.....
Shop A
Item27
127
Shop B
Item1
201
Shop B
.....
.....
Shop B
Item27
227
Shop C
Item1
301
Shop C
.....
.....
Shop C
Item27
327
Suppose I have a table like above in which I would like to convert to table below
Shop ID
Item1
Item2
Item3
Item4
.....
Item27
Shop A
101
102
103
104
.....
127
I tried using query with multiple left joins
SELECT T1.[Shop ID], T1.[Price] AS Item1 .... T27.[Price] AS Item 27
FROM (TableA AS T1
LEFT JOIN TableA AS T2 ON T1.[Shop ID] = T2.[Shop ID])
LEFT JOIN TableA AS T3 ON T1.[Shop ID] = T3.[Shop ID])
...
LEFT JOIN TableA AS T27 ON T1.[Shop ID] = T27.[Shop ID]
WHERE T1.[Item] = 'Item1'
AND T2.[Item] = 'Item2'
...
AND T27.[Item] = 'Item27'
AND T1.[Shop ID] = 'Shop A'
This work for smaller number of LEFT JOIN but when number of LEFT JOIN > 20, Access essentially stopped forever, my actual table is a bit more complicate. Any one can suggest ways for the conversion? TIA
One approach is to use a pivot query:
SELECT
[Shop ID],
MAX(IIF([Item] = "Item1", Price, NULL)) AS Item1,
MAX(IIF([Item] = "Item2", Price, NULL)) AS Item2,
MAX(IIF([Item] = "Item3", Price, NULL)) AS Item3,
...
MAX(IIF([Item] = "Item27", Price, NULL)) AS Item27
FROM TableA
GROUP BY [Shop ID]
ORDER BY [Shop ID];
You may also look into cross tab options with MS Access.
Try CrossTab query like-
TRANSFORM First(t.Price) AS FirstOfPrice
SELECT t.ShopID
FROM TableA as t
GROUP BY t.ShopID
ORDER BY t.Item
PIVOT t.Item;
Input:
Output:
Related
I have four table and structure as below :
1)Budget
id Budget_name
1 test1
2 test2
3 test3
2)Yearly Budget
id amount_yearly budgetid
1 1000 1
2 2000 2
3 5000 3
ri_spent
id Spent_amount budgetid
1 100 2
2 100 2
3 200 3
4)FI_spent
id Spent_amount budgetid
1 100 2
2 100 3
3 200 3
i want to fetch data accourding to or based on first budget table id
below is the query i was trying:
select d.centers as Cost_Center,
ud.BUDGET_ANNUAL_AMOUNT as Annual_Budget,
l.LEAD_ID as Lead_Id,
l.AMOUNT as Lead_Amount,
f.FINANCEADD_ID as Finance_Id,
f.AMOUNT as Finance_Amount
from Cost_centers as d
inner join ANNUAL_BUDGET_BUDGET_CENTER as ud on d.id = ud.BUDGET_ID
inner join RI_DETAILS as l on l.COST_CENTER = d.id
inner join F_RI_DETAILS as f on f.COST_CENTER = d.id
ORDER BY d.id DESC
I want output of the following way:
Id Name ri_id FI_ID RI_Spent_Amount FI_Spent_Amount Annual_Buget
1 test1 1000
2 test2 1 1 100 200 1000
2 test2 2 100 2000
3 test3 3 2 300 100 2000
3 test3 3 200 2000
Any way if possible then please help me.
I want to minus annual budget with spent_amount later.
If possible then help me .
Please Try This Query ,
select b.id ,
b.Budget_name,
y.id,
y.amount_yearly,
r.id,
r.ri_spent,
f.id,
f.Spent_amount
from Budget b
INNER JOIN Yearly_Budget y on y.budgetid =b.id
INNER JOIN ri_spent r on r.budgetid =b.id
INNER JOIN FI_spent f on f.budgetid =b.id
ORDER BY b.id DESC
Could you please use left join instead of inner join. Requirement and sql arent matching so i may be little off. But you can get the idea.
select d.centers as Cost_Center,
ud.BUDGET_ANNUAL_AMOUNT as Annual_Budget,
l.LEAD_ID as Lead_Id,
l.AMOUNT as Lead_Amount,
f.FINANCEADD_ID as Finance_Id,
f.AMOUNT as Finance_Amount
from Cost_centers as d -- I assume this is the budget table
left outer join ANNUAL_BUDGET_BUDGET_CENTER as ud on d.id = ud.BUDGET_ID
left outer join RI_DETAILS as l on l.COST_CENTER = d.id
left outer join
(select sum(FINANCEADD_ID) FINANCEADD_ID,sum(AMOUNT) AMOUNT,id from
F_RI_DETAILS group by id) as f
on f.COST_CENTER = l.id
ORDER BY d.id DESC
I am trying to get a count(*) for different column from a different table using union.
//tbl_churidar
order_id order_no_first order_no
--------------------------------------
1 C 1000
2 C 1001
3 C 1002
//tbl_anarkali
order_id order_no_first order_no
--------------------------------------
1 A 1003
2 A 1004
3 A 1005
//tbl_assign
assign_id order_id order_no_first
---------------------------------------
1 1 C
2 1 A
3 2 C
4 3 C
5 2 A
6 3 A
//tbl_unit_status
status_id assign_id status_status stitching_worker
-----------------------------------------------------------
1 1 Stitch AA
2 2 QC {null}
3 3 Stitch BB
4 4 Stitch BB
5 5 Stitch AA
6 6 Stitch CC
from the table tbl_unit_status where status_status = Stitch should INNER JOIN with other two table and get the total count of churidar and anarkali each stitching_worker taken.
the required output is,
churidar anarkali stitching_worker
----------------------------------------
1 1 AA
2 0 BB
0 1 CC
I have tried to get the above output but got stuck. Below is my code,
SELECT churidar, anarkali, stitching_worker
FROM ((
SELECT count(*) AS churidar, NULL AS anarkali,
us.stitching_worker
FROM tbl_unit_status us
INNER JOIN tbl_assign a ON a.assign_id = us.assign_id
INNER JOIN tbl_churidar o ON
(o.order_id = a.order_id AND
o.order_no_first = a.order_no_first)
INNER JOIN tbl_contacts c ON c.contacts_id = o.contacts_id
LEFT JOIN tbl_title t ON t.title_id = c.title_id
WHERE us.status_status = "Stitch" AND
o.order_no_first = "C"
GROUP BY us.stitching_worker
)
UNION (
SELECT NULL AS churidar, count(*) AS anarkali,
us.stitching_worker
FROM tbl_unit_status us
INNER JOIN tbl_assign a ON a.assign_id = us.assign_id
INNER JOIN tbl_anarkali o ON (
o.order_id = a.order_id AND
o.order_no_first = a.order_no_first)
INNER JOIN tbl_contacts c ON c.contacts_id = o.contacts_id
LEFT JOIN tbl_title t ON t.title_id = c.title_id
WHERE us.status_status = "Stitch" AND
o.order_no_first = "A"
GROUP BY us.stitching_worker
)
) AS T1
the output for the above code is,
churidar anarkali stitching_worker
----------------------------------------
1 0 AA
{null} 1 AA
2 0 BB
0 1 CC
how to get the required output. I have tried a lot. Help me find the answer. Thankyou.
If I understand correctly (which I may not), you don't need the first two tables. You can get the information you need from tbl_assign and just use aggregation:
select us.stitching_working,
sum(a.order_no_first = 'C') as churidar,
sum(a.order_no_first = 'A') as anarkali
from tbl_unit_status us join
tbl_assign a
on us.assign_id = a.assign_id
where us.status_status = 'Stitch'
group by us.stitching_working;
I have a database with 3 tables records, categories, relational.
records (id, lat, lng)
categories (c_id, c_value)
relational (r_id, c_id)
records
id | lat | lng
----------------------
1 23.57258 -35.28412
2 23.54855 -35.18881
3 23.74128 -35.17469
categories
c_id | c_value
---------------
100 groceries
101 bags
102 drinks
relational
id | c_id
------------
1 100
1 102
2 101
3 100
The relational.r_id = records.id and the relational.c_id = categories.c_id
I want to take pairs from records with different c_value, so I want to make a self join in records and inner join in categories and relational.
I've made this without the self join in records
SELECT id, lat, lng, c_value
FROM records
JOIN relational
ON records.id = relational.id
JOIN categories
ON relational.c_id = categories.c_id
WHERE c_value = "V1"
I tried something like this but it didn't work. I have problem with the R1.c_value.
SELECT R1._id, R1.lat, R1.lng, R1.c_value, R2._id, R2.lat, R2.lng,
R2.c_value
FROM records R1, records R2
JOIN relational
ON records.id = relational.id
JOIN categories
ON relational.c_id = categories.c_id
WHERE R1.c_value = "groceries" AND R2.c_value = "bags"
Do you know how can I combine those 3 joins in order to take 2 rows from records with the criteria of the other tables?
I want to have an output like this:
For "groceries" and "bags" as c.value
1 | 23.57258 | -35.28412 | groceries | 2 | 23.54855 | -35.18881 | bags
You can self join records table along with categories and relational like this:
SELECT r1.id, r1.lat, r1.lng, r2.id, r2.lat, r2.lng, c.c_value
FROM records r1 JOIN relational rl ON r1.id = rl.r_id
JOIN records r2 ON r2.id = rl.r_id
JOIN categories c ON rl.c_id = c.c_id
WHERE r1.id <> r2.id;
I finally found exactly what I wanted. My query should be like this. Thanks for the help and the idea anyway.
SELECT r1.id, r1.lat, r1.lng, c1.c_value, r2.id, r2.lat, r2.lng, c2.c_value
FROM records r1 JOIN relational rl1 ON rl1.id = r1.id
JOIN categories c1 ON rl1.c_id = c1.c_id
JOIN records r2 JOIN relational rl2 ON rl2.id = r2.id
JOIN categories c2 ON rl2.c_id = c2.c_id
WHERE c1.c_value = "groceries" AND c2.c_value = "bags"
I have three table in mysql:
1.t_item
itemID item_name
1 pen
2 luxury pen
2.s_item
itemID item_name
1 shoes
2 clothes
3 computer
3.track
trackID item_no item_type
1 1 t_item
2 2 t_item
3 2 s_item
And I want to get trackID, item name for item_no in table track.
How can I write the sql statement?In track,trackID 2,3 have same item_no but different item type, Can I use join?
Here you are:
(SELECT track.trackID, t_item.item_name FROM track
LEFT JOIN t_item ON track.item_no=t_item.itemID
WHERE track.item_type = 't_item')
UNION
(SELECT track.trackID, s_item.item_name FROM track
LEFT JOIN s_item ON track.item_no=s_item.itemID
WHERE track.item_type = 's_item')
Here you go, It will work:
(SELECT t.trackID,ti.item_name
FROM track t
LEFT JOIN t_item ti
ON t.item_no=ti.itemID AND t.item_type='t_item')
UNION
(SELECT t.trackID,si.item_name
FROM track t
LEFT JOIN s_item si
ON t.item_no=si.itemID AND t.item_type='s_item')
Yes, you can use JOIN like this:
SELECT t.trackID, t.item_no,
(CASE WHEN t.item_type = 't_item' THEN ti.item_name
WHEN t.item_type = 's_item' THEN si.item_name
END) AS item_name
FROM track t
LEFT JOIN t_item ti ON t.item_no = ti.itemID AND t.item_type = 't_item'
LEFT JOIN s_item si ON t.item_no = si.itemID AND t.item_type = 's_item';
Here is the sqlfiddle.
Using AdventureWorks 2008 R2, I want to query a grid that
Lists each territory / sales person combination on the vertical axis
Lists each subcategory on the horizontal axis
Each cell specifies the total number of sales (unique SalesOrderID s) where all the line items in that category / territory / sales person combination have 2 or more quantity
Here's a sample (made up data, I don't know how to query the real stuff!):
M.Bikes Chains Gloves
Northwest John Doe 15 4 3
Canada John Doe 4 2 1
Northwest Jill Doe 0 5 3
Canada Jill Doe 1 5 1
etc
I think the following SQL will get me this for mountain bikes (subcat id 1), but I don't know how to add more columns easily. If I start making an independent query for every column, its going to get very slow, very quickly (especially when I have to rejoin all those columns together!)
select
terr.Name as Territory,
sp.BusinessEntityID,
SUM(case when detail.OrderQty > 1 then 1 else 0 end) as MatchingSales
from
sales.salesorderdetail detail
inner join sales.SalesOrderHeader header on header.SalesOrderID = detail.SalesOrderID
inner join sales.SalesPerson sp on sp.BusinessEntityID = header.SalesPersonID
inner join sales.SalesTerritory terr on terr.TerritoryID = sp.TerritoryID
inner join sales.SpecialOfferProduct sop on sop.ProductID = detail.ProductID
inner join Production.Product on Product.ProductID = sop.ProductID
inner join Production.ProductSubcategory subcat on subcat.ProductSubcategoryID = Product.ProductSubcategoryID
where
subcat.ProductSubcategoryID = 1 --mountain bikes
I want to preface this by saying I don't have AdventureWorks installed so I can't Check the query... I also don't know what the sales person name column is called so you will more than likely have to change that but here you go, you will just have to add a column at the top for each sub category. I am also assuming the original query is correct.
select
terr.Name as Territory,
sp.Name SalesPerson,
SUM(case when detail.OrderQty > 1 and subcat.ProductSubcategoryID = 1 then 1 else 0 end) as MBikes,
SUM(case when detail.OrderQty > 1 and subcat.ProductSubcategoryID = 2 then 1 else 0 end) as Chains,
SUM(case when detail.OrderQty > 1 and subcat.ProductSubcategoryID = 3 then 1 else 0 end) as Gloves
from
sales.salesorderdetail detail
inner join sales.SalesOrderHeader header on header.SalesOrderID = detail.SalesOrderID
inner join sales.SalesPerson sp on sp.BusinessEntityID = header.SalesPersonID
inner join sales.SalesTerritory terr on terr.TerritoryID = sp.TerritoryID
inner join sales.SpecialOfferProduct sop on sop.ProductID = detail.ProductID
inner join Production.Product on Product.ProductID = sop.ProductID
inner join Production.ProductSubcategory subcat on subcat.ProductSubcategoryID = Product.ProductSubcategoryID
group by terr.Name, sp.Name