SUM string with comma (SQL Server Reporting Services) - sql-server-2008

I would like to get string sum check "column 1".
Do you have idea how to do it?
jack, john, bill, joe, sindy
Something similar to
=JOIN(Fields!Column1.Value, ",")

SAMPLE TABLE
CREATE TABLE #TEMP(COLUMN1 VARCHAR(100),COLUMN2 VARCHAR(100))
INSERT INTO #TEMP
SELECT 'jack',1
UNION ALL
SELECT 'john',1
UNION ALL
SELECT 'bill',1
UNION ALL
SELECT 'joe',1
UNION ALL
SELECT 'sindy',1
QUERY
If you need the value with comma separate values and count, you can avoid query before UNION ALL and execute the query only after UNION ALL.
SELECT NULL,COLUMN1,COLUMN2
FROM #TEMP
UNION ALL
SELECT DISTINCT 'TOTAL',
-- Here we convert to comma separated values
SUBSTRING(
(SELECT ', ' + COLUMN1
FROM #TEMP T2
--WHERE C2.Id=Id AND C2.COLUM=COLUM
FOR XML PATH('')),2,200000) COLUMN1,
COUNT(COLUMN1) COLUMN2
FROM #TEMP T1

Related

Mysql support this kind of "with" sql?

In postgres, I use this kind of sql often.
with list (id, name) as (
values
(1004007, 'aaa'),
(1002147, 'bbb'),
(1004493, 'ccc'),
(1007978, 'ddd'),
(1005218, 'eee'),
(1005507, 'fff')
)
select * from list;
Dose Mysql support that kind of sql ?
You can use select:
with list (id, name) as (
select 1004007, 'aaa' union all
select 1002147, 'bbb' union all
select 1004493, 'ccc' union all
select 1007978, 'ddd' union all
select 1005218, 'eee' union all
select 1005507, 'fff'
)
select l.*
from list l;
Here is a db<>fiddle. Note: This also works in Postgres.
Pre 8.0 versions of MySQL do not support with. You can create a derived table:
select list.*
from (select 1004007, 'aaa' union all
select 1002147, 'bbb' union all
select 1004493, 'ccc' union all
select 1007978, 'ddd' union all
select 1005218, 'eee' union all
select 1005507, 'fff'
) list;
This can then be used just a like a table reference.
Later 8.0 in mysql , we can use it like bellow
with
list (a, b, c) as (VALUES
ROW('1',-2,3),
ROW('5',7,9),
ROW('4',6,8)
)
select * from list;

Can I concatenate multiple MySQL Column into One Column?

Good day i would like to ask if this is possible in MySQL
SELECT id,label,name,age,sex FROM table LIMIT 3
Output
[row1] id,label,name,age,sex
[row2] id,label,name,age,sex
[row3] id,label,name,age,sex
My Output Needed
[row1] id
[row2] label
[row3] name
[row4] age
[row5] sex
[row6] id
[row7] label
[row8] name
[row9] age
[row10] sex
[row11] id
[row12] label
[row13] name
[row14] age
[row15] sex
You can do something like this:
SELECT * FROM
((SELECT id AS id1, 1 AS rownum, 'id' AS colname, id AS Data_value FROM mytable LIMIT 3)
UNION ALL
(SELECT id, 2, 'label', label FROM mytable LIMIT 3)
UNION ALL
(SELECT id, 3, 'name', name FROM mytable LIMIT 3)
UNION ALL
(SELECT id, 4, 'age', age FROM mytable LIMIT 3)
UNION ALL
(SELECT id, 5, 'sex', sex FROM mytable LIMIT 3)) A
ORDER BY id1, rownum
Here's a fiddle: https://www.db-fiddle.com/f/dvg6x1vBg6H5bDNp9VZxQa/4
I've added 3 additional column id AS id1, rownum and colname. The first two additional column is used for ORDER BY at the outer query. If you don't want to see the additional column, you can just type SELECT Data_value FROM ... at the outer query.
You can use group_concat() to aggregate rows by string concatenation. For the LIMIT to work you then need to use a derived table. But you should be careful with a LIMIT without an ORDER BY. As the order of a query result can be random unless an explicit ORDER BY is issued, you may get different results each time you run the query.
SELECT group_concat(id,
'\n',
label,
'\n',
name,
'\n',
age,
'\n',
sex
SEPARATOR '\n')
FROM (SELECT id,
label,
name,
age,
sex
FROM elbat
LIMIT 3) x;
If you just want to concatenate the columns but keep the rows just use concat().
SELECT concat(id,
'\n',
label,
'\n',
name,
'\n',
age,
'\n',
sex)
FROM elbat
LIMIT 3;
yes,you can use union all like below :
SELECT id FROM table LIMIT 3
union all
SELECT label FROM table LIMIT 3
union all
SELECT name FROM table LIMIT 3
union all
SELECT age FROM table LIMIT 3
union all
SELECT sex FROM table LIMIT 3
That what you looking is to Unpivot data. For more info about pivot and unpivot you can check here.
http://archive.oreilly.com/oreillyschool/courses/dba1/dba110.html
Unfortunately there is no easy way to unpivot in mysql.
The below script will work for MySQL 8.0
set #rowNum :=0;
set #string :=(
select group_concat(id,',',label,',',name,',',age,',',sex separator ',')
from (
select id, label, name, age, sex from mytable limit 3
) x
);
with recursive
R1 as ( select #string as items),
R2 as ( select 1 as n
union
select n + 1 from R2, R1
where n <= length(items) - length(replace(items, ',', '')))
select distinct #rowNum := #rowNum+1 as rowNum, substring_index(substring_index(items, ',', n), ',', -1) output from R2, R1;

sql server 2008 - Add a line that counts different values

Using SQL Server 2008 I get the following output:
I would like to know if there is a way to sum up, in new lines, all the different texts of Col3 to show something like this:
The following is a quick and dirty method, like jayvee suggested. It will only work if the first column of your query is a type you can put a string into. If it not, you may be able to move the summary output to another column.
DECLARE #TestData as TABLE (Col1 nvarchar(50), Col2 nvarchar(50), Col3 nvarchar(50));
Insert Into #TestData
Select 'Whatever1', 'Day1', 'Text1'
UNION ALL Select 'Whatever1', 'Day2', 'Text1'
UNION ALL Select 'Whatever1', 'Day3', 'Text2'
UNION ALL Select 'Whatever2', 'Day1', 'Text1'
UNION ALL Select 'Whatever2', 'Day2', 'Text3'
UNION ALL Select 'Whatever3', 'Day1', 'Text3'
UNION ALL Select 'Whatever3', 'Day2', 'Text2'
UNION ALL Select 'Whatever3', 'Day3', 'Text3';
With OriginalQuery as (
--This should be your original query
Select Col1, Col2, Col3 from #TestData
), GroupData as(
-- This is the summary data you want
Select Col3, COUNT(Col3) as ColCount from OriginalQuery Group By Col3
)
Select * from OriginalQuery --Output the original data
UNION ALL
--This is formatting the group data,
--and adding null columns so it matches the original query length, or the union will fail
Select 'Number of ' + Col3 + ' = ' + Cast(ColCount as nvarchar(10)),
NULL, NULL --One for each column in original query except the first one
From GroupData;

Selecting distinct values from multiple column of a table with their count

i want to to select distinct values from multiple columns of same table with their count
example
and the output should be like this
Since the OP says in his comment that he uses MYSQL this should work
SELECT data,COUNT(data)
FROM
(
SELECT COL1 data
FROM tableso
UNION ALL
SELECT COL2
FROM tableso
UNION ALL
SELECT COL3
FROM tableso
UNION ALL
SELECT COL4
FROM tableso
UNION ALL
SELECT COL6
FROM tableso
UNION ALL
SELECT COL7
FROM tableso
) finaltable group by data;
SQL FIDDLE: http://sqlfiddle.com/#!2/1f8cf/10
I dont think the accepted answer works on MYSQL
UPDATE:
The op has changed his mind about the database(to MSSQL) and the accepted answer has both db versions
Since you are using Sql Server too, will suggest you this way
Use Table valued constructor to convert the columns to rows then count the name
This will avoid multiple table scan's.
SELECT name,
[count]=Count(1)
FROM yourtable
CROSS apply (VALUES (col1),(COL2),(col3),(col4),(COL6),(col7)) cs (name)
GROUP BY name
Note : This will not work in Mysql
SQLFIDDLE DEMO
Get all column values to one row and find the count
SQL SERVER
;WITH CTE AS
(
SELECT COL1 Name
FROM YOURTABLE
UNION ALL
SELECT COL2
FROM YOURTABLE
UNION ALL
SELECT COL3
FROM YOURTABLE
UNION ALL
SELECT COL4
FROM YOURTABLE
UNION ALL
SELECT COL6
FROM YOURTABLE
UNION ALL
SELECT COL7
FROM YOURTABLE
)
SELECT DISTINCT Name,COUNT(Name) OVER(PARTITION BY Name) [COUNT]
FROM CTE
MYSQL
SELECT Name,COUNT(*) [COUNT]
(
SELECT COL1 Name
FROM #TEMP
UNION ALL
SELECT COL2
FROM #TEMP
UNION ALL
SELECT COL3
FROM #TEMP
UNION ALL
SELECT COL4
FROM #TEMP
UNION ALL
SELECT COL6
FROM #TEMP
UNION ALL
SELECT COL7
FROM #TEMP
)TAB
GROUP BY Name

MySQL; Get information from 3 table same time

People
here is my little problem.
I have three table:
a_names_1
b_names_2
c_names_3
they are same by structure. all of them has two item: name and used
Is there any QUERY to run to get and count all the 'name' that has 'used'=1 from all those three tables together.
I've tried this one, but didn't work:
(SELECT COUNT(*) 'name' from a_names_1) UNION
(SELECT COUNT(*) 'name' from a_names_2) UNION
(SELECT COUNT(*) 'name' from a_names_3) WHERE `used`=1
I'm using PHPMyAdmin for MySQL.
Any Help would be appreciated.. thanks in advance
This query outputs count of distinct names from all tables with used=1
select count(distinct name)
from
(
select name,used from a_names_1 where used=1
union all
select name,used from a_names_2 where used=1
union all
select name,used from a_names_3 where used=1
) t
If you need to SUM all USED for each NAME from all tables and output only with SUM of used=1 then:
select count(*) from
(
select name, SUM(used)
from
(
select name,used from a_names_1
union all
select name,used from a_names_2
union all
select name,used from a_names_3
) t
GROUP BY name
HAVING SUM(used)=1
) t1
select count(*) as name
from
(
select name, used from a_names_1
union
select name, used from a_names_2
union
select name, used from a_names_3) t
where t.used = 1
Probably this is slow, because you lose the index optimizations. What I would do is do the three queries, something like
SELECT SUM('name') AS name_sum
FROM ((SELECT COUNT(*) 'name' from a_names_1 WHERE `used`=1)
UNION (SELECT COUNT(*) 'name' from a_names_2 WHERE `used`=1));
If this doesn't work, it is probably a problem with the usage of name
Maybe you wanted this way:
select count(*) as cnt
from
(
select name from a_names_1 t1 where t1.used = 1
union
select name from a_names_2 t2 where t2.used = 1
union
select name from a_names_3 t3 where t3.used = 1
) t
The straight forward solution;
SELECT SUM(used) FROM (
SELECT used FROM a_names_1 WHERE used=1
UNION ALL
SELECT used FROM a_names_2 WHERE used=1
UNION ALL
SELECT used FROM a_names_3 WHERE used=1
) a
SQLfiddle for testing
An alternative if you have an index on used (and the only values of used are 0 or 1) is to just do the counting using the index;
SELECT SUM(used) total FROM (
SELECT SUM(used) used FROM a_names_1
UNION ALL
SELECT SUM(used) FROM a_names_2
UNION ALL
SELECT SUM(used) FROM a_names_3
) a
SQLfiddle for this example.
If you look at the query plan of the latter query, you can see it uses the indexes effectively.