get record according to date - sql-server

I try this query according to date i have only column datecurrent in this record according to current date so there is almost 5 days record with different dates now i want to fetch record according to date i use this but this shows all records whereas i want to get record in specific dates
select c.Orderid,c.DateCurrent,c.Quantity,c.ItemCost,i.ItemCost*c.quantity
as Bill from CustomerOrder c inner join item i on i.ItemId=c.ItemId
inner join userinformation ui on ui.userid=c.userid
where c.datecurrent as fromdate > '2017-04-13' union all
select null,null,null,null,sum (i.ItemCost*c.Quantity) bill
from CustomerOrder c inner join item i on i.itemid=c.ItemId
inner join userinformation ui on ui.userid=c.userid where c.datecurrent as todate > '2017-04-15'
there is records of 16 date and above query shows 16 date records where i want only those records which is between 13 and 15 and same with other dates .. i try this query in winforms with datepicker
this is data
Orderid DateCurrent Quantity ItemCost Bill
101 2017-04-16 14:35:45.823 12 10 120
1093 2017-04-16 17:32:36.250 2 10 20
2093 2017-04-16 17:32:36.250 2 10 20
2094 2017-04-13 17:32:36.250 4 10 400
2095 2017-04-15 17:32:36.250 5 10 50
2096 2017-04-15 17:32:36.250 10 10 1000
2097 2017-04-14 17:32:36.250 12 10 120
NULL NULL NULL NULL 1210
this is i want to get data if i enter only dates between 13 and 15
Orderid DateCurrent Quantity ItemCost Bill
2094 2017-04-13 17:32:36.250 4 10 400
2095 2017-04-15 17:32:36.250 5 10 50
2096 2017-04-15 17:32:36.250 10 10 1000
2097 2017-04-14 17:32:36.250 12 10 120
NULL NULL NULL NULL 1210

I suspect that this does what you want:
select c.DateCurrent, sum(i.ItemCost * c.quantity) as Bill
from CustomerOrder c inner join
item i
on i.ItemId = c.ItemId
where c.datecurrent >= '2017-04-13'
group by grouping sets ( (c.DateCurrent), () );
This returns one row per date plus the total of all dates, along with the sum of the cost times quantity.

Related

is there any unique SQL function to retrieve only year and month from the timestamp and based on that result i need to find out the earliest made

I TRIED THE BELOW MENTIONED CODE BUT IT GIVING DIFFERENT ANSWERS
CODE USED
$SELECT MIN(DATENAME(MM,orders.occurred_at)) AS 'Month',
MIN(DATENAME(YY,orders.occurred_at)) AS 'Year'
FROM orders
TABLE
id account_id occurred_at
1 1001 2015-10-06 17:31:14.000
2 1001 2015-11-05 03:34:33.000
3 1001 2015-12-04 04:21:55.000
4 1001 2016-01-02 01:18:24.000
5 1001 2016-02-01 19:27:27.000
6 1001 2016-03-02 15:29:32.000
7 1001 2016-04-01 11:20:18.000
8 1001 2016-05-01 15:55:51.000
9 1001 2016-05-31 21:22:48.000
10 1001 2016-06-30 12:32:05.000
11 1001 2016-07-30 03:26:30.000
12 1001 2016-08-28 07:13:39.000
13 1001 2016-09-26 23:28:25.000
14 1001 2016-10-26 20:31:30.000
15 1001 2016-11-25 23:21:32.000
16 1001 2016-12-24 05:53:13.000
17 1011 2016-12-21 10:59:34.000
18 1021 2015-10-12 02:21:56.000
19 1021 2015-11-11 07:37:01.000
20 1021 2015-12-11 16:53:18.000
I didn't understand if you want to see all of the results and just sort them so that the earliest instance would show up first in the list, or if you only want the earliest record by itself.
Sorting the list by the timestamp and displaying the Month() and Year() from the timestamp is easy enough:
SELECT
ID
,Account_ID
,Occurred_At
,MONTH(Occurred_At) AS Occurred_Month
,YEAR(Occurred_At) AS Occurred_Year
FROM Orders
ORDER BY Occurred_At ASC -- This puts the oldest record at the top of the list
;
If you only want to retrieve only the oldest record, then you could do this:
SELECT TOP 1 -- This returns only the topmost record
ID
,Account_ID
,Occurred_At
,MONTH(Occurred_At) AS Occurred_Month
,YEAR(Occurred_At) AS Occurred_Year
FROM Orders
ORDER BY Occurred_At ASC -- This puts the oldest record at the top of the list
;
Presumably, you don't need to extract the month and year from Occurred_At in order to know which order was first... The timestamp gives you that information. But if, for some reason, you want to sort by the month and year instead of the full timestamp:
SELECT TOP 1
ID
,Account_ID
--,Occurred_At -- If you don't need this line, you can remove it...
,MONTH(Occurred_At) AS Occurred_Month
,YEAR(Occurred_At) AS Occurred_Year
FROM Orders
ORDER BY Occurred_Year ASC, Occurred_Month ASC
;
Try,
SELECT MONTH(occurred_at) AS Month,
YEAR(occurred_at) AS Year
FROM orders
OR
SELECT FORMAT(occurred_at, 'MMMM') AS Month,
FORMAT(occurred_at, 'yyyy') AS Year
FROM orders
OR
SELECT
FORMAT(occurred_at, 'y') AS 'Month and Year',
FROM orders

using Full join in sql server

i have 2 table data want join all together,each table are less 1 row date
example : table A have 8 row date and table b also have 8 row date but both table have 1 row date is different .i want my result show out as 10 row
table A
RN USERID ClockIn CHECKTIME badgenumber
1 6 8:24AM 2017-03-02 107
1 6 7:57AM 2017-03-03 107
1 6 8:23AM 2017-03-06 107
1 6 8:26AM 2017-03-07 107
1 6 8:57AM 2017-03-08 107
1 6 8:33AM 2017-03-09 107
1 6 8:36AM 2017-03-10 107
1 6 8:15AM 2017-03-13 107
table B
RN USERID ClockOut CHECKTIME badgenumber
1 6 9:31PM 2017-03-01 107
1 6 10:28PM 2017-03-02 107
1 6 8:22PM 2017-03-03 107
1 6 9:18PM 2017-03-06 107
1 6 9:48PM 2017-03-07 107
1 6 9:11PM 2017-03-08 107
1 6 11:31PM 2017-03-09 107
1 6 6:30PM 2017-03-10 107
my result show as
SELECT #clockin.ClockIn, #clockOut.ClockOut,#clockin.USERID,#clockin.CHECKTIME
FROM #clockin
FULL JOIN #clockOut
ON #clockin.CHECKTIME=#clockOut.CHECKTIME
where #clockin.userid = 6 and #clockOut.userid = 6
ORDER BY #clockin.userid;
<!DOCTYPE html>
<html>
<body>
<h2>result</h2>
<img src="https://i.stack.imgur.com/IcdSS.png" alt="result" >
</body>
</html>
Because of your where clause, you are filtering out rows where x.userid is null (where there is no match). This essentially turns your full join into an inner join. You can use coalesce() to return the first non-null value from your two columns and compare that to 6 like so:
SELECT #clockin.ClockIn, #clockOut.ClockOut,#clockin.USERID,#clockin.CHECKTIME
FROM #clockin
FULL JOIN #clockOut
ON #clockin.CHECKTIME=#clockOut.CHECKTIME
where coalesce(#clockin.userid,#clockOut.userid)=6
ORDER BY #clockin.userid;
SELECT ClockIn, ClockOut,
ISNULL(ci.USERID, co.USERID) AS USERID,
CONVERT(VARCHAR(10), ISNULL(ci.CHECKTIME, co.CHECKTIME), 101) AS CHECKTIME
FROM #ClockIn AS ci
FULL JOIN #ClockOut AS co ON (co.CHECKTIME = ci.CHECKTIME);
This should give your desired output for the sample data. However you may have to consider adding RN, USERID etc. in the JOIN filter depending on what you want.
The ISNULL() replaces the NULL id of ClockIn with the id from ClockOut.

Sqlserver group by pending days period

I have table structure as below,
State Application_count Pending_Days
_________________________________________
TN 10 0
TN 20 1
TN 60 2
TN 10 3
MH 40 1
MH 50 3
MH 20 5
MH 30 8
I want to sum Application_count based on State and Pending_Days period.
I have to group Pending_days 0 to 1 Days, 1 to 3 days, morethan 3 days
Expected Output:
State Application_count
_________________________
TN 30
TN 70
MH 40
MH 50
MH 50
Give an additional column, group_num using a CASE expression based on the condition of Pending_Days column.
Then find the sum group by group_num and state columns.
Query
select t.[state], sum(t.[Application_Count]) as [Application_Count] from(
select [group_num] = (
case when [Pending_Days] between 0 and 1 then 1
when [Pending_Days] between 2 and 3 then 2
when [Pending_Days] > 3 then 3 else 4 end
), *
from [your_table_name]
)t
group by t.[group_num], t.[state];
Find demo here
Note:
For Pending_Days condition, 0 to 1 Days I have taken 0 and 1
for 1 to 3 days I have taken 2 and 3 , for morethan 3 days I have taken the value greater than 3.

SQL Server 2008 grouping based on 2 columns which contains null values

My question is in the table structure below. All items grouped by storeName and storeId then displays the total of each item for each store. But some of the items not sold in some stores therefore displays NULL.. Is that possible to
display egg&bacon instead of NULL for the first 8 records in itemName column and
display coke for instead of NULL the second 8 in itemName column and so on.
I do not know the how many item they have. So it is dynamic.
I want to do this because I have created a crosstab report with pentaho report designer. It works fine but it expects data in order to display it properly. So each item must have 8 records, one for each store. So I can group it by store and itemname. So the report displays - or 0 for the items not sold for those stores.
what I have
storeId storeName itemName total
1 storeA egg&bacon 75
2 storeB NULL NULL
3 storeC egg&bacon 30
4 storeD NULL NULL
5 storeE NULL NULL
6 storeF egg&bacon 50
7 storeG NULL NULL
8 storeH NULL NULL
1 storeA coke 105
2 storeB coke 90
3 storeC coke 60
4 storeD NULL NULL
5 storeE coke 20
6 storeF coke 80
7 storeG NULL NULL
8 storeH NULL NULL
what I want (If the table below is not realistic, I can create a separate column to group them but again how?)
storeId storeName itemName total
1 storeA egg&bacon 75
2 storeB egg&bacon 0
3 storeC egg&bacon 30
4 storeD egg&bacon 0
5 storeE egg&bacon 0
6 storeF egg&bacon 50
7 storeG egg&bacon 0
8 storeH egg&bacon 0
1 storeA coke 105
2 storeB coke 90
3 storeC coke 60
4 storeD NULL 0
5 storeE coke 20
6 storeF coke 80
7 storeG NULL 0
8 storeH NULL 0
This is the sample SQL statement
SELECT storeId , storeName, itemName, total FROM STORE
LEFT OUTER JOIN
(
SELECT storeId, storeName, itemId, total FROM REVENUE_CENTER as RVC
LEFT OUTER JOIN MENU_ITEM_TOTAL as MIT ON STORE.storeId = MIT.storeId
) as subQ ON STORE.storeId = subQ.storeId
LEFT OUTER JOIN MENU_ITEM as MI ON MI.itemId = subQ.itemId
thanks in advance
Oz.
If you want to know you have zero egg&bacon in a store you must at least set the item name to 'egg&bacon'
to change null to zero in the total column you can use ISNULL(itemName, 0)
since storeid and name are constant you should only keep storeid in the table, and group by it (better performance, less data space) and keep store name in another table. That will change your group by statements to be based only on store ID.
Consider not saving records for items shat are now null, null - this is something your code should handle.

Add dynamic column in select query - MS SQL

Im using Ms SQL database,
My table looks,
SrNo Emp_ID Date Time
------------------------------------
1 25 03-Sep-12 9:35:35 AM
2 25 03-Sep-12 10:31:32 AM
3 25 03-Sep-12 10:34:13 AM
4 25 03-Sep-12 11:05:08 AM
5 25 03-Sep-12 11:08:39 AM
6 25 04-Sep-12 09.05.40 AM
The expected output is
SrNo Emp_ID Date Time Type
---------------------------------------------
1 25 03-Sep-12 9:35:35 AM IN
2 25 03-Sep-12 10:31:32 AM OUT
3 25 03-Sep-12 10:34:13 AM IN
4 25 03-Sep-12 11:05:08 AM OUT
5 25 03-Sep-12 11:08:39 AM IN
6 25 04-Sep-12 09.05.40 AM IN
For an employee, type "in" and "out" has to be added simultaneously for particular date. For the next date or next /same employee, type has to start with "IN". Can any 1 help me in writing the sql query.
You can achieve this using an additional column
ROW_NUMBER() OVER (PARTITION BY Emp_ID, Date ORDER BY Time) AS RN
then checking modulus 2 on it, if remainder is 1, it's an IN, if it's 0 it's an OUT, based on your requirements.
More specifically
CASE WHEN
ROW_NUMBER() OVER (PARTITION BY Emp_ID, Date ORDER BY Time) % 2 = 1
THEN 'IN'
ELSE 'OUT'
END AS [Type]

Resources