SQL: Add dash Line (-) at the end of query result - mysql

Here Im trying to add extra row by using union query between data and result data.
select color, item, sum(qua) from inventory
Group by color, item
UNION
Select '----','----','----'
Union
select Count(color), 'total', sum(qua)
from inventory
Result would be:
ITME COLOR QUA
---- ----- ----
chair Black 520
chair pink 1028
chair Red 1050
chair Yellow 524
table Black 1048
table Blue 124
table pink 624
table Red 524
table Yellow 548
----- ----- ----- <----This extra row.
13 total 5990
I used above query but It show me Data type mismatch error.
Conversion failed when converting the varchar value '----' to data type int.
I'm using Microsoft SQL server management studio r2 2008

No, you shouldn't do this. To do it you'd have to cast the value of sum(qua) to a varchar, but this is a bad idea.
Drawing a line should be handled in your presentation layer.

Use UNION ALL instead of UNION
UNION The UNION command is used to select related information from two tables, much like the JOIN command. However, when using the
UNION command all selected columns need to be of the same data
type. With UNION, only distinct values are selected.
UNION ALL The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values.
Reference
You should always be using UNION ALL unless you are removing duplicates.

There are two things here. First is that types of sum(qua) and '----' are different (number vs. text).
select color, item, convert(nvarchar(30),sum(qua)) from inventory
Group by color, item
UNION
Select '----','----','----'
Union
select Count(color), 'total', sum(qua)
from inventory
Second is that you expect SQL to return data in order of SELECTs and that is not the case. Rows from unions can come in any order. To achieve the result you want an ordering trick is neccessary:
select color, item, summary from
(
select 0 as ToOrder, color, item, convert(nvarchar(30),sum(qua)) as summary from inventory
Group by color, item
UNION
Select 1, '----','----','----'
Union
select 2, Count(color), 'total', sum(qua)
from inventory
) as x
order by ToOrder asc

This should be used for testing in the query window only otherwise, follow Mark Byers advice
select color, item, CAST(sum(qua) as VARCHAR) As QUA from inventory
Group by color, item
UNION ALL
Select '----','----','----'
UNION ALL
select CAST(Count(color) AS VARCHAR), 'total', CAST(sum(qua) AS VARCHAR)
from inventory

Related

SQL Query to Find Duplicate form two different tables

I have two tables ganadordia and ganadornoche with numbers in column winNumber for both tables. I would like to create a new table based on the duplicated numbers found in each table
However, I'm encountering an issue: winNumber is creating an additional duplicate. For example, if I have two numbers 101 instead of getting only 2 I get 3
SELECT Distinct ganadoresdia.winNumber as 'Winner Day',ganadoresdia.Month as 'Month Day', ganadoresnoche.winNumber as ' Winner Night',
ganadoresnoche.month as 'Month Night'
FROM ganadoresdia, ganadoresnoche
where ganadoresnoche.winNumber = ganadoresdia.winNumber
ORDER BY ganadoresdia.winNumber asc
Does anyone know how to fix this? it show 13 instead of 8
Table Example
This is the result for ganadornocheit should only display 5
exemple
This is the result for ganadordia it should only display 3
example
From your sample I assume what you actually want to do is union the data rather than join (5+3=8). To do a join you need a primary:foreign key relationship, which you don't seem have.
SELECT 'day' as day_night_indicator,
winNumber,
month
FROM ganadoresdia
UNION
SELECT 'night' as day_night_indicator,
winNumber,
month
FROM ganadoresnoche

Showing a single column to multiple (n times)

I have a normal report like the below one,
Animal
Dog
Cat
Elephant
Tiger
Lion
Man
Panda
Which i want to do a break after n number of columns.
Animal Animal
Dog Cat
Elephant Tiger
Lion Man
Panda
I tried the page break which breaks into a new page. Did some grouping etc.. still didn't work out. Is there a way in SSRS to work this out. I know how to do it via SQL, iam asking about doing it via SSRS.
Edit:
I have used the below query in SSRS.
SELECT 610 AS [Period],1 AS RowNumber
UNION
select 611, 2
UNION
select 612, 3
UNION
select 613, 4
UNION
select 614, 5
UNION
select 615, 6
UNION
select 616, 7
UNION
select 617, 8
I added a new Matrix table, then i added 'Period' field to the matrix table (Screenshot Num 1). In the column grouping expression. i have used the below expressions. One at a time
=Floor(Fields!RowNumber.Value / 3) and =Fields!RowNumber.Value Mod 3
but now it shows only 3 columns now.
What i have expected to see is (Screenshot Num 3) and what iam getting is (Screenshot Num 2).
Screenshot:
You can't use SSRS aggregate functions like RowNumber in grouping expressions or dataset calculated fields. So you would need to add a number to your rows in the SQL. I would recommend using ROW_NUMBER for this. You could also use a RANK function. You don't have to partition by anything, just give it something to sort by and you're good to go.
Then in the report, you can use a column grouping expression like this to get 5 rows per column:
=Floor(Fields!RowNumber.Value / 5)
To use a specific number of columns you could use an expression like this:
=Fields!RowNumber.Value Mod 5

MySQL Sum and Case Query

I create a ReportViewer with VB.NET connecting to a MySQL database. The data appears like below.
IdProduct Quantity TotalPrice OrderDate
0001 1 10 29/09/2014
0002 2 40 29/09/2014
0001 4 40 29/09/2014
0001 2 20 29/09/2014
0001 2 20 29/09/2014
Based on the records above, I'd like the result to appear like below
0001 0002
9 2
90 40
What is Query Sum Case the best use here? Thanks in advance.
NOTE: It's not possible for a query to "dynamically" alter the number or datatype of the columns returned, those must be specified at the time the SQL text is parsed.
To return the specified resultset with a query, you could do something like this:
SELECT SUM(IF(t.IdProduct='0001',t.Quantity,NULL)) AS `0001`
, SUM(IF(t.IdProduct='0002',t.Quantity,NULL)) AS `0002`
FROM mytable t
UNION ALL
SELECT SUM(IF(t.IdProduct='0001',t.TotalPrice,NULL)) AS `0001`
, SUM(IF(t.IdProduct='0002',t.TotalPrice,NULL)) AS `0002`
FROM mytable t
Note that the datatypes returned by the two queries will need to be compatible. This won't be a problem if Quantity and TotalPrice are both defined as integer.
Also, there's no specific guarantee that the "Quantity" row will be before the "TotalPrice" row; we observe that behavior, and it's unlikely that it will ever be different. But, to have a guarantee, we'd need an ORDER BY clause. So, including an additional discriminator column (a literal in the SELECT list of each query), that would give us something we could ORDER BY.
Note that it's not possible to have this single query dynamically create another column for IdProduct '0003'. We'd need to add that to the SELECT list of each query.
We could do this in two steps, using a query to get the list of distinct IdProduct, and then use that to dynamically create the query we need.
BUT... with all that said... we don't want to do that.
The normative pattern would be to return Quantity and TotalPrice as two separate columns, along with the IdProduct as another column. For example, the result returned by this statement:
SELECT t.IdProduct
, SUM(t.Quantity) AS `Quantity`
, SUM(t.TotalPrice) AS `TotalPrice`
FROM mytable t
GROUP BY t.IdProduct
And then the client application would be responsible for transforming that resultset into the desired display representation.
We don't want to push that job (of transforming the result into a display representation) into the SQL.
select idproduct, sum(quantity), sum(totalprice)
from your_table
group by idproduct

Union Query Across Databases

I want to run a union query on qry_1 in C:\DB\DB1.mdb and qry_2 in C:\DB\DB2.mdb
All examples I have seen only show how to run a Union Query on query's in the same database, how can you do this when the query's are in different databases?
EDIT ---
Grr, not quite displaying results like I was after. That shows each entry twice. Is there a way to only show the entry one time? For example if qry_1 returns Joe 14, Jack 16, Jimmy 12 and qry_2 returns Joe 22, Jack 48, Jimmy 66 is there a way to SUM those results in the UNION Query?
I tried changing the syntax to a group by like so, but didn't work:
Select Name, Count FROM [C:\DB\DB1.mdb].qry_1
UNION ALL
SELECT Name, Count FROM [C:\DB\DB2.mdb].qry_2
GROUP BY Name, Count
For your case nothing changed, use:
SELECT * FROM [C:\DB\DB1.mdb].qry_1
UNION ALL
SELECT * FROM [C:\DB\DB2.mdb].qry_2
UPD: To SUM them i would recommend GROUP BY. Let's say we saved upper query as qry_U then try smth like this:
SELECT [Name], SUM([Numbers]) FROM [qry_U] GROUP BY [Name]

SQL GROUP BY - Multiple results in one column?

I am trying to perform a SELECT query using a GROUP BY clause, however I also need to access data from multiple rows and somehow concatenate it into a single column.
Here's what I have so far:
SELECT
COUNT(v.id) AS quantity,
vt.name AS name,
vt.cost AS cost,
vt.postage_cost AS postage_cost
FROM vouchers v
INNER JOIN voucher_types vt
ON v.type_id = vt.id
WHERE
v.order_id = 1 AND
v.sold = 1
GROUP BY vt.id
Which gives me the first four columns I need in the following format.
quantity | name | cost | postage_cost
2 X 5 1
2 Y 6 1
However, I would also like a fifth column to be displayed, showing all of the codes associated with each line of the order like this:
code
ABCD, EFGH
IJKL, MNOP
Where the comma separated values are pulled from the voucher table.
Is this possible?
Any advice would be appreciated.
Thanks
This is what GROUP_CONCAT does.
Assuming the column is called code you would just add ,GROUP_CONCAT(v.code) As Codes to your select list.