how to doing group with two cels in a single sql - mysql

Here is my brief snipt:
select * from tbl_test
What I want to do is:
1.
select g_size, sum(g_num)
from tbl_test
group by g_size
2.
select m_size, sum(m_num)
from tbl_test
group by m_size
Here is my question, how can I push all these stuffs into one single sql string?
Is it possible to do this?

If you want single row for eigher single g_size or m_size then you can't do in single sql, but if it is okay for you that there can be multiple row for 1st group by field then you can do-
For example if you have one more row g_size=5, g_num=1, m_size=2, m_num=1 and you use below query-
select g_size, sum(g_num),m_size, sum(m_num)
from mytable
group by g_size,by m_size;
then you will get 2 rows for g_size=5 as per below-
g_size= 5, sum(g_num)=5, m_size=1, sum(m_num)=30
and
g_size= 5, sum(g_num)=1, m_size=2, sum(m_num)=1
Further you can use with rollup function which will first provide sub-total (m_size wise) and then total (g_size wise) and at the end grand total (all m_size grand total).

Simply add the columns and the group by:
select g_size, sum(g_num),m_size, sum(m_num) from tbl_test group by g_size, m_size;

Related

My objective is to merge the two queries. I'm having discrepancies in executing in separate manner

Image1ImageHelp me to merge these 2 given queries
SELECT DISTINCT vehicle_no FROM petty_cash;
SELECT COUNT(variation) as bal FROM petty_cash WHERE variation<0;
Thanks in advance.
I'm not sure about your output but i can combine those 2 queries as below.
SELECT DISTINCT vehicle_no, COUNT(variation) as bal FROM petty_cash WHERE variation < 0;
Do you want to execute two statment in one query? Is answer is yes then it
SELECT (
SELECT DISTINCT vehicle_no FROM petty_cash
) AS firstStatment,
(
SELECT COUNT(variation) as bal FROM petty_cash WHERE variation<0
) AS secondStatment
You can use case to get your desired result,
like,
SELECT DISTINCT vehicle_no,CASE WHEN variation==0 THEN 'HIDE ROW' END FROM petty_cash;
After executing this query you can get the number of vehicle with HIDE ROW value where variation is zero.
In the place of 'HIDE ROW' you can set 0 or 1 for your understanding.
I Hope this will help you.

Compare field in single row to entire column

I have a column of dates, and I would like to have a second column that tallies the total amount of times a date in the first column shows up over the whole column. I am trying to use Count(IIf(...)), but I don't know how to specify that SSRS use the current row as the standard, and then check the whole column for the count.
Dates Records
9/14/18 2
9/14/18 2
9/15/18 1
9/16/18 3
9/16/18 3
9/16/18 3
Assuming you are not using a very old version of SQL Server then you could just do something simple like
SELECT
*
, COUNT(*) OVER(PARTITION BY PartTran_TranDate) AS DateCount
FROM myTable
Perhaps this will help. I changed it up a little bit for simplicity:
DECLARE #Table TABLE (DateCol DATE)
INSERT #Table
VALUES ('9/14/18'),
('9/14/18'),
('9/15/18'),
('9/16/18'),
('9/16/18'),
('9/16/18')
SELECT t.DateCol, r.rn
FROM #Table t
OUTER APPLY (SELECT COUNT(DateCol) rn
FROM #Table t2
WHERE t2.DateCol = t.DateCol
GROUP BY t2.DateCol) r

group by id in descending order in select statement

I have MySQL statement where I'm trying to select distinct rows with the latest date.
This is the SQL statement:
SELECT st.seno, tc.pl, tc.sno, st.val1, st.val2, st.date
FROM tc
LEFT JOIN st ON tc.seno = st.seno AND tc.pl = st.pl AND st.seno = 1304239136
WHERE tc.pl = 1
ORDER BY st.date DESC
This is the data returned:
I want to distinct by unique 'tc.sno', therefore I only want the first and third row returned as the middle date is earlier then the top one for the 'sno' 3. The 'sno' could always be different so I do not want to hardcode those numbers. I was trying to use 'GROUP BY' but it just picks the first value of the date, tried using 'HAVING' and combining select statements together but can't seem to get it working. (the val1 and val2 in row 2 could be random it is just a coincidence that they exact in this example)

The query gives single row query returns more than one row

I'm trying to show staff_code, staff_name and dept_name for those who have taken one book.
Here's my query:
SELECT SM.STAFF_CODE,SM.STAFF_NAME,DM.DEPT_NAME,BT.BOOK_CODE
FROM STAFF_MASTER SM,DEPARTMENT_MASTER DM,BOOK_TRANSACTIONS BT
WHERE SM.DEPT_CODE =DM.DEPT_CODE
AND SM.STAFF_CODE = (
SELECT STAFF_CODE
FROM BOOK_TRANSACTIONS
HAVING COUNT(*) > 1
GROUP BY STAFF_CODE)
It gives the error:
single-row subquery returns more than one row.
How to solve this?
Change = to IN:
WHERE SM.STAFF_CODE IN (SELECT ...)
Because the select returns multiple values, using equals won't work, but IN returns true if any of the values in a list match. The list can be a hard-coded CSV list, or a select with one column like your query is.
That will fix the error, but you also need to remove BOOK_TRANSACTIONS from the table list and remove BOOK_CODE from the select list.
After making these changes, your query would look like this:
SELECT SM.STAFF_CODE,SM.STAFF_NAME,DM.DEPT_NAME
FROM STAFF_MASTER SM,DEPARTMENT_MASTER DM
WHERE SM.DEPT_CODE =DM.DEPT_CODE
AND SM.STAFF_CODE IN (
SELECT STAFF_CODE
FROM BOOK_TRANSACTIONS
HAVING COUNT(*) > 1
GROUP BY STAFF_CODE)
I recommend learning the modern (now over 25 year old) JOIN syntax.

How to Display 1 Column sum in other Column

I have a table that has different columns display different values.
I need to add a new column that displays sum of 1 column in each row of other column.
This is what i need to display.
I have written following query but its only displaying 1 in each row of last column.
select inStation.name TapInStation , outStation.name TapOutStation,
count(trx.passengerCount) PassengerCount, sum(trx.amount) Fare,
(select sum(passengerCount) from transactions iTrx
where iTrx.ID = trx.ID) PassengerPercent
from transactions trx
inner join
station inStation on inStation.ID = trx.fromStation
inner join
station outStation on outStation.ID = trx.toStation
GROUP BY
TapInStation, TapOutStation
If you want the total, then remove the correlation clause. This may do what you want:
select inStation.name as TapInStation , outStation.name as TapOutStation,
count(trx.passengerCount) as PassengerCount,
sum(trx.amount) as Fare,
(select sum(passengerCount) from transactions iTrx) as PassengerPercent
I'm not sure why you would called the "total" something like PassengerPercent, but this should return the overall total.
I also suspect that you might want a sum() for the previous expression.