I'd appreciate if someone could help me with my query.
I have a table containing Currencies and Amounts for each currency.
My first query is:
select Currency, SUM(Amount) amnt from [MyTable]
where CODE in(410, 420)
group by Currency
So, I get:
Currency | amnt
USD | 15
I want to show all available currencies from my table and to join them with this result (amount for these currencies must be null)
Currency | amnt
USD | 15
EUR |
AED |
I get the list of all currencies by query:
select currency from [MyTable] group by currency
So, How can I join these two queries?
Thanks in advance!
Try this:
SELECT a.Currency, SUM(CASE WHEN a.CODE IN (410, 420) THEN a.Amount ELSE 0 END) amnt
FROM MyTable a
GROUP BY a.Currency
OR
SELECT A.Currency, SUM(B.Amount) amnt
FROM (SELECT DISTINCT Currency FROM MyTable) A
LEFT JOIN MyTable B ON A.Currency = B.Currency AND B.CODE IN (410, 420)
GROUP BY A.Currency
OR
SELECT A.Currency, SUM(B.Amount) amnt
FROM (SELECT Currency FROM MyTable GROUP BY Currency) A
LEFT JOIN MyTable B ON A.Currency = B.Currency AND B.CODE IN (410, 420)
GROUP BY A.Currency
Related
I want to search both table with all the records for cid 23 here
Total is table1-cid:23&w_id:1+2->(500+300),
Advance is table1-cid:23&w_id:1+2(100+100)+table 2-w_id:1+2(100+100+100+150)
Pending is Total-Advance
Tried using below query to display last table in pic with no luck.
SELECT E.cid, SUM(E.total) as Total, SUM(E.advance)as Advance, (SUM(E.total)-SUM(E.advance)- SUM(R.advance)) as Pending
FROM table1 AS E
LEFT JOIN table2 R ON E.w_id=R.w_id
WHERE (E.cid =23)
This is not the best query I've done but I obtain the result you want:
wwtest1 = table-1 , wwtest2 = table 2.
SELECT w1.cid AS cid,
(SELECT SUM(total) FROM wwtest WHERE cid = 23) AS total,
((SELECT SUM(advance) FROM wwtest WHERE cid = 23) + (SELECT SUM(advance) FROM wwtest2)) AS advance,
((SELECT SUM(total) FROM wwtest WHERE cid = 23) - ((SELECT SUM(advance) FROM wwtest WHERE cid = 23) + (SELECT SUM(advance) FROM wwtest2))) AS pending
FROM wwtest w1
WHERE w1.cid = 23 GROUP BY w1.cid;
I'm trying t create a compare, now i'm only able to make it as multiple result (two different result).
both of the result above is from two queries.
My first result query
SELECT
customercode,
CONVERT(DATE, TransDate) transdate,
SUM(TotalReceivable) AS total
FROM
SalesOrderHeader
WHERE
CustomerCode = 'K-MDMM4'
AND TransDate BETWEEN '2016-07-25' AND '2016-07-30'
GROUP BY
CONVERT(DATE, TransDate), customercode
and my second query
SELECT
b.OutletCode AS outlet,
tanggal,
(cash + cc + dc + flash + piutang + reject + disc50 +
isnull(spesial_item,0)) total
FROM
transaksi a
LEFT JOIN
Outlet b ON a.Outlet = b.OutletCode
LEFT JOIN
area c ON b.areacode = c.areacode
WHERE
b.active = 1
AND b.OutletCode LIKE 'K-MDMM4'
AND flag = 1
AND tanggal BETWEEN '2016-07-25' AND '2016-07-30'
GROUP BY
b.OutletCode, tanggal, cash, cc, dc, flash,
piutang, reject, disc50, spesial_item, ba, mpm, tf,
ul,remarks
ORDER BY
tanggal DESC
I want this result.
customercode | transdate | total_tbl1 | total_tbl2
K-MDMM4 2016-07-25 6004050 6004050
K-MDMM4 2016-07-26 6880340 6880340
K-MDMM4 2016-07-27 5745040 5745040
K-MDMM4 2016-07-28 7424820 7424820
I can't use jsfiddle :(. I don't know why. I can't create table via queries.
From now, I have this query
SELECT
b.OutletCode AS outlet,
tanggal,
(cash + cc + dc + flash + piutang + reject + disc50 +
isnull(spesial_item, 0)) total,
SUM(d.TotalReceivable) AS total
FROM
transaksi a
LEFT JOIN
Outlet b ON a.Outlet = b.OutletCode
LEFT JOIN
area c ON b.areacode = c.areacode
LEFT JOIN
salesorderheader d ON CONVERT(DATE, a.tanggal) = CONVERT(DATE, d.transdate)
WHERE
b.active = 1
AND b.BrandCode LIKE '%%'
AND b.OutletCode LIKE '%%'
AND flag = 1
AND YEAR(tanggal) = '2016'
AND MONTH(tanggal) = '7'
AND outlet = 'K-MDMM4'
GROUP BY
OutletCode, tanggal, cash, cc, dc, flash,
piutang, reject, disc50, spesial_item, transdate, totalreceivable
ORDER BY
tanggal DESC
and the result so far from my desired result....
Combine both queries into a single join and select
SELECT tbl1.customercode,
CAST(tbl1.transdate AS DATE) AS transdate,
tbl1.total AS total_tbl1,
tbl2.total AS total_tbl2
FROM
(
-- Query 1
SELECT customercode,convert(date,TransDate) transdate,SUM(TotalReceivable) as total
FROM SalesOrderHeader
where CustomerCode = 'K-MDMM4'
and TransDate between '2016-07-25' and '2016-07-30'
group by convert(date,TransDate),customercode
) AS tbl1
INNER JOIN (
-- Query 2
select b.OutletCode as outlet,tanggal, (cash + cc + dc + flash + piutang + reject + disc50 +
isnull(spesial_item,0)) total From transaksi a
left join Outlet b on a.Outlet = b.OutletCode
left join area c on b.areacode = c.areacode
where b.active = 1 and b.OutletCode like 'K-MDMM4' and flag = 1 and tanggal
between '2016-07-25' and '2016-07-30'
group by b.OutletCode,tanggal,cash,cc,dc,flash,piutang,reject,disc50,spesial_item,ba,mpm,tf,ul,remarks
) AS tbl2 ON tbl2.outlet = tbl1.customercode AND CAST(tbl2.trnggal AS DATE) = CAST(tbl1.transdate AS DATE)
order by CAST(tbl1.transdate AS DATE) DESC;
I don't have a database installed on this PC but what you're looking for is:
SELECT val1, val2 FROM
(SELECT1_of_your_code AS table1) INNER JOIN
(SELECT2_of_your_code AS table2) ON
table1.x == table2.y
I have a following query:
SELECT COUNT (*) AS Total, Program, Status
FROM APP_PGM_CHOICE
WHERE Program IN ( 'EX', 'IM')
AND APP_PGM_REQ_DT >= '20150101'
AND APP_PGM_REQ_DT <= '20150131'
AND Status IN ( 'PE','DN','AP')
GROUP BY Program, Status
ORDER BY Program, Status
And the output is:
Total Program Status
12246 "EX" "AP"
13963 "EX" "DN"
21317 "EX" "PE"
540 "IM" "AP"
2110 "IM" "DN"
7184 "IM" "PE"
And I want the output like:
Total1 Program1 Total2 Program2 Status
12246 EX 540 IM AP
13963 EX 2110 IM DN
21317 EX 7184 IM PE
Can I do ii? If yes whats the way?
Yes you could do it this way:
Select T1.Total Total1, T1.Program Program1, T2.Total Total2, T2.Program Program2, T1.Status
From
(SELECT COUNT (*) AS Total, Program, Status
FROM APP_PGM_CHOICE
WHERE Program = 'EX'
AND APP_PGM_REQ_DT >= '20150101'
AND APP_PGM_REQ_DT <= '20150131'
AND Status IN ( 'PE','DN','AP')
GROUP BY Program, Status
ORDER BY Program, Status) T1
INNER JOIN
(SELECT COUNT (*) AS Total, Program, Status
FROM APP_PGM_CHOICE
WHERE Program = 'IM'
AND APP_PGM_REQ_DT >= '20150101'
AND APP_PGM_REQ_DT <= '20150131'
AND Status IN ( 'PE','DN','AP')
GROUP BY Program, Status
ORDER BY Program, Status) T2 on T1.Status = T2.Status
You can do this with a UNION query and some simple selects
SELECT GROUP_CONCAT(total1) as total1, GROUP_CONCAT(proram1) as program1, GROUP_CONCAT(total2) as total2, GROUP_CONCAT(program2) as program2
FROM
(SELECT total AS total1, program AS program1, null AS total2, null AS program2
WHERE program = 'EX'
UNION
SELECT null AS total1, null AS program1, total AS total2, program AS program2
WHERE program = 'IM') t
This is the easy way to pivot rows into columns
In sql Temp Table is Very Usefull and Easy Funda..
Easy Way to Get Output.
select Total,Prog,Status
Into #TempAA
from CGT
Where Prog='EX'
Group by Status,Total,Prog
select Total,Prog,Status
Into #TempBB
from CGT
Where Prog='IM'
Group by Status,Total,Prog
select A.Total as [Total1],A.Prog as [Program1],B.Total as [Total2],B.Prog as [Program2],A.Status
from #TempAA A
Inner join #TempBB B On b.Status =A.status
I have this query which does some calculations based on some derived tables that are linked with an INNER JOIN.
At the moment I have a WHERE clause which pulls out one id at a time. But how can I make it list all the ids?
I have tried GROUP BY in various places but can't figure it out.
My query so far is as follows:
SELECT
equipment_id,
service_duration,
available_duration,
(available_duration / service_duration)*100 AS availability
FROM (
SELECT
SUM(service_end_time - service_start_time) AS service_duration
FROM(
SELECT equipment_id,
(CASE
END) AS service_start_time,
(CASE
END) AS service_end_time
FROM t1
WHERE equipment_id = 'EX123'
)AS A
) AS B
JOIN(
SELECT equipment_id,
SUM(available_end_time - available_start_time) AS available_duration
FROM (
SELECT equipment_id,
(CASE
END) AS available_start_time,
(CASE
END) AS available_end_time
FROM t2
WHERE equipment_id = 'EX123'
) AS C
) AS D
ON equipment_id=D.equipment_id
What I want to do is replace the WHERE clause with a GROUP BY to list all the ids, or similar, but getting that to work is beyond my skill level... Any help greatly appreciated :)
Try below:
SELECT
equipment_id, service_duration, available_duration,
(available_duration / service_duration)*100 AS availability
FROM
(
SELECT equipment_id,
SUM(service_end_time - service_start_time) AS service_duration
FROM
(
SELECT equipment_id,
(CASE ... END) AS service_start_time,
(CASE ... END) AS service_end_time
FROM t1
) AS A
GROUP BY equipment_id
) AS B
JOIN
(
SELECT equipment_id,
SUM(available_end_time - available_start_time) AS available_duration
FROM
(
SELECT equipment_id,
(CASE ... END) AS available_start_time,
(CASE ... END) AS available_end_time
FROM t2
) AS C
GROUP BY equipment_id
) AS D
ON equipment_id=D.equipment_id
Try this (replace my field names with your field names):
SELECT
a.emp_id,
service_duration,
available_duration
FROM
(
SELECT
emp_id,
SUM(service_end_time - service_start_time) AS service_duration
FROM
data
GROUP BY
emp_id
) a
JOIN
(
SELECT
emp_id,
SUM(available_end_time - available_start_time) AS available_duration
FROM
data
GROUP BY
emp_id
) b
ON a.emp_id = b.emp_id
GROUP BY
a.emp_id
My question is to retrieve a shop who sell each item at more than the average market price;
SELECT SHOP_NAME, Trade_Name
FROM SELL_ITEM f
WHERE PRICE >
(SELECT
AVG(PRICE)
FROM SELL_ITEM s
WHERE f.Trade_Name = s.Trade_Name
GROUP BY TRADE_NAME);
This query return me all the shop selling item > than average market price, but how can I filter out those not for each item?
Edited ... (missed a nuance the first time)
SELECT shop_name FROM
(
SELECT shop_name, MIN(case when f.price > a.avg_price then 1 else 0 end) AS is_always_higher_than_avg
FROM SELL_ITEM f
INNER JOIN
(
SELECT avg(price) AS avg_price, trade_name
FROM sell_item
GROUP BY trade_name
) a
ON f.trade_name = a.trade_name
WHERE f.price > a.avg_price
GROUP BY shop_name
) b
WHERE is_always_higher_than_avg = 1