SQL Server 2008.
declare #pardate table ( pardateid int, pardatewhen datetime2(3) )
insert into #pardate values ( 1 , '2011-09-17 12:43' )
insert into #pardate values ( 2 , '2011-09-17 12:44' )
insert into #pardate values ( 3 , '2011-10-11 12:45' )
insert into #pardate values ( 4 , '2011-10-12 12:46' )
insert into #pardate values ( 5 , '2011-10-13 12:47' )
insert into #pardate values ( 6 , '2011-11-20 12:48' )
insert into #pardate values ( 7 , '2011-11-21 12:49' )
insert into #pardate values ( 8 , '2011-11-22 12:50' )
declare #child table ( childid int , pardateid int , childvalue char(6) )
insert into #child values ( 1 , 1 , 'aaaaaa' )
insert into #child values ( 2 , 2 , 'bbbbbb' )
insert into #child values ( 3 , 3 , 'cccccc' )
insert into #child values ( 4 , 4 , 'dddddd' )
insert into #child values ( 5 , 5 , 'cccccc' )
insert into #child values ( 6 , 6 , 'cccccc' )
insert into #child values ( 7 , 7 , 'eeeeee' )
insert into #child values ( 8 , 8 , 'ffffff' )
select pardatewhen , childvalue , COUNT(childvalue)
from #child childtable join #pardate parenttable on childtable.pardateid=parenttable.pardateid
group by pardatewhen , childvalue
I am trying to get a count of #child.childvalue every day, every hour, so there would be 8760 rows in my result.
First pass had a loop and a CONVERT which takes ~5 minutes to run with the actual result set (this is just a sample for illustation). I did create a CTE to make a calendar temp table (using http://www.sqlpointers.com/2006/07/generating-temporary-calendar-tables.html), and thought it could be joined somehow to add "empty values" into the result set.
I need to get a result set that looks like this
date hour count
...
2011-09-17 0 0
....
2011-09-17 12 2
....
2011-10-11 12 1
How can that be done efficiently?
Thanks.
try this.
;WITH cal AS
(SELECT CAST('2011-01-01' AS DATETIME) AS cal_date
UNION ALL
SELECT DATEADD(hour,1,cal_date)
FROM cal
WHERE cal_date < '2011-12-31 23:00'
)
, par AS
(
select CAST(pardatewhen AS DATE) AS pardate, DATEPART(hh,pardatewhen) AS parhour , COUNT(childvalue) as num
from #child childtable
join #pardate parenttable on childtable.pardateid=parenttable.pardateid
group by CAST(pardatewhen AS DATE), DATEPART(hh,pardatewhen)
)
SELECT CAST(cal.cal_date AS DATE) AS [date],DATEPART(hh,cal.cal_date) AS [hour],ISNULL(par.num,0) AS [childvalue_count]
FROM cal
LEFT JOIN par
ON CAST(cal.cal_date AS DATE) = par.pardate
AND DATEPART(hh,cal.cal_date) = par.parhour
OPTION (MAXRECURSION 9999)
Something like (have childvalue in your query but not in your example result?)
select Cast(pardatewhen as Date) as [date], DatePart(hour,pardatewhen) as [hour] , childvalue , COUNT(childvalue)
from #child childtable
join #pardate parenttable on childtable.pardateid=parenttable.pardateid
group by Cast(pardatewhen as Date), DatePart(hour,pardatewhen), childvalue
Note Date type was introduced in SQL 2008
Related
I am new to this and i am wondering how to input data into SSRS table and auto generate for the subsequent months. This is the format of the table.
Appreciated for any help given.
You can generate a date range using the following SQL
DECLARE #date_start AS DATETIME
SET #date_start = '01-DEC-2017'
;WITH
finalvalues
AS
(
SELECT tbl.* FROM (VALUES
( '01-Dec-2017', 6414.6563, 429.6846, -1390.8474)
, ( '02-Dec-2017', 6476.6563, 432.751, -1312.4928)
, ( '03-Dec-2017', 6538.6563, 435.8174, -1234.1382)
, ( '04-Dec-2017', 6600.6563, 438.8838, -1155.7836)
, ( '05-Dec-2017', 6662.6563, 441.9502, -1077.429)
, ( '06-Dec-2017', 6724.6563, 445.0166, -999.074399999999)
, ( '07-Dec-2017', 6786.6563, 448.083, -920.719799999999)
, ( '08-Dec-2017', 6848.6563, 451.1494, -842.365199999999)
, ( '09-Dec-2017', 6910.6563, 454.2158, -764.010599999999)
, ( '10-Dec-2017', 6972.6563, 457.2822, -685.655999999999)
, ( '11-Dec-2017', 7034.6563, 460.3486, -607.301399999999)
, ( '12-Dec-2017', 7096.6563, 463.415, -528.946799999999)
, ( '13-Dec-2017', 7158.6563, 466.4814, -450.592199999999)
, ( '14-Dec-2017', 7220.6563, 469.5478, -372.2376)
, ( '15-Dec-2017', 7282.6563, 472.6142, -293.883)
, ( '16-Dec-2017', 7344.6563, 475.6806, -215.5284)
) tbl ([Date], [IncLoad], [ITLoad], [RH])
)
,
manufactured_dates
AS
(
SELECT
day_date = DATEADD(day, dte.[number], #date_start)
FROM
master.dbo.spt_values AS dte
WHERE
1=1 -- <-- used in testing to be able to comment out other clauses below
AND dte.[type] = 'P'
AND dte.[number] <= 365 -- <-- filter how many rows you want to see here
)
SELECT
'Date' = md.[day_date]
, 'IncLoad' = AVG(incload)
, 'ITLoad' = AVG(ITLoad)
FROM
finalvalues AS fv
FULL OUTER JOIN manufactured_dates AS md ON md.[day_date] = fv.[Date]
GROUP BY
md.[day_date]
I am using SQL server 2008. I want to insert data from a temp table to database. I am using While loop to insert data from temp table to database table.
Now I am facing an issue:
object already exist in the database.
declare #rev as int ,
#sQuotationNo NVARCHAR(15),
#sQRevNo int
set #rev=(select top 1 QRevNo from PDBCompr Where QuotationNo='JCS_G1415_008' and QRevNo<>'3' order by QRevNo desc)
;with cte as
(
SELECT
ROW_NUMBER() OVER(ORDER BY QuotationNo) AS sSLNO,
[CompanyCode] ,
[ProjectCode] ,
[PRevNo],
[CSlNo],
[ComprDescription] ,
[PID] ,
[RatingCode] ,
[Rating] ,
[StdSystems] ,
[BoosterSystems] ,
[GCUSystems] ,
[KOFSystems] ,
[HeaterSystems] ,
[OtherSystems] ,
[Comments] ,
[Currency1] ,
[UnitPrice1] ,
[Currency2] ,
[ExchRate2] ,
[UnitPrice2] ,
[Currency3],
[ExchRate3] ,
[UnitPrice3] ,
[CreateId] ,
[CreateDate] ,
[UpdateId] ,
[UpdateDate]
from PDBCompr
where QuotationNo='JCS_G1415_008' and CompanyCode ='001' and QRevNo ='2'
and AddCmprId not in (select distinct AddCmprId from PDBCompr where QuotationNo='JCS_G1415_008' and CompanyCode ='001' and QRevNo ='3' )
)
select * into #temp from cte
declare #cnt int , #loopCnt int=1
select #cnt =( select COUNT(*) from #temp)
while (#loopCnt<=#cnt)
begin
;with cte2 as
(
SELECT
sSLNO,
[CompanyCode] ,
[ProjectCode] ,
(select Max(PRevNo)+1 from PDBCompr) [PRevNo],
(select Max(CSlNo) +1 from PDBCompr)[CSlNo],
[ComprDescription] ,
[PID] ,
[RatingCode] ,
[Rating] ,
[StdSystems] ,
[BoosterSystems] ,
[GCUSystems] ,
[KOFSystems] ,
[HeaterSystems] ,
[OtherSystems] ,
[Comments] ,
[Currency1] ,
[UnitPrice1] ,
[Currency2] ,
[ExchRate2] ,
[UnitPrice2] ,
[Currency3],
[ExchRate3] ,
[UnitPrice3] ,
[CreateId] ,
[CreateDate] ,
[UpdateId] ,
[UpdateDate]
from #temp where sSLNO=#loopCnt
)
select * into PDBCompr from cte2
set #loopCnt= #loopCnt+1
drop table #temp
end
Please help me to find a proper solution. Thanks in advance
select * into PDBCompr from cte2
in this line you are again making a table "PDBCompr", but in above code you are selecting records from this table it means its already exist.
so if you want to insert record in this table then you need to change your query like below:
insert into PDBCompr select * from cte2
I have the following SQL query which is somehow broken:
SELECT *
FROM (
SELECT ID, TEST, CHR, NUMBER
FROM Test_Table
JOIN ...
WHERE ...
) TEMP_TABLE
FROM TEMP_TABLE a
LEFT
JOIN TEMP_TABLE b
ON b.test = a.test
AND b.chr = 'x'
WHERE a.number IN (5,6)
AND b.id IS NULL
GROUP
BY a.test
HAVING COUNT(*) = 2;
From the first FROM statement I get the following temporary table:
ID , TEST, CHR , NUMBER
------------------------------
( 1 , 7 , 'C' , 5),
( 2 , 7 , 'T' , 6),
( 3 , 8 , 'C' , 4),
( 4 , 8 , 'T' , 5),
( 5 , 9 , 'A' , 4),
( 6 , 9 , 'G' , 5),
( 7 , 10 , 'T' , 4),
( 8 , 10 , 'A' , 5),
( 9 , 10 , 'X' , 6),
(10 , 14 , 'T' , 4),
(11 , 14 , 'G' , 5);
From FROM TEMP_TABLE ... I try to implement the following conditions:
For example test column 7 contains two rows, if the number column contains values 5 AND 6, AND the value is NOT X in the chr column, I would like to select select the rows with 7 in the test column.
For example test column 10 contains three rows, if the number column contains values 5 AND 6, AND the value X exists in the chr column, I would like to exclude rows with 10 in the test column.
As result it should only be test column with 7, because test column 7 have 5 and 6 in the number column and not X.
Result example:
ID | TEST | CHR | NUMBER
1 | 7 | C | 5
2 | 7 | T | 6
What did go wrong with the above SQL query?
Your query has two FROM clauses remove one and try like below
SELECT *
FROM (
SELECT ID, TEST, CHR, NUMBER
FROM Test_Table
JOIN ...
WHERE ...
) a
LEFT JOIN
(
SELECT ID, TEST, CHR, NUMBER
FROM Test_Table
JOIN ...
WHERE ...
) b
ON b.test = a.test
AND b.chr = 'x'
WHERE a.number IN (5,6)
AND b.id IS NULL
GROUP
BY a.test
HAVING COUNT(*) = 2;
A easier way to do this is to create a TEMPORARY table using TEMPORARY keyword like mentioned here
CREATE TEMPORARY TABLE TEMP_TABLE (ID int, TEST varchar(100), CHR char, NUMBER int );
INSERT INTO TEMP_TABLE
(SELECT ID, TEST, CHR, NUMBER
FROM Test_Table
JOIN ...
WHERE ...);
SELECT *
FROM TEMP_TABLE a
LEFT
JOIN TEMP_TABLE b
ON b.test = a.test
AND b.chr = 'x'
WHERE a.number IN (5,6)
AND b.id IS NULL
GROUP
BY a.test
HAVING COUNT(*) = 2;
Table looks like this:
create table #rankme (rankmeid int identity(1000, 1) primary key,
step int null, checkvalue int null)
insert into #rankme values ( 10 , 1 )
insert into #rankme values ( 15 , null )
insert into #rankme values ( 20 , null )
insert into #rankme values ( 40 , null )
select * from #rankme order by step,checkvalue
Taking step as a parameter, I am trying to find out if the requested checkvalue for the one before the step I asked for is null.
So I want to select where step=20 and get NULL.
And I want to select where step=15 and get a 1.
I was trying to come up with something based on "rank-1" but so far no cigar.
Help?
declare #step int = 15
select top(1) R.checkvalue
from #rankme as R
where R.step < #step
order by R.step desc
SQL Server 2008, I have the following parent/child table schemata and rows:
create table #par( parid int primary key identity(1,1) , partext varchar(6) )
create table #chi( chiid int primary key identity(1,1) , parid int null , chirefid int null , chiinfo varchar(6) )
create table #chiref ( chirefid int primary key identity(1,1) , chisubdesc varchar(9) )
insert into #par values ( 'par1' ) , ('par2')
insert into #chiref values ( 'chi1' )
insert into #chiref values ( 'chi2' )
insert into #chiref values ( 'chi3' )
insert into #chi values ( 1 , 1 , 'aaa' )
insert into #chi values ( 1 , 2 , 'bbb' )
insert into #chi values ( 2 , 1 , 'ccc' )
insert into #chi values ( 2 , 2 , 'ddd' )
insert into #chi values ( 2 , 3 , 'eee' )
The child #chi has just key/value pairs inside, and I need to convert the text (key) into a column and put the value inside, so the result set is shaped like the below. What is the best way to do that (I cannot change the key/value stuff, it is inherited from another system). And there is a join on the #chiref table for the actual column names (which is really killing me).
partext chi1 chi2 chi3
par1 aaa bbb
par2 ccc ddd eee
Thanks.
EDIT - The thing to remember is that the column names have to match the "key value" in the table. So if the EAV row key is "chi1" then the pivot has to have "chi1". I had simply "1" which broke. So I got it.
Thanks again!
select partext, [chi1], [chi2], [chi3]
from
(
select p.partext, c.chitext, c.chiinfo
from #chi c
join #par p
on c.parid = p.parid
) AS SourceTable
pivot
(
min(chiinfo)
for chitext in ([chi1], [chi2], [chi3])
) as PivotTable
or this one is a little more efficient, although more code:
with c
as
(
select parid, [chi1], [chi2], [chi3]
from
(
select parid, chitext, chiinfo
from #chi
) AS SourceTable
pivot
(
min(chiinfo)
for chitext in ([chi1], [chi2], [chi3])
) as PivotTable
)
select p.partext, c.*
from c
join #par p
on c.parid = p.parid
if you need a dynamic column number, you can do dynamic query:
declare #ColumnList varchar(max)
select #ColumnList = isnull(#columnList + ', ', '') + '[' + chitext + ']'
from
(
select distinct chitext
from #chi
) tt
order by chitext
declare #Command varchar(max) = '
with c
as
(
select parid, ' + #ColumnList + '
from
(
select parid, chitext, chiinfo
from #chi
) AS SourceTable
pivot
(
min(chiinfo)
for chitext in (' + #ColumnList + ')
) as PivotTable
)
select p.partext, ' + #ColumnList + '
from c
join #par p
on c.parid = p.parid
'
execute(#Command)