Alright.. So I know enough sql that I can always figure out some way to get what I want out of the database, even if it's not pretty. For this, we have a customer (international farmers market) who already have this report on their retail system. Now they want it on the Micros system. Essentially the report lists all of the items sold with other menu items, and how many.
This is the sql I came up with:
Thing is that it can take upwards of 40 seconds or more just to run the query, depending on how many menu items they select and the date range they select. Any sql gurus out there know a better, more efficient ways to do this?
This is the sql I came up with:
Code:
CREATE TABLE #temp_values (
key_seq SEQ_NUM,
key_name NAME16,
mi_seq SEQ_NUM,
name_1 NAME16,
qty INTEGER
)
DECLARE @mi_key SEQ_NUM
DECLARE @name_1 NAME16
SELECT mi_seq,name_1 INTO #temp_mi_seq FROM micros.mi_def
WHILE EXISTS(SELECT 1 FROM #temp_mi_seq)
BEGIN
SET ROWCOUNT 1
SELECT @mi_key = mi_seq, @name_1 = name_1 FROM #temp_mi_seq
SET ROWCOUNT 0
DELETE FROM #temp_mi_seq WHERE mi_seq = @mi_key
INSERT INTO #temp_values
SELECT
@mi_key AS key_seq,
@name_1 AS key_name,
midtl.mi_seq,
midef.name_1,
COUNT(midtl.mi_seq) AS qty
INTO #temp_values
FROM micros.mi_dtl midtl
JOIN micros.mi_def midef
ON midef.mi_seq = midtl.mi_seq
JOIN micros.trans_dtl tdtl
ON tdtl.trans_seq = midtl.trans_seq
WHERE midtl.trans_seq IN(
SELECT
i.trans_seq
FROM
micros.mi_dtl i
WHERE
i.mi_seq = key_seq)
AND midtl.mi_seq <> key_seq
AND tdtl.business_date BETWEEN '2013/09/7' AND '2013/09/27'
GROUP BY
key_seq,
midtl.mi_seq,
midef.name_1
ORDER BY
qty DESC,
name_1
END
SELECT * FROM #temp_values;
Thing is that it can take upwards of 40 seconds or more just to run the query, depending on how many menu items they select and the date range they select. Any sql gurus out there know a better, more efficient ways to do this?