Return null row on select sql - sql

I know this might seem a bit strange but I want to build a stored procedure that returns some values but I want the first row to be a null value. What I have for the query and returns are
SELECT CONCAT(id, ' ', item)As DisplayBox, id, item FROM table
DisplayBox id expense
1 Value2 1 Value2
2 Value3 2 Value3
3 Value4 3 Value4
4 Value5 4 Value5
But I am interested in having a result like
DisplayBox id Item
<BlankText> Null <BlankText>
1 Value2 1 Value2
2 Value3 2 Value3
3 Value4 3 Value4
4 Value5 4 Value5

You can use UNION ALL:
SELECT NULL AS DisplayBox, NULL AS id, NULL AS item
UNION ALL
SELECT CONCAT(id, ' ', item)As DisplayBox, id, item FROM table

SELECT null as displaybox, null as id, null as item from dual
union all
SELECT CONCAT(id, ' ', item)As DisplayBox, id, item FROM table
order by (case when id is null then 0 else 1 end)

Related

Get count of common field among the same records, SQL Server?

Assume we have the following records:
Id
Column A
Column B
Column C
Common
1
Value1
Value2
Value3
12
2
Value1
Value2
Value3
13
3
Value1
Value2
Value3
08
4
Value1
Value2
Value3
10
5
Value4
Value5
Value6
18
6
Value4
Value5
Value6
22
7
Value4
Value5
Value6
37
How can we get the following result
Id
Column A
Column B
Column C
CommonCount
1
Value1
Value2
Value3
4
5
Value4
Value5
Value6
3
I wrote this query:
SELECT
jr.*,
CommonCount = (SELECT COUNT(1)
FROM la.JudicialReference AS jr1
WHERE ((jr.ColumnA = jr1.ColumnA)
OR ISNULL(jr.ColumnA, jr1.ColumnA) IS NULL)
AND ((jr.ColumnB = jr1.ColumnB)
OR ISNULL(jr.ColumnB, jr1.ColumnB) IS NULL)
AND ((jr.ColumnC = jr1.ColumnC)
OR ISNULL(jr.ColumnC, jr1.ColumnC) IS NULL))
FROM
la.JudicialReference AS jr
But it doesn't get me the first Id and I'm looking for a better query to save IO
Simply
SELECT MIN(ID), ColumnA, ColumnB, ColumnC, COUNT(*)
FROM la.JudicialReference
GROUP BY ColumnA, ColumnB, ColumnC
Optionally with HAVING COUNT(*) > 1

Combining data from 2 tables and pivoting data from 1 of them

I have the current situation:
TableA
----------
SN Date OpsID
1234 2018-02-12 1
1324 2018-02-12 2
2134 2018-02-13 3
TableB
----------
Name String Number OpsID
Value1 Hello NULL 1
Value2 NULL 1111 1
Value3 Apples NULL 1
Value1 There NULL 2
Value2 NULL 2222 2
Value3 Pears NULL 2
Value1 Baby NULL 3
Value2 NULL 3333 3
Value3 Bananas NULL 3
...and the result I'm looking for is:
SN Date Value1 Value2 Value3
1234 2018-02-12 Hello 1111 Apples
1324 2018-02-12 There 2222 Pears
2134 2018-02-13 Baby 3333 Bananas
The query I ran is obviously wrong because I'm getting mostly NULL and only Value data, but here it is anyway:
SELECT SN,Date,Value1,Value2,Value3
FROM(
SELECT TableA.SN,
TableA.Date,
TableB.Name,
TableB.String,
TableB.Number
FROM TableA,TableB
WHERE
TableA.OpsID = TableB.OpsID
ORDER BY Date ASC
)Temp
PIVOT(
MAX(Value)
FOR Name in(
Value1,Value2,Value3
)
)PIV
Note that Value1 and Value3 are always VARCHAR and Value2 is always an INT
You combine String and Number into a single column in a subquery, and then pivot the values back out into their own columns like this:
SELECT SN, Date, Value1, CAST(Value2 AS INT) Value2, Value3
FROM
(
SELECT a.SN, a.Date, B.Name, ISNULL(b.String, CAST(b.Number AS VARCHAR(10))) Value
FROM TableA A
INNER JOIN TableB B ON A.OpsID = B.OpsID
)sq1
PIVOT (MAX(Value) FOR Name IN ([Value1], [Value2], [Value3]))sq2
You can do conditional aggregation :
select a.SN, a.date,
max(case when b.Name = 'Value1' then coalesce(b.String, cast(b.Number as varchar(255))) end) as Value1,
max(case when b.Name = 'Value2' then coalesce(b.String, cast(b.Number as varchar(255))) end) as Value2,
max(case when b.Name = 'Value3' then coalesce(b.String, cast(b.Number as varchar(255))) end) as Value3
from tablea a inner join
tableb b
on b.OpsID = a.OpsID
group by a.SN, a.date;

how to get rows which have max value2 when value1 is equal using django queryset

for example
rowid value1 value2
1 1 2
2 1 3
3 2 4
then select (1,1,3) and (3,2,4) because row1 and row2 have the same value1, and value2 of row2 is bigger than value2 of row1
thank you!
In sql this is a simple aggregation query using group by and min()/max()
select
min(rowid) as rowid
, value1
, max(value2) as value2
from t
group by value1

Inserting multiple rows with column into single row

I am trying to write a query to copy set of rows from one table to another table in this format
Table 1
ColumnName ColumnValue RowId
Column1 Value1 1
Column2 Value2 1
Column3 Value3 1
Column1 Value4 2
Column2 Value5 2
Column3 Value6 2
Column1 Value7 3
Column2 Value8 3
Column3 Value9 3
Table2
Column1 Column2 Column3
Value1 Value2 Value3
Value4 Value5 Value6
Value7 Value8 Value9
Here basically table 1 is input and table 2 is the output which I am trying. I used the pivot and row number but none worked.
What will be the insert query to achieve this. Here all rows of rowId 1 from table 1 will form form one row of table2.
select rowID,
max(case when columnName = 'Column1' then value else null end) as column1,
max(case when columnName = 'Column2' then value else null end) as column2,
max(case when columnName = 'Column3' then value else null end) as column3
from table1
group by rowID

How to count the column

Table1' Sample data:
ID value1 Value2 value3
001 10 20 30
002 20 10 null
003 10 null null
004 10 null 30
....
From the table1, I want to make a coulmn count.
Count of row query
Select count(*) from table1 'It will give only row count.
But i need column count, which column value should not be null
Expected output
ID | totcolumn
-----------------
001 3
002 2
003 1
004 2
....
How to make a query, need query help
Use SUM as follows:
SELECT id,
SUM(CASE WHEN value1 IS NULL then 0 ELSE 1 END) +
SUM(CASE WHEN value2 IS NULL then 0 ELSE 1 END) +
SUM(CASE WHEN value3 IS NULL then 0 ELSE 1 END) AS 'COUNT'
FROM table1
group by id
DEMO
This is a nice opportunity to use UNPIVOT if you are on MS SQL Server 2005 or later. First set up some sample data:
create table ColumnCount (
ID char(3) not null,
Value1 int,
Value2 int,
Value3 int
)
insert into ColumnCount(ID,Value1,Value2,Value3)
select '001',10,20,30
union all select '002',20,10,null
union all select '003',10,null,null
union all select '004',10,null,30
union all select '005',null,null,null
I added 005 to the sample data you provided above, to show how to handle the case where all ValueX columns are null.
UNPIVOT "normalizes" the data for you so that you can use COUNT against the rows:
select *
from ColumnCount
unpivot (Value for ValueN in (Value1, Value2, Value3)) as upvt
ID Value ValueN
001 10 Value1
001 20 Value2
001 30 Value3
002 20 Value1
002 10 Value2
003 10 Value1
004 10 Value1
004 30 Value3
Notice that it eliminated the NULL values already, so you do not have to. Unfortunately this means that rows with NULL in all ValueX columns (e.g., ID='005') will not show up if you do a simple count. Instead, gather all ID's in a subquery or CTE like AllIds below:
with ColumnsOnRows as (
select *
from ColumnCount
unpivot (Value for ValueN in (Value1, Value2, Value3)) as upvt
),
AllIds as (
select distinct ID
from ColumnCount
)
select AllIds.ID, count(distinct Value) as totcolumn
from AllIds
left outer join ColumnsOnRows
on ColumnsOnRows.ID = AllIds.ID
group by AllIds.ID
Result:
ID totcolumn
001 3
002 2
003 1
004 2
005 0
If I understand you correctly what you need is something like this
SELECT ISNULL(Value1, 0) + ISNULL(Value2, 0) + ISNULL(Value3, 0) AS _Sum FROM table1
ISNULL will replace the value with 0 if it is NULL